siteCache = new Array();
ie6 = (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7);

var introTimeout;
var menuVisible = true;
var garbageHeight = 238; // height of bottom container + main container paddings

function toggleMenu() {
    var viewportHeight = jQuery(window).height();
    var divHeight = jQuery('#mainpart').height();
    garbageHeight = (menuVisible) ? garbageHeight-110 : garbageHeight+110;
    var difference = Math.max(0, viewportHeight-garbageHeight-divHeight);
    //alert(difference);
    jQuery('#mainpart').animate({'padding-top':(difference)+'px'});
    if (menuVisible) {
        jQuery('#bottomholder').animate({'height':'73px'});
        jQuery('#toggledbottomcontainer').slideUp();
        jQuery('#menuexpander').fadeOut('normal', function() {
            jQuery('#menuexpander img').attr('src', '/_gfx/arrow-down.png');
            jQuery('#menuexpander').fadeIn('normal');
        });
        jQuery('#bottomspacer').animate({'height': '80px'}, function() {
            menuVisible = !menuVisible;
        });
    }
    else {
        jQuery('#bottomholder').animate({'height':'183px'});
        jQuery('#toggledbottomcontainer').slideDown();
        jQuery('#menuexpander').fadeOut('normal', function() {
            jQuery('#menuexpander img').attr('src', '/_gfx/arrow-up.png');
            jQuery('#menuexpander').fadeIn('normal');
        });
        jQuery('#bottomspacer').animate({'height': '190px'}, function () {
            menuVisible = !menuVisible;
        });
    }
}

function ie6BottomHolderFix() {
    jQuery('#bottomholder').css( {
        'position': 'absolute',
        'left': '0px',
        'top': (jQuery(window).height()-jQuery('#bottomholder').height()+jQuery(window).scrollTop())+'px'
    });
    fixDivSize();
}

function fixDivSize() {
    var viewportHeight = jQuery(window).height();
    var divHeight = jQuery('#mainpart').height();
    var difference = Math.max(0, viewportHeight-garbageHeight-divHeight);
    //alert(difference);
    jQuery('#spacer').css({'height': (jQuery(window).height()+1)+'px'})
    jQuery('#mainpart').css({'padding-top':(difference)+'px'});
    fixHorizontalScroll();
}

function fixHorizontalScroll() {
    var p = jQuery('#bottomholder').css('position');
    if (jQuery('#bottomholder').css('position') == 'fixed') {
        jQuery('#bottomholder').css('left', (-jQuery(document).scrollLeft())+'px');
    }
}

function goForLink(linkToGoTo) {
    linkToGoTo = linkToGoTo.replace(/^http:\/\/.+?(\/.*)/, '$1');
    if (linkToGoTo=='/') {
        if (jQuery('#mainintro').css("display")=='none' || jQuery('#mainintro').height() == 0) {
            jQuery(window).unbind('resize');
            jQuery(window).resize(moveIntro);
            jQuery('#rightcontent').animate({'opacity':'0.0'}, 'fast');
            jQuery('#maincontainer').fadeOut(
                function() {
                    jQuery('#copyright_background').css('background-image', 'none');
                    jQuery('#intromenu a').unbind('hover');
                    location.replace('/#');
                    jQuery('#mainintro').css({'top':'-500px'});
                    intro_start();
                });
        }
        return;
    }
    var bhOffset = jQuery('#bottomholder').offset().top;
    var testIt = jQuery(window).scrollTop();
    var bottomHeight = (menuVisible) ? 183 : 73;
    jQuery('#bottomholder').css({'bottom':(jQuery(window).height()-bottomHeight-bhOffset+testIt)+'px','position':'fixed','top':'auto'});
    clearTimeout(introTimeout);
    jQuery('#mainintro').animate({'top':'0px','height':'1px','opacity':'0.5'}, 350,
                                 function() { jQuery('#mainintro').hide() });
    jQuery(window).unbind('resize');
    jQuery(window).resize(fixDivSize);
    jQuery('#bottomholder').animate({'bottom':'0px','height':bottomHeight+'px'}, 'slow', function () {
        jQuery('#maincontainer').fadeIn();
        fixDivSize();
    });
    jQuery('#rightcontent').animate({'opacity':'0.0'},'fast');
    jQuery('#mainpart').animate({'opacity':'0.0'},'fast', function () {
        if (siteCache[linkToGoTo] == undefined) {
            jQuery('#page_loader').show();
            // call AJAX
            new Ajax.Request('/dispatcher.php',
                             { method: 'get',
                               parameters: { 'class': 'cAjaxPage',
                                             'method': 'fetchAll',
                                             'link': linkToGoTo },
                               onSuccess: updateSite,
                               onFailure: displayError
                             }
                            );
        }
        else {
            jQuery(window).scrollTop(0);
            document.title = 'Fundacja Europejskie Centrum Ziemi - '+siteCache[linkToGoTo].title;
            jQuery('#ajax_title').html(siteCache[linkToGoTo].title);
            jQuery('#ajax_abovebody').html(siteCache[linkToGoTo].abovebody);
            jQuery('#ajax_body').html(siteCache[linkToGoTo].body);
            jQuery('#ajax_belowbody').html(siteCache[linkToGoTo].belowbody);
            jQuery('#ajax_rightmenu').html(siteCache[linkToGoTo].rightmenu);
            jQuery('#mainpart, #rightcontent').animate({'opacity':'1.0'});
            jQuery('#copyright_background').css('background-image', 'url("/_gfx/footer_background.png")');
            fixDivSize();
            jQuery('#page_loader').hide();
            rebindOnClicks();
        }
        linkToGoTo = '/#'+linkToGoTo.substring(1);
        location.replace(linkToGoTo);
    });
}

function updateSite(transport) {
    jQuery(window).scrollTop(0);
    response = transport.responseText.evalJSON();
    jQuery('#ajax_title').html(response.title);
    if (response.title) {
        document.title = 'Fundacja Europejskie Centrum Ziemi - '+response.title;
    }
    jQuery('#ajax_abovebody').html(response.abovebody);
    jQuery('#ajax_body').html(response.body);
    jQuery('#ajax_belowbody').html(response.belowbody);
    jQuery('#ajax_rightmenu').html(response.rightmenu);
    jQuery('#mainpart, #rightcontent').animate({'opacity':'1.0'});
    jQuery('#copyright_background').css('background-image', 'url("/_gfx/footer_background.png")');
    fixDivSize();
    jQuery('#page_loader').hide();
    siteCache[transport.request.parameters.link] = response;
    rebindOnClicks();
}

function displayError(transport) {
    jQuery(window).scrollTop(0);
    if (jQuery('#mainbox').attr("class") == "mainintro") {
        jQuery('#mainbox').addClass('mainbox').removeClass('mainintro');
        jQuery('#mainintro').hide();
    }

    jQuery('#ajax_body').html('Wystąpił błąd w pobieraniu artykułu :<');
    jQuery('#mainpart').animate({'opacity':'1.0'});
    fixDivSize();
    jQuery('#page_loader').hide();
    rebindOnClicks();
}

function doTheClick(attr) {
    goForLink(jQuery(attr).attr('href'));
    jQuery('.rightmenu_highlighter:visible').slideUp('fast');
}

function bindOnClicks() {
    jQuery("a[href^='/'][href$='.html']").focus( function () { jQuery(this).blur() } );
    jQuery("a[href^='/'][href$='.html']").click( function () { doTheClick(this); return false } );
    jQuery("a[href='/']").focus( function () { jQuery(this).blur() } );
    jQuery("a[href='/']").click( function () { doTheClick(this); return false } );
}

function rebindOnClicks() {
    jQuery("div#mainpart a[href^='/'][href$='.html'], div#mainpart a[href^='http://"+location.host+"/'][href$='.html']").focus( function ()  { jQuery(this).blur() } );
    jQuery("div#mainpart a[href^='/'][href$='.html'], div#mainpart a[href^='http://"+location.host+"/'][href$='.html']").click( function () { doTheClick(this); return false } );
    jQuery("div#mainpart a[href='/'], div#mainpart a[href='http://"+location.host+"/']").focus( function () { jQuery(this).blur() } );
    jQuery("div#mainpart a[href='/'], div#mainpart a[href='http://"+location.host+"/']").click( function () { doTheClick(this); return false } );
    jQuery('#lightbox, #overlay').remove();
    jQuery('a[rel^=lightbox]').attr('onclick', null);
    jQuery('a.lightbox, a.fancybox, a[rel^=lightbox]').fancybox();
    var links = jQuery('div#mainpart, div#rightcontent').find("a[href^='/'][href$='.html'], a[href^='http://"+location.host+"/'][href$='.html'], a[href='/'], a[href='http://"+location.host+"/']");
    links.focus( function(e) { jQuery(e.target).blur() } );
    links.click( function(e) { doTheClick(e.target); return false } );
}

if (!ie6) {
    if (location.pathname != '/') {
      if (location.pathname.substring(0,2) != '/_') {
        location.replace('/#'+location.pathname.substring(1));
      }
      else {
        jQuery(document).ready(function() {
                                jQuery(window).unbind('resize');
                                jQuery(window).resize(fixDivSize);
                                jQuery('#bottomholder').animate({'bottom':'0px'}, 'slow', function () {
                                                                  jQuery('#maincontainer').fadeIn();
                                                                  fixDivSize();
                                                                });
                              });
      }
    }
    jQuery(document).ready(function() {
        bindOnClicks();
        jQuery(document).scroll(fixHorizontalScroll);
    });
}
else {
    if (location.hash) {
        var newLocation = location.hash.replace("#", '/');
        setTimeout(function ()  { location.replace(newLocation) }, 10);
    }
    jQuery(document).ready(function () {
        ie6BottomHolderFix();
        jQuery(window).scroll(ie6BottomHolderFix);
        jQuery(window).resize(ie6BottomHolderFix);
    });
}

jQuery(document).ready(function () {
    jQuery(window).resize(fixDivSize);
    jQuery('a').focus(function() { jQuery(this).blur() });
    jQuery('a#menuexpander').click(function() { toggleMenu(); return false; });
});
