﻿var $CURRENT_PAGE_ID = "#page1"; // global ID for the current page.

function setupPages() {
    var windowHeight = $(window).height() - $("#header").outerHeight(true) - $("#links").outerHeight(true) - $("#footer").outerHeight(true);
    $(".page").css({
        'min-height': 337 + 'px'
    });
}

function goToPage(pageId, animate) {
    // Animate by default.
    if (animate === undefined) animate = true;

    var pageMap = { '#1': '#page1', '#2': '#page2', '#3': '#page3', '#4': '#page4', '#5': '#page5', '#6': '#page6', '#7': '#page7' };
    var page = $(pageMap[pageId]);
    //console.log(page);
    if (animate) {
        $('#pages-inner').stop(true, true).animate({
           top: '-' + page.position().top + 'px'
        }, 800);
        $("#pages").stop(true, true).animate({
            height: page.height() + 'px'
        }, 800);
    } else {
        $('#pages-inner').css({ top: '-' + page.position().top + 'px' });
        $('#pages').css({ height: page.height() + 'px' });
    }

    $("#links a").removeClass("selected");
    $("#links a[href='" + pageId + "']").addClass("selected");

    $CURRENT_PAGE_ID = pageMap[pageId];
}

$(document).ready(function () {
    var autoloop_carrousel;

    // Initialize the height of main elements.
    setupPages();
    $("#pages-inner").css({
        overflow: 'hidden',
        position: 'relative'
    });
    $('#pages').css({
        overflow: 'hidden',
        position: 'relative' 	// fix for IE bug with overflow:hidden.
    });

    // Hook up the link events
    var links = $("#links a");
    links.each(function (i) {
        $(this).click(function (e) {
            clearInterval(autoloop_carrousel);
            goToPage($(this).attr('href'), true);
            e.preventDefault();
        });
    });

    // Add hookup for when the user resizes the window.
    $(window).bind("resize", function (e) {
        // Readjust the size of main elements.
        setupPages();
        // Readjust the scroll position of the pages.
        var page = $($CURRENT_PAGE_ID);
        $('#pages-inner').css({ top: '-' + page.position().top + 'px' });
        $('#pages').css({ height: page.height() + 'px' });
    });

    // Directly go to the requested page based on the #hash of the adress bar.
    // (by default, go to the first page)
    var requestedAnchor = window.location.hash;
    if (requestedAnchor == '' || requestedAnchor == '#')
        requestedAnchor = '#1';

    $('html, body').scrollTop(0);
    goToPage(requestedAnchor, false);

    // Set autoloop
    var linksArray = new Array();
    $('#links-inner li').children('a').each(function () {
        linksArray.push($(this).attr('href'));
        //console.log($(this).attr('href'));
    });

    var currentPos = 0;

    if($('#links-inner li').children('a').size() > 1){
    autoloop_carrousel = setInterval(function () {
        goToPage(linksArray[currentPos], true);
        currentPos++;
        if (currentPos == linksArray.length) { currentPos = 0; }
    }, 6000);
    }
});
