﻿google.load("swfobject", "2.1");

function onYouTubeStateChange(newState) {
    if (newState == 0) {
        $("#imgYouTubePlay").attr("src", "/imgs/btn-play.png");
        onYouTubeNext();
    }
    else if (newState == 1) 
    {
        $("#imgYouTubePlay").attr("src", "/imgs/btn-pause.png");
    }
    else if (newState == 2) {
        $("#imgYouTubePlay").attr("src", "/imgs/btn-play.png");
    }
    else if (newState == 5) {
        $("#imgYouTubePlay").attr("src", "/imgs/btn-play.png");
    } 
}

function onYouTubeDocumentReady() {
    var atts, params;

    atts = { id: "myytplayer" };
    params = { allowScriptAccess: "always", wmode: "opaque" };

    //$("#slideshow").css("display", "none");
    //$("#youTubePlayer").css("display", "none");

    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "920", "419", "8", null, null, params, atts);
}

function onYouTubeInterval() {
    function toTimeSpan(totalSec) {
        var minues, seconds

        totalSec = Math.round(totalSec);

        minutes = parseInt(totalSec / 60) % 60;
        seconds = totalSec % 60;

        return (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
    }

    $("#youTubeCurrentTime").text(toTimeSpan(ytplayer.getCurrentTime()));
    $("#youTubeDuration").text(toTimeSpan(ytplayer.getDuration()));
}

function onYouTubeMouseOut() {
    //$("#youTubeControls").fadeOut("fast");
    $("#youTubeControls").hide();
}

function onYouTubeMouseOver() {
    //$("#youTubeControls").fadeIn("fast");
    $("#youTubeControls").show();
}


function onYouTubeNext() {
    ytplayer.stopVideo();
    $("#youTubePlayer").fadeOut("slow");
    $("#slideshow").fadeIn("slow");
}

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");    
    ytplayer.addEventListener("onStateChange", "onYouTubeStateChange");
    ytplayer.loadVideoById("YedOcEtx_jw", 0);
    ytplayer.setVolume(9);

    setInterval(onYouTubeInterval, 1000);    
}

function onYouTubePlay() {
    if (ytplayer.getPlayerState() == 1) ytplayer.pauseVideo();
    else ytplayer.playVideo();
}

function onYouTubeStop() {
    ytplayer.stopVideo();
    ytplayer.seekTo(0);
    ytplayer.stopVideo();
}
