// JavaScript Document
// TOS Admotion

var TO20;
if (typeof(admtv_TO) != "undefined") TO20 = new admtvTO(admtv_TO);

function admtvTO(o) {
	this._plDiv = document.getElementById("TVlayout");
	this._toDiv = document.createElement("div");
	this._objData = o;
	
	this.show = function () {
		this._plDiv.style.visibility = "hidden";
		this._toDiv.innerHTML = this.generateSwf(this._objData.file, 730, 450, this._objData.file, "clickTAG="+unescape(this._objData.click));
		this._toDiv.style.position = "absolute";
		this._plDiv.parentNode.insertBefore(this._toDiv, this._plDiv);
		setTimeout("TO20.out()", this._objData.duration*1000);
	}
	this.out = function () {
		this._toDiv.innerHTML = "";
		this._plDiv.style.visibility = "";
		try {
			admtv.getPlayerById(20).Continue();
		} catch(e) {
			admtv_goStandBy_20 = false;
		}
	}
	this.generateSwf = function(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;
	}
	this.show();
}

// Custom
var _imgHost = "http:\/\/fsi.dmmotion.com\/10087\/img\/2\/";
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;

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 5 Hoy
	player.getMostViewed(5, 2);
	// Mas enviados TOP 5 Hoy
	player.getMostSent(5, 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+"sol";
		if (activo) u+= "a";
		u+= "01.gif' width='4' heigth='26' \/><\/td>";
		u+= "<td"+oncl+"style='heigth:26;cursor:pointer; background: repeat-x url("+_imgHost+"sol";
		if (activo) u+= "a";
		u+= "02.gif); padding-left:3px;padding-right:3px; font-family: Trebuchet MS; font-size:11px; font-weight:normal;color:#";
		u+= (activo) ? "123557" : "ffffff";
		u+= ";'>"+thisCanal.Title+"<\/td>";
		u+= "<td style='cursor:pointer;heigth:24;'"+oncl+"width='4' valign='bottom'><img src='"+_imgHost+"sol";
		if (activo) u+= "a";
		u+= "03.gif' width='4' \/><\/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='1' height='9' \/><\/td>";
		u+= "<td style='cursor:pointer; color:#123557; font-family: Trebuchet MS; font-size:11px;' 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);

	//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:#cc0000;'>"+anio+"<\/span><span style='color:#999999;'>"+ " | "+ _imgTime+" "+ hora+"<\/span><br \/>";
		u+= "<span class='title'>"+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:#cc0000;'>"+anio +"<\/span><span style='color:#999999;'>"+ " | "+ _imgTime+" "+ hora+"<\/span><br \/>";
		u+= "<span class='title'>"+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+= '<div class="contenedorDest">';
		t+= '<div class="fileteDestacado">';
		t+= '</div>';
		t+= '<div class="ThumbVideo">';
		t+= '<img width="60" height="40" src="'+player.getItemThumbnail(clickArray, "m")+'"/>';
		t+= '</div>';
		t+= '<div 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+= '</div>';
		t+= '</div>';
		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='374' 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) ? "124' height='94" : "60' height='70";
	u += "'><img src='";
	u += (num == 0) ? admtv.getThumbnailURL(admtv.Players[PyIndex].Content.Id, thisItem, "m")+"' width='120' height='90'" : 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-family: Trebuchet MS; font-size: 11px;' valign='top'>";
	u += "<span style='color: #999999;'>"+anio+ " | "+ _imgTime+" "+ hora+"<\/span><br \/>";

	u += "<span style='color: #00659a; font-weight:bold;"
	u += (num == 0) ? " font-size: 14px;" : "";
	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;
}
