﻿var IDs;

$(document).ready(function() {

    //Center screen at low resolution
    $(this).hCenter();

    // Imagegallery
    $(".imagePopup").colorbox({ photo: true });


    /*Home Slideshow*/
    $("#slide_nav").empty();
    $('#slideshow').cycle({
        fx: 'scrollHorz',
        speed: 500,
        timeout: 6000,
        pager: '#slide_nav',
        pagerAnchorBuilder: function(idx, slide) {
            // return selector string for existing anchor
            return '<li></li>';
        }
    });

    /*Home Slideshow - get left margin*/
    //alert($("#slide_nav").children().size());
    var noSlides = $("#slide_nav").children().size();
    var marginLeft = 950 - (noSlides * 20);
    $("#slide_nav").css('margin-left', marginLeft + 'px');

    /*Logo slideshow*/
    $('#logo_slideshow').cycle({
        fx: 'fade',
        speed: 5,
        timeout: 5000,
        cleartypeNoBg: true
    });

    $('.logo_item').hover(function() {
        $('#logo_slideshow').cycle('pause');
        $('.quote_item').hide();
        var index = $('.logo_item').index(this);
        var numberOfLogos = $('.logo_item').size() - 1;
        if (index == numberOfLogos)
            $('#logo_slideshow').cycle(0);
        else
            $('#logo_slideshow').cycle(index + 1);
        $('#logo_slideshow').cycle(index);
    },
        function() {
            $('#logo_slideshow').cycle('resume');
        });


    /*Customer Logotypes*/
    $(".logo_item").hover(
        function() {
            $(this).toggleClass('logo_item_hover');
        }
    );

    /*Tour Slideshow*/
    $('#tour_slideshow').cycle({
        fx: 'scrollLeft,scrollDown,scrollRight,scrollUp',  //List of effects http://jquery.malsup.com/cycle/browser.html
        randomizeEffects: false,
        timeout: 0,
        speed: 500,
        startingSlide: 0
    });

    /*Tour History*/

    //Find all div elements IDs
    IDs = $("#tour_content div[id]")         // find divs with ID attribute
    .map(function() { return this.id; }) // convert to set of IDs
    .get(); // convert to instance of Array (optional)

    var divs = $.makeArray($("#tour_content div[id]")); //Array of all div elements


    if (window.location.hash == "") {
        if (divs.length > 0) { //To check if we are on the tour
            $(divs).hide(); //Chrome fix
            $(divs[0]).show(); //Chrome fix
        }
    }

    $.history.init(function(url) {
        var index = $.inArray('div_' + url, IDs);
        if (index == -1)
            index = 0;
        var selectedDiv = $(divs).get(index);
        $(divs).hide();
        $(selectedDiv).show();

        $('#tour_slideshow').cycle(index);
        SelectMenuItem($("#tour_menu_content .root").get(index));
    });

    $('#tour_menu_content a').live('click', function(e) {
        var url = $(this).attr('href');
        url = url.replace(/^.*#/, '');
        var pageNodeID = ($(this).attr('id')).substring(7);
        if (pageNodeID != "") {
            setTimeout("$f('player" + pageNodeID + "').play()", 800);
        }

        $.history.load(url);

        return false;
    });

    //Tour Forward
    $('#tour_forward').click(
        function() {
            var url = GetHash();
            var index = $.inArray('div_' + url, IDs);
            if (url == "")
                index = 0;
            if (index == -1)
                index = 0;
            else if (index == (IDs.length - 1))
                index = 0;
            else
                index = index + 1;

            ForwardBackward(index);
        }
    );

    //Tour Backward
    $('#tour_back').click(
        function() {
            var url = GetHash();
            var index = $.inArray('div_' + url, IDs);
            if (url == "")
                index = 0;
            if (index == -1)
                index = 0;
            else if (index == 0)
                index = IDs.length - 1;
            else
                index = index - 1;

            ForwardBackward(index);
        }
    );


    //References
    HideAllReferences(true);
});


function SelectMenuItem(item) {
    if (item != null) {
        
        //Set visit in clicky
        if (typeof(analytics) != "undefined") {
            var link = $(item).find("a");
            var filename = location.pathname + "/" + link.attr("href").substring(1);
            var title = link.text();
            analytics.log(filename, title, "pageview");
        }

        //Reset all menu items
        $("#tour_menu_content div").removeClass("tour_item_selected");
        $("#tour_menu_content div").addClass("tour_item_wrap");
        var allSpans = $("#tour_menu_content").find("span");
        $(allSpans).removeClass("tour_gradient");
        $(allSpans).addClass("tour_menu_item");
        var allWhites = $("#tour_menu_content div div");
        $(allWhites).removeClass("tour_selected_arrow");
        $(allWhites).addClass("white");

        //Select the correct item
        $(item).removeClass("tour_item_wrap");
        $(item).addClass("tour_menu_item_selected");
        var selectedSpan = $($(item).find("span"));
        $(selectedSpan).removeClass("tour_menu_item");
        $(selectedSpan).addClass("tour_gradient");
        $($(item).find("div")).addClass("tour_selected_arrow");

    }
}

function ForwardBackward(index) {
    var newUrl = IDs[index].substring(4);
    $.history.load(newUrl);
    //Autoplayfix
    var links = $('.root a');
    for (var i = 0; i < links.length; i++) {
        var href = $(links[i]).attr('href');
        if (href == '#' + newUrl) {
            var pageNodeID = ($(links[i]).attr('id')).substring(7);
            if (pageNodeID != "") {
                setTimeout("$f('player" + pageNodeID + "').play()", 800);
            }
        }
    }
}

function GetHash() {
    var url = window.location.hash;
    return url.replace(/^.*#/, '');
}

function ReferenceClick(title) {
    HideAllReferences(false);
    ShowReference(title);
}

function HideAllReferences(firstLoad) {
    $('#referenceWrap .referenceItem').hide();
    if (firstLoad) {
        SelectStartReference();
    }
}

function ShowReference(title) {
    $('#Ref_' + title).show();

    //Set visit in clicky
    if (typeof (analytics) != "undefined") {
        var filename = location.pathname + "/" + title;
        analytics.log(filename, title, "pageview");
    }
}

function SelectStartReference() {
    var hash = GetHash();
    if (hash != "") {
        ShowReference(hash);
    } else {
        //var total = $('#referenceWrap .referenceItem').size();
        //var randomnumber=parseInt(Math.floor(Math.random()*total));
        //$('#referenceWrap .referenceItem:eq('+randomnumber+')').show();
        $('#referenceWrap .referenceItem:first').show();
    }
}

var carusel_init = false;
function caruselInit() {
    if (!carusel_init) {
        $('.jMyCarousel ul').stop();
        $(".jMyCarousel").jMyCarousel({
            visible: '100%',
            eltByElt: false,
            circular: true,
            start: 3
        });
        carusel_init = true;
    }
}

function TourAutoPlayClick() {
    var url = $(this).attr('href');
    url = url.replace(/^.*#/, '');
    var pageNodeID = ($(this).attr('id')).substring(7);
    if (pageNodeID != "") {
        setTimeout("$f('player" + pageNodeID + "').play()", 800);
    }

    $.history.load(url);

    return false;
}









