﻿// ROTATES HOME PAGE BANNERS
//LSC.BannerState = function() { return { LOADING: 0, PLAYING: 1, PAUSED: 2 }; } ();

function BannerRotator(iSelector, iScroll, iPause, showNav) {
    var base = this;
    var c = $(iSelector + ' .slide').length;
    var sel = iSelector;
    base.transitionTime = iScroll;
    var pauseTime = iPause;
    base.Play = new Object;
    base.Pause = new Object;

    if (c > 1) {
        if (base.transitionTime == null) { base.transitionTime = 400; } //default scroll time (length of transition)
        if (pauseTime == null) { pauseTime = 5000; } //default pause time (how long to hold the image between transitions)
        $(sel + ' .slide').each(function(intIndex) { $(this).attr('rel', (intIndex + 1)); }); //add the list position to the each of the items

        if (showNav != null) {
            var i = 1;

            // create the banner pager
            // $(sel).append("<div id='bannerpager'><div><img src='//static.preen.com/skin/1/bg/bannernavleft.gif' alt='' /></div></div>");
            $(sel).append('<div id="bannerpager"><div></div></div>');
            $pager = $(sel + ' #bannerpager div').append("<img src='//static.preen.com/skin/1/bg/bannernavleft.gif' />");
            while (i <= c) {
                if (i == $(sel + ' .slide:visible').attr('rel')) { //if its the nav item that belongs to the visible image, mark it as the active nav item
                    $('#bannerpager div').append('<a class="slidenav active" rel="' + i + '" href="#"></a>');
                } else {
                    $('#bannerpager div').append('<a class="slidenav" rel="' + i + '" href="#"></a>');
                };
                i++;
            };

            $pager.append('<span class="pause"></span>').append('<span href="#" class="play" style="display:none;"></span>'); //play button
            $pager.append("<img src='//static.preen.com/skin/1/bg/bannernavright.gif' />");
            base.Pause = $('#bannerpager .pause');
            base.Play = $('#bannerpager .play');

            $('#bannerpager a').click(function() { //handle navigation by clicking nav items
                $('#bannerpager a.active').removeClass('active');
                $(this).addClass('active'); //move the active nav item to this item
                var currentClassName = $(sel + ' .slide:visible').attr('rel');
                var nextClassName = $(this).attr('rel');
                clearTimeout($('#bannerpager').attr('TID')); //stop the images from looping when a nav button is pressed
                base.Pause.hide();
                base.Play.show();
                if (nextClassName != currentClassName) { //only change images if they clicked on a new item (not the one they are viewing)
                        $(sel + ' .slide:visible').fadeOut(base.transitionTime, function() {
                        $(sel + ' .slide[rel=' + nextClassName + ']').fadeIn(base.transitionTime);
                    });
                }
                return false;
            });

            base.Pause.click(function() {
                clearTimeout($('#bannerpager').attr('TID')); //stop the images looping on pause click
                base.Pause.hide();
                base.Play.show();
            });

            base.Play.click(function() { //start the images looping on play click
                playerActive = false;
                scroll();
                base.Play.hide();
                base.Pause.show();
            });

        };

        $('#bannerpager').attr('TID', setTimeout(function() { scroll(); }, pauseTime)); //save the timeout id so we can cancel the loop later if a nav button is pressed

    };

    var scroll = function() {
        if (playerActive == true) {// do not scroll if the user has initiated a YouTube video player
            clearTimeout($('#bannerpager').attr('TID'));
            base.Pause.hide();
            base.Play.show();
            return false;
        };
        currentClass = $(sel + ' .slide:visible').attr('rel'); //get the list position of the current image
        nextClass = $(sel + ' .slide:visible').attr('rel'); //open a new variable for the next class
        if (currentClass == c) { nextClass = 1; } //if you've reached the end of the images... start from number 1 again
        else { nextClass++; } //if not just add one to the last number

        $(sel + ' .slide[rel=' + currentClass + ']').fadeOut(base.transitionTime, function() { //fade out old image
            $(sel + ' .slide[rel=' + nextClass + ']').fadeIn(base.transitionTime); //fade in new image once old one has finished fading out
        });

        $("#bannerpager a.active").removeClass('active'); //remove active class from our nav
        $("#bannerpager a[rel=" + nextClass + "]").addClass('active'); //add new active class to the next nav it

        $(sel).height(parseInt($(sel + ' .slide:visible:eq(0)').height()) + parseInt($(sel + ' #bannerpager').height()));
        var timeout = setTimeout(function() { scroll(c, sel, base.transitionTime, pauseTime); }, pauseTime); //scroll the banners again after waiting for pauseTime
        $("#bannerpager").attr('TID', timeout); //save the timeout id so we can cancel the loop later if a nav button is pressed
    };
};

$(document).ready(function() {
    bannerRotator = new BannerRotator('#bannerslides', 500, 7000, true);

    // Added to handle iframe embedded videos
    var overiFrame = -1;
    $('iframe').hover(function() {
        overiFrame = $(this).closest('.video-banner').attr('bannerid');
    }, function() {
        overiFrame = -1
    });
    $(window).blur(function() {
    if (overiFrame != -1)
            bannerRotator.Pause.click();
    });
});
