﻿////------------------------------------------------------ iframe management

function ResizeIFrameContainer(width, height)
{
    var container = parent.document.getElementById('IFrameContainer_JSResized');
    container.style.width = width + "px";
    container.style.height = height + "px";
}

function checkContainer()
{
    var container = null;
    try
    {
        container = parent.document.getElementById('IFrameContainer_JSResized');
    }
    catch(e)
    { 
        container = null;
    }
   
    if (container == null)
    {
        if (window.location.hostname == "localhost")
            window.location = "http://localhost/MyTube2/";
        else
            window.location = "http://www.salsatube.it/";
    }
}

////------------------------------------------------------ load video in player

function PlayVideo(boxNr, playerNr)
{
    // check if we need to load the video before
    /*
    var PlayerVideoID = parent.document.getElementById("Player" + playerNr + "VideoID").src;
    var VideoID = document.getElementById("VideoID_" + boxNr).value;

    if (VideoID != PlayerVideoID)
    */
    LoadVideo(boxNr, playerNr);
        
    // play the video
    
    player = window.salsatubePlayerApi; // parent.document.getElementById('playerObject' + playerNr);
    player.playVideo();
}

function PauseVideo(playerNr)
{
    window.salsatubePlayerApi.pauseVideo();
}

function LoadVideo(boxNr, playerNr)
{
    // Get video info
    
    var VideoID = document.getElementById("VideoID_" + boxNr).value;
    var Title = document.getElementById("Title_" + boxNr).value;
    var Description = document.getElementById("Description_" + boxNr).value;
    var Published = document.getElementById("Published_" + boxNr).value;
    
    // Load video on player

    var baseUrl = "http://www.youtube.com/v/";
    var params = "&enablejsapi=1&disablekb=1&showinfo=0&iv_load_policy=3&showsearch=0&rel=1&fs=1"
    var videoUrl = baseUrl + VideoID + params;
    
    player = window.salsatubePlayerApi; //parent.document.getElementById('playerObject' + playerNr);
    player.cueVideoByUrl(videoUrl,0);

/*
    try {
        player = parent.document.getElementById('playerEmbed' + playerNr);
        player.cueVideoByUrl(videoUrl,0);
    }
    catch (e) {
    }
*/    
    // Display video info
/*
    parent.document.getElementById("playerTitle" + playerNr).innerHTML = Title;
    parent.document.getElementById("detailsDescription" + playerNr).innerHTML = Description;
    parent.document.getElementById("detailsPublished" + playerNr).innerHTML = "Pubblicato il " + Published;
*/
    // Set video box color
/*
    var color;
    if (playerNr == 1)
        color = "Blue";
    else
        color = "Red";

    var PlayerVideoIDHidden = parent.document.getElementById("Player" + playerNr + "VideoID");

    var prevBox = document.getElementById("ThumbnailImg_" + PlayerVideoIDHidden.value);
    if (prevBox != null)
        setBorderColor(prevBox, "Gray");

    var currBox = document.getElementById("ThumbnailImg_" + VideoID);
    if (currBox != null)
        setBorderColor(currBox, color);

    PlayerVideoIDHidden.value = VideoID;
*/    
}

////------------------------------------------------------ color video box

function ColorVideoBox()
{
    var Player1Video = parent.document.getElementById("Player1VideoID").value;
    var Player2Video = parent.document.getElementById("Player2VideoID").value;

    var box1 = document.getElementById("ThumbnailImg_" + Player1Video);
    var box2 = document.getElementById("ThumbnailImg_" + Player2Video);

    if (box1 != null)
        setBorderColor(box1, "blue");
    if (box2 != null)
        setBorderColor(box2, "red");       
}

////------------------------------------------------------ thumbnail rotation

var thumbArray = new Array();
var thumbIndex;
var thumbRotationID;

function StartThumbRotation(VideoID)
{
    // clear array
    thumbIndex=0;
    thumbArray[0] = "";
    thumbArray[1] = "";
    thumbArray[2] = "";
    //thumbArray[0] = "http://i.ytimg.com/vi/0v1SpGs1YgM/1.jpg";
    //thumbArray[1] = "http://i.ytimg.com/vi/0v1SpGs1YgM/2.jpg";
    //thumbArray[2] = "http://i.ytimg.com/vi/0v1SpGs1YgM/3.jpg";

    // invoke service
    invokeThumbnailService(VideoID, thumbArray);

    // start rotation
    //thumbRotationID = setInterval(ThumbRotation, 600, VideoID);   // not working in IE
    thumbRotationID = setInterval( function() { ThumbRotation(VideoID); }, 600 );
}

function StopThumbRotation()
{
    clearInterval(thumbRotationID);
}

function ThumbRotation(VideoID)
{
    var img = document.getElementById("ThumbnailImg_" + VideoID);

    if (thumbArray[thumbIndex] != "")
    {
        img.src = thumbArray[thumbIndex];
    }

    thumbIndex++;

    if (thumbIndex == 3)
        thumbIndex = 0;
}

////------------------------------------------------------ pop up Video Form

var popupVideoFormRef = null;

function PopUpVideoForm(VideoID, Title) {
    //if (popupVideoFormRef != null && !popupVideoFormRef.closed)
    //    popupVideoFormRef.focus();
    //else 
    {
        var formUrl = "VideoForm.aspx";
        if ((VideoID != null) && (VideoID != ""))
            formUrl += "?Action=Update&VideoID=" + VideoID;
            
        popupVideoFormRef = window.open(formUrl, "", "left=380,top=100,width=500,height=480,status=no,titlebar=no,menubar=no");
        if (popupVideoFormRef == null)
            alert("Disabilita il blocco Pop Up");
        else
            popupVideoFormRef.focus();
    }
}

////------------------------------------------------------ ....
