function intro_start() {
    if (location.hash && location.hash != '#') {
        goForLink('/'+location.hash.substring(1));
    }
    else {
        document.title = 'Fundacja Europejskie Centrum Ziemi';
        initIntro();
        moveIntro();
        jQuery(window).resize(moveIntro);
    }
}

function moveIntro() {
    var margin = (jQuery(window).height()-588)/2;
    jQuery('#bottomholder').css({'position':'absolute', 'top':jQuery('#bottomholder').offset().top});
    jQuery('#mainintro').animate({'opacity':'1.0','height':'435px', 'top':(Math.max(20,margin))+'px'});
    jQuery('#bottomholder').animate({'top': 435+Math.max(20,margin)+'px'});
    jQuery('#spacer').css({'height': (jQuery(window).height()+1)+'px'});
}

var currIntro = null;
var currPic = Math.floor(Math.random()*5);
var background = new Array(3);
var nextIntro = null;
var nextPic = null;
var introListRequest = null;
var introList = null;
var introCurrentImage = 1;
var introOtherImage = 0;

var customSlideshow = false;

function loadIntroList(transport) {
    introList = transport.responseText.evalJSON();
}

function initIntro() {
    if (currIntro == null) {
        currIntro = parseInt(jQuery('#intro_picture_1 a').attr('rel'));

        jQuery('.intro_menu').mouseenter(
            function(ev) {
                var imgs = jQuery(ev.currentTarget).find('img');
                if (jQuery(imgs[1]).css('display') == 'none') {
                    jQuery(imgs[1]).css({'opacity':'0.0', 'display':'block'});
                    jQuery(imgs[3]).css({'opacity':'0.0', 'display':'block'});
                }
                jQuery(imgs[1]).stop().animate({'opacity':'1.0'},250);
                if (jQuery(imgs[2]).css('opacity') == 1 && jQuery(imgs[2]).css('display') != 'none') {
                    jQuery(imgs[3]).stop().animate({'opacity':'1.0'},250);
                }
            }
        );
        jQuery('.intro_menu').mouseleave(
            function(ev) {
                var imgs = jQuery(ev.currentTarget).find('img');
                jQuery(imgs[1]).stop().animate({'opacity':'0.0'},1000);
                jQuery(imgs[3]).stop().animate({'opacity':'0.0'},1000);
            }
        );

    }
    clearTimeout(introTimeout);
    if (introListRequest == null) {
        introListRequest = new Ajax.Request('/dispatcher.php',
                                            { method: 'get',
                                              parameters: { 'control': 'Intro',
                                                            'method': 'fetchList'},
                                              onSuccess: loadIntroList
                                            }
                                           );
    }
    if (jQuery('#intro_picture_'+introCurrentImage+' img')[0].complete) {
        jQuery('#intro_picture_'+introCurrentImage).fadeIn(700);
        rotateIntro();
    }
    else {
        jQuery('#intro_picture_'+introCurrentImage).css({'overflow':'hidden','display':'block', 'width':'1px', 'height': '1px'});
        jQuery('#intro_picture_'+introCurrentImage+' img').load(function () {
            jQuery('#intro_picture_'+introCurrentImage).css({'overflow':'visible','display':'none', 'width':'auto', 'height': 'auto'});
            jQuery('#intro_picture_'+introCurrentImage).fadeIn(700);
            //clearInterval(firstImageInterval);
            rotateIntro();
            jQuery('#intro_picture_'+introCurrentImage+' img').unbind('load');
        });
    }
}

function showIntro() {
    if (jQuery('#intro_picture_'+introOtherImage+' img')[0].complete) {


        jQuery('#intro_menu_'+currIntro+' img').eq(2).stop().animate({'opacity':'0.0'},1500);
        jQuery('#intro_menu_'+currIntro+' img').eq(3).stop().animate({'opacity':'0.0'},1500);
        jQuery('#intro_menu_'+currIntro+' img').eq(0).show();

        jQuery('#intro_picture_'+introCurrentImage).fadeOut(350, function () {
            jQuery('#intro_picture_'+introOtherImage).fadeIn(350);
            jQuery('#intro_picture_'+introOtherImage+' a').attr('href', introList.links[nextIntro].link);
            currPic = nextPic;
            currIntro = nextIntro;
            introCurrentImage = introOtherImage;
            clearTimeout(introTimeout);
            introTimeout = setTimeout(rotateIntro,1000);
            var imgs = [];
            if (!customSlideshow) {
              imgs = jQuery('#intro_menu_'+nextIntro+' img');
            } else {
              imgs = jQuery('#intro_menu_extra img');
            }
            if (jQuery(imgs[2]).css('display') == 'none') {
                jQuery(imgs[2]).css({'opacity':'0.0', 'display':'block'});
            }
            jQuery(imgs[2]).animate({'opacity':'1.0'},250);
            if (jQuery(imgs[1]).css('opacity') == 1 && jQuery(imgs[1]).css('display') != 'none') {
                jQuery(imgs[3]).stop().animate({'opacity':'1.0'},250);
            }
       });
        jQuery('#menu_caption').fadeOut(350, function() {
            if (!customSlideshow) {
              jQuery('#menu_caption').html(introList.captions[nextIntro][nextPic]);
              jQuery('#menu_caption').fadeIn(350);
            }
        });
    }
    else {
        clearTimeout(introTimeout);
        introTimeout = setTimeout(showIntro, 1000);
    }
}

function rotateIntro() {
    if (introList == null) {
        clearTimeout(introTimeout);
        introTimeout = setTimeout(rotateIntro, 1000);
        return;
    }

    introOtherImage = (introCurrentImage+1)%2;
    if (customSlideshow) {
        nextPic = (nextPic+1) % 15;
        jQuery('#intro_picture_'+introOtherImage+' img').attr('src', '/_gfx/slideshow/'+(nextPic+1)+'.jpg');
    } else {
        nextIntro = (currIntro+(Math.floor(Math.random()*2)+1))%3;
        nextPic = Math.floor(Math.random()*5);
        jQuery('#intro_picture_'+introOtherImage+' img').attr('src', '/_gfx/intro_'+nextIntro+'_'+nextPic+'.jpg');
    }

    if (customSlideshow) {
        clearTimeout(introTimeout);
        introTimeout = setTimeout(showIntro, 4000);
    }
    else {
        clearTimeout(introTimeout);
        introTimeout = setTimeout(showIntro, 7000);
    }
}

function slideshow() {
    customSlideshow = !customSlideshow;
    if (customSlideshow) {
        nextPic = -1;
    }

    var imgs = [];
    if (customSlideshow) {
        imgs = jQuery('#intro_menu_'+currIntro+' img');
        if (jQuery(imgs[2]).css('display') == 'none') {
            jQuery(imgs[2]).css({'opacity':'0.0', 'display':'block'});
        }

        imgs = jQuery('#intro_menu_extra img');
        jQuery(imgs[2]).animate({'opacity':'1.0'},250);
        if (jQuery(imgs[1]).css('opacity') == 1 && jQuery(imgs[1]).css('display') != 'none') {
            jQuery(imgs[3]).stop().animate({'opacity':'1.0'},250);
        }
    }


    if (!customSlideshow) {
        imgs = jQuery('#intro_menu_extra img');
        jQuery(imgs[2]).css({'opacity':'0.0', 'display':'block'});
        jQuery(imgs[3]).animate({'opacity':'0.0', 'display':'block'});
    }


    clearTimeout(introTimeout);
    rotateIntro();
    clearTimeout(introTimeout);
    showIntro();

}

function slideNext() {
    
}
