// JavaScript Document
// Custom
//var _imgHost = "img\/";
var _imgHost = "http:\/\/fsi.dmmotion.com\/10216\/img\/";
//var _imgHost = "http:\/\/desarrollo-cstv.nspmotion.com\/10216\/img\/";
var _imgTime = "<img src='"+_imgHost+"img_hour.gif' width='9' height='9' \/>";
var selectedItem = [0, 0, 0];
var _channelsPerPage = 7;
var _channelsPage = 0;
var seleccionado;

function onPlayerInit(PyIndex){
	var player = admtv.Players[PyIndex];
	// Defino eventos
	document.getElementById("admtv_term").disabled = false;
	document.getElementById("admtv_term").onkeypress = function (e) {
		if (typeof(e)=="undefined") e = event;
		buscarVideoInput(e, player.Id);
	}
	document.getElementById("admtv_term_bt").disabled = false;
	document.getElementById("admtv_term_bt").onclick = function () {
		buscarVideo(player.Id);
	}
	player.onContentChange = function (evObj) {
		var ItemId = this.getCurrentItemInfo();
		var Item = this.getItemById(ItemId[0], ItemId[1], ItemId[2]);
		document.getElementById("SPEstasViendo").innerHTML = Item.Title;
		setADMactive(ItemId);
	}
	player.onMostViewedFetched = function (mv) {
		mostrarTop(mv, this.Id, document.getElementById("TVvistos"));
				
	}
	player.onMostSentFetched = function (ms) {
		mostrarTop(ms, this.Id, document.getElementById("TVenviados"));
	}
	player.onSearchFinished = function (evObj) {
		showSearch(this.Index, evObj);
	}
	// Destacados
	Destacados = player.getHighlights();
	if (Destacados.length>0) showDestacados(0, PyIndex);
	// Mas vistos TOP 6 inicio
	player.getMostViewed(6, 2);
	// Mas enviados TOP 6 inicio
	player.getMostSent(6, 2);
	// Canalera
	mostrarCanales(PyIndex);
}

function generarCanal(PyIndex, ChIndex) {
	var thisPlayer = admtv.Players[PyIndex];
	var thisCanal = thisPlayer.Content.Channels[ChIndex];
	var activo = (ChIndex == selectedItem[0]);
	var oncl = " onclick='selectedItem[0]="+ChIndex+";mostrarCanales("+PyIndex+")' ";
	var u = "<td"+oncl+"style='cursor:pointer;' width='4' valign='bottom'><img src='"+_imgHost;
		u+= (activo) ? "sola01" : "trans";
		u+= ".gif' width='4' height='26' \/><\/td>";
		u+= "<td"+oncl+"style='height:26;cursor:pointer;";
		if (activo) u+= "background:url(\""+_imgHost+"sola02.gif\") repeat-x;";
		u+= "padding-left:3px;padding-right:3px; font-size:11px;color:#";
		u+= (activo) ? "0066CB" : "ffffff";
		u+= ";font-weight:";
		u+= (activo) ? "bold" : "normal";
		u+= ";'>"+thisCanal.Title.toUpperCase()+"<\/td>";
		u+= "<td style='cursor:pointer;'"+oncl+"width='4' valign='bottom'><img src='"+_imgHost;
		u+= (activo) ? "sola03" : "trans";
		u+= ".gif' width='4' height='26' \/><\/td>";
	return u;
}
function mostrarCanales(PyIndex) {
	var thisPlayer = admtv.Players[PyIndex];
	var iu = _channelsPage*_channelsPerPage;
	var it = Math.min(iu+_channelsPerPage, thisPlayer.Content.Channels.length);
	var u = "<table border='0' cellspacing='0' cellpadding='0'><tr>";
	for (var i = iu; i<it; i++) {
		u+= generarCanal(PyIndex, i);
	}
		u+= "<\/tr><\/table>";
		u+= "<div id='BTNmas'>";
		u+= "<input type='image' onclick='masCanales("+PyIndex+", false)' ";
		if (_channelsPage==0) {
			u+= "src='"+_imgHost+"bt_ant_off.gif' disabled='disabled'";
		} else u+= "src='"+_imgHost+"bt_ant.gif' style='cursor:pointer;'";
		u+= " />";
		u+= "<input type='image' onclick='masCanales("+PyIndex+", true)' ";
		if (iu+_channelsPerPage >= thisPlayer.Content.Channels.length) {
			u+= "src='"+_imgHost+"bt_sig_off.gif' disabled='disabled'";
		} else u+= "src='"+_imgHost+"bt_sig.gif' style='cursor:pointer;'";
		u+= " />";
		u+= "<\/div>";
	document.getElementById("canales").innerHTML=u;
	mostrarPlaylists(PyIndex, selectedItem[0]);
}
function masCanales(PyIndex, avanza){
	_channelsPage = (avanza) ? _channelsPage+1 : _channelsPage-1;
	mostrarCanales(PyIndex);
}

function generarPlaylist(PyIndex, ChIndex, PlIndex) {
	var thisPlayer = admtv.Players[PyIndex];
	var thisCanal = thisPlayer.Content.Channels[ChIndex];
	var thisPlaylist = thisCanal.Playlists[PlIndex];
	var u = (PlIndex==0) ? "" : "<td style='padding-left:4px; padding-right:4px'><img src='"+_imgHost+"separadorItem.gif' width='7' height='12' \/><\/td>";
		u+= "<td style='cursor:pointer; color:#3D3D3D; font-size:12px; font-weight:800;' onclick='selectedItem[1]="+PlIndex+";mostrarItems("+PyIndex+", "+ChIndex+", "+PlIndex+")'>"+thisPlaylist.Title+"<\/td>";
	
	return u;
}
function mostrarPlaylists(PyIndex, ChIndex) {
	var thisPlayer = admtv.Players[PyIndex];
	var thisCanal = thisPlayer.Content.Channels[ChIndex];
	var it = thisCanal.Playlists.length;
	if (it<=selectedItem[1]) selectedItem[1] = 0;
	var u = "<table border='0' cellspacing='6' cellpadding='0'><tr>";
	for (var i=0; i<it; i++) {
		u+= generarPlaylist(PyIndex, ChIndex, i);
	}
	u+= "<\/tr><\/table>";
	document.getElementById("TVItems").innerHTML=u;
	mostrarItems(PyIndex, selectedItem[0], selectedItem[1]);
}

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;
	
	var anio = formatDate(fecha).toString().substring(0,8);
	var hora = formatDate(fecha).toString().substring(11);


	
	if (num/2 == Math.round(num/2)){
		var u = "<table id='"+assetId+"' class='admTV_ItemsPar' style:'font-size:12px;' onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})' border='0' cellspacing='0' cellpadding='0'><tr";
		u+= "><td width='90' style='padding:5px;'><img src='"+admtv.getThumbnailURL(thisPlayer.Content.Id, thisItem, "m")+"' width='90' height='60' style='border-width:1px;border-style:solid;border-color:#555555' \/><\/td>";
		u+= "<td width='317' style='padding:10px;padding-left:5px;color:#444444;'>";
		u+= "<span style='color:#0066CB;'>"+anio+ " | "+"<\/span><span style='color:#737477;'>"+ hora+"<\/span><br \/>";
		u+= "<span class='itemTitle'>"+thisItem.Title+"<\/span><br \/>";
		if (typeof(thisItem.Description)!="undefined") {
			if (thisItem.Description!="null") u += "<span style='color:#737477;'>"+thisItem.Description+"<\/span><br \/><br \/>";
		}
		u += "<span style='color: #737477;'>Reproducido: "+thisItem.Impressions+" veces";
		u+= "<\/tr><\/table>";
	}else{
		var u = "<table id='"+assetId+"' class='admTV_Items' style:'font-size:12px;' onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})' border='0' cellspacing='0' cellpadding='0'><tr";
		u+= "><td width='90' style='padding:5px;'><img src='"+admtv.getThumbnailURL(thisPlayer.Content.Id, thisItem, "m")+"' width='90' height='60' style='border-width:1px;border-style:solid;border-color:#555555' \/><\/td>";
		u+= "<td width='317' style='padding:10px;padding-left:5px;color:#444444;'>";
		u+= "<span style='color:#0066CB;'>"+anio + " | "+"<\/span><span style='color:#737477;'>"+hora+"<\/span><br \/>";
		u+= "<span class='itemTitle'>"+thisItem.Title+"<\/span><br \/>";
		if (typeof(thisItem.Description)!="undefined") {
			if (thisItem.Description!="null") u += "<span style='color:#737477;'>"+thisItem.Description+"<\/span><br \/><br \/>";
		}
		u += "<span style='color: #737477;'>Reproducido: "+thisItem.Impressions+" veces";
		u+= "<\/tr><\/table>";
	}
	return u;
	
}

function mostrarItems(PyIndex, ChIndex, PlIndex) {
	var thisPlayer = admtv.Players[PyIndex];
	var thisCanal = thisPlayer.Content.Channels[ChIndex];
	var thisPlaylist = thisCanal.Playlists[PlIndex];
	var it = thisPlaylist.Items.length;
	var u = "";
	for (var i=0; i<it; i++) u += generarItem(PyIndex, thisCanal.Id, thisPlaylist.Id, thisPlaylist.Items[i].Id, i);
	document.getElementById("TVPlayList").innerHTML=u;
	document.getElementById("TVPlayListTitle").innerHTML=thisPlaylist.Title.toUpperCase();
	admtvSL_setSelected(true);
}

function setupSearch(PyIndex) {
	var oEl = document.getElementById("TVPlayListTitle");
	oEl.innerHTML = "";
	return oEl;
}
function showSearch(PyIndex, obj) {
	var oEl = document.getElementById("TVPlayList");
	oEl.innerHTML = "";
	var player = admtv.Players[PyIndex];
	var oTit = setupSearch(PyIndex);
	oTit.innerHTML = "Resultados para: \""+player.searchingFor+"\"";
	var u = '';
	var it = obj.length;
	for (var i = 0; i<it; i++) {
		u += generarItem(PyIndex, obj[i].ChannelId, obj[i].PlaylistId, obj[i].Id, i);
	}
	oEl.innerHTML = u;
	if(it == 0){
		oEl.innerHTML = "No se encontraron resultados";
	}
}
function buscarVideo(PyId) {
	if (typeof(admtv.getPlayerById(PyId))!="undefined") {
		var player = admtv.getPlayerById(PyId);
		var s=document.getElementById("admtv_term").value.replace(/^\s*|\s*$/g,"");
		if (s.length>3) {
			var oEl = setupSearch(player.Index);
			oEl.innerHTML="Buscando...";
			player.search(s);
		}
	}
}
function buscarVideoInput(evObj, PyId) {
	var keyCode = evObj.keyCode ? evObj.keyCode : evObj.which ? evObj.which : evObj.charCode; 
	if (keyCode == 13) {
		buscarVideo(PyId);
	}
}
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'>";
			t+= "<tr>";
			t+= '<td class="ThumbVideo" valign="top"><img width="100" height="65" src="'+player.getItemThumbnail(clickArray, "m")+'"\/><\/td>';
			t+= '<td id="DescVideo">';
				var fecha = new Date(objDes.StartDateMS);
				
				//formato de fecha
				var anio = formatDate(fecha).toString().substring(0,8);
				var hora = formatDate(fecha).toString().substring(11);
					
				
				t+= "<span style='color:#0066CB;padding-left:10px;'>"+anio + " | "+"<\/span><span style='color:#737477;'>"+hora+"<\/span><br \/>";
				t+= '<div class="TituloVideo">'+objDes.Title+'<\/div>';
				if (typeof(objDes.Description)!="undefined") {
					t+= '<div class="DescVideo">'+objDes.Description+'<\/div>';
				}
			t+= '<\/td>';
			t+= "<\/tr>";
			t+= "<\/table>";

		var obj = document.getElementById("destacados");
		obj.innerHTML = t;
		obj.style.cursor = "pointer";
		obj.onclick = function () {
			player.playVideo(clickArray);
		}
		n++;
		if (Destacados.length>1){
			setTimeout("showDestacados("+n+", "+PyIndex+")", 8000);
		}
	}
	else showDestacados(n+1, PyIndex);
}

function generarTop(PyIndex, ChIndex, PlIndex, ItIndex, num) {
	var thisCanal = admtv.Players[PyIndex].Content.Channels[ChIndex];
	var thisPlaylist = thisCanal.Playlists[PlIndex];
	var thisItem = thisPlaylist.Items[ItIndex];
	var fecha = new Date(thisItem.StartDateMS);
	//formato de fecha
	var anio = formatDate(fecha).toString().substring(0,8);
	var hora = formatDate(fecha).toString().substring(11);
	
	var assetId = "ADM_AS_"+thisItem.Id;
	
	var u = "<table id='"+assetId+"' class='admTV_Items' width='380' style=' background-color:#FFFFFF;' ";
	u += "onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})' ";
	//u += "onclick='selected(true, "+PyIndex+","+thisCanal.Id+", "+thisPlaylist.Id+", "+thisItem.Id+")' ";
	u += "border='0' cellspacing='0' cellpadding='4'><tr>";

	u += "<td valign='top' width='";
	u += (num == 0) ? "130' height='90" : "90' height='70";
	u += "'><img src='";
	u += (num == 0) ? admtv.getThumbnailURL(admtv.Players[PyIndex].Content.Id, thisItem, "m")+"' width='130' height='80'" : admtv.getThumbnailURL(admtv.Players[PyIndex].Content.Id, thisItem, "m")+"' width='90' height='60'";
	u += "\/><\/td>";

	u += "<td style='padding:5px; font-size: 11px;' valign='top'>";
	u += "<span style='color: #0066CB;'>"+anio+ " | "+"<\/span><span style='color:#737477;'>"+hora+"<\/span><br \/>";

	u += "<span class='itemTitle' style='"
	u += (num == 0) ? "font-size:120%;" : "";
	u += "'>"+thisItem.Title+"<\/span>";
	if (typeof(thisItem.Description)!="undefined") {
		if (thisItem.Description != "null") u += "<br \/><div style='color:#737477; width:200px; overflow:hidden;'>"+thisItem.Description+"<\/div>";
	}
	u += "<span style='color: #0066CB;'>Reproducido: "+thisItem.Impressions+" veces";
	u += "<\/td>";

	u +="<tr><td colspan='2' style='background:url("+_imgHost;
	u +=  "at_pun.gif";
	u +=") repeat-x'><img src='"+_imgHost+"trans.gif' width='3' height='1' \/><\/td><\/tr>";
	u += "<\/table>";
	
	return u;	
}
function mostrarTop(mvObj, id, divHolder) {
	var PyIndex = admtv.getPlayerIndexById(id);
	var it = mvObj.length;
	var fecha= new Date();
	var u = "<img src='"+_imgHost+"trans.gif' width='3' height='10' \/>";
	for (var i=0; i<it; i++) {
		var chIndex = admtv.Players[PyIndex].getChannelIndexById(mvObj[i]["ChannelId"]);
		var plIndex = admtv.Players[PyIndex].getPlaylistIndexById(mvObj[i]["ChannelId"], mvObj[i]["PlaylistId"]);
		var itIndex = admtv.Players[PyIndex].getItemIndexById(mvObj[i]["ChannelId"], mvObj[i]["PlaylistId"], mvObj[i]["Id"]);
		if (typeof(itIndex)!="undefined") u+=generarTop(PyIndex, chIndex, plIndex, itIndex, i);
	}
	divHolder.innerHTML=u;
	//admtvSL_setSelected(true);
}

function formatTime(t) {
	var m = Math.floor(t/60);
	var s = Math.floor(t%60);
	if (s<10) s = ("0"+s);
	if (m<10) m = ("0"+m);
	return m+":"+s;
}
function formatDate(d) {
	var dia = (d.getDate()<10) ? "0"+d.getDate() : d.getDate();
	var mes = (d.getMonth()<9) ? "0"+(d.getMonth()+1) : d.getMonth()+1;
	
	var hora = d.getHours();
	var ind = "AM";
	if (hora >= 12) {
		hora = (hora!=12) ? hora-12 : hora;
		ind = "PM";
	} else if (hora == 0) {
		hora = 12;
	}
	hora = (hora<10) ? "0"+hora : hora;

	var minu = (d.getMinutes()<10) ? "0"+d.getMinutes() : d.getMinutes();
	return dia+"-"+mes+"-"+d.getFullYear().toString().substring(2,4)+" | "+hora+":"+minu+" "+ind;
}

function textEllipsis(t, l) {
	var u = t.substr(0, l);
	if (u != t) u+="...";
	return u;
 }
//************ ELEMENTO SELECIONADO ***********//
var admtvSL_myClass_Channel = "admTV_Channel";
var admtvSL_myClass_playlist = "admTV_Playlist";
var admtvSL_myClass_Items = "admTV_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;
}
