// CAROUSEL //
// on document ready
$(function() {

    function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function rotate(el, deg) {
        el.css({
            "-moz-transform": "rotate(" + deg + "deg)",    /* Firefox */
            "-webkit-transform": "rotate(" + deg + "deg)", /* Safari */
            "-o-transform": "rotate(" + deg + "deg)",      /* Opera */
            "transform": "rotate(" + deg + "deg)",          /* CSS3 Standard */
            "-ms-transform": "rotate(" + deg + "deg)"   /* IE9 */
        });
    }


    var allHeroPromos = $("#vhc-hp-carousel .heroPromo");
    var allHeroThumbs = $("#heroThumbs li a");

    allHeroPromos.each(function(i) {
        var promo = $(this);
        promo.attr("id", "hero" + i);
        // fix the jitter on first playback of carusel
        $('#hero' + i).css({ opacity: 0.0 });
    })

    // fix the jitter on first playback of carusel
    $('#hero0').css({ opacity: 1.0 });
    // highlight
    HighlightHero(0);

    allHeroThumbs.each(function(i) {

        // reference
        var href = $(this);
        $(this).attr("href", "#hero" + i);
        $(this).attr("class", "hero" + i);
        // });

        // onclick
        href.click(function(e) {

            paused = true;

            // stop it jumping
            e.preventDefault();

            // highlight
            HighlightHero(i);

            //        // elements
            var toFadeOut = $("#vhc-hp-carousel .active");
            var toFadeIn = $(allHeroPromos[i]);

            //        // guards - same clicked or is animating
            if (toFadeOut.attr("id") === toFadeIn.attr("id")) { return; };
            if (toFadeOut.is(":animated") || toFadeIn.is(":animated")) { return; };

            //        // animation
            toFadeOut.addClass('last-active');
            toFadeIn.css({ opacity: 0.0 })
                             .addClass('active')
                             .animate({ opacity: 1.0 }, 1000, function() {
                                 toFadeOut.removeClass('active last-active');
                             });
        });
    });

    //rotate the banners
    BuildEvents();
    setTimeout("PlayCarusel()", 1000);
    //PlayCarusel();

});

// aidan hutchinson (zolv) - additional routines to animate the carusel
var CTimer;
var rotation = 1;
var heromax = 2; // the number of slides in use where the amount is -1 becuase 0=1 (zero based)

var allHeroPromos = $("#vhc-hp-carousel .heroPromo");
var allHeroThumbs = $("#heroThumbs li a");
var paused = false;
var firstTime = true;

function BuildEvents() {
    for (var i = 0; i < heromax; i++) {
        $('#hero' + i).mouseout(function(e) { paused = false; });
        $('#hero' + i).mouseover(function(e) { paused = true; });
    }
}


function PlayCarusel() {

    if (paused == false) {
        // animate the carusel

        var hero = rotation;

        var toFadeOut = $("#vhc-hp-carousel .active");
        toFadeOut.addClass('last-active');

        $('#hero' + hero).css({ opacity: 0.0 })
                     .addClass('active')
                     .animate({ opacity: 1.0 }, 1000, function() {
                         toFadeOut.removeClass('active last-active');
                     });

        HighlightHero(hero);
        rotation += 1;
        if (rotation == heromax + 1) { rotation = 0; }
    }

    ResetTimer();
}

function ResetTimer() {
    CTimer = setTimeout('PlayCarusel()', 6000);
}

function HighlightHero(current) {
    var allHeroThumbs = $("#heroThumbs li a");
    allHeroThumbs.each(function(i) {
        var href = $(this);
        if (current == i) {
            //highlight with class
            href.css({ border: '3px solid #cc0000' });
        } else {
            href.css({ border: '3px solid #ffffff' });
        }
    });
}
