// JavaScript Document
// Custom
//var _imgHost = "img\/";
var _imgHost = "http:\/\/fsi.dmmotion.com\/10196\/img\/";
//var _imgHost = "http:\/\/desarrollo-cstv.nspmotion.com\/10196\/img\/";
var _imgTime = "<img src='"+_imgHost+"img_hour.gif' width='9' height='9' \/>";
var selectedItem = [0, 0, 0];
var _channelsPerPage = 8;
var _channelsPage = 0;
var seleccionado;
var contPaginado;
var py = "";
var indicatorText = "Ingrese su nombre...";

var permalinkInit = false;

function ge(x) {
	return document.getElementById(x);
}

function onPlayerInit(PyIndex){
	var player = admtv.Players[PyIndex];
	var playlist = ge("TVMostContent");
	playlist.innerHTML = paginadoDinamico();
	py = PyIndex;
	// Defino eventos
	var fVars= "PyIndex="+PyIndex;
	ge("admtv_term").disabled = false;
	ge("TVEstasViendo").innerHTML = genSWFcode("SWFVotar",555, 80, _imgHost+"votar.swf", fVars, "transparent");
	ge("admtv_term").onkeypress = function (e) {
		if (typeof(e)=="undefined") e = event;
			buscarVideoInput(e, player.Id);
		}
	ge("admtv_term_bt").disabled = false;
	ge("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]);
		
		this.getComments({ChannelId:ItemId[0],PlaylistId:ItemId[1],Id:ItemId[2]});
		UpdateVotes(PyIndex);

		if(!permalinkInit){
			setChPlActive(ItemId, PyIndex);setADMactive(ItemId);
		}else setADMactive(ItemId);
		this.setRotateAllowed(true);
		UserLogin(this.userIsLogged(),PyIndex);

		
	}
	player.onMostViewedFetched = function (mv) {
		mostrarTop(mv, this.Id, ge("TVMostContent"));
	}
	player.onMostSentFetched = function (ms) {
		mostrarTop(ms, this.Id, ge("TVMostContent"));
	}
	player.onSearchFinished = function (evObj) {
		showSearch(this.Index, evObj);
	}
	player.onTopRatedFetched  = function (evObj) {
		mostrarTop(evObj, this.Id, ge("TVMostContent"))
	}
	player.onCommentsFetched = function(co) {
		comentarios(co);
	}
		
	// Canalera
	mostrarCanales(PyIndex);
	UserLogin(player.userIsLogged(),PyIndex);
	most(PyIndex);
	//geid("admtvLogoUniversal").onclick = function(){location.href = "http://www.eluniversal.com";};

}

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' ><img src='"+_imgHost;
		u+= (activo) ? "sola01" : "trans";
		u+= ".gif' width='4' height='31' \/><\/td>";
		u+= "<td"+oncl+"style='cursor:pointer;";
		if (activo) u+= "background:url(\""+_imgHost+"sola02.gif\") repeat-x;";
		u+= " height:31px;padding-left:5px;padding-right:5px;	font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:";
		u+= (activo) ? "bold;":"normal";
		u+="font-size:12px; ;color:#";
		u+= (activo) ? "CE9901" : "5A4A42";
		u+= ";'>"+thisCanal.Title+"<\/td>";
		u+= "<td style='cursor:pointer;'"+oncl+"width='4' valign=''><img src='"+_imgHost;
		u+= (activo) ? "sola03" : "trans";
		u+= ".gif' width='4' height='31' \/><\/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><td width='35'></td>";
	for (var i = iu; i<it; i++) {
		u+= generarCanal(PyIndex, i);
	}
		u+= "<\/tr><\/table>";
		u+= "<div id='BTNmasL'>";
		u+= "<input class='noPadding' 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+= " /></div><div id='BTNmasR'>";
		u+= "<input class='noPadding' 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>";
	ge("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 activo = (PlIndex == selectedItem[1]);
	
	
	var u = "";
		u+= "<td style='cursor:pointer; font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:";
		u+= (activo)?"bold;":"normal";
		u+="font-size:12px; font-weight:";
		u+= (activo)? "bold;":"normal;";
		u+= (activo)?"color:#CE9901;":"color:#5A4A42;";
		u+="padding-left:14px;padding-top:0px;' onclick='selectedItem[1]="+PlIndex+";mostrarPlaylists("+PyIndex+", "+ChIndex+");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='0' height='30'><tr>";
	for (var i=0; i<it; i++) {
		u+= generarPlaylist(PyIndex, ChIndex, i);
	}
	u+= "<\/tr><\/table>";
	ge("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 puntaje = thisPlayer.getItemRange({ChannelId:ChId,PlaylistId:PlId,Id:ItId},5);

	var anio = formatDate(fecha).toString().substring(0,8);
	var hora = formatDate(fecha).toString().substring(11);
	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 style='width:60px;padding:5px;'><img src='"+admtv.getThumbnailURL(thisPlayer.Content.Id, thisItem, "s")+"' width='80' height='60' style='border-width:1px;border-style:solid;border-color:#555555' \/><\/td>";
		u+= "<td  style='padding-left:5px;padding-right:0px;padding-top:10px;padding-bottom:10px;color:#444444; width:170px;'>";
		u+= "<span style='color:#4B4B4B;font-size:9px;font-family:Arial;'>"+anio+"<\/span><span style='color:#4B4B4B;font-size:9px;font-family:Arial;'>"+ " | "+ _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+= "<td style='width:80px;'><div id='itemsTPlay'><img src='"+_imgHost+"tri.gif' \/>&nbsp;PLAY<div>"+generateVoting(puntaje,270)+"</td><\/tr><\/table>";
	
	return u;
	
}
function setChPlActive(arrId, PyIndex){
	var player = admtv.Players[PyIndex];
	var ChIndex = player.getChannelIndexById(arrId[0]);
	var PlIndex = player.getPlaylistIndexById(arrId[0], arrId[1]);
	selectedItem[0] = ChIndex;
	selectedItem[1] = PlIndex;
	mostrarCanales(PyIndex);
	generarCanal(PyIndex, ChIndex);
	mostrarPlaylists(PyIndex, ChIndex);
	
	permalinkInit = true;

	
}
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);
	ge("contPlayList").innerHTML=u;
		admtvSL_setSelected(true);
}

function setupSearch(PyIndex) {
	var oEl = ge("contPlayList");
	oEl.innerHTML = "";
	return oEl;
}
function showSearch(PyIndex, obj) {
	var oEl = ge("contPlayList");
	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=ge("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 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_MOST_"+thisItem.Id;
	
	var u = "<table id='"+assetId+"' class='admTV_Items' width='480' height='55' style=' background-color:#FFFFFF;' ";
	
	u += "onclick='admtv.Players["+PyIndex+"].playVideo({ChannelId:"+thisCanal.Id+", PlaylistId:"+thisPlaylist.Id+", Id:"+thisItem.Id+"}); ' ";
	u += "border='0' cellspacing='0' cellpadding='0'><tr id='mostPastilla'>";

	u += "<td align='center'  width='40' style='border-bottom:#ECECEC 1px solid'><div id='numberTop'>"+(Number(num)+1)+".</div><\/td>";
	u += "<td  style='border-bottom:#ECECEC 1px solid' width='";
	u += "60' height='55";
	u += "'><img src='";
	u += admtv.getThumbnailURL(admtv.Players[PyIndex].Content.Id, thisItem, "s")+"' width='70' height='45'";
	u += "\/><\/td>";

	u += "<td  valign='top' width='345' style='border-bottom:#ECECEC 1px solid'>";
	u += "<div class='titlePastillaTop' "
	u += ">"+thisItem.Title+"<\/div>";
	if (typeof(thisItem.Description)!="undefined") {
		if (thisItem.Description != "null") u += "<div id='itemDescTop'>"+thisItem.Description+"<\/div>";
	}
	u += "<\/td>";
	u +="<\/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;
	//selMostPastilla(pastiId)
	//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) {
	
	//formato: 07/03/08   dia/mes/año
	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 formatDate2(d) {
		//formato: 07.03.2008   dia.mes.año
	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()+" | "+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 = ge("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 = ge(admtvSL_ch);
	var PL_elem = ge(admtvSL_pl);
	var AS_elem = ge(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 generateVoting(rating,posx) {
	var canEstrellas = 5;
	var _separacion = 12;
	var estrellaLlena = "estrellaLlena.gif";
	var estrellaMedia = "estrellaMedia.gif";
	var estrellaVacia = "estrellaVacia.gif";
	
	
	var roundRating = Math.round(rating);
	var resto = rating - roundRating;
	var u="<div  style='position:absolute; left:"+posx+"px;'>";
	for(var i=0; i<canEstrellas ; i++)
	{
		
		if(i < roundRating){
		u+= "<div style='position:absolute; left:"+_separacion*i+"px;'><img src='"+_imgHost + estrellaLlena+"' /></div>";
		}else
		{
			if(resto >= 0.3)
			{
				u+= "<div style='position:absolute; left:"+_separacion*i+"px;'><img src='"+_imgHost+estrellaMedia+"' /></div>";
				resto=0;
			}else
			{
				u+= "<div style='position:absolute; left:"+_separacion*i+"px;'><img src='"+_imgHost+estrellaVacia+"' /></div>";
			}
		}
	}
u+= "</div>";
	
return u;
	
}

var ft ;
function UserLogin(userIsLogged,PyIndex) {
	ft = true;
	var holderLogin = geid("TVLogin"); 
	var holderComments  = geid("holderPost");
	
	if(!userIsLogged) {	
		holderLogin.style.display="";
		holderComments.style.display="none";
		
	} else {
		
		holderLogin.style.display="none"
		holderComments.style.display="";
		holderComments.innerHTML = generateForm(PyIndex);
		var inputUser = geid("fHSMTV_username");
		
		inputUser.blur();
		inputUser.onfocus = function() {
			cantRotate();
			if (ft) {
				this.value =""; 
				ft = false;
				this.className = "indicadorOff";
			}
		} 
		mostrarForm(py);
	}
}


var lastSolSel =""; ge("mostSol_0");
function most(PyIndex) {
	var player = admtv.Players[PyIndex];
	var holder = ge("TVMost");
	var title  = ge("TVMostTitulo");
	var solapasHolder   = ge("TVMostSolapas");
	var content = ge("TVMostContent");
	var solTitle = ["visto","enviado","votado"];
	title.innerHTML  = "Lo m&aacute;s";
	
	var u = "<table border='0' cellspacing='0' cellpadding='0'><tr>";
	for (var i = 0; i<3; i++) {
		u+= generarSolapasMost(i,solTitle[i],PyIndex);
	}
		u+= "<\/tr><\/table>";
	solapasHolder.innerHTML  = u;
	lastSolSel = ge("mostSol_0");
	
	player.getMostViewed(10, 4);
}
var lastMostPasti = "";
var pastiId = "";
function generarSolapasMost(id,t,PyIndex) {
	
	var activo = ("mostSol_"+id == "mostSol_0");
	var oncl = " onclick='mostSelected("+id+","+PyIndex+"); ' ";
	var u="";
		u+= "<td "+oncl+" id='mostSol_"+id+"' style='padding-top:1px;padding-left:30px;padding-right:30px;cursor:pointer;background:#";
		if(id == 0) u+= "FFFFFF;"
		u+= " height:16px;	font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;color:#";
		if(id == 0) u+= "C79E00;"
		u +="font-weight:";
		if(id ==0) u += "bold";
		u+= ";'>"+t+"<\/td>";
		u+="<\/td>";
		
	return u;
}

function mostSelected(id,PyIndex) {
		
		var sol;
		var player = admtv.Players[PyIndex];
		try {
			lastSolSel.style.background = "#DBD7CC";
			lastSolSel.style.color = "#343333";
			lastSolSel.style.fontWeight = "normal"
		}
		catch(e){}
		sol = ge("mostSol_"+id);
		sol.style.background = "#FFFFFF";
		sol.style.color = "#C79E00";
		sol.style.fontWeight = "bold"
		lastSolSel = sol;
		
		
		switch(id) {
			case 0:
				player.getMostViewed(10, 4);
				break;
			case 1:
				player.getMostSent(10, 4);
				break;
			case 2:
				player.getTopRated(10, 4);
				break;
		}
}


function selMostPastilla(id) {		
	
	pastiId = id;
	
	var sol;
	try{ lastMostPasti.style.background = "#FFFFFF";}catch(e){}
	try{ 	
	sol = ge(id);
	sol.style.background = "#F3F1E9";
	lastMostPasti = sol;
	}catch(e){}
	
	
}


function clearMost(id) {
	try{ lastMostPasti.style.background = '#FFFFFF';}catch(e){}
}
	

function UpdateVotes(PyIndex){	
	var player = admtv.Players[PyIndex];
	var mov2 = "SWFVotar";
	var swfObj2 = (navigator.appName.indexOf("Microsoft") != -1) ? window[mov2] : document[mov2];
	if(typeof(swfObj2)== "undefined"){	setTimeout("UpdateVotes()", 100)}else{
	var ItemId = player.getCurrentItemData();
	var Item = player.getItemById(ItemId.ChannelId, ItemId.PlaylistId, ItemId.Id);	
	var voteDto = Item.RangeVoting;
	swfObj2.update(player.Content.Id, ItemId.ChannelId, ItemId.PlaylistId, Item ,voteDto);
	}
 }
 
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 paginadoDinamico() { //Creacion de divs dinamicamente.
	contPaginado =  makeDiv("contPaginado", ge("TVMostPaginado"));

	makeDiv("mostrandoTitulo", contPaginado);
	makeDiv("mostrandoContenido", contPaginado);
	makeDiv("paginadoTitulo", contPaginado);
	makeDiv("paginado", contPaginado);
	makeDiv("contentPag", contPaginado);
	return contPaginado;
}

function makeDiv(name, holder) {
	var refSpan;
	refSpan = document.createElement("div");
	
	refSpan.id = name;
	refSpan.name = name;
	
	holder.appendChild(refSpan);
	
	return refSpan;
}

function showPaginado(param) {
	if (param) {
		contPaginado.style.visibility = "visible";
		contPaginado.style.height = "30px";
		playlist.style.height = "465px";
	} else {
		contPaginado.style.visibility = "hidden";
		contPaginado.style.height = "0px";
		playlist.style.height = "495px";
	}
}

var p = new Paginado();
p.setHolder("TVComentariosHolder");
function comentarios(co)
{
	
	
	var holder = ge("TVComentarios");
	var title  = ge("TVComentariosTitulo");
	var content   = ge("TVComentariosHolder");
	var paginado = ge("TVComentariosPag");
	var cont = "";
	var cant = co.length;
	title.innerHTML  = "Comentarios <span style='font-size:12px;font-weight:normal;'>("+cant+")</span>";

	
	p.setContenido(co);

}




function Paginado ()
{ 
    //Inicializo las propiedades y métodos 
    
		var _itemsPorPagina;
		var _totalItems;
		var _cantidadPaginas;
		var _primerElemento;
		var _cantidadIndicesMostrar;
		var _filas;
		var _columnas;
		var desp;
		var _contenido;
		var _holder;
		var _hContent;
		var _hIndex;
	
		var _leftBtn;
		var _holderIndex; 
		var _rightBtn;
		var _indexVisible;
		var  pagIndex ;
	 

	
		var c =0;
		var f =0;

		this.setContenido = function(cont) {
		
			_contenido = cont;
			_filas = 5;
			_columnas = 1;
			_itemsPorPagina = (_filas * _columnas);
			_totalItems = _contenido.length;
			_cantidadPaginas = Math.ceil(_totalItems/_itemsPorPagina);
			_indexVisible = 5;
			pagIndex = 0;
			

			 _holderIndex.innerHTML = "";
			if(_cantidadPaginas>1) {
				generarIndices(0,_indexVisible);
				
				if(_cantidadPaginas<=_indexVisible) showBtn(false);
				else showBtn(true);
				
			}else {
				showBtn(false);
				_hIndex.style.visibility = "hidden";
			}
			initHandler();
			mostrarPagina(0);	
		
		}
	
	
		function generarIndices(inicio, fin) {
		
			var u ="";
			_hIndex.style.visibility = "visible";
			//alert("_cantidadPaginas: "  + _cantidadPaginas + "fin: " + fin)
			
			fin = (_cantidadPaginas<=fin)?_cantidadPaginas : fin;
			//_hIndex.style.position = "relative";
			_holderIndex.innerHTML = "";
			var arrInd = new Array();
			var sep = 10;
			var offSet = 0;
			var lastIdSel;
			for (var i = inicio; i<fin; i++) {
				
				arrInd[i] = makeElement("ind"+i,"div",_holderIndex)	;
				arrInd[i].innerHTML = i+1;
				arrInd[i].className = "pIndex";
				//arrInd[i].style.paddingLeft = (i*sep)+"px";
				arrInd[i].style.position = "relative";
				arrInd[i].style.float = "left";
				arrInd[i].style.display = "inline";
				arrInd[i].style.left = (offSet*sep)+"px";
				
				arrInd[i].onclick = function() {
					
					var thisId = parseInt(parseInt(this.id.substr(this.id.length -1,this.id.length)));
					try{	arrInd[lastIdSel].className = "pIndex";	}catch(e){}
				
					this.className = "pIndex_S";
					mostrarPagina(thisId);
					lastIdSel = thisId;
					
				};
				offSet++;

			}

		}
		function mostrarPagina(paginaID) {

			var hasta = (paginaID == 0)?_itemsPorPagina-1 : (_itemsPorPagina * (paginaID +1)-1);
			var itInicial = (_itemsPorPagina*paginaID !=0)? (_itemsPorPagina*paginaID) : 0 ;
			
			f = 0;
			c =0 ;
			var sepx = 10;
			var sepy = 10;
			
			var arrItems = new Array();
			_hContent.style.position = "relative";
			_hContent.innerHTML ="";
			
			
			
				while (itInicial <=hasta) 
				{


					try{
						if (typeof(_contenido[itInicial].Date) != "undefined"   ) 
						{
							arrItems[itInicial] = makeElement("item"+itInicial,"div",_hContent)	;
							arrItems[itInicial].className = "pItemTemplate";
							arrItems[itInicial].innerHTML = itemTemplate(_contenido[itInicial],itInicial);
						}
					}catch(e){}
					
					c++;
					itInicial++;
					if (c == _columnas) {c = 0;	f++;}
				}
		}
	
		this.setHolder = function(h) {
			
			_holder = ge(h);
			initStruct(_holder);
		}
		function itemTemplate(co,id){
			
					var cont= "";
					var anio = formatDate2(new Date(co.Date)).toString().substring(0,10);
					var hora = formatDate2(new Date(co.Date)).toString().substring(12);
					cont+= "<table id='comment_"+id+"' border='0' cellspacing='0' cellpadding='0'><tr >";
					cont+= "<td width='100' style='padding:9px 5px 9px 5px;'><div id='divComment'><div id ='commentPor'>Por</div><div id='commentUsername'>"+co.User+"</div><div id='commentTime'>"+anio+"</div><div id='commentTime'>"+hora+"</div></div><\/td>";
					cont+= "<td width='265' valign='top'><div id='commentText'>";
					if (typeof(co.Comment)!="undefined") {
						if (co.Comment!="null") cont += co.Comment;
					}
					
					cont+= "</div></td><\/tr><\/table>";
					return cont;
		}
		function initStruct(h) { 
			//Creacion de divs dinamicamente.
			
			_hContent= makeElement("PaginadoContent","div", h);
			_hIndex = makeElement("PaginadoIndexHolder","div", h);
			_hIndex.style.visibility = "hidden";
			
			_leftBtn = makeElement("PaginadoLeftBtn","div", _hIndex);
			_holderIndex = makeElement("PaginadoIndex","div", _hIndex);
			_rightBtn= makeElement("PaginadoRightBtn","div", _hIndex);
			showBtn(false)
		}
	
		function initHandler() {		
			_leftBtn.onclick = atras;
			_rightBtn.onclick = adelante;
		}
		function showBtn(param)	{
			if(param)
			{
				_leftBtn.style.visibility = "visible";
				_rightBtn.style.visibility = "visible";
			}else
			{
				
				_leftBtn.style.visibility = "hidden";
				_rightBtn.style.visibility = "hidden";
			}
		}
	
		function atras() {					
			if(pagIndex>=0)
			{
				if(pagIndex !=0){pagIndex--}
				var hasta = (pagIndex == 0)?_indexVisible : (_indexVisible * (pagIndex +1))+1;
				var itInicial = (_indexVisible*pagIndex !=0)? (_indexVisible*pagIndex) : 0 ;
						
				generarIndices(itInicial, hasta);
			}
		}
		
		function adelante()	{
			if(pagIndex <  Math.ceil(_cantidadPaginas/_indexVisible)-1)	{
				pagIndex++;
				var hasta = (pagIndex == 0)?_indexVisible : (_indexVisible * (pagIndex +1));
				hasta = (hasta<=_cantidadPaginas)?hasta:_cantidadPaginas;
				var itInicial = (_indexVisible*pagIndex !=0)? (_indexVisible*pagIndex) : 0 ;
				generarIndices(itInicial, hasta); 
			}
		}
		
		function makeElement(name,type, holder) {
			var refSpan;
			refSpan = document.createElement(type);
			refSpan.id = name;
			refSpan.name = name;
			holder.appendChild(refSpan);
			return refSpan;
		}
	
} 


function generateForm(PyIndex) {

	var u =  "";
	
	u += "<div class='tituloParticipa'>Participa</div>";
    u += "<div class='envianos'><span id='loginName'></span>env&iacute;anos tu comentario.&nbsp;<span id='logoutDisplay' ></span></div>";
	u += "<br><input type='text' id='fHSMTV_username' class='indicadorOn' value='"+indicatorText+"'>"
	u += "<br \/><textarea class='CommentForms' name='fHSMTV_comment'  id='fHSMTV_comment' cols='50'></textarea>";
	u += "<div id='textKey'>Ingresa el siguiente c&oacute;digo:</div>";
	u += "<div id='HSMvalidationKey'></div>";
	u += "<div id='keyValidationInput'><input name='fHSMTV_keyword' type='text' id='fHSMTV_keyword' maxlength='4' style='width: 70px; height: 18px;' /></div>";
	u += "<div id='CommentSent'></div><div name='fHSMTV_sendButton'  id='fHSMTV_sendButton'  ></div>";
	
	
	return  u ;	
}

var maxLength = 500; //cantidad maxima de caracteres por comentario.
function mostrarForm(PyIndex) {
	 	

		geid("HSMvalidationKey").innerHTML = '<img src="'+admtv.getValidationImg()+'" width="50" height="22" style="border: #666666 solid 1px;" \/>';
		var player = admtv.Players[PyIndex];
		var rDTO = player.getCurrentItemData();
		
		
		geid("fHSMTV_comment").onfocus  = cantRotate;
		geid("fHSMTV_keyword").onfocus  = cantRotate;
		
		
		geid("fHSMTV_sendButton").onclick = function () 
		{
		
			var u = geid("fHSMTV_username").value;
			var kw = geid("fHSMTV_keyword").value;
			var co = geid("fHSMTV_comment").value.substr(0,maxLength);
			
			if (u!=""  && u!=indicatorText) 
			{
				if (kw!="" && kw.length == 4) 
				{
					if (co!="" ) 
					{
						player.postComment({ChannelId:rDTO.ChannelId, PlaylistId:rDTO.PlaylistId, Id:rDTO.Id}, kw, co,u);
						
						
						geid("fHSMTV_username").value = "";
						geid("fHSMTV_keyword").value ="";
						geid("fHSMTV_comment").value ="";
						geid("CommentSent").style.display = "";
						geid("CommentSent").innerHTML = "Su mensaje ha sido enviado...";
						player.setRotateAllowed(true);
						
						window.setTimeout("UserLogin("+true+", "+PyIndex+")",4000);
						

					} else alert("No has ingresado un COMENTARIO.");
				} else alert("Revisa el CODIGO DE SEGURIDAD, e intenta nuevamente.");
			} else alert("No has ingresado tu NOMBRE.");
		}
	
 }
 
 function geid(x){return ge(x);}
 

 function cantRotate(){
	 var player = admtv.Players[py];
	 player.setRotateAllowed(false);
 }

/*
	Esto se deberia disparar desde : ttp://www.eluniversal.com/js/public/registry/util.js
	e indica cuando el usuario se logueo.
	cuando llega a  esta funcion logueo al usuario con el id que me trae el evento AdmTVLoginCallback
*/

function AdmTVLoginCallback(id)
 {
	
	var player = admtv.Players[py];	
	player.onUserLogin = function (resultCode) {
		// Muestro form de comments
		if(resultCode == "1") UserLogin(true,py);
	}
	player.userLogin(id);
};

