// JavaScript Document
// Custom
//var _imgHost = "img\/";
//var _imgHost = "http:\/\/desarrollo-cstv.nspmotion.com\/10181\/img\/";
var _imgHost = "http:\/\/fsi.dmmotion.com\/10181\/img\/";

var _urlVerMasV = "";

function onPlayerInit(PyIndex){
	var player = admtv.Players[PyIndex];

	
	
	var verMasVideos = document.getElementById("ADMVermasvideos");
	var admLogo = document.getElementById("ADMLogo");
	

	verMasVideos.innerHTML = "<img src='"+_imgHost+"vermasvideos.gif' />";
	
	player.onContentChange = function (evObj) {
	
		_urlVerMasV = player.generatePermalink();
	}

	verMasVideos.onclick = function()
	{
		window.open(_urlVerMasV,'_blank');
	}
	player.onMostRecentFetched = function (mv) {
		mostrarTop(mv, this.Id, document.getElementById("ADMPlaylist"));
	}
	
	// Mas vistos TOP 5 Hoy
	player.getMostRecent(3, 2);
	
}












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='340' style=' background-color:#eaeaea;cursor:pointer;' ";
	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  width='";
	u += "48' height='35";
	u += "'><img src='";
	u += admtv.Players[PyIndex].getItemThumbnail({ChannelId:thisCanal.Id, PlaylistId:thisPlaylist.Id, Id:thisItem.Id}, "s")+"' width='45' height='35'";
	u += " style='border: #c7d2db solid 1px;' \/><\/td>";

	u += "<td style='padding:5px; font-size: 11px;' valign='top'>";
	

	u += "<span style='color:#000000;font-size:11px;font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:bold;height:15px;overflow:hidden;"
	
	u += "'>"+textEllipsis(thisItem.Title, 25)+"<\/span>";
	if (typeof(thisItem.Description)!="undefined") {
		if (thisItem.Description != "null") u += "<br \/><span style='color:#000000;font-size:11px;font-family:Verdana, Arial, Helvetica, sans-serif;height:30px;overflow:hidden;'>"+textEllipsis(thisItem.Description,64)+"<\/span>";
	}
	u += "<\/td>";
	u += "<td width='40'>";
	u += "<img src='"+_imgHost+"iconPlay.jpg' width='24' height='24' \/>"
	u += "<\/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 = "";
	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;
}


//************ 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;
}


function textEllipsis(t, l) {
	var u = t.substr(0, l);
	if (u != t) u+="...";
	return u;
}