﻿$(document).ready(function () {

    $(".homepage-slides").cycle({
        fx: 'scrollHorz',
        prev: '#home-prev',
        next: '#home-next',
        after: onAfter,
        before: onBefore,
        timeoutFn: calculateTimeout
    });

    $(".homepage-slides2")
	.before('<div id="slidenav">')
	.cycle({
	    fx: 'scrollHorz',
	    speed: 'fast',

	    pager: '#slidenav',
	    after: onAfter,
	    before: onBefore,
	    timeoutFn: calculateTimeout
	});

	$('.homepage-slides2').hover(
        function () {$('.homepage-slides2').cycle('pause');},
        function () {$('.homepage-slides2').cycle('resume');}
        );

    $(".tab_content").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab_content:first").show();
    $("ul.tabs li").click(function () {

        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();

        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });


    $('.office-location-dot').mouseenter(function () {
        $('.office-location-details').hide();
        $('.office-location').css("z-index", "1000");
        $(this).parent().find('.office-location-details').show();
        $(this).parent().css("z-index", "2000");

    });

    $('.office-location-details').mouseleave(function () {
        $(this).hide();
        $('.office-location').css("z-index", "1000");
    });

    $('.hotspot-close').click(function () {
        $(this).parent().parent().hide();
        $(this).parent().parent().parent().find('.hotspot-bottom').hide();
    });



});

function calculateTimeout(currElement, nextElement, opts, isForward) {
    var index = opts.currSlide;
    return index == 0 ? 10000 : 10000;
} 
    

function onBefore(curr, next, opts) {
    var description = $(next).find('.description');
    $(description).css("left", -335);

    var collection = $(next).find('.brands-belt img');

    var beltWidth = 0;
    $(collection).each(function (index) {
        beltWidth += $(this).width() + 20;
    });
    /*
    if (beltWidth < 900) {
        var content = $(next).find(".scrollableArea");
        $(content).append(content.html());
    }

    beltWidth = 0;
    var updatedcollection = $(next).find('.brands-belt img');
    $(updatedcollection).each(function (index) {
        beltWidth += $(this).width() + 20;
    });
    if (beltWidth < 900) {
        var content = $(next).find(".scrollableArea");
        $(content).append(content.html());
    }
    */
}

function onAfter(curr, next, opts) {
    AnimateHomepageSlide($(next));
}

function AnimateHomepageSlide(slide) {

    var description = $(slide).find('.description');

    $(description).animate({
        left: 81
    }, 500, function () {
    });

    var collection = $(slide).find('.brands-belt img');
    var brandsBelt = $(slide).find('.brands-belt');

    var beltWidth = 0;
    $(collection).each(function (index) {
        beltWidth += $(this).width() + 20;
    });


    /*
    if (beltWidth > 900) {
        var logoParade = $(slide).find('#logoParade');

        $(logoParade).smoothDivScroll({ autoScroll: "always", autoScrollDirection: "endlessloopright", autoScrollStep: 3, autoScrollInterval: 40 });

        // Logo parade
        $(logoParade).bind("mouseover", function () {
            $(this).smoothDivScroll("stopAutoScroll");
        }).bind("mouseout", function () {
            $(this).smoothDivScroll("startAutoScroll");
        });
    } else if ($(collection).length > 0) {

        AnimateSinglePageLogos(collection, 0, collection.length);
    }
    */


}

function AnimateSinglePageLogos(logos, index, numImages) {

    totalWidth = 0;

    $(logos[index]).css("left", 975);

    $(logos[index]).show();

    $(logos[index]).animate({
        left: totalWidth
    }, 250, function () {
        if (index < (numImages - 1)) {
            var nextIndex = index + 1;
            AnimateSinglePageLogos(logos, nextIndex, numImages);
        }
    });
}

function Goto(url) {
    window.open(url);
}

function ToggleHotpotHtml(hotspotId) {
    var $hotspotHtml = $(hotspotId);
    var visibleAlready = false;
    if ($($hotspotHtml).css("display") == "block") {
        visibleAlready = true;
    }
    $('.hotspot').hide();
    $('.pulser-box').css("z-index", "1000");
    $('.hotspot-bottom').hide();
    if (!visibleAlready) {
        $hotspotHtml.show();
        $($hotspotHtml).parent().find('.hotspot-bottom').show();
        $($hotspotHtml).parent().css("z-index", "2000");

        // Check if the pulser box would appear out of bounds, if so, then move it
        var pulserBoxLeft = parseInt(($hotspotHtml).parent().css("left"));
        if (pulserBoxLeft > 666) {
            $hotspotHtml.css("left", -(pulserBoxLeft - 666));
            $($hotspotHtml).parent().find('.hotspot-bottom').css("left", -(pulserBoxLeft - 666));
        }
        
    }
}

function ShowLocation(hotspotId) {
    var $hotspotHtml = $(hotspotId);
    $hotspotHtml.show();
}

function HideLocation(hotspotId) {
    var $hotspotHtml = $(hotspotId);
    $hotspotHtml.hide();
}




(function ($) {
    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var ah = $(this).height();
            var ph = $(this).parent().parent().parent().height();
            var mh = (ph - ah) / 2;
            $(this).css('margin-top', mh);
        });
    };
})(jQuery);




