﻿/* v 2.5.3
- added ansa live support
*/

/* v 2.5
- added debug support
- all calls to players apis have moved within try-catch blocks
*/

/* v 2.3
- some minor bug fixes
- $("#AccordionMenuWrapper").accordion({...}) has been removed, if needed it should be handled in the style sheet page
- changed LS seek 'algorithm' because of LS player update
- ls channel name is set at embedding time through flashvars
- lsApi is set by the ready event handler
*/

/* v 2.1 rev d
- replaced all flashembed calls with swfobject calls, so as to fix IE compatibility problems
*/

/* v 2.0 rev b
*/

var debug = false;

// ------------------------------------------------------------ Player APIs

var wmpApi = null;
var ytApi  = null;
var lsApi  = null;
var fpApi  = null;

// ------------------------------------------------------------ Ansa News

function getAnsaNewsURL()
{
    var today       = new Date();   
    var hour        = today.getHours();     // 0-23
    var dayOfWeek   = today.getDay();       // 0=domenica, 1=lunedì - 6=sabato
    var year        = today.getFullYear(); 	// e.g. 2010 (4 cifre)
    var month       = today.getMonth() + 1;	// 1=gennaio
    var dayOfMonth  = today.getDate();      // 1-31
    var minutes     = today.getMinutes();  	// 0-59

    // publishing time
    if (minutes < 11)
        hour--;

    // too early
    if ((dayOfWeek >= 2 && dayOfWeek <= 4 && hour < 8) ||   // martedì-giovedì
        (dayOfWeek == 5 && hour < 11) ||                    // venerdì
        (dayOfWeek == 6 && hour < 10) ||                    // sabato
        (dayOfWeek == 0 && hour < 8))                       // domenica
    {
        dayOfWeek--;
        
        if (dayOfWeek < 0)
            dayOfWeek = 6;

        if (dayOfMonth == 1)
        {        
            month--;
            if (month == 2)
                dayOfMonth = 28;
            else if (month == 0 || month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)
                dayOfMonth = 31;
            else
                dayOfMonth = 30;
        }
            
        if (month == 0)
        {
            month = 12;
            year--;
        }

        hour = 23;       
    }

    // too late
    if (dayOfWeek == 6 && hour > 20)
    {
        hour = 20;       
    }

    // build url
    var url = "flv:g" + year + 
                (month < 10 ? "0" : "") + month + 
                (dayOfMonth < 10 ? "0" : "") + dayOfMonth + 
                (hour < 10 ? "0" : "") + hour + "00";    

    return url;
}

// ------------------------------------------------------------ 
// ------------------------------------------------------------ Player Control functions
// ------------------------------------------------------------ 

/*
The following functions perform player control through these steps:
1. control players through their apis,
2. update player config variables,
3. for ls, animate control icons through jsTVControl functions.

Remember that content title is not set here, but it's set in the showPlayer method.
*/

function play(player, params)
{   
    try
    {
        if (player == "ls" && (lsApi != null) && lsConfig.isReady)
        {
            // play on-demand video 
            if (params != null && params.guid != null)
            {   
          	    lsApi.setChannel(params.channelName);  
                lsApi.setClipID(params.guid);
                
                lsConfig.channelName = params.channelName,
                lsConfig.guid = params.guid;
                lsConfig.isLive = false;
            }        
            // go live
            else if (params != null && params.isLive == true)
            {
          	    lsApi.setChannel(params.channelName);        
                lsApi.setClipID(null);

                lsConfig.channelName = params.channelName,
                lsConfig.guid = null;
                lsConfig.isLive = true;            
            }

            // start playback        
            lsApi.startPlayback();
            lsConfig.isPlaying = true;  
            
            // animate controls
            jsControlPlay();
        }
        else if (player == "fp" && (fpApi != null) && fpConfig.isReady)
        {    
            // play new clip
            if (params != null && params.url != null)
            {
                fpApi.setClip(
                {
                    url: params.url,
                    provider: params.provider,
                    live: params.live,
                    autoPlay: true
                });
                
                fpConfig.url = params.url;
                fpConfig.provider = params.provider;
                fpConfig.live = params.live;
            }        

            // start playback
            fpApi.play();    
            fpConfig.isPlaying = true;
        }
        else if (player == "wmp" && (wmpApi != null) && wmpApi.controls && wmpApi.settings)
        {    
            // play new clip
            if (params != null && params.src != null)
            {
                wmpApi.URL = params.src;

                wmpConfig.src = params.src; 
            }

            // start playback        
            wmpApi.controls.play();
            wmpConfig.isPlaying = true;
        }
        else if (player == "yt" && (ytApi != null) && ytConfig.isReady)
        {    
            // play on-demand video
            if (params != null && params.videoid != null)
            {   
                ytApi.cueVideoById(params.videoid);

                ytConfig.videoid = params.videoid;
            }        

            // start playback
            ytApi.playVideo();
            ytConfig.isPlaying = true;                      
        }
        else if (player == "si")
        {
            // play new clip
            if (params != null)
            {
                siConfig.src = params.src; 
            }
                
            // start playback
            // if src is set already, we won't set it over otherwise it will reload
            if ($("#siIframe").attr("src") != params.src)
                $("#siIframe").attr("src", params.src); 
            siConfig.isPlaying = true;
        }
    }
    catch(e) { 
        if (debug)
            alert("error: play(), player: " + player);
    }
}

function pause(player)
{
    try
    {
        if (player == "ls" && (lsApi != null) && lsConfig.isReady)
        {
            lsApi.stopPlayback();
            lsConfig.isPlaying = false;
            
            // animate controls   
            jsControlPause();
        }
        else if (player == "wmp" && (wmpApi != null) && wmpApi.controls && wmpApi.settings)
        {
            wmpApi.controls.pause();
            wmpConfig.isPlaying = false;
        }
        else if (player == "fp" && (fpApi != null) && fpApi.isLoaded())
        {
            fpApi.pause();
            fpConfig.isPlaying = false;
        }
        else if (player == "yt" && (ytApi != null) && ytConfig.isReady)
        {    
            ytApi.pauseVideo();
            ytConfig.isPlaying = false;
        }
        else if (player == "si")
        {
            $("#siIframe").attr("src","");  // rude stop!    
        }
    }
    catch(e) { 
        if (debug)
            alert("error: pause(), player: " + player);
    }
}

function stop(player)
{   
    try
    {
        if (player == "ls" && (lsApi != null) && lsConfig.isReady)
        {
            lsApi.stopPlayback();
            lsConfig.isPlaying = false;   
            
            // animate controls  
            jsControlPause();
        }
        else if (player == "wmp" && (wmpApi != null) && wmpApi.controls && wmpApi.settings)
        {
            wmpApi.controls.stop();
            wmpConfig.isPlaying = false;
        }
        else if (player == "fp" && (fpApi != null) && fpApi.isLoaded())
        {
            fpApi.stop();
            fpConfig.isPlaying = false;        
        }
        else if (player == "yt" && (ytApi != null) && ytConfig.isReady)
        {    
            ytApi.stopVideo();
            ytConfig.isPlaying = false;
        }   
        else if (player == "si")
        {
            $("#siIframe").attr("src","");
        }
    }
    catch(e) { 
        if (debug)
            alert("error: stop(), player: " + player);
    }    
}

function setMute(player, isMute)
{
    try
    {
        if (player == "ls" && (lsApi != null) && lsConfig.isReady)
        {
            lsApi.setMute(isMute);
            
            // animate controls
            if (isMute)
                jsControlMute();
            else
                jsControlUnMute();
        }
        else if (player == "wmp" && (wmpApi != null) && wmpApi.controls && wmpApi.settings)
        {
            wmpApi.settings.mute = isMute;
        }
        else if (player == "fp" && (fpApi != null) && fpApi.isLoaded())
        {
            isMute ? fpApi.mute() : fpApi.unmute();
        }
        else if (player == "yt" && (ytApi != null) && ytConfig.isReady)
        {
            isMute ? ytApi.mute() : ytApi.unMute();
        } 
        else if (player == "si")
        {
        }
    }
    catch(e) { 
        if (debug)
            alert("error: setMute(), player: " + player);
    }    
}

function setVolume(player, volume)
{
    try
    {
        if (player == "ls" && (lsApi != null) && lsConfig.isReady)
        {
            lsApi.setVolume(volume / 100);      // float ranging between 0 (mute) and 1 (max)
            
            // animate controls
            jsSliderVolumeSet(volume);
        }
        else if (player == "wmp" && (wmpApi != null) && wmpApi.controls && wmpApi.settings)
        {
            wmpApi.settings.volume = volume     // integer ranging from 0 to 100
        }
        else if (player == "fp" && (fpApi != null) && fpApi.isLoaded())
        {
            fpApi.setVolume(volume);            // integer ranging from 0 to 100
        }
        else if (player == "yt" && (ytApi != null) && ytConfig.isReady)
        {
            ytApi.setVolume(volume);            // integer ranging from 0 to 100
        }
        else if (player == "si")
        {
        }
    }
    catch(e) { 
        if (debug)
            alert("error: setVolume(), player: " + player);
    }    
}

function seek(player, secs)
{
    try
    {
        if (player == "ls" && (lsApi != null) && lsConfig.isReady)
        {   
            lsApi.seek(secs);   // remember that LS-seeking stops playback
        }
        else if (player == "wmp" && (wmpApi != null) && wmpApi.controls && wmpApi.settings)
        {
            // to do
        }
        else if (player == "fp" && (fpApi != null) && fpApi.isLoaded())
        {
            //fpApi.play(0);
        }
        else if (player == "yt" && (ytApi != null) && ytConfig.isReady)
        {
            //ytApi.seekTo(0);
        }
        else if (player == "si")
        {
        }
    }
    catch(e) { 
        if (debug)
            alert("error: seek(), player: " + player);
    }        
}

function fullScreen(player)
{
}

// ------------------------------------------------------------ 
// ------------------------------------------------------------ jsTVControl functions
// ------------------------------------------------------------ 

/*
Notice
- the following methods should be called only by the above TV Control functions,
  as control functions and appearance should be seen as tied together.
*/

function jsControlPlay()
{
    $("#jsControlPlay").css("visibility","hidden");
    $("#jsControlPause").css("visibility","visible");        
}

function jsControlPause()
{
    $("#jsControlPlay").css("visibility","visible");
    $("#jsControlPause").css("visibility","hidden");        
}

function jsControlMute()
{
    $("#jsControlMute").css("visibility","hidden");
    $("#jsControlUnMute").css("visibility","visible");
}

function jsControlUnMute()
{
    $("#jsControlMute").css("visibility","visible");
    $("#jsControlUnMute").css("visibility","hidden");
}

// ------ //

function jsSliderVolumeSet(value, endis, min, max)
{
    var jelem = $("#jsSliderVolume");
    
    if (value != null)
        jelem.slider('value',value);     
    if (endis != null)
        jelem.slider(endis);            
    if (min != null)            
        jelem.slider('option', 'min', min);
    if (max != null)
        jelem.slider('option', 'max', max);
}

function jsSliderVolumeOnSlide(pos)
{
    setVolume("ls", pos);
}

// ------ //

function jsSliderPositionSet(value, endis, min, max, forceLabelTime)
{
    var jelem = $("#jsSliderPosition");
    
    if (value != null)
        jelem.slider('value',value);                
    if (endis != null)
        jelem.slider(endis);            
    if (min != null)            
        jelem.slider('option', 'min', min);
    if (max != null)
        jelem.slider('option', 'max', max);
    
    if (forceLabelTime != null)
        $("#jsLabelTime").html(forceLabelTime);
    else
    {
        if (max == null)    
            max = $("#jsSliderPosition").slider('option','max');

        $("#jsLabelTime").html(
            "<span style='color:#01daff'>" +
            VisioTec.Utilities.DateTime.sec2str(value)
            + "/</span>" + 
            VisioTec.Utilities.DateTime.sec2str(max)
        );
    }        
}

function jsSliderPositionOnStart()
{
    //stop("ls");
    jsSliderPositionStopLSSynch();
}

/*
Notes for LS Api:
- if you want to seek to a specified position, you must stop, then seek, then seek again after around one second and finally play!!!
  otherwise, either seeking to the specified position does not work or it may work but lsApi.getTime() stops working!!!
- remember that seeking stops playback.
*/

function jsSliderPositionOnStop(pos)
{
    try {
        lsApi.startPlayback(pos);
    }    
    catch(e) { 
        if (debug)
            alert("error: seek(), player: " + player);
    }        
    
    setTimeout(function() {
        jsSliderPositionStartLSSynch();
    },6000);
    
}

function jsSliderPositionOnSlide(pos)
{
    //stop("ls");
    jsSliderPositionSet(pos);
}

function jsSliderPositionOnChange(pos)
{           
}

function jsSliderPositionLSSynch()
{
    if (lsConfig.isReady)
    {
        var secs = parseInt(lsApi.getTime());
        if (secs > 0)
            jsSliderPositionSet(secs);
    }
}

var jsSliderPositionLSSynchRef = null;

function jsSliderPositionStartLSSynch()
{
    clearInterval(jsSliderPositionLSSynchRef);
    jsSliderPositionLSSynchRef = setInterval(jsSliderPositionLSSynch,1000);
}

function jsSliderPositionStopLSSynch()
{
    clearInterval(jsSliderPositionLSSynchRef);
}

// ------------------------------------------------------------ 
// ------------------------------------------------------------ Switch source
// ------------------------------------------------------------ 

function powerOff()
{
    switchSource(null);
}

function startUp()
{
    switchSource(mediaPlayer.startWith, mediaPlayer.startParams);
}
 
function switchSource(player, params)
{
    // check if the player is ready
    
    if ((player == 'ls'  && !lsConfig.isReady) ||
        (player == 'yt'  && !ytConfig.isReady) ||
        (player == 'fp'  && !fpConfig.isReady) ||
        (player == 'wmp' && !wmpConfig.isReady) ||
        (player == 'si'  && !siConfig.isReady))
        return;

    mediaPlayer.shown = player;    

    // stop players (except for 'ls', to improve performance)

    setMute("ls", true);
    jsSliderPositionStopLSSynch();
    stop("wmp");
    stop("yt");
    stop("si");
    stop("fp");

    // hide players
    
    hidePlayer("ls");
    hidePlayer("wmp");
    hidePlayer("si");
    hidePlayer("fp");
    hidePlayer("yt");
    
    // show standby screen

    jscreen = $('#TVScreen');
    jscreen.find('#TVLoadingAbsolute').css("display","none");
    jscreen.find('#TVTuningInAbsolute').css("display","none");
    jscreen.find('#TVReadyAbsolute').css("display","none");

    if (player == null)
        jscreen.find('#TVReadyAbsolute').css("display","block");
    else
        jscreen.find('#TVTuningInAbsolute').css("display","block");        
        
    // show selected player and start playback

    // play on-demand video -> params is { channelName: 'xyz', guid: 'xyz', title: 'xyz', duration: xyz } 
    // go live -> params is { channelName: 'xyz', isLive: true }
    if (player == "ls")
    {
        setMute("ls", true);    
        play("ls", params);

        setTimeout(function ()
        {
            setVolume("ls",80);
            setMute("ls", false);
                     
            if (params != null)
            {
                if (params.isLive == true || params.duration == Number.NaN)
                    jsSliderPositionSet(0, "disable", null, null, "live");
                else if (params.duration != null)
                {
                    jsSliderPositionSet(0, "enable", 0, params.duration);
                    jsSliderPositionStartLSSynch();
                }
            }
            
            showPlayer("ls");            

            if (params != null)
            {
                if (params.title != null)
                    setContentTitle(params.title);
                else if (params.isLive == true)
                    updateLivestreamContentTitle();
            }

            $('#TVTuningInAbsolute').css("display","none");            
        },1000);
    }
    // play new clip -> params is { url: 'url', provider: 'xyz', live: true/false, title: 'xyz' }    
    else if (player == "fp")
    {
        setMute("fp", true);
        play("fp", params);
        
        setTimeout(function ()
        {                  
            setVolume("fp",80);
            setMute("fp", false);
            
            showPlayer("fp");            

            if (params != null && params.title != null)           
                setContentTitle(params.title);
            
            $('#TVTuningInAbsolute').css("display","none");  
        },1000);
    }    
    // play new clip -> params is { src: 'url', title: 'xyz' }    
    else if (player == "wmp")
    {    
        // remember that player.URL may not be set if the player is not shown,
        // therefore we first show it, then call play()
        setTimeout(function ()
        {           
            showPlayer("wmp");

            $('#TVTuningInAbsolute').css("display","none");
        },500);
            
        setTimeout(function ()
        {           
            setVolume("wmp",80);
            setMute("wmp", false);

            play("wmp", params);
            
            if (params != null && params.title != null)
                setContentTitle(params.title);                                                
        },1000);
    }
    // play on-demand video -> params is { videoid: 'xyz', title: 'xyz' }     
    else if (player == "yt")
    {
        setTimeout(function ()
        {                  
            setVolume("yt",80);
            setMute("yt",false);
            
            play("yt", params);
            showPlayer("yt");     
                        
            if (params != null && params.title != null)
                setContentTitle(params.title);            

            $('#TVTuningInAbsolute').css("display","none");  
        },500);
    }
    // play new clip -> params is { src: 'url', title: 'xyz' }
    else if (player == "si")
    {
        play("si", params);
         
        setTimeout(function ()
        {
            showPlayer("si");            

            if (params != null && params.title != null)
                setContentTitle(params.title);            

            $('#TVTuningInAbsolute').css("display","none");        
        },1000);
    }
    else // if (player == null)
    {
        setContentTitle('');
        // TV powered off (players have been muted/stopped and not restored)
    }
}

function hidePlayer(player)
{
    if (player == null)
        player = mediaPlayer.shown;

    if (player == "ls")
    {
        $('#lsAbsolute').css("visibility","hidden");   
        $('#lsAbsolute').css("left","5000px");
        $('#jsTVControls').css("visibility","hidden");   
    }
    else if (player == "wmp")
    {
        //$('#wmpObject').attr("hidden","hidden");  // this doesn't seem to be needed
        $('#wmpAbsolute').css("visibility","hidden");   
        $('#wmpAbsolute').css("left","5000px");       
    }
    else if (player == "si")
    {
        $('#siAbsolute').css("visibility","hidden");    
        $('#siAbsolute').css("left","5000px");       
    }
    else if (player == "fp")
    {
        $('#fpAbsolute').css("visibility","hidden");    
        $('#fpAbsolute').css("left","5000px");       
    }
    else if (player == "yt")
    {
        $('#ytAbsolute').css("visibility","hidden");    
        $('#ytAbsolute').css("left","5000px");       
    }
}

function showPlayer(player)
{
    if (player == null)
        player = mediaPlayer.shown;

    if (player == "ls")
    {
        $('#lsAbsolute').css("left","0px");
        $('#lsAbsolute').css("visibility","visible");   
        $('#jsTVControls').css("visibility","visible");   
    }
    else if (player == "wmp")
    {
        $('#wmpAbsolute').css("left","0px");   
        $('#wmpAbsolute').css("visibility","visible");   
    }
    else if (player == "si")
    {
        $('#siAbsolute').css("visibility","visible");    
        $('#siAbsolute').css("left","0px");       
    }
    else if (player == "fp")
    {
        $('#fpAbsolute').css("visibility","visible");    
        $('#fpAbsolute').css("left","0px");       
    }
    else if (player == "yt")
    {
        $('#ytAbsolute').css("visibility","visible");    
        $('#ytAbsolute').css("left","0px");       
    }
}

// ------------------------------------------------------------ 
// ------------------------------------------------------------ Content Title
// ------------------------------------------------------------ 

var setContentTitleRef = null;

var currentContentTitle = '';

function setContentTitle(title)
{
    clearTimeout(updateLivestreamContentTitleRef);

    if (title != currentContentTitle)
    {
        currentContentTitle = title;
    
        $("#TVContentTitle").hide("slide", { direction: "down" }, 1000,
            function() { 
                $("#TVContentTitle").text(title);
                $("#TVContentTitle").show("slide", { direction: "down" }, 1000);
            }
        );
    }
}

var updateLivestreamContentTitleRef = null;

function updateLivestreamContentTitle()
{
    var title = null;
    
    if (lsConfig.isReady)
        title = lsApi.getCurrentContentTitle();
    
    if (title == null)
    { 
        setContentTitle('');
        updateLivestreamContentTitleRef = setTimeout(updateLivestreamContentTitle,5000);     	
    }
    else
    {
        setContentTitle(title);
        updateLivestreamContentTitleRef = setTimeout(updateLivestreamContentTitle,10000);     	
    }
}

// ------------------------------------------------------------ 
// ------------------------------------------------------------ OnLoad callbacks
// ------------------------------------------------------------ 

/*
Notes
- ls, fp e yt players must be loaded in a visibile container, they are hidden in their onload event callbacks.
- if autoStart is set to true, player is started muted, audio will be set when the player is shown
*/ 

function livestreamPlayerCallback(event)
{ 
    if (event == 'ready')
    {        
        lsApi = document.getElementById("lsEmbedPlaceHolder");
    
        // once it's loaded we can hide it without affecting playback control
           
        hidePlayer("ls");      	    
      	
        // player settings      	

      	//lsApi.setDevKey('wdWBGRcXyfmRBgfiZpL9ttMZWl5iQFhoggDGNJrMTbwmoUUKJaqihB-MrcOB8NC2ALJD60-KYPiGZOWTW4xbVwmaNVFAP-WdlMrKR3WsSGk');

      	lsApi.showPlayButton(false);
      	lsApi.showPauseButton(false);
      	lsApi.showMuteButton(false);
      	lsApi.showFullscreenButton(true);
      	lsApi.showSpinner(true);
         
        // player's ready

        lsConfig.isReady = true;      	    
         
        // autostart
              
      	if (lsConfig.autoStart)
      	{
          	setMute("ls", true);
            play("ls", lsConfig.autoStartParams);
      	}
    }
}

function flowplayerOnLoadCallback()
{ 
    // once it's loaded we can hide it without affecting playback control
       
    hidePlayer("fp");      	    
       
    // player's ready
      	    
    fpConfig.isReady = true;

    // autostart
                 
  	if (fpConfig.autoStart)
  	{
        setMute("fp", true);
  	    play("fp", fpConfig.autoStartParams);
  	}
}

function onYouTubePlayerReady()
{ 
    // once it's loaded we can hide it without affecting playback control
       
    hidePlayer("yt");      	    

    // player's ready
      	    
    ytConfig.isReady = true;  	    

    // player settings      	

  	if (ytConfig.autoStart)
  	    play("yt", ytConfig.autoStartParams);
}

function wmpOnLoad()
{
    // player's ready

    wmpConfig.isReady = true;      	    

    // autostart
                 
  	if (wmpConfig.autoStart)
  	{
        setMute("wmp", true);
  	    play("wmp", wmpConfig.autoStartParams);
  	}
}

function siOnLoad()
{ 
    // player's ready

    siConfig.isReady = true;      	    

    // autostart

    if (siConfig.autoStart)
        play("si",siConfig.autoStartParams);    // this sets the iframe src attribute
    else
        stop("si");                             // this clears the iframe src attribute
}

// ------------------------------------------------------------ 
// ------------------------------------------------------------ Document Ready
// ------------------------------------------------------------ 

$(document).ready(function()
{
    /*
     * Channel menù items and OnDemand panels
     */

    $(".channelMenuItem").mouseenter(function()
    {
        if (!$(this).hasClass("active"))
            $(this).addClass("hilight")
    }).mouseleave(function()
    { 
        $(this).removeClass("hilight")
    });

    $(".channelMenuIcon").mouseenter(function()
    {
        $(this).css("opacity",0.9)
    }).mouseleave(function()
    { 
        $(this).css("opacity",1)
    });

    $(".channelMenuItem, .channelMenuIcon").click(function()
    {
        var action = $(this).attr("ref")

        // show panel
        if (action.indexOf('panel:') != -1)
        {
            var panelName = action.substring(6);
        
            $(".channelMenuItem.active").removeClass("active");
            $("#ChannelMenuItem_" + panelName).addClass("active");
            
            $(".channelPanel.active").removeClass("active").fadeOut(300,function() {        
                $("#ChannelPanel_" + panelName).addClass("active").fadeIn(300);
            });
        }
        // eval javascript
        else //if (action.indexOf('javascript:') != -1)
        {
            eval(action.substring(11));
        }
    });

    /*
     * Power-off and Start-up controls
     */

    $("#powerOffIcon").click(function() { powerOff(); });
    $("#powerOffIcon").mouseenter(function() { $(this).css("opacity",0.9) });
    $("#powerOffIcon").mouseleave(function() { $(this).css("opacity",1) });

    $("#startUpIcon").click(function() { startUp(); });
    $("#startUpIcon").mouseenter(function() { $(this).css("opacity",0.9) });
    $("#startUpIcon").mouseleave(function() { $(this).css("opacity",1) });

    /*
     * js TV Controls
     */

    $(".jsControlIcon").mouseenter(function() { $(this).addClass("hilight"); });    
    $(".jsControlIcon").mouseleave(function() { $(this).removeClass("hilight"); });

    $("#jsControlPlay").click(function() { play("ls") });
    $("#jsControlPause").click(function() { pause("ls") });
    $("#jsControlMute").click(function() { setMute("ls", true) });
    $("#jsControlUnMute").click(function() { setMute("ls", false) });
        
    $("#jsSliderPosition").slider(
            {
                min: 0,
                max: 0,
                start:  function(event, ui) { jsSliderPositionOnStart() },
                stop:   function(event, ui) { jsSliderPositionOnStop(ui.value) },
                slide:  function(event, ui) { jsSliderPositionOnSlide(ui.value) },
                change: function(event, ui) { jsSliderPositionOnChange(ui.value) }
            });        
    $("#jsSliderVolume").slider(
        {
            min: 0,
            max: 100,
            slide: function(event, ui) { jsSliderVolumeOnSlide(ui.value) }
        });        
    
    // ---------------------------
    // --------------------------- Hide player containers
    // ---------------------------
    
    hidePlayer("wmp");
    hidePlayer("si");
    //the following players must be loaded in a visibile container,
    //they'll be hidden in their onload event callbacks.
    //hidePlayer("ls");
    //hidePlayer("fp");
    //hidePlayer("yt");
    
    $('#TVTuningInAbsolute').css("display","none");  
    $('#TVReadyAbsolute').css("display","none");  
    $('#TVLoadingAbsolute').show();  

    // ---------------------------
    // --------------------------- Players embedding
    // ---------------------------
    
    /*
     * FP
     */

    /*
     * The same guidelines for LS apply here
     */

    if ($("#fpEmbedWrapper").length != 0)    
    {        
        fpApi = flowplayer("fpEmbedWrapper",
        {
            src: "Flash/flowplayer-3.1.5.swf",
            wmode: "transparent"
        },
        { 
            // common clip: these properties are common to all clips of the playlist        
            clip:
            { 
                url: 'Movies/empty3secs.flv', // dummy file
                accelerated: true,
                autoBuffering: true            
            },
            plugins:
            { 
                //controls: null,
                controls:
                { 
                    url: 'Flash/flowplayer.controls-3.1.5.swf' 
                },      
                // providers     
                ansa_rtmpvod:
                {  
                    url: 'Flash/flowplayer.rtmp-3.1.3.swf',  
                    netConnectionUrl: 'rtmp://play.ansa.it/vod'  
                }
            },       
            
            onLoad: function() { flowplayerOnLoadCallback(); }
        });
    }

    /*
     * LS
     */
    
    // You may embed the player in a hidden container, but the 'ready' event won't be triggered and therefore you may not control the player.
    // Particularly, at least one pixel of the player must be visible (literally, by human eye)
    // for the 'ready' event to be triggered and start control the player.
    // Once the 'ready' event has been triggered, you may hide the player (see below) and the player won't stop.
    //
    // Notice that you may hide a flash movie (once it's started) without stopping it by, alternatively (or in conjunction for safety):
    // 1. setting its container visiblity to hidden (visibility:hidden)
    // 2. placing it absolutely 4000 pixels right out of the screen
    // 3. placing it out of the visibile area of a overflow:hidden container

    if ($("#lsEmbedWrapper").length != 0)    
    {
        /**/
        swfobject.embedSWF(
            "http://cdn.livestream.com/chromelessPlayer/wrappers/JSPlayer.swf",
            "lsEmbedPlaceHolder",
            "100%", "100%",
            "9.0.0", "Flash/ExpressInstall.swf",
            // flashvars
            {
                devKey : 'wdWBGRcXyfmRBgfiZpL9ttMZWl5iQFhoggDGNJrMTbwmoUUKJaqihB-MrcOB8NC2ALJD60-KYPiGZOWTW4xbVwmaNVFAP-WdlMrKR3WsSGk',
                channel : lsConfig.autoStartParams.channelName
            },
            // params
            {
                allowfullscreen: "true",
                allowscriptaccess: "always",
                wmode: "transparent",
                quality: "high"
            },
            // object's attributes
            {
            },
            // callback function
            null
        );
        /**/
        
        /*
        lsApi = $("#lsEmbedShieldWrapper").flashembed
            (
                // configuration (flashembed and flash parameters)
                {
                    src: "http://cdn.livestream.com/chromelessPlayer/wrappers/JSPlayer.swf",
                    expressInstall: "Flash/ExpressInstall.swf",
                    allowfullscreen: "true",
                    allowscriptaccess: "always",
                    wmode: "transparent",
                    quality: "high"
                },
                // flashvars
                {
                    //devKey : 'wdWBGRcXyfmRBgfiZpL9ttMZWl5iQFhoggDGNJrMTbwmoUUKJaqihB-MrcOB8NC2ALJD60-KYPiGZOWTW4xbVwmaNVFAP-WdlMrKR3WsSGk',
                    //channel : lsConfig.channelName
                }
            ).getApi();
        */
    }

    // the rest of the settings (e.g. volume, autoStart, etc.) are set in the player callback function

    /*
     * WMP
     */
    
    // Display property.
    // Player embedding works if its container div has display:none,
    // but controls do not work until the container (and therefore the player) is shown (display:block).
    // With FF each time the container's display:block property is set, the playback starts over.
    // Pay attention that, after the container display:block is set, player controls will be accessible after a few hundreds milliseconds.
    
    // Visibility property: just better!
    // You may embed the player in a visibility:hidden container.
    // Playback can start, player controls are accessible and are not affected by the container visibility 
    // You may toggle visibility without problems, playback won't take over.
    //
    // Notice that you may hide a wmp object without stopping it by, alternatively (or in conjunction for safety):
    // 1. setting its container visiblity to hidden (visibility:hidden)
    // 2. placing it absolutely 4000 pixels right out of the screen
    // It doesn't work with FF to hide the player placing it out of the visibile area of a overflow:hidden container: the player won't be hidden
    
    // !Unfortunately, you may not set the player.URL attribute if the player is not visible!

    if ($("#wmpEmbedWrapper").length != 0)    
    {
        
        $('#wmpEmbedWrapper').media({ 
            width:      wmpConfig.width, 
            height:     wmpConfig.height,
            bgColor:    "Black",
            // dummy URL, otherwise the player won't be loaded
            src:        "Movies/empty3secs.wmv",
            // added to the generated OBJECT element as param elements; added to the EMBED element as attributes 
            params:     
            { 
                uiMode: wmpConfig.uiMode,
                enableContextMenu: wmpConfig.enableContextMenu,
                autoStart: false,
                stretchToFit: true,
                // When windowlessVideo is set to true, the Player control renders video directly in the client area, so you can apply special effects or layer the video with text.
                windowlessVideo: true 
            },
            // added to OBJECT and EMBED as attributes
            attrs:      
            {
                id: "wmpObject"
            },  
            // added to flash content as flashvars param/attr 
            flashvars:  
            {
            },
            // supress caption text
            caption:   false  
        }); 

        wmpApi = document.getElementById("wmpObject");

        // FF requires to set uiMode to the player object (the 'param' does not work)
        wmpApi.uiMode = wmpConfig.uiMode;

        // OnLoad handler must be invoked manually
        wmpOnLoad();
    }

    /*
     * YT
     */

    /*
     * The same guidelines for LS apply here
     */

    if ($("#ytEmbedWrapper").length != 0)    
    {        
        /**/
        swfobject.embedSWF(
            "http://www.youtube.com/v/ShsZ5PeSwG4?enablejsapi=1&version=3&playerapiid=ytplayer",
            "ytEmbedPlaceHolder",
            "100%", "100%",
            "9.0.0", "Flash/ExpressInstall.swf",
            // flashvars
            {
            },
            // params
            {
                allowfullscreen: "true",
                allowscriptaccess: "always",
                wmode: "transparent",
                quality: "high"
            },
            // object's attributes
            {
            },
            // callback function
            function() { ytApi = document.getElementById("ytEmbedPlaceHolder"); }
        );
        /**/
        
        /*
        ytApi = $("#ytEmbedWrapper").flashembed
        (
            // configuration (flashembed and flash parameters)
            {
                // chromeless player
                //src: "http://www.youtube.com/apiplayer?enablejsapi=1&version=3",
                // embedded player with dummy VideoID
                src: "http://www.youtube.com/v/ShsZ5PeSwG4?enablejsapi=1&version=3&playerapiid=ytplayer",

                expressInstall: "Flash/ExpressInstall.swf",
                allowfullscreen: "true",
                allowscriptaccess: "always",
                wmode: "transparent",
                quality: "high"
            },
            // flashvars
            {
            }
        ).getApi();
        */
    }
    
    /*
     * SI
     */

    // You may enclose the iframe (that embeds the si flash player) in a visibility:hidden container
    // then playback will start once you set the iframe src property, and it won't be affected by container visibility,
    // so if you hide the container iframe and want to stop the player you have to clear the iframe src property.

    if ($("#siIframeWrapper").length != 0)    
    {             
        siOnLoad();
    }
       
    // ---------------------------
    // --------------------------- Start with player
    // ---------------------------

    // We show a "loading" message for a while, so as to give players time to load,
    // and then we switch to the starting source (possibly null).
    // Notice that before switching to the starting source, for safety we check mediaPlayer.shown,
    // if it's not null then the user has already clicked on a source.

    setTimeout(function()
    {
        if (mediaPlayer.shown == null)
        {
            if (mediaPlayer.startWith != 'ls')
                switchSource(mediaPlayer.startWith, mediaPlayer.startParams);
            else if (lsConfig.isReady == true)
                switchSource("ls", mediaPlayer.startParams);
            else
            {
                var checkerRef = setInterval(function()
                { 
                    if (lsConfig.isReady)
                    {   
                        clearInterval(checkerRef); 
                        if (mediaPlayer.shown == null)
                            switchSource("ls", mediaPlayer.startParams);
                    }
                },2000);
            }
        }
    }, mediaPlayer.startDelay);
});
