﻿var current = "";
var rotateInterval = 15000;
var useEffect = false;

var currentState = null;
//var $("#SpotlightWrapper > ol") = $("#SpotlightWrapper > ol");

$(document).ready(function() {
});
function RunSpotlight() {
    (useEffect == true) ? effect = 'fade' : effect = 'none';
    $("#SpotlightWrapper > ol").after('<div id="SpotlightNavigation">').cycle({ fx: effect, pause: 1, timeout: rotateInterval, pager: '#SpotlightNavigation' });
    $("#SpotlightNavigation a:last").after('<a href="#" id="SpotlightPauser">Pause</a>').after('<a href="#" id="SpotlightResumer">Play</a>');
    $("#SpotlightResumer").click(function() { ResumeSpotlight(); });
    $("#SpotlightPauser").click(function() { PauseSpotlight(); });
    $("#SpotlightResumer").hide();
}

function PauseSpotlight() {
    $("#SpotlightPauser").hide();
    $("#SpotlightResumer").show();
    $("#SpotlightWrapper > ol").cycle('pause');
}

function ResumeSpotlight() {
    $("object").each(
        function(i) {
            player = $(this)[0];
            if (player.parentNode.style.display != "none")
                player.pauseVideo();
        }
    );
    $("#SpotlightPauser").show();
    $("#SpotlightResumer").hide();
    $("#SpotlightWrapper > ol").cycle('resume', true);
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {    
    ytplayer = document.getElementById("yt" + playerId);
    // This causes the updatePlayerInfo function to be called every 250ms to
    // get fresh data from the player
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
    alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
// Possible values are unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5). 
// When the SWF is first loaded it will broadcast an unstarted (-1) event. When the video is cued and ready to play it will broadcast a video cued event (5).
function onPlayerStateChange(newState) {
    switch (newState) {
        case 3:
        case 1:
            PauseSpotlight();
            break;
        case 5:
            break;
        default:
            // Stopped or paused
            // ResumeSpotlight();
            break;
    }
    currentState = newState;
//    alert( newState);
}
