/* ----------------------------------------------------------------
	APP BROWSER
	JS -> CINTA :: GDRipoll
-------------------------------------------------------------------*/

//alert("BROWSER:CINTA:JS v0.41 Loading!");
function appCinta(pNombre,pDiv, itemWidth,itemTotal,stepItem,chunkRead,callback,pagsPaginador, sModulo, sRegion, sFrame)
{
		this.debug=false;
	// -[Constructor]---------------------
		this.nombre=pNombre;
		this.nombreDiv=pDiv;
		this.dataPack=null;
		this.dataPl=null;
		this.dataParameters=null;
		this.direccion="adelante";		
		if(typeof sModulo!="undefined"){
			this.sModulo=sModulo;
		}else{
			this.sModulo="browser";
		}
		if(typeof sRegion!="undefined"){
			this.sRegion=sRegion;
		}else{
			this.sRegion="browser";
		}
		
		if(typeof sFrame!="undefined"){
			this.sFrame=sFrame;			
		}else{
			this.sFrame="principal";			
		}
		
		
	//--[armado del movimiento]-------------------------------------
		this.laCinta			=	document.getElementById(this.nombreDiv);
		this.item				=	1; //item actual
		this.items				=	parseInt((itemTotal)?itemTotal:0); //cantidad de paginas
		this.itemWidth			=	parseInt((itemWidth)?itemWidth:100); //ancho a contar por item.
		this.itemsTotal			=	0; //esto se llena cuando se carga la cinta
		this.step				=	parseInt((stepItem)?stepItem:1);
		this.chunkReadOriginal	=	parseInt((chunkRead)?chunkRead:12);
		this.chunkRead			=	parseInt((chunkRead)?chunkRead:12);
		this.pasos				=	100; //cantidad de intermedios de los saltos.

	//--[paginador]-------------------------------------
		this.paginador = new Paginador(this.nombreDiv,this,pagsPaginador);
		
	//--[objetos]-------------------------------------
		this.domDesde=document.getElementById(this.nombreDiv+'_desde');
		this.domHasta=document.getElementById(this.nombreDiv+'_hasta');
		this.domTotal=document.getElementById(this.nombreDiv+'_total');

	//-[metodos]------------------------------------------------------------
	this.jumpToPixel = function(pixel) 
	{ 
		var p1 = parseInt(this.laCinta.scrollLeft);
		var p2 = parseInt(pixel);
		var paso = 0.5/this.pasos; //delta PI
		for(t=1.5; t<=2; t+=paso) //subo por cuarto cuadrante
			this.laCinta.scrollLeft=p1+(p2-p1)*Math.cos(t*Math.PI);
	}
	this.jumpToItem = function(itemNro) 
	{ 
		this.jumpToPixel( ((itemNro-1)*this.itemWidth) - (this.itemsDesde*this.itemWidth)); 
		this.item=itemNro; 
		this.manageButtons(); 
	}
	this.jumpTo = function(pageNro) 
	{ 
		this.jumpToPixel((this.step*this.itemWidth)*(pageNro-1)); 
		this.item=this.step*(pageNro-1)+1;  
		this.manageButtons();
	}
	this.jumpNext = function() 
	{ 
		if(!this.eof()) this.jumpToItem(this.item+this.step); 
		else 
		{
			//alert("Esto es un eof()");
			if(callback!=null) {eval("oCallback = " + callback); oCallback("eof");} 
			if(this.itemsTotal > this.items) this.loadNext();
		}
		this.private_posicion();
	}
	this.jumpPrevius = function() 
	{ 
		if(!this.bof()) this.jumpToItem(this.item-this.step); 
		else
		{
			if(callback!=null) {eval("oCallback = " + callback); oCallback("bof");} 
			if(this.itemsDesde > 0) this.loadPrevius();
		}
		this.private_posicion();
	}
	this.eof = function() 
	{
		return( (this.item+this.step)>(this.items) );
	}
	this.bof = function() 
	{ 
		return(this.item-this.step<=this.itemsDesde); 
	}
	this.manageButtons = function()
	{
		/*
		ant=document.getElementById(this.nombreBotones+"_anterior");
		sig=document.getElementById(this.nombreBotones+"_siguiente");
		if(this.bof())	ant.className="anterior_disabled";
		else						ant.className="anterior";
		if(this.eof())	sig.className="siguiente_disabled";
		else						sig.className="siguiente";
		*/
	}

	//--[metodos de posicion]-------------------------------------

	this.private_posicion	=	function()
	{
		//this.domDesde.innerHTML=this.item; //primero de la pagina
		if (!this.itemsTotal)
			this.domDesde.innerHTML=0;
		else
			this.domDesde.innerHTML=this.item;
		this.domHasta.innerHTML=((this.item+this.step-1)<=this.itemsTotal)? this.item+this.step-1 : this.itemsTotal;	//--[ultimo de la pagina]-------------------------------------
		this.domTotal.innerHTML=this.itemsTotal;
		this.paginador.refresh(parseInt(this.item/this.step+1));
	};
	this.setTotales = function(general)
		{
			this.itemsTotal=general;
			this.items=this.dataParameters.desde_row+this.dataParameters.cant_row;
			this.itemsDesde=this.dataParameters.desde_row;
			this.itemsHasta=this.itemsDesde+this.dataParameters.cant_row-1;
			if(this.itemsTotal<this.itemsHasta) this.itemsHasta=this.itemsTotal;
			if(this.itemsTotal<this.items) this.items=this.itemsTotal;
			this.private_posicion();
			//--[sobre la disertacion de si voy para atras, o para adelante]-----------
			switch(this.direccion)
			{
				case	'adelante'	: //--[va para adelante]---------------------------------
					//this.paginador.refresh(); //--[paginator]-----
					break;
				case	'atras'			: //--[vuelta atras]-------------------------------------
					itemNro=this.posicionAnterior-this.step;
					this.laCinta.scrollLeft=((itemNro-1)*this.itemWidth) - (this.itemsDesde*this.itemWidth);
					this.item=itemNro; 
					this.manageButtons(); 
					this.private_posicion();
					this.direccion="adelante";
					//this.paginador.refresh(parseInt(this.item/this.step+1));
					break;
				case	'especifica'	: //--[va a una pagina]-------------------------------------
					itemNro=this.posicionDestino;
					this.laCinta.scrollLeft=((itemNro-1)*this.itemWidth) - (this.itemsDesde*this.itemWidth);
					this.item=itemNro; 
					this.manageButtons(); 
					this.private_posicion();
					this.direccion="adelante";
					break;
			}
		}

	//-----------------------------------------------------------------
	this.setCinta = function(pack,pl,parameters,xsl)
		{
			//--[ajuste para paginado]-------------------------------------
			parameters.desde_row=0; 
			parameters.cant_row=0+this.chunkRead;
			this.item=0; this.laCinta.scrollLeft=0;			
			this.chunkRead = this.chunkReadOriginal;
			return(this.private_setCinta("cinta",pack,pl,parameters,xsl));
		}
	this.setCintaDescargados = function(pack,pl,parameters,xsl)
		{
			//--[ajuste para paginado]-------------------------------------
			parameters.desde_row=0; 
			parameters.cant_row=0+this.chunkRead;
			this.item=0; this.laCinta.scrollLeft=0;			
			this.chunkRead = this.chunkReadOriginal;
			return(this.private_setCinta("cinta_descargados",pack,pl,parameters,xsl));
		}
	this.setCintaDump = function(parameters)
		{
			//--[incorporacion de parametros]-------------------------------------
			this.dataPack="";
			this.dataPl="";
			this.dataParameters=parameters;
			this.item=0; this.laCinta.scrollLeft=0;
			for(var i in this.dataParameters) parametros+="&parametros["+i+"]="+this.dataParameters[i];
			//FWK.loadApiInto(this.nombreDiv,"browser|browser|browser|principal","cinta_dump","&cmd=setCinta"+parametros);
			this.chunkRead = 999999;			
			//FWK.loadApiCallback("browserUbicador","browser|browser|browser|principal","cinta_dump","&cmd=setCinta"+parametros);
			FWK.loadApiCallback("browserUbicador","browser|browser|browser|"+this.sFrame,"cinta_dump","&cmd=setCinta"+parametros);
			
			this.item=1;
			this.paginador.limpiar();
			this.paginador.refresh(1);
			return;
		}
	this.setCintaBuscador = function(pack,pl,parameters,xsl)
		{
			//--[ajuste para paginado]-------------------------------------
			this.chunkRead = this.chunkReadOriginal;
			parameters.desde_row=0; 
			parameters.cant_row=0+this.chunkRead;
			this.item=0; this.laCinta.scrollLeft=0;			
			return(this.private_setCinta("cinta_buscador",pack,pl,parameters,xsl));
		}
	this.private_setCinta = function(tipo,pack,pl,parameters,xsl)
		{
			//--[validacion de parametros]-------------------------------------
			if(arguments.length>3)	this.xsl=xsl;
			else										this.xsl=null;
			//--[incorporacion de parametros]-------------------------------------
			this.dataPack=pack;
			this.dataPl=pl;
			this.dataParameters=parameters;
			this.dataTipo=tipo;
			//-- para debug ------------
			if(this.debug)
			{
				a="["; for(var i in this.dataParameters) a+=i+" ="+this.dataParameters[i]+"][";
				//alert("Llamado al seteo de cinta "+this.nombre+"\npack = "+pack+"\npl = "+pl+"\nparametros = "+a);
			}
			//--parametros de llamada a la API-----
			parametros=((this.xsl!=null)?"&xsl="+this.xsl:"")+"&pk="+this.dataPack+"&pl="+this.dataPl;
			for(var i in this.dataParameters)
			{
				parametros+="&parametros["+i+"]="+this.dataParameters[i];
			}
			//--llamada a la API-------------------
			//FWK.loadApiCallback("browserUbicador","browser|browser|browser|principal",tipo,"&cmd=setCinta"+parametros);
			FWK.loadApiCallback("browserUbicador","browser|browser|browser|"+this.sFrame,tipo,"&cmd=setCinta"+parametros);
			this.item=(this.dataParameters.desde_row)+1;
			this.paginador.limpiar();
		};

	//-----------------------------------------------------------------
	this.loadNext = function()
		{
			this.laCinta.innerHTML=""; //mientras tanto, no se ve nada
			//--[preparo lo nuevo]-------------------------------------
			this.dataParameters.desde_row+=this.dataParameters.cant_row;
			//--[llamada]-------------------------------------
			this.private_setCinta(this.dataTipo,this.dataPack,this.dataPl,this.dataParameters);
		};
	this.loadPrevius = function()
		{
			this.laCinta.innerHTML=""; //mientras tanto, no se ve nada
			this.posicionAnterior=this.item;
			//--[preparo lo nuevo]-------------------------------------
			this.dataParameters.desde_row-=this.dataParameters.cant_row;
			//--[llamada]-------------------------------------
			this.private_setCinta(this.dataTipo,this.dataPack,this.dataPl,this.dataParameters);
			//--[posicionamiento]-------------------------------------
			this.direccion="atras";
		};

	//-----------------------------------------------------------------
	//-----------------------------------------------------------------
	//-----------------------------------------------------------------
	//-----------------------------------------------------------------
	//-----------------------------------------------------------------
	//-----------------------------------------------------------------

	this.btnVerTodos = function()
		{
			//alert( this.dataParameters.p_id_nodo+" | "+this.dataParameters.n_id_tipo_cont+" | "+this.dataParameters.p_tipo_consulta );
			playlistVerTodos(	
				((this.dataParameters.p_id_nodo!=null) ? this.dataParameters.p_id_nodo : ""),
				((this.dataParameters.n_id_tipo_cont!=null) ? this.dataParameters.n_id_tipo_cont : "" ),
				((this.dataParameters.p_tipo_consulta!=null) ? this.dataParameters.p_tipo_consulta : "" ) 
				);
		}
	this.btnAgregarTodos = function()
		{
		//alert( this.dataParameters.p_id_nodo+" | "+this.dataParameters.n_id_tipo_cont+" | "+this.dataParameters.p_tipo_consulta );
		playlistAgregarTodos(	
			((this.dataParameters.p_id_nodo!=null) ? this.dataParameters.p_id_nodo : ""),
			((this.dataParameters.n_id_tipo_cont!=null) ? this.dataParameters.n_id_tipo_cont : "" ),
			((this.dataParameters.p_tipo_consulta!=null) ? this.dataParameters.p_tipo_consulta : "" ) 
			);
		}
	this.btnIraPrincipio = function()
		{
			this.btnGoPage(1);
		}
	this.btnIraFinal = function()
		{
			this.btnGoPage(this.paginador.maxPage);
		}

	this.btnGoPage = function(pag)
		{
			goDesde=((pag-1)*this.step)+1;
			if(goDesde>=(this.itemsDesde+1) && goDesde<=this.itemsHasta) 
			{
				this.jumpToItem(goDesde);
				this.private_posicion();
			}
			else
			{
				this.laCinta.innerHTML=""; //mientras tanto, no se ve nada
				//--[averiguo el primer item del chunk]-----------
				puntero=0;
				while(puntero<goDesde) puntero+=this.chunkRead; //--[si esto anda, soy Harry Potter]
				puntero-=this.chunkRead; //--[Harry Tupper?]---------
				//--[me posiciono del otro lado]------------------
				this.direccion="especifica"; 
				this.posicionDestino=goDesde;
				//--[llamada]-------------------------------------
				this.dataParameters.desde_row=puntero;
				this.private_setCinta(this.dataTipo,this.dataPack,this.dataPl,this.dataParameters);
			}
			this.paginador.refresh(pag);
		}

	//-----------------------------------------------------------------
	// Inicializacion
	//-----------------------------------------------------------------
	this.private_posicion();
}

// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================

function Paginador(nombre,pHub,pagsPaginador)
{
	//--[propiedades]-------------------------------------
	this.pagsPaginador=pagsPaginador;
	this.nombre=nombre;
	this.hub=pHub;
	this.paginas=document.getElementById(nombre+"_paginas");
	this.cmdInicio=document.getElementById(nombre+"_inicio");
	this.cmdFin=document.getElementById(nombre+"_fin");
	this.maxPage=0;
	//--[refresh]-------------------------------------
	this.limpiar = function()
	{
		this.paginas.innerHTML="&nbsp;";
	}
	this.refresh = function(pag)
	{
		this.curPage=(pag!="undefined")?pag:1;
		this.maxPage=Math.ceil(this.hub.itemsTotal/this.hub.step); //maxPage;
		//--[inicializacion]-------------------------------------
		this.paginas.innerHTML="&nbsp;";
		desde=this.curPage-this.pagsPaginador; if(desde<1) desde=1;
		hasta=this.curPage+this.pagsPaginador; if(hasta>this.maxPage) hasta=this.maxPage;
		for(a=desde;a<=hasta;a++) 
			if(a==this.curPage) this.paginas.innerHTML+="<span id='"+nombre+"_p_"+a+"' class='actual'>"+a+"</span>&nbsp;|&nbsp;"	
			else								this.paginas.innerHTML+="<span id='"+nombre+"_p_"+a+"' class='numpag' onclick='"+this.hub.nombre+".btnGoPage("+a+");'>"+a+"</span>&nbsp;|&nbsp;"	;
		this.paginas.innerHTML=this.paginas.innerHTML.substr(0,this.paginas.innerHTML.length-7); //le saco los ultimos tres
	}
	//--[metodos]-------------------------------------
	this.refreshCurPage = function()
	{
		for(a=1;a<=this.maxPage;a++) 
			if(a==this.curPage) document.getElementById(nombre+"_p_"+a).className="actual";
			else  document.getElementById(nombre+"_p_"+a).className="link";
	}
	this.setPage = function(nro)
	{
		this.curPage=nro;
		this.refreshCurPage();
	}
	this.nextPage = function() { if(this.curPage<this.maxPage) this.setPage(this.curPage+1); }
	this.prevPage = function() { if(this.curPage>1) this.setPage(this.curPage-1); }

	//--[initilize]-------------------------------------
	this.refresh();

}










// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================

function browserUbicador(respuesta)
{
	var resultado; //agregado para que al cargar por ajax la linea de abajo no deerror
	lacinta=document.getElementById("cinta");
	losalts=document.getElementById("alts");
	resultado=respuesta.responseText;
	aRes=resultado.split("%%||%%");
	losalts.innerHTML=aRes[1];
	lacinta.innerHTML=aRes[0];
}

function appAlts()
{
	this.offetX=5;
	this.offetY=5;
	this.on	=	function()
	{
		e=event;
		ob=(e.srcElement!=null)? e.srcElement : e.target;
		theAlt=document.getElementById('m_'+ob.id.substr(3));
		if (!document.getElementById("tipo_contenido_"+ob.id.substr(3)))
			tipo_contenido = "OTHER";
		else
			tipo_contenido = document.getElementById("tipo_contenido_"+ob.id.substr(3)).innerHTML;
		
		if(theAlt)
		{  
			theAlt.style.display='block';
			switch(tipo_contenido)
			{
				case "1"://audio
					theAlt.style.left	= (e.x!=null)? e.x-50 : e.clientX+this.offetX;
					theAlt.style.top	= (e.y!=null)? e.y-100 : e.clientY+this.offetY;
					theAlt.style.height = 200;
				break;
				case "2"://video
					theAlt.style.left	= (e.x!=null)? e.x+this.offetX : e.clientX+this.offetX;
					theAlt.style.top	= (e.y!=null)? e.y+this.offetY : e.clientY+this.offetY;
				break;
				default://otros
					theAlt.style.left	= (e.x!=null)? e.x+this.offetX : e.clientX+this.offetX;
					theAlt.style.top	= (e.y!=null)? e.y+this.offetY : e.clientY+this.offetY;
					break;
			}
		}
	}
	this.off	=	function()
	{
		e=event;
		ob=(e.srcElement!=null)? e.srcElement : e.target;
		theAlt=document.getElementById('m_'+ob.id.substr(3));
		if(theAlt) theAlt.style.display='none';
	}
	this.move = function(n)
	{
		e=event;
		ob=(e.srcElement!=null)? e.srcElement : e.target;
		theAlt=document.getElementById('m_'+ob.id.substr(3));
		theAlt.style.left	= (e.x!=null)? e.x+this.offetX : parseInt(e.clientX+this.offetX)+"px";
		theAlt.style.top	= (e.y!=null)? e.y+this.offetY : parseInt(e.clientY+this.offetY)+"px";
	}
}

//--[zona para pruebas]-------------------------------------

function prueba()
{
	aParametros={	
								p_id_instalacion 	: 3,
								p_id_nodo 			: null,
								p_id_genero 		: null,
								p_id_fuente 		: null,
								n_id_tipo_cont 		: 2,
								p_id_formato 		: null,
								p_id_grupo_wg 		: null,
								p_id_reprod 		: null,
								p_adulto 			: null,
								p_estado 			: null,
								p_countrycode 		: countrycode,
								p_idioma 			: 'ESP',
								p_idioma_padre 		: 'ESP',
								p_idioma_default 	: 'ESP',
								p_camp_grp_orden 	: null,
								p_tipo_orden 		: null,
								p_tipo_consulta 	: 'ULT',
								desde_row 			: 0,
								cant_row 			: 4
							};
	//browserCinta.debug=true;
	browserCinta.setCinta('PACK_LISTADOS','PR_LISTADOR_GENERICO_VH',aParametros);
}
// javascript:alert(top.principal.prueba());

/*
javascript:alert(pruebaParam={id:8003, nombre:'pepito', saldo:3500 });
javascript:alert(top.principal.browserCinta.setCinta('pack','pl',pruebaParam));
---------------------------
Microsoft Internet Explorer
---------------------------
url:[/scripts/app/framework.php?APP=browser|browser|browser|principal&API=cinta&cmd=setCinta&pk=PACK_LISTADOS&pl=PR_LISTADOR_GENERICO_VH&parametros[p_id_instalacion]=8103&parametros[p_id_nodo]=null&parametros[p_id_genero]=null&parametros[p_id_fuente]=null&parametros[p_id_tipo_cont]=null&parametros[p_id_formato]=null&parametros[p_id_grupo_wg]=null&parametros[p_id_reprod]=null&parametros[p_adulto]=null&parametros[p_estado]=null&parametros[p_idioma]=ESP&parametros[p_idioma_padre]=ESP&parametros[p_idioma_default]=ESP&parametros[p_camp_grp_orden]=null&parametros[p_tipo_orden]=null&parametros[p_tipo_consulta]=ULT&parametros[desde_row]=0&parametros[cant_row]=4]
dest:[cinta]
app:[browser|browser|browser|principal]
param:[&cmd=setCinta&pk=PACK_LISTADOS&pl=PR_LISTADOR_GENERICO_VH&parametros[p_id_instalacion]=8103&parametros[p_id_nodo]=null&parametros[p_id_genero]=null&parametros[p_id_fuente]=null&parametros[p_id_tipo_cont]=null&parametros[p_id_formato]=null&parametros[p_id_grupo_wg]=null&parametros[p_id_reprod]=null&parametros[p_adulto]=null&parametros[p_estado]=null&parametros[p_idioma]=ESP&parametros[p_idioma_padre]=ESP&parametros[p_idioma_default]=ESP&parametros[p_camp_grp_orden]=null&parametros[p_tipo_orden]=null&parametros[p_tipo_consulta]=ULT&parametros[desde_row]=0&parametros[cant_row]=4]
---------------------------
Aceptar   
---------------------------
*/

//alert("BROWSER:CINTA:JS v0.46 Loaded!");




/*IMPORTS*/
/*FUNCIONES Y CLASES*/
window.appCinta			=	appCinta;
window.Paginador		=	Paginador;
window.browserUbicador	=	browserUbicador;
window.appAlts			=	appAlts;
window.prueba			=	prueba;