﻿/* v 1.1

- fash embedding is performed with a few seconds delay with respect to document.ready

*/

/* v 1.0
*/

// ------------------------------------------------------------ Player Callback

function livestreamPlayerCallback(event)
{ 
    if (event == 'ready')
    {    
        //playerApi.setDevKey('wdWBGRcXyfmRBgfiZpL9ttMZWl5iQFhoggDGNJrMTbwmoUUKJaqihB-MrcOB8NC2ALJD60-KYPiGZOWTW4xbVwmaNVFAP-WdlMrKR3WsSGk');
      	//playerApi.setChannel('tvsalsa');
      	
      	tvPlayerApi.setVolume(tvPlayerVolume);
      	tvPlayerApi.setMute(tvPlayerIsMute);
      	if (tvPlayerIsPlaying)
      	    tvPlayerApi.startPlayback(0);
    }
}

// ------------------------------------------------------------ Content Title

function setContentTitle()
{
    var title = null;
    
    if (!tvPlayerIsBroadcastingLive)
        title = onDemandVars.title;
    else try
    {
        title = tvPlayerApi.getCurrentContentTitle();
    }
    catch(e) { }
    
    if (title == null)
        setTimeout(setContentTitle,10000);     	
    else
    {
   	    $("#TVContentTitle").text(title);	
        setTimeout(setContentTitle,15000);     	
    }
}

function forceContentTitle(title)
{
    $("#TVContentTitle").text(title);
}

// ------------------------------------------------------------ Play On-Demand/Live Video

var onDemandVars =
{
    title: null
}

function playOnDemandVideo(guid, title)
{
    onDemandVars.title = title;
    forceContentTitle(title);
    tvPlayerIsBroadcastingLive = false;
    tvPlayerApi.setClipID(guid);
   	tvPlayerApi.startPlayback(0);

    $("#TVisLive").css("display","none");
    $("#TVisOnDemand").css("display","block");
}

function playLive()
{
    forceContentTitle("");
    tvPlayerIsBroadcastingLive = false;
    tvPlayerApi.setClipID(null);
   	tvPlayerApi.startPlayback(0);

    $("#TVisOnDemand").css("display","none");
    $("#TVisLive").css("display","block");
}

// ------------------------------------------------------------ Thumbnail Rotation

var liveThumbRotVars        = { jimg: null, time: 0, updateThumbnailRef: null }
var onDemandThumbRotVars    = { jimg: null, time: 0, updateThumbnailRef: null, guid: null }
var updateLiveThumbnail     = function() { updateThumbnail(liveThumbRotVars) }
var updateOnDemandThumbnail = function() { updateThumbnail(onDemandThumbRotVars) }

function updateThumbnail(params)
{
    // On-Demand video thumbnail
    // http://clipthumbnail.api.livestream.com/?clipID=flv_dfb71da2-f13b-4962-8f25-95bd6d3e4bd2&channel=tvsalsa&time=10
    if (params.guid != null)
        params.jimg.attr("src","http://clipthumbnail.api.livestream.com/?clipID=" + params.guid + "&channel=" + tvChannelName + "&time=" + params.time);
    // Live broadcasting thumbnail
    // documentation url: http://www.livestream.com/userguide/index.php/Live_Thumbnail_API
    // http://thumbnail.api.livestream.com/thumbnail?name=tvsalsa&t=[random number]
    else        
        params.jimg.attr("src","http://thumbnail.api.livestream.com/thumbnail?name=" + tvChannelName + "&t=" + Math.floor(Math.random()*10000));
    
    params.time += 20;    
    if (params.time > 120)
        params.time = 0;    
}

function startLiveThumbnailRotation(interval, jimg)
{
    liveThumbRotVars.jimg = jimg;
    liveThumbRotVars.time = 0;

    jimg.attr("src", "Images/Master/TVMiniNoSignal.jpg");
    setTimeout(updateLiveThumbnail, 3000);
    liveThumbRotVars.updateThumbnailRef = setInterval(updateLiveThumbnail, interval);
}

function startOnDemandThumbnailRotation(interval, jimg, guid)
{
    onDemandThumbRotVars.jimg = jimg;
    onDemandThumbRotVars.time = 0;
    onDemandThumbRotVars.guid = guid;
    
    updateOnDemandThumbnail();
    onDemandThumbRotVars.updateThumbnailRef = setInterval(updateOnDemandThumbnail, interval);
}

function stopLiveThumbnailRotation()
{
    clearInterval(liveThumbRotVars.updateThumbnailRef);
}

function stopOnDemandThumbnailRotation()
{
    clearInterval(onDemandThumbRotVars.updateThumbnailRef);
}

// ------------------------------------------------------------ Document Ready

var tvChannelName;
var tvPlayerApi;
var tvPlayerVolume;
var tvPlayerIsMute;
var tvPlayerIsPlaying;
var tvPlayerIsBroadcastingLive = true;

function StartLivestreamTV()
{
    var jelem;  

    /*
     * TV
     */ 

    // get channel name
    
    jelem = $("#TVChannelName");
    if (jelem.length != 0)
        tvChannelName = jelem.val();

    // embed player
    
    if ($("#TVPlayerOutput").length != 0)
    {    
        // get player parameters
        tvPlayerVolume = parseFloat($("#TVPlayerVolume").val());
        tvPlayerIsMute = $("#TVPlayerIsMute").val() == "true" ? true : false;
        tvPlayerIsPlaying = $("#TVPlayerIsPlaying").val() == "true" ? true : false;
    
        // embed player
        var api = $("#TVPlayerEmbedPlaceHolder").flashembed(
            // configuration (flashembed and flash parameters)
            {
                src: "http://cdn.livestream.com/chromelessPlayer/wrappers/JSPlayer.swf",
                expressInstall: "Flash/ExpressInstall.swf",
                allowfullscreen: "true",
                allowscriptaccess: "always",
                wmode: "opaque"
            },
            // flashvars
            {
                devKey : 'wdWBGRcXyfmRBgfiZpL9ttMZWl5iQFhoggDGNJrMTbwmoUUKJaqihB-MrcOB8NC2ALJD60-KYPiGZOWTW4xbVwmaNVFAP-WdlMrKR3WsSGk',
                channel : tvChannelName
            }); 
            
        tvPlayerApi = api.getApi();
    }
    
    // live TV content title
        
    if ($("#TVContentTitle").length != 0)
  	    setTimeout(setContentTitle,10000);

    
    // live TV thumbnail rotation
    
    jelem = $("#TVThumbnailImg");
    if (jelem.length != 0)
        startLiveThumbnailRotation(15000, jelem)
}

$(document).ready(function()
{
    setTimeout(StartLivestreamTV,3000);
});

