// JavaScript Document
// Custom
//var _imgHost = "img\/";
//var _imgHost = "http:\/\/desarrollo-cstv.nspmotion.com\/10201\/img\/";
var _imgHost = "http:\/\/fsi.dmmotion.com\/10201\/img\/";
//var _imgTime = "<img src='"+_imgHost+"img_hour.gif' width='9' height='9' \/>";

var selectedItem 	= [0, 0, 0];
var MaxPlayList		= 2;
var PlIndex;

function onPlayerInit(PyIndex){
	
	// Inicializacion
	var player = admtv.Players[PyIndex];

	player.onContentChange = function (evObj) {
		var ItemId = this.getCurrentItemInfo();
		var Item = this.getItemById(ItemId[0], ItemId[1], ItemId[2]);
		document.getElementById("adm_evTexto").innerHTML = Item.Title;
		
		setADMactive(ItemId);
	}
	// PlayList
	mostrarPlaylists(PyIndex, 0);
	// Destacados
	Destacados = player.getHighlights();
	showDestacados(0, PyIndex);
}

function generarPlaylist(PyIndex, ChIndex, PlIndex, n) {
	var thisPlayer 		= admtv.Players[PyIndex];
	var thisCanal 		= thisPlayer.Content.Channels[ChIndex];
	var thisPlaylist 	= thisCanal.Playlists[PlIndex];
	var activo 			= (n == selectedItem[0]);
	var u = "<td><div onclick='selectedItem[0]="+n+";mostrarItems("+PyIndex+", "+ChIndex+", "+PlIndex+");mostrarPlaylists("+PyIndex+", "+ChIndex+")'  style='cursor:pointer;'";
	u+= (activo) ? "class='amd_plist1'" : "class='amd_plist'";
	u+= ">"+thisPlaylist.Title;
	u+= "<\/div><\/td>";
	return u;
}

function mostrarPlaylists(PyIndex, ChIndex) {
	var thisPlayer 	= admtv.Players[PyIndex];
	var thisCanal 	= thisPlayer.Content.Channels[ChIndex];
	
	var num = 0;
	var it 	= thisPlayer.Content.Channels.length;
	var u = "<table border='0' cellspacing='0' cellpadding='0'><tr>";
		for (var i=0; i<it; i++) {
			var channelPls = thisPlayer.Content.Channels[i].Playlists.length;
			for(var t=0; t < channelPls; t++){
				if(num <= MaxPlayList){
					u+= generarPlaylist(PyIndex, i, t, num);
				}
				num++;
			}
		}
		u+="<\/tr><\/table>";
	document.getElementById("adm_playList").innerHTML=u;
	mostrarItems(PyIndex, ChIndex, 0);
}



function generarItem(PyIndex, ChId, PlId, ItId, num) {
	var thisPlayer = admtv.Players[PyIndex];
	var thisCanal = thisPlayer.getChannelById(ChId);
	var thisPlaylist = thisPlayer.getPlaylistById(ChId, PlId);
	var thisItem = thisPlayer.getItemById(ChId, PlId, ItId);
	var fecha = new Date(thisItem.StartDateMS);
	var assetId = "ADM_AS_"+thisItem.Id;
	
	if (num/2 == Math.round(num/2)){
		var	u= "<td onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})'><table id='"+assetId+"' class='adm_ItemsPar' border='0' cellspacing='0' cellpadding='0'><tr";
		u+= "><td width='60' style='padding:5px;'><img src='"+admtv.getThumbnailURL(thisPlayer.Content.Id, thisItem, "s")+"' width='55' height='43' style='border-width:1px;border-style:solid;border-color:#555555' \/><\/td>";
		u+= "<td width='317' valign='top' style='padding:10px;padding-left:5px;color:#444444;'>";
		u+= "<span class='adm_itemTitle'>"+thisItem.Title+"<\/span><br \/>";
		u+= "<span class='adm_flechaTrailer'>&nbsp;&nbsp;&nbsp;&nbsp;Ver Trailer<\/span><br \/>";
		u+= "<\/tr><\/table><\/td>";
	}else{
		var	u= "<td onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})'><table id='"+assetId+"' class='adm_Items' border='0' cellspacing='0' cellpadding='0'><tr";
		u+= "><td width='60' style='padding:5px;'><img src='"+admtv.getThumbnailURL(thisPlayer.Content.Id, thisItem, "s")+"' width='60' height='40' style='border-width:1px;border-style:solid;border-color:#555555' \/><\/td>";
		u+= "<td width='317'  valign='top' style='padding:10px;padding-left:5px;color:#444444;'>";
		u+= "<span class='adm_itemTitle'>"+thisItem.Title+"<\/span><br \/>";
		u+= "<span class='adm_flechaTrailer'>&nbsp;&nbsp;&nbsp;&nbsp;Ver Trailer<\/span><br \/>";
		u+= "<\/tr><\/table><\/td>";
	}
	return u;
}

function mostrarItems(PyIndex, ChIndex, PlIndex) {
	var indice = PyIndex;
	var thisPlayer = admtv.Players[PyIndex];
	var thisCanal = thisPlayer.Content.Channels[ChIndex];
	var thisPlaylist = thisCanal.Playlists[PlIndex];
	var it = thisPlaylist.Items.length;
	var u = "";
		u+="<table>";
		u+="<tr>";
		for (var i=0; i<it; i++) u += generarItem(PyIndex, thisCanal.Id, thisPlaylist.Id, thisPlaylist.Items[i].Id, i);
		document.getElementById("adm_plitems").innerHTML=u;
		u+="</tr>";
		u+="</table";
	//document.getElementById("TVPlayListTitle").innerHTML=thisPlaylist.Title;
	admtvSL_setSelected(true);
}

function showDestacados(n, PyIndex) {
	var player = admtv.Players[PyIndex];
	if (n==Destacados.length) n = 0;
	var clickArray = {ChannelId:Destacados[n].ChannelId, PlaylistId:Destacados[n].PlaylistId, Id:Destacados[n].Id};
	if (Destacados[n].Id != player.getCurrentItemInfo()[2]) {
	var objDes = player.getItemById(Destacados[n].ChannelId, Destacados[n].PlaylistId, Destacados[n].Id);
		var t = "";
			t+= "<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
			t+= "<tr>";
			t+= "<td height='20' style='padding:10px;'>";
			t+= '<span id="adm_flecha2">&gt; <\/span><span class="adm_proximas">PR&Oacute;XIMAS PREMIERE<\/span>';
			t+= "<\/td>";
			t+= "<\/tr>";
			t+= "<\/table>";
			t+= "<table cellpadding='0' cellspacing='10' border='0' width='100%'>";			
			t+= "<tr>";
			t+= '<td valign="top" width="115"><img class="adm_ThumbVideo" src="'+player.getItemThumbnail(clickArray, "m")+'"\/><\/td>';
			t+= '<td valign="top" id="adm_DescVideo">';
			t+= '<div class="adm_premiereTitulo">'+objDes.Title+'<\/div>';
			if (typeof(objDes.Description)!="undefined") {
				t+= '<div class="adm_DescVideo">'+objDes.Description+'<\/div>';
			}
			t+= "<br><span class='adm_flechaTrailer'>&nbsp;&nbsp;&nbsp;&nbsp;Ver Trailer<\/span>";
			t+= '<\/td>';
			t+= "<\/tr>";
			t+= "<\/table>";

		var obj = document.getElementById("adm_premiere");
		obj.innerHTML = t;
		obj.style.cursor = "pointer";
		obj.onclick = function () {
			player.playVideo(clickArray);
		}
		n++;
	}
	else showDestacados(n+1, PyIndex);
}

function genSWFcode(id, width, height, file, fv, wmode, bgcolor) {
	if (typeof(fv)=="undefined") fv="";
	if (typeof(wmode)=="undefined") wmode="window";
	var h = '';
	h+= '<object id="'+id+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'"><param name=movie value="'+file+'" \/><param name=quality value=high \/><param name=menu value=false \/>';
	if (typeof(bgcolor)!="undefined") h+= '<param name="bgcolor" value="'+bgcolor+'" \/>';
	h+= '<param name="wmode" value="'+wmode+'" \/>';
	h+= '<param name=FlashVars value="'+fv+'" \/>';
	h+= '<param name="allowScriptAccess" value="always" \/><param name="allowFullScreen" value="true" \/>';
	h+= '<embed name="'+id+'" src="'+file+'" menu=false quality=high width="'+width+'" height="'+height+'" type="application\/x-shockwave-flash" pluginspace="http:\/\/www.macromedia.com\/go\/getflashplayer" wmode="'+wmode+'"';
	if (typeof(bgcolor)!="undefined") h+= ' bgcolor="'+bgcolor+'"';
	h+= ' FlashVars="'+fv+'"';
	h+= ' allowScriptAccess="always" allowFullScreen="true"><\/embed>';
	h+= '<\/object>';
	return h;
}

//************ ELEMENTO SELECIONADO ***********//
var admtvSL_myClass_Channel = "adm_Channel";
var admtvSL_myClass_playlist = "adm_Playlist";
var admtvSL_myClass_Items = "adm_Items";

var admtvSL_ch = "";
var admtvSL_pl = "";
var admtvSL_as = "";

function setADMactive(It){
	admtvSL_setSelected(false);
	try {
		admtvSL_myClass_Items = document.getElementById("ADM_AS_"+It[2]).className;
	} catch (e) {}
		
	admtvSL_ch = "ADM_CH_"+It[0];
	admtvSL_pl = "ADM_PL_"+It[1];
	admtvSL_as = "ADM_AS_"+It[2];
	admtvSL_setSelected(true);
}
function admtvSL_setSelected(bool){
	//
	var aclass_0 = (bool) ? admtvSL_myClass_Channel+"_S" : admtvSL_myClass_Channel;
	var aclass_1 = (bool) ? admtvSL_myClass_playlist+"_S" : admtvSL_myClass_playlist;
	var aclass_2 = (bool) ? admtvSL_myClass_Items+"_S" : admtvSL_myClass_Items;
	
	var CH_elem = document.getElementById(admtvSL_ch);
	var PL_elem = document.getElementById(admtvSL_pl);
	var AS_elem = document.getElementById(admtvSL_as);
	if (CH_elem) AS_elem.className = aclass_0;
	if (PL_elem) PL_elem.className = aclass_1;
	if (AS_elem) AS_elem.className = aclass_2;
}
