﻿// JScript File

$(function() {



    // homepage
    // homepage slider
    $.preload('#homeSlide div', { onFinish: playHome });


    $('#control1').css('cursor', 'pointer').click(function() { actionCall(1) });
    $('#control2').css('cursor', 'pointer').click(function() { actionCall(2) });
    $('#control3').css('cursor', 'pointer').click(function() { actionCall(3) });
    $('#control4').css('cursor', 'pointer').click(function() { actionCall(4) });
    $('#control5').css('cursor', 'pointer').click(function() { actionCall(5) });
    $('#control6').css('cursor', 'pointer').click(function() { actionCall(6) });


    

    //On Mega Hover Over

    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
        (function($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() { //for each ul...
                    rowWidth += $(this).width(); //Add each ul's width together
                });
            };
        })(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...

            var biggestRow = 0;

            $(this).find(".row").each(function() {	//for each row...
                $(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });

            $(this).find(".sub").css({ 'width': biggestRow }); //Set width
            $(this).find(".row:last").css({ 'margin': '0' });  //Kill last row's margin

        } else { //If row does not exist...

            $(this).calcSubWidth();  //Call function to calculate width of all ul's
            $(this).find(".sub").css({ 'width': rowWidth }); //Set Width

        }
    }

    //On Hover Out
    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });
    }

    //Set custom configurations
    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 500, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    $("ul#supNav li .sub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
    $("ul#supNav li").hoverIntent(config); //Trigger Hover intent with custom configurations

    $(".video-link").colorbox({ iframe: true, innerWidth: 600, innerHeight: 400 });

});



// functions

// you can change these values
var delay = 10000;
var playCountMax = 2
// do not change these values
var playCount = 0;
var timeOne = 0;
var timeTwo = 0;
var timeThree = 0;
var timeFour = 0;
var timeFive = 0;
var timeSix = 0;
var timeNext = 0;
function playHome() {
    $('#homeSlide').css('background-image', 'none');
    actionCall(1);
}
function show1() {
    playCount++;
    $('#slide6').fadeOut('slow');
    $('#control6').removeClass('active');
    $('#slide1').fadeIn('slow', function() { timeOne = setTimeout("show2()", delay); });
    $('#control1').addClass('active');
}
function show2() {
    if (playCount <= playCountMax) {
        $('#slide1').fadeOut('slow');
        $('#control1').removeClass('active');
        $('#slide2').fadeIn('slow', function() { timeTwo = setTimeout("show3()", delay); });
        $('#control2').addClass('active');
    }
}
function show3() {
    if (playCount <= playCountMax) {
        $('#slide2').fadeOut('slow');
        $('#control2').removeClass('active');
        $('#slide3').fadeIn('slow', function() { timeThree = setTimeout("show4()", delay); });
        $('#control3').addClass('active');
    }
}
function show4() {
    if (playCount <= playCountMax) {
        $('#slide3').fadeOut('slow');
        $('#control3').removeClass('active');
        $('#slide4').fadeIn('slow', function() { timeFour = setTimeout("show5()", delay); });
        $('#control4').addClass('active');
    }
}
function show5() {
    if (playCount <= playCountMax) {
        $('#slide4').fadeOut('slow');
        $('#control4').removeClass('active');
        $('#slide5').fadeIn('slow', function() { timeFour = setTimeout("show6()", delay); });
        $('#control5').addClass('active');
    }
}
function show6() {
    if (playCount <= playCountMax) {
        $('#slide5').fadeOut('slow');
        $('#control5').removeClass('active');
        $('#slide6').fadeIn('slow', function() { timeFour = setTimeout("show1()", delay); });
        $('#control6').addClass('active');
    }
}
function actionCall(sec) {
    clearTimeout(timeNext);
    if (sec <= 3) {
        var nextSec = sec + 1;
    } else {
        var nextSec = 1;
    }
    clearTimeout(timeOne);
    clearTimeout(timeTwo);
    clearTimeout(timeThree);
    clearTimeout(timeFour);
    clearTimeout(timeFive);
    clearTimeout(timeSix);
    $('#homeSlide div').fadeOut('fast');
    $('#controls li').removeClass('active');
    $('#slide' + sec + '').fadeIn('slow');
    $('#control' + sec + '').addClass('active');
    // continuous play control, comment for no autoplay
    if (playCount <= playCountMax) {
        timeNext = setTimeout("show" + nextSec + "()", delay);
    }
}




