﻿var fadeTime = 5000; //time in between slides (in milliseconds)
var fadeTo = 0.6; //opacity of thumbnails when not active.
var stopSlider = false; //global var defaulting to false
var ytplayer = ""; //global var to hold youtube player object
var moogaloop = ""; //global var to hold vimeo player object
$(document).ready(function () {
    $("#featured")
        .bind("tabsselect", function (event, ui) {
            $('.ui-tabs-nav-item').each(function (index) {
                if ($(this).hasClass('ui-state-active')) {
                    $('img', this).stop().animate({ opacity: fadeTo });
                }
            });
            $('#' + ui.tab.parentNode.id + ' img').stop().animate({ opacity: 1 });
        })
        .tabs()
    //.tabs({ fx: { opacity: "toggle"} })
        .tabs("rotate", fadeTime, true);

    $('.ui-tabs-nav-item').each(function (index) {
        if (!$(this).hasClass('ui-state-active')) {
            $('img', this).stop().animate({ opacity: fadeTo });
        }
    });

    $("#featured").hover(
        function () {
            sliderStop();
            //console.log('Hover: Stopped Slider');
        },
        function () {
            if (!stopSlider) {
                sliderStart();
                //console.log('Hover: Started Slider');
            }
        }
    );
    $(".ui-tabs-nav-item a").click(
        function () {
            stopSlider = true;
            stopVideos();
            sliderStop();
            _gaq.push(['_trackEvent', 'Hero', 'Click', $(this).find('img').attr('alt')]); //the alt attribute from the thumbnail img is passed to GA to record this event
            //console.log('Click: Stopped Slider' + $(this).find('img').attr('alt'));
        }

    );
    //handle opacity change for inactive thumbnails
    $('.ui-tabs-nav-item').hover(function () {
        if (!$(this).hasClass('ui-state-active')) {
            $('img', this).stop().animate({ opacity: 1 });
        }
    }, function () {
        if (!$(this).hasClass('ui-state-active')) {
            $('img', this).stop().animate({ opacity: fadeTo });
        }
    });
});

function sliderStop() {
    $("#featured").tabs("rotate", 0, true);
}

function sliderStart() {
    $("#featured").tabs("rotate", fadeTime, true);
}

function stopVideos() {
    //stop all videos IE issue
    try {
        ytplayer.pauseVideo();
    }
    catch (err) { }
    try {
        moogaloop.api_pause();
    }
    catch (err) { }
}

//youtube JS API
function onYouTubePlayerReady(playerId) {
    var spotPos = playerId.toString().split('%5F')[1];
    ytplayer = document.getElementById('myytplayer_' + spotPos);
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
function onytplayerStateChange(newState) {
    if (newState == 1 || newState == 3) {
        sliderStop();
        stopSlider = true;
        //console.log('YouTube: Stopped Slider');
        if (newState == 1)
            _gaq.push(['_trackEvent', 'Video', 'Play', ytplayer.id]);
    }
    else if (newState == 0)
        stopSlider = false; //remove if you don't want the hover to start the slideshow again
}



//vimeo JS API
function vimeo_player_loaded(swf_id) {
    //console.log(swf_id);
    var spotPos = swf_id.toString().split('_')[1];

    moogaloop = document.getElementById('myvmplayer_' + spotPos);
    moogaloop.api_addEventListener("onPlay", "vimeo_on_play");
    moogaloop.api_addEventListener("onFinish", "vimeo_on_finish");
}
function vimeo_on_play(swf_id) {
    //console.log('Vimeo: Stopped Slider');
    _gaq.push(['_trackEvent', 'Video', 'Play', swf_id]);
    sliderStop();
    stopSlider = true;
}
function vimeo_on_finish(swf_id) {
    stopSlider = false; //remove if you don't want the hover to start the slideshow again
}


