// JavaScript Document
// Custom
//var _imgHost = "img\/";
var _imgHost = "http:\/\/desarrollo-cstv.nspmotion.com\/10181\/img\/";
//var _imgHost = "http:\/\/fsi.dmmotion.com\/10181\/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;

document.getElementById("uploadVideo").style.visibility = 'hidden';

function onPlayerInit(PyIndex){
	
	initAction = admtv.queryToObj(location.search.substring(1)).action;
	// Inicializacion
	var player = admtv.Players[PyIndex];
	
	if (typeof(initAction) != "undefined" && initAction == "upload") {
		
		showUpload(PyIndex);
	} else mostrarCanales(PyIndex);//showCanal(PyIndex, "3");
	

	// 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("eViendo").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 5 Hoy
	player.getMostViewed(5, 2);
	// Mas enviados TOP 5 Hoy
	player.getMostSent(5, 3);
	// Canalera
	//mostrarCanales(PyIndex);

	sube(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+");showCanalDelay("+PyIndex+", "+activo+")' ";
	var u = "<td"+oncl+"style='cursor:pointer;' width='4' width='4' valign='bottom'><img src='"+_imgHost;
		u+= (activo) ? "sola01" : "trans";
		u+= ".gif' width='4' height='26' \/><\/td>";
		u+= "<td"+oncl+"style='cursor:pointer;";
		if (activo) u+= "background:url(\""+_imgHost+"sola02.gif\") repeat-x;";
		u+= "padding:3px 5px 0px 5px; font-size:10px; font-weight:normal;color:#";
		u+= (activo) ? "f80000" : "ffffff";
		u+= ";'>"+thisCanal.Title+"<\/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 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;
}

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:#f80000; font-size:10px;' onclick='selectedItem[1]="+PlIndex+";mostrarItems("+PyIndex+", "+ChIndex+", "+PlIndex+");showCanalDelay("+PyIndex+", "+ChIndex+")'>"+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);

	//u += "onclick='selected(true, "+PyIndex+","+thisCanal.Id+", "+thisPlaylist.Id+", "+thisItem.Id+")' ";
	
	if (num/2 == Math.round(num/2)){
		var u = "<table id='"+assetId+"' class='admTV_ItemsPar' onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})' 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' style='padding:10px;padding-left:5px;color:#444444;'>";
		u+= "<span style='color:#003063;'>"+anio+"<\/span><span style='color:#999999;'>"+ " | "+ _imgTime+" "+ hora+"<\/span><br \/>";
		u+= "<span class='itemTitle'>"+thisItem.Title+"<\/span><br \/>";
		if (typeof(thisItem.Description)!="undefined") {
			if (thisItem.Description!="null") u += thisItem.Description;
		}
		//u+= " <span style='color:#999999;'>| <br\/>"+_imgTime+" "+formatTime(thisItem.Assets[0].Length/1000)+"<\/span><\/td>";
		u+= "<\/tr><\/table>";
	}else{
		var u = "<table id='"+assetId+"' class='admTV_Items' onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"})' 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' style='padding:10px;padding-left:5px;color:#444444;'>";
		u+= "<span style='color:#003063;'>"+anio +"<\/span><span style='color:#999999;'>"+ " | "+ _imgTime+" "+ hora+"<\/span><br \/>";
		u+= "<span class='itemTitle'>"+thisItem.Title+"<\/span><br \/>";
		if (typeof(thisItem.Description)!="undefined") {
			if (thisItem.Description!="null") u += thisItem.Description;
		}
		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;
	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="60" height="40" 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+= '<div class="fechaDestacado">'+anio+ " | "+ _imgTime+" "+ hora+'</div>';
				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='325' 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 += "60' height='70";
	u += "'><img src='";
	u += admtv.getThumbnailURL(admtv.Players[PyIndex].Content.Id, thisItem, "s")+"' width='60' height='40'";
	u += " style='border: #c7d2db solid 3px;' \/><\/td>";

	u += "<td style='padding:5px; font-size: 11px;' valign='top'>";
	u += "<span style='color: #999999;'>"+anio+ " | "+ _imgTime+" "+ hora+"<\/span><br \/>";

	u += "<span class='itemTitle' style='"
	
	u += "'>"+thisItem.Title+"<\/span>";
	if (typeof(thisItem.Description)!="undefined") {
		if (thisItem.Description != "null") u += "<br \/><span style='color: #666666;'>"+thisItem.Description+"<\/span>";
	}
	u += "<\/td>";

	u +="<tr><td colspan='2' style='background:url("+_imgHost;
	u += (num == 4) ? "trans.gif" : "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;
}


//************ 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;
}

// Upload de Videos

function sube(PyIndex){
	//alert('entra a Sube');
	document.getElementById("subeVideo").onclick = function () {
		showUpload(PyIndex);
	}
}

function oSw(x) {
	return (navigator.appName.indexOf("Microsoft") != -1) ? window[x] : document[x];
}

function confirmaUpload(ob){
	if (typeof(ob.Url)=="undefined") {
		//alert(customTexts.utop+' Muestra canales');
		//showCanal(0, "1");
	} else{
		oSw("TVPlayListSWF").confirmaUpload(ob.Url);
	}
	
}

function showUpload(PyIndex) {
	
	
											    
	var player = admtv.Players[PyIndex];
	
	player.onUploadDataFetched = function (ob) {
		confirmaUpload(ob);
	}
	
	player.pause(true);

	if (player.userIsLogged()) {
		UploadArea(PyIndex);
	}else{
		
		UploadArea(PyIndex);
		initAction = "";
	}

	//ci.scrollTop = "0px";
}

function UploadArea(PyIndex){	
	var player = admtv.Players[PyIndex];
	var fVars = "";
		fVars+= "PyIndex="+PyIndex;
		fVars+= "&PyId="+player.Id;
		fVars+= "&API="+player;
		fVars+= "&screen=Upload";
		fVars+= "&action="+initAction;
	var ci = document.getElementById("uploadVideo");
	try
	{
		ci.style.visibility = 'visible';
		ci.innerHTML = genSWFcode("TVPlayListSWF", 324, 338, _imgHost+"upload.swf", fVars, "transparent");	
	}
	catch (e)
	{
		alert("error: " + e);
	}
	
}

// Canalera
function showCanalDelay(PyIndex, n) {
	setTimeout("showContent("+PyIndex+")", 600);
	document.getElementById("uploadVideo").style.visibility = 'hidden';
}

function showContent(PyIndex){
	mostrarCanales(PyIndex);
}