var ajaxContent = null;
var debug = 1;
var menue = null;
var pageContent = null;
var navigation = null;
var teaser = null;



var slideshow = null;
var slideshowCurrentPage = 0;
var slideshowPageCount = 0;
var slideshowTimerActive = true;
var slideShowTimerInterval = 5000;



$(document).ready(function ()
{

    // Set up the competition overlay close button
    $('#competitionOverlay .closeButton').click(function()
    {
        // Fade out the overlay
        $('#competitionOverlay').fadeOut(250);
    });


    if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
    {

        $(window).scroll(function ()
        {
            $('title').text($('body').scrollTop());

            $('.videoOverlay,#errorOverlay').css('top', $(window).scrollTop());

        });

        $('.videoOverlay,#errorOverlay').css('top', $(window).scrollTop());

    };

    registerCompetitionButton();

    // Get some elements
    ajaxContent = $('#ajaxContent');
    menue = $('#menue ul');
    navigation = $('#headerCenter .links .navigation .subnavigation');
    navigation2 = $('#headerCenter .links .navigation2 .subnavigation2');
    pageContent = $('#pageContent');
    teaser = $('#teaser');



    navigation.parent()
        .mouseenter(function ()
        {
            if ($.browser.msie() && $.browser.version.number() == 7 && $('#teaserCenter').length == 0)
            {
                $('body').addClass('ie7naviFix');
            }

            navigation
                .css({ display: 'block' })
                .find('.subnavigationList')
                    .stop()
                    .animate({ height: 298 }, 400 * debug, function ()
                    {
                        $(this).animate({ height: 278 }, 200 * debug);
                    });

            navigation.find('.mediaButtons').stop().animate({ opacity: 1 }, 400 * debug);
        })
        .mouseleave(function ()
        {
            navigation
                .find('.subnavigationList')
                    .stop()
                    .animate({ height: '+=20' }, 200 * debug, function ()
                    {
                        $(this).animate({ height: 0 }, 400 * debug, function ()
                        {
                            if ($.browser.msie() && $.browser.version.number() == 7 && $('#teaserCenter').length == 0)
                            {
                                $('body').removeClass('ie7naviFix');
                            }

                            $(this).parent().css('display', 'none');
                        });
                    });
            navigation.find('.mediaButtons').stop().animate({ opacity: 0 }, 400 * debug);
        })
        ;


navigation2.parent()
        .mouseenter(function ()
        {
            if ($.browser.msie() && $.browser.version.number() == 7 && $('#teaserCenter').length == 0)
            {
                $('body').addClass('ie7naviFix');
            }

            navigation2
                .css({ display: 'block' })
                .find('.subnavigationList2')
                    .stop()
                    .animate({ height: 40 }, 400 * debug, function ()
                    {
                        $(this).animate({ height: 40 }, 200 * debug);
                    });

            navigation2.find('.mediaButtons').stop().animate({ opacity: 1 }, 400 * debug);
        })
        .mouseleave(function ()
        {
            navigation2
                .find('.subnavigationList2')
                    .stop()
                    .animate({ height: '+=20' }, 200 * debug, function ()
                    {
                        $(this).animate({ height: 0 }, 400 * debug, function ()
                        {
                            if ($.browser.msie() && $.browser.version.number() == 7 && $('#teaserCenter').length == 0)
                            {
                                $('body').removeClass('ie7naviFix');
                            }

                            $(this).parent().css('display', 'none');
                        });
                    });
            navigation2.find('.mediaButtons').stop().animate({ opacity: 0 }, 400 * debug);
        })
        ;


    menue.find('a').click(function ()
    {
        // Fade out the competition button
        $('#competitionButton').fadeOut(function ()
        {
            $(this).remove();
        });

        // Get the link
        var link = $(this);
        menue.find('a.selected').removeClass('selected');
        link.addClass('selected');

        ajaxContent.fadeOut(250);


        pageContent.animate({ marginTop: -479 }, 100 * debug, function ()
        {
            if ($.browser.msie() && $.browser.version.number() == 7)
            {
                $('#teaserContent').animate({ height: 0, opacity: 0 }, 500 * debug);
            }


            teaser.animate((!$.browser.msie() ? { height: 0, opacity: 0} : { height: 0 }), 500 * debug, function ()
            {
                $('#teaserCenter').remove();
            });
            menue.animate({ top: -11 }, 500 * debug);

            $.get(link.attr('href'), function (data)
            {
                // Try to match the content
                var source = data.substring(data.indexOf('<!-- CONTENT START -->'), data.indexOf('<!-- CONTENT END -->'));




                ajaxContent.oneTime(1000, 'fadeInContent', function ()
                {
                    ajaxContent.html(source);

                    // Hide the (new) competition button
                    $('#competitionButton').hide();

                    registerCompetitionButton();
                    
                    ajaxContent.fadeIn(250, function ()
                    {
                        // Fade in the competition button
                        $('#competitionButton').fadeIn();

                    });



                    addVideoButtons();
                });
            });

        });




        return false;
    });




    // Try to get the slideshow
    slideshow = $('#teaserContent');

    // Check whether we found a slideshow
    if (slideshow.length > 0)
    {
        // Count all childs
        slideshowPageCount = slideshow.find('> ul > li').length;

        // Check whether we got more than one page
        if (slideshowPageCount > 1)
        {
            // Build the page selection
            var pageSelectionList = $('<ul></ul>').addClass('pageSelection');
            for (i = 0; i < slideshowPageCount; ++i)
            {
                pageSelectionList.append($('<li></li>').text('0' + (i + 1)).addClass('_' + i).click(function ()
                {
                    var pageNumber = pageSelectionList.find('li').index($(this));

                    moveToSlideShowPage(pageNumber);
                    slideshowCurrentPage = pageNumber;
                    resetSlideshowTimer();
                }));
            }
            slideshow.append(pageSelectionList);

            // Highlight the first selector
            pageSelectionList.find('li:eq(0)').addClass('selected');

            // Build the play/pause button
            var playPauseButton = $('<span></span>').addClass('playPauseButton pause').click(function ()
            {
                $(this).toggleClass('pause play');

                slideshowTimerActive = !slideshowTimerActive;

                if (!slideshowTimerActive)
                {
                    slideshow.stopTime('slideshow');

                }
                else
                {
                    slideshow.everyTime(slideShowTimerInterval, 'slideshow', nextSlideshowPage);

                }

                //


            });
            slideshow.append(playPauseButton);

            slideshow.append($('<span></span>').addClass('previousSlideshowPage').click(previousSlideshowPage));
            slideshow.append($('<span></span>').addClass('nextSlideshowPage').click(nextSlideshowPage));

            slideshow.everyTime(slideShowTimerInterval, 'slideshow', nextSlideshowPage);
        }
    }



    addVideoButtons();


});

var addVideoButtons = function ()
{
    // Add close buttons to video overlays
    $('.videoOverlay,#errorBox').append($('<span></span>').addClass('close').text('Schließen'));
    $('.videoOverlay span,#errorBox span').click(function () { $(this).parent().hide(); $('#errorOverlay').hide(); })
}

var moveToSlideShowPage = function (pageIndex)
{
    if (!pageIndex && pageIndex != 0)
    {
        pageIndex = slideshowCurrentPage;
    }

    slideshow.find('ul.pageList').stop().animate(
    {
        marginLeft: (pageIndex * 985) * -1
    }, 500, function ()
    {

        // Highlight the selector
        $('#teaserContent .pageSelection li.selected').removeClass('selected');
        $('#teaserContent .pageSelection li:eq(' + pageIndex + ')').addClass('selected');

    });
}

var nextSlideshowPage = function ()
{
    // Increase the current page
    ++slideshowCurrentPage;

    // Check the bounds
    if (slideshowCurrentPage >= slideshowPageCount)
    {
        slideshowCurrentPage = 0;
    }
    resetSlideshowTimer();

    moveToSlideShowPage();
}

var previousSlideshowPage = function ()
{
    // Decrease the current page
    --slideshowCurrentPage;

    // Check the bounds
    if (slideshowCurrentPage < 0)
    {
        slideshowCurrentPage = slideshowPageCount - 1;
    }
    resetSlideshowTimer();

    moveToSlideShowPage();
}

var registerCompetitionButton = function ()
{
    // Register the competition button function
    $('#competitionButton').click(function ()
    {
        // Get the document height
        var documentHeight = $(document).height();

        // Check some bounds
        if (documentHeight < 1050)
        {
            documentHeight = 1050;
        }

        $('#competitionOverlay').height(documentHeight).fadeIn(250);

    });



}

var resetSlideshowTimer = function ()
{
    slideshow.stopTime('slideshow');

    if (slideshowTimerActive)
    {
        slideshow.everyTime(slideShowTimerInterval, 'slideshow', nextSlideshowPage);
    }
}

