﻿var hTime;
var sSID = '';
var bPlaying = false;
var sLinkOut = '';          // Pagina di redirect alla fine del filmato. Se sono in una landingpage è ''


function ChangeState(playerId, newState) {
    
    if (playerId > 0) {
        ytplayer = document.getElementById("ytPlayer" + playerId);
    }

    /* 
    This event is fired whenever the player's state changes. 
    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)
    */

    switch (newState) {
        case -1: break;
        case 0:
            bPlaying = false;
            if (sLinkOut != '') window.location = sLinkOut;     // Se sono in una landingpage rimango dove sono.
            break;        // fine play
            
        case 1:
            bPlaying = true;
            break;    // inizio play
        case 2:
            bPlaying = false;
            break;      // pausa
        case 3: break;
        //        case 4: break; 
        case 5: break;
    }
}

/*
* Polling the player for information
*/

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError2(errorCode) {
    alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
function onPlayerStateChange2(newState) {
    setTimeout("ChangeState(" + 2 + "," + newState + ")", 200)
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
    var ytplayer = document.getElementById("ytPlayer" + playerId);
    // This causes the updatePlayerInfo function to be called every 250ms to
    // get fresh data from the player
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange" + playerId);
    ytplayer.addEventListener("onError", "onPlayerError" + playerId);

    //Load an initial video into the player

    //if (playerId = 2) ytplayer.cueVideoById("7dHhWeHVdeQ");
    //if (playerId = 2) ytplayer.cueVideoById("Rf_WTXUMPz4");   sostituito il 20110315
    if (playerId = 2) ytplayer.cueVideoById("Fe3ITRXGR3s");
    
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer2(iWidth, iHeight) {
    // The video to load
    // Lets Flash from another domain call JavaScript & trasparency
    var params = { allowScriptAccess: "always", wmode:"transparent"};     
    // The element id of the Flash embed
    var atts = { id: "ytPlayer2" };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    swfobject.embedSWF("http://www.youtube.com/apiplayer?" + "&enablejsapi=1&playerapiid=2", "videoDiv2", iWidth, iHeight, "8", null, null, params, atts);
}

function _run(iVideoWidth,iVideoHeight,sLink) {

    if (iVideoWidth == undefined || iVideoHeight == undefined) {
        iVideoWidth = 970;
        iVideoHeight = 546;
    }
    if (sLink == undefined) {
        sLinkOut = 'http://www.innerweb.it'
    } else { 
        sLinkOut = sLink
    }
    loadPlayer2(iVideoWidth, iVideoHeight);
}

function startVideo(playerId) {
    bPlaying = true;
    var ytplayer = document.getElementById("ytPlayer" + playerId);
    if (ytplayer) {
//        ytplayer.loadVideoById("Rf_WTXUMPz4");   sostituito il 20110315
        ytplayer.loadVideoById("Fe3ITRXGR3s");
        //ytplayer.playVideo();
    }
}

function playVideo(playerId) {
    bPlaying = true;
    var ytplayer = document.getElementById("ytPlayer" + playerId);
    if (ytplayer)  ytplayer.playVideo();
}

function pauseVideo(playerId) {
    bPlaying = false;
    var ytplayer = document.getElementById("ytPlayer" + playerId);
    if (ytplayer) ytplayer.pauseVideo();
}

function stopVideo(playerId) {
    bPlaying = false;
    var ytplayer = document.getElementById("ytPlayer" + playerId);
    if (ytplayer) {
        //ytplayer.stopVideo();
        //ytplayer.clearVideo();
//        ytplayer.cueVideoById("Rf_WTXUMPz4");   sostituito il 20110315
        ytplayer.cueVideoById("Fe3ITRXGR3s");
    }
}




