/**
 * @author Gianfranco Todini 
 * @copyright UNICITY SpA 
 */

var scrollChannelsList;
var scrollRigthContent;
var oIntervalLocalPlayList;
var oIntervalBroadcast;
var odtBroadcastLastModified;
var iSelPLClip = -1;

$(document).ready(function(){

	scrollChannelsList = new ScrollObj("channelsList");
	scrollRigthContent = new ScrollObj("tabsContentHolder");
	
	MakeRequest2("Template=Global\\ChannelList","#channelsList .contentArea");
	MakeRequest2("Template=Global\\ContentListBroadcast","#rightDynamicContent");
	MakeRequest2("Template=Global\\ContentPlayer","#mediaplayer");
	MakeRequest2('Template=Global\\LocalPlayList&WebTVLocalPlaylistAction=count');
	MakeRequest2("Template=Global\\TemplateCaption","#caption");
});

function RefreshHeightscrollChannelsList() {
	scrollChannelsList.RefreshHeightscroller();
}
function RefreshHeightscrollRigthContent() {
	scrollRigthContent.RefreshHeightscroller();
	//alert("");
}

function ClearBoxes()
{
	MakeRequest2("Template=Global\\TemplateCaption","#caption");
}

function LocalPlayListSetCurrent(idWinMediaPlayer)
{
	
	oIntervalLocalPlayList = window.setInterval(
		function () 
		{ 
			$('#contentSecond .lnkStartPlayList').css('visibility','visible'); 
			var oMediaPlayer=document.getElementById(idWinMediaPlayer);  
			var iCurrent=0;
				
			if (oMediaPlayer!=null)
			{
				
				if (oMediaPlayer.playState == 3)
				{
					iCurrent = Number(oMediaPlayer.currentMedia.name)-1;
					if (iCurrent != iSelPLClip)
					{
						var oPlayList = oMediaPlayer.currentPlaylist;
						if(oPlayList.name == "Kboard.Webtv.LocalPlayList")
						{
							$("#LocalPlayListContent" + iSelPLClip ).css("background","url(/pix/bg_item_playlist.gif) no-repeat left top") ;
							$("#LocalPlayListContent" + iCurrent ).css("background","url(/pix/bg_item_playlist_selected.gif) no-repeat left top") ;
							iSelPLClip = iCurrent;
						}
						else
							window.clearInterval(oIntervalLocalPlayList);
					}
				}
				 
			}
			else
				window.clearInterval(oIntervalLocalPlayList);
		}
		,5000);
}

function PlayerFunction(idWinMediaPlayer,sAction)
{
	 
	var oMediaPlayer=document.getElementById(idWinMediaPlayer);  
	if (oMediaPlayer!=null)
	{
		switch(sAction.toLowerCase())
		{
			case "fullscreen":
			
				if (oMediaPlayer.playState == 3)
					oMediaPlayer.fullScreen=true;
				 
				break;
			case "localplaylist":
				
				LocalPlayListSetCurrent(idWinMediaPlayer);
				break;
						 
		}
	}  
}

 

function CallbackOperations(Xml)
{
	 
	//BEGIN BroadcastChannel Current play list entry
	try{	
	var Control_node = Xml.getElementsByTagName('Channel')[0];
	if (Control_node!=null && Control_node.getAttribute('isbroadcast')) 
	{
		var CurrentPlayListEntry = Control_node.getAttribute('currentplaylistentry');
		if (CurrentPlayListEntry!=null)
		{
			var ContentList_node = Xml.getElementsByTagName('ContentList')[0];
			var iCountContentList = Number(ContentList_node.getAttribute('count'));
			 
			if (odtBroadcastLastModified != null && String(odtBroadcastLastModified) !=  String(ContentList_node.getAttribute('lastmodified')))
			{
				window.clearInterval(oIntervalBroadcast);
				MakeRequest2("Template=Global\\ContentListBroadcast","#rightDynamicContent");
				odtBroadcastLastModified=String(ContentList_node.getAttribute('lastmodified'));
			}	
			else
			{	
				odtBroadcastLastModified=String(ContentList_node.getAttribute('lastmodified'));
			
				var iCurrentPlayListEntry = Control_node.getAttribute('currentplaylistentry');
				
				$("#Content" + String( Number(iCurrentPlayListEntry)-1) ).css("background","url(/pix/bg_item.gif) no-repeat left top") ;
				if(Number(iCurrentPlayListEntry)==1)
					$("#Content" + String( Number(iCountContentList)) ).css("background","url(/pix/bg_item.gif) no-repeat left top") ;
					
				 
				$("#Content" + iCurrentPlayListEntry).css("background","url(/pix/bg_item_selected.gif) no-repeat left top") ;
				oIntervalBroadcast = window.setInterval(
			 		function () {
			 			MakeRequest2("Template=Global\\ContentListBroadcast&WebTVBroadcastAction=currentplaylistentry",null,true);
				 			
				},Number(30000));
			}
		}
	}
	}
	catch(e){}
	//END   BroadcastChannel Current play list entry
	
	//BEGIN LocalPlaylist
	try{
	 var LocalPlayList_node = Xml.getElementsByTagName('PlayList')[0];
	if (LocalPlayList_node != null)
	{
		
		var ContentList_node = LocalPlayList_node.firstChild;
		var iCountContentList = ContentList_node.getAttribute('count');
		if(isNaN(iCountContentList) || Number(iCountContentList)<=0)
		{
			$("#tabMyPlayList").html("MY PLAYLIST");
			
		}
		else
		{
			$("#tabMyPlayList").html("MY PLAYLIST (" + iCountContentList + ")" ); 
		}
		
		if (ContentList_node.getAttribute('action')=="remove")
			$("#contentSecond .titoloContenuto").html($("#tabMyPlayList").html());	
			
		 
	} 
	}
	catch(e){}
	//END LocalPlaylist
	 
}
 
function MakeRequest2(Param, DomElement,noloading,sAttrName) 
{ 
	window.clearInterval(oIntervalBroadcast);
	window.clearInterval(oIntervalLocalPlayList);	
	//$("#generalLoading").clone().prependTo($(DomElement).parent()).css("visibility","visible");
	if (noloading!=true)
	{
		$("#generalLoading").css("visibility","visible");
	}
	 
	if(String(Param).indexOf('Template=Global\\LocalPlaylist')==-1)
		$('#contentSecond .lnkStartPlayList').css('visibility','hidden');
	//$(DomElement).css("visibility","hidden");
	$.get("/site/GetAjaxContent.html?"+Param, function(Xml){
		$(DomElement).html($("ContentHTML",Xml).text());
		$("#generalLoading").css("visibility","hidden");
		//$("#generalLoading").clone().prependTo($(DomElement)).css("visibility","hidden");
		
	
		CallbackOperations(Xml);
		
		switch (DomElement)
		{
			case "#channelsList .contentArea":
				$(DomElement).fadeIn(500,RefreshHeightscrollRigthContent);
				$("#channelsList .item .lnk").mousedown(function(){
				 
					$("DIV.container:visible",this.parentNode.parentNode.parentNode).BlindUp(500,RefreshHeightscrollChannelsList,"bounceout");
					 
					if(Number(this.getAttribute('count'))>0)
						$("DIV.container:hidden",this.parentNode).BlindDown(700,RefreshHeightscrollChannelsList,"bounceout");
					//$("DIV.container",this.parentNode).BlindToggleVertically(700,RefreshHeightscrollChannelsList,"bounceout");
		
					$("#contentSecond .titoloContenuto").html("ON DEMAND - " + String(this.innerHTML).toUpperCase());
					$("#channelsList .item .lnk").removeClass("current");
					$(this).addClass("current"); 
				});
				RefreshHeightscrollChannelsList();
				break;
			case "#rightDynamicContent":
				/*
				$("#rightDynamicContent .item .bottomLinks a").click(function(){
					$(".scheda:visible",this.parentNode.parentNode.parentNode.parentNode.parentNode).BlindUp(300,RefreshHeightscrollRigthContent,"easein");
					$(".scheda:hidden",this.parentNode.parentNode.parentNode.parentNode.parentNode).BlindDown(300,RefreshHeightscrollRigthContent,"easein");
				});
				*/
				$(DomElement).fadeIn(500,RefreshHeightscrollRigthContent);
				break;
			case "#mediaplayer":
				
				$('#mediaplayer object').ready(function(){
					//$(DomElement).fadeIn(500,RefreshHeightscrollRigthContent);
					//alert("chow");
				});
				break;
			default:
				if($(DomElement).attr("class")=="scheda")
				{	 
					if ( $(DomElement).attr("name") == sAttrName )
					{
						$(".scheda:visible",$(DomElement).parent()).BlindUp(300,RefreshHeightscrollRigthContent,"easein")
						$(DomElement).attr("name",sAttrName+"Closed");
					}
					else
					{
						$(".scheda:hidden",$(DomElement).parent()).BlindDown(300,RefreshHeightscrollRigthContent,"easein");
						$(DomElement).attr("name",sAttrName);
					}
				}
				 
				RefreshHeightscrollRigthContent();
				break;
		}
	}) ;
	
}

function rightTabsClick(Param,objLink)
{
	if(Param!='')
	{ 
		$('#contentSecond .sectionTabs li').removeClass('current');
		objLink.parentNode.className+='current';
		$("#contentSecond .titoloContenuto").html(objLink.innerHTML);
		
		MakeRequest2(Param,'#rightDynamicContent'); 
	}
}

function unselectTabs()
{
	$('#contentSecond .sectionTabs li').removeClass('current');
}
 