var aktiv;
var leftt;
var right;

function isset(varname){
    return(varname !== undefined);
}

function str_pad (input, pad_length, pad_string, pad_type) {
    var half = '',
        pad_to_go;

    var str_pad_repeater = function (s, len) {
        var collect = '',
            i;

        while (collect.length < len) {
            collect += s;
        }
        collect = collect.substr(0, len);

        return collect;
    };

    input += '';
    pad_string = pad_string !== undefined ? pad_string : ' ';

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') {
        pad_type = 'STR_PAD_RIGHT';
    }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') {
            input = str_pad_repeater(pad_string, pad_to_go) + input;
        } else if (pad_type == 'STR_PAD_RIGHT') {
            input = input + str_pad_repeater(pad_string, pad_to_go);
        } else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}

var john = function(e) {
    var s = $(this).attr("href").slice(1).split("?");
    var params = s[0].split("&");
    var obj = {};
    for (var i = 0, l = params.length; i < l; i++) {
        var pair = params[i].split("=");
        obj[pair[0]] = pair[1];
        }
    if(isset(content[obj['screen']])){
        var result = '';
        var count = 0;
            for(var i = 0; i < obj['screen'].length-2; i++) {
                count++;
                result += obj['screen'][i];
            }
        var num = obj['screen'].slice(count,obj['screen'].length);
        
        if(result != 'berichten' && result != 'verbinden' && result != 'besuchen'){
            if(parseInt(num,10) == 1 && parseInt(num,10) != maxx[result]) {
                right = parseInt(num)+1;
                $('#subnav').html('<a href="?screen='+content[result+num]['last']+'" class="forward red" ><</a> <a href="?screen='+result+str_pad(right, 2, "0", 'STR_PAD_LEFT')+'" class="forward red" >></a>');
            }
            else if(parseInt(num,10) == maxx[result] && parseInt(num,10) != 1) {
                leftt = parseInt(num)-1;
                $('#subnav').html('<a href="?screen='+result+str_pad(leftt, 2, "0", 'STR_PAD_LEFT')+'" class="forward red" ><</a> <a href="?screen='+content[result+num]['next']+'" class="forward red" >></a>');
            }
            else if(parseInt(maxx[result],10) == 1) {
                $('#subnav').html('<a href="?screen='+content[result+num]['last']+'" class="forward red" ><</a> <a href="?screen='+content[result+num]['next']+'" class="forward red" >></a>');
            }
            else {
                leftt = parseInt(num,10)-1;
                right = parseInt(num,10)+1;
                $('#subnav').html('<a href="?screen='+result+str_pad(leftt, 2, "0", 'STR_PAD_LEFT')+'" class="forward red" ><</a> <a href="?screen='+result+str_pad(right, 2, "0", 'STR_PAD_LEFT')+'" class="forward red" >></a>');
            }
        }
        else{
            $('#subnav').html('');
        }
        
        if(content[obj['screen']]['text'] == ''){
        
            if(result != 'besuchen'){
        
                if((parseInt(num,10) == maxx[result] && parseInt(num,10) != 1) || parseInt(maxx[result],10) == 1) {
                    leftt = parseInt(num)-1;
                    mainframe = '<a href="?screen='+content[result+num]['next']+'" class="forward red" ><img onLoad="$(\'#content_image\').fadeIn(\'slow\');$(\'#bottom\').fadeIn(\'slow\');" src="inc/images/'+result+parseInt(num,10)+'.jpg" id="content_image" style="display:none;" /></a>';
                }
                else {
                    right = parseInt(num,10)+1;
                    mainframe = '<a href="?screen='+result+str_pad(right, 2, "0", 'STR_PAD_LEFT')+'" class="forward red" ><img onLoad="$(\'#content_image\').fadeIn(\'slow\');$(\'#bottom\').fadeIn(\'slow\');" src="inc/images/'+result+parseInt(num,10)+'.jpg" id="content_image" style="display:none;" /></a>';
                }
                
            }
            else{
            
                if((parseInt(num,10) == maxx[result] && parseInt(num,10) != 1) || parseInt(maxx[result],10) == 1) {
                    leftt = parseInt(num)-1;
                    mainframe = '<img onLoad="$(\'#content_image\').fadeIn(\'slow\');$(\'#bottom\').fadeIn(\'slow\');" src="inc/images/'+result+parseInt(num,10)+'.jpg" id="content_image" style="display:none;" />';
                }
                else {
                    right = parseInt(num,10)+1;
                    mainframe = '<img onLoad="$(\'#content_image\').fadeIn(\'slow\');$(\'#bottom\').fadeIn(\'slow\');" src="inc/images/'+result+parseInt(num,10)+'.jpg" id="content_image" style="display:none;" />';
                }
                
            }
        
            supplement = content[result+num]['supplement'];
        }
        else {
            mainframe = '<onLoad="$(\'#content_text\').fadeIn(\'slow\');$(\'#bottom\').fadeIn(\'slow\');" id="content_text" >'+content[result+num]['text']+'</div>';
            supplement = '';
        }

        $('#grand').html(mainframe);
        $('#bottom').html(supplement);
        
        
        noProject = new Array();
        noProject['besuchen'] = true;
        noProject['denken'] = true;
        noProject['menschen'] = true;
        noProject['verbinden'] = true;
        noProject['berichten'] = true;
        
        document.title = result;

        if(isset(noProject[result])){
            arbeiten = false;
            if(typeof(old) == 'undefined'){old = 'buffer';}
            if(result != old){
                $('#menu_'+result).css('color', '#345984');
                if(isset(noProject[old])){
                    $('#menu_'+old).css('color', '#4B493C');
                }
                else{
                    $('#menu_'+old).css('color', '#345984');
                    $('#menu_arbeiten').css('color', '#4B493C');
                }
            }
            old = result;
        }
        else if(result == 'arbeiten'){
            if(old != 'arbeiten'){
                $('#menu_arbeiten').css('color', '#345984');
            }
        }
        else{
            arbeiten = true;
            if(typeof(old) == 'undefined'){old = 'buffer';}
            if(isset(noProject[old])){
                $('#menu_'+old).css('color', '#4B493C');
                $('#menu_arbeiten').css('color', '#345984');
            }
            else{
                $('#menu_'+old).css('color', '#345984');
                $('#menu_arbeiten').css('color', '#345984');
            }
            $('#menu_'+result).css('color', '#8c2c08');
            old = result;
        }

        if(isset(noProject[result])){
            $('#sub').css('visibility', 'hidden');
        }
        else {
            $('#sub').css('visibility', 'visible');
        }
    }
    return false;
};

function cscroll(p){
    if(p == 1){
        if(parseInt($('#scrollframe').css('top'),10) <= 0){
            $('#scrollframe').css('top',parseInt($('#scrollframe').css('top'),10)+p+'px');
        }
        else{
            window.clearInterval(aktiv);
        }
    }
    if(p == -1){
        console.log(parseInt($('#scrollframe').css('top'),10));
        console.log(parseInt($('#scrollframe').height(),10));
        console.log(parseInt($('#subnavscroll').outerHeight(),10));
        if(Math.abs(parseInt($('#scrollframe').css('top'),10)) + parseInt($('#subnavscroll').outerHeight(),10) < parseInt($('#scrollframe').outerHeight(),10)){
            $('#scrollframe').css('top',parseInt($('#scrollframe').css('top'),10)+p+'px');
        }
        else{
            window.clearInterval(aktiv);
        }
    }
}

$(document).ready(function(){
    $(document).delegate("a:not(.nohref)","click",john);
	$("#scroller").simplyScroll({
        'speed': 3
	});
	$(document).delegate("#scrollup","mouseenter",function(){aktiv = window.setInterval(function() { cscroll(1); }, 20);});
	$(document).delegate("#scrollup","mouseleave",function(){window.clearInterval(aktiv);});
	$(document).delegate("#scrolldown","mouseenter",function(){aktiv = window.setInterval(function() { cscroll(-1); }, 20);});
	$(document).delegate("#scrolldown","mouseleave",function(){window.clearInterval(aktiv);});
});

