﻿/***********************
FRAME DIGITAL
YOUTUBE CONTROLLER
***********************/

var params = { allowScriptAccess: "always", rel: "0", border: "1", color2: "0x21014d", color1: "0x21014d", hd: "1", autoplay: "0"};
var atts = { id: "myytplayer" };
var ytplayer;

$(document).ready(function() {
    LoadYouTube();
  //  setupRating();

    $("#videoId").val("dtR9Fxz2lng");

});

function LoadYouTube() {
    swfobject.embedSWF("http://www.youtube.com/v/uc_ED-EDN2E?enablejsapi=1&amp;playerapiid=ytplayer&amp;rel=0&amp;border=0&amp;color1=0x21014d&amp;color2=0x21014d&amp;hd=1&amp;autoplay=0", "ytapiplayer", "640", "360", "8", null, null, params, atts);
    play_vid("uc_ED-EDN2E");
}

// ------------------------
// API callback function
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    ytplayer.addEventListener("onError", "onPlayerError");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.setPlaybackQuality("large");
    ytplayer.pauseVideo();
}

// ------------------------
// state change listener
// Possible values are unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).
function onytplayerStateChange(newState) {
    if (newState == 1) {
        callToActionscript(); // mute flash
    } 
}

// ------------------------
// error listener
function onPlayerError(errorCode) {
    alert("An error occured: " + errorCode);
}

// ------------------------
// play new video
function play_vid(youtube_id) {
    if (ytplayer) {
        ytplayer.loadVideoById(youtube_id, 0);
        callToActionscript();
    }


    $("#videoId").val(youtube_id);

    // get video information
    $.getJSON("/common/handlers/video.ashx?ut=" + youtube_id,
        function(video) {
            $("div.info-box").fadeOut("fast", function() {
                $("p.now-playing").html("Now Playing: " + video.Title);
                $("p#description").html(video.Description.replace(/\n/g, "<br />"));
                $("div.views").html(video.ViewCountFormatted + " views");
                $("ul.stars li").width(GetStarWidth(video.RatingFormatted));
                $("div.info-box").fadeIn("medium"); // fade in again
            }); // got the info so fade out while replacing?
        });
}


function GetStarWidth(Rating) {
    var totalWidth = $("ul.stars").width();
    return Math.round(totalWidth * (Rating / 5));
}



function stop() {
    if (ytplayer) {
        ytplayer.stopVideo();
    }
}



// ------------------------
// stop flash audio if player is started
function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}


function callToActionscript(str) {
    getFlashMovie("flash").sendToActionscript(str);
}

