var anispeed = 150;


$(document).ready(function(){

    $('body a').each(function() {
        $(this).focus(function() {
            if ( $(this).blur() ) { $(this).blur(); }
        })
    });

    resizer();
    $(window).resize(function() {
        resizer();
    });

    startSlideshow();

    initLeftmenuEvents();
    initRightmenuEvents();
    initFootermenuEvents();
    initContentLinks();

});

function initContentLinks() {

    $('#ContentContainer a').click(function(){

        var SiteURL = $(this).attr('href')

	    if( SiteURL.match(/http/) || SiteURL.match(/pdf/) || SiteURL.match(/xls/) || ( SiteURL.match(/pics/) && SiteURL.match(/jpg/) ) ) {
		    return true;
	    }
	    
	    

        var textLoad = SiteURL + " #ContentContainer";
        var menuLoad = SiteURL + " #LeftMenu";
        var slideload = SiteURL + " #SlideshowContainer";

        $('#slideshowcontent').animate({ 'margin-left': '-206px' }, anispeed);
        $('#menucontent').animate({ 'padding-left': '186px' }, anispeed, function() {
            $('body').css('color','#4c4c4c');
            $('#content').fadeOut(anispeed, function() {
                $('#AjaxLoader').fadeIn('fast', function() {

                    $('#menucontent').load(menuLoad, '', function(){
                        $('#content').load(textLoad, '', function(){
                            $('#slideshowcontent').load(slideload, '', function(){
                                $('#AjaxLoader').fadeOut('fast', function() {
                                    $('#content').fadeIn(anispeed, function() {
                                        $('body').css('color','#1129b2');
                                        $('#slideshowcontent').animate({ 'margin-left': 0 }, anispeed);
                                        $('#menucontent').animate({ 'padding-left': 0 }, anispeed, function(){
                                            initLeftmenuEvents();
                                            initContentLinks();
                                            startSlideshow();
                                            startLightbox();
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
        return false;

    });
    
}


function initLeftmenuEvents() {
    $('#LeftContainer a').click(function(){

        var SiteURL = $(this).attr('href')

        var textLoad = SiteURL + " #ContentContainer";
        var menuLoad = SiteURL + " #LeftMenu";
        var slideload = SiteURL + " #SlideshowContainer";

        $('#slideshowcontent').animate({ 'margin-left': '-206px' }, anispeed);
        $('#menucontent').animate({ 'padding-left': '186px' }, anispeed, function() {
            $('body').css('color','#4c4c4c');
            $('#content').fadeOut(anispeed, function() {
                $('#AjaxLoader').fadeIn('fast', function() {
                    $('#menucontent').load(menuLoad, '', function(){
                        $('#content').load(textLoad, '', function(){
                            $('#slideshowcontent').load(slideload, '', function(){
                                $('#AjaxLoader').fadeOut('fast', function() {
                                    $('#content').fadeIn(anispeed, function() {
                                        $('body').css('color','#1129b2');
                                        $('#slideshowcontent').animate({ 'margin-left': 0 }, anispeed);
                                        $('#menucontent').animate({ 'padding-left': 0 }, anispeed, function(){
                                            initLeftmenuEvents();
                                            initContentLinks();
                                            startSlideshow();
                                            startLightbox();

                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
        return false;

    });
}

function initRightmenuEvents() {
    $('#RightContainer a').click(function(){

        var SiteURL = $(this).attr('href')

        var textLoad = SiteURL + " #ContentContainer";
        var menuLoad = SiteURL + " #LeftMenu";
        var slideload = SiteURL + " #SlideshowContainer";

        $('#slideshowcontent').animate({ 'margin-left': '-206px' }, anispeed);
        $('#menucontent').animate({ 'padding-left': '186px' }, anispeed, function() {
            $('body').css('color','#4c4c4c');
            $('#content').fadeOut(anispeed, function() {
                $('#AjaxLoader').fadeIn('fast', function() {
                    $('#menucontent').load(menuLoad, '', function(){
                        $('#content').load(textLoad, '', function(){
                            $('#slideshowcontent').load(slideload, '', function(){
                                $('#AjaxLoader').fadeOut('fast', function() {
                                    $('#content').fadeIn(anispeed, function() {
                                        $('body').css('color','#1129b2');
                                        $('#slideshowcontent').animate({ 'margin-left': 0 }, anispeed);
                                        $('#menucontent').animate({ 'padding-left': 0 }, anispeed, function(){
                                            initLeftmenuEvents();
                                            initContentLinks();
                                            startSlideshow();
                                            startLightbox();

                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
        return false;

    });
}

function initFootermenuEvents() {
    $('#FooterMenu a').click(function(){

        var SiteURL = $(this).attr('href')

        var textLoad = SiteURL + " #ContentContainer";
        var menuLoad = SiteURL + " #LeftMenu";
        var slideload = SiteURL + " #SlideshowContainer";

        $('#slideshowcontent').animate({ 'margin-left': '-206px' }, anispeed);
        $('#menucontent').animate({ 'padding-left': '186px' }, anispeed, function() {
            $('body').css('color','#4c4c4c');
            $('#content').fadeOut(anispeed, function() {
                $('#AjaxLoader').fadeIn('fast', function() {
                    $('#menucontent').load(menuLoad, '', function(){
                        $('#content').load(textLoad, '', function(){
                            $('#slideshowcontent').load(slideload, '', function(){
                                $('#AjaxLoader').fadeOut('fast', function() {
                                    $('#content').fadeIn(anispeed, function() {
                                        $('body').css('color','#1129b2');
                                        $('#slideshowcontent').animate({ 'margin-left': 0 }, anispeed);
                                        $('#menucontent').animate({ 'padding-left': 0 }, anispeed, function(){
                                            initLeftmenuEvents();
                                            initContentLinks();
                                            startSlideshow();
                                            startLightbox();

                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
        return false;

    });
}

function resizer() {

    var winHeight = $(window).height();
    if(winHeight < 628) {
        winHeight = 628;
    }

    $('.MainContainer ').css( 'height', (winHeight-34) + 'px' );
    $('#menucontent').css( 'height', (winHeight-324) + 'px' );
    $('#content').css( 'height', (winHeight-314) + 'px' );
    $('#RightContainer ').css( 'height', (winHeight-194) + 'px' );

}

function startSlideshow() {

    $('#Slideshow').bxSlider({
        mode: 'fade',
        speed: 1000,
        pause: 5000,
        auto: true,
        controls: false,
        auto_direction: 'left',
        next_text: 'next image',
        prev_text: 'previous image',
        width: 186,
        wrapper_class: 'ssc'
    });

}

function loadSitefromLeftmenu(SiteURL) {

    var textLoad = SiteURL + " #ContentContainer";
    var menuLoad = SiteURL + " #LeftMenu";

    $('#menucontent').animate({ 'padding-left': '186px' }, anispeed, function() {
        $('#content').fadeOut(anispeed, function() {

          $('#menucontent').load(menuLoad, '', function(){
            $('#content').load(textLoad, '', function(){
              $('#content').fadeIn(anispeed, function() {
                $('#menucontent').animate({ 'padding-left': 0 }, anispeed, function(){
                    initLeftmenuEvents();
                    alert('fertig');
                });

              });
            });
          });

        });
    });
    return false;

} 

function startLightbox() {
   $('.GalerieContainer a').lightBox({
         fixedNavigation:true,
         imageLoading:'fileadmin/templates/images/lightbox-ico-loading.gif',
         	imageBtnPrev:'fileadmin/templates/images/lightbox-btn-prev.gif',
	imageBtnNext:'fileadmin/templates/images/lightbox-btn-next.gif',
	imageBtnClose:'fileadmin/templates/images/lightbox-btn-close.gif',
	imageBlank:'fileadmin/templates/images/lightbox-blank.gif'
   });
}
