//=============================================================================
// FILE: class.cinta.js
//=============================================================================


if(typeof(defCinta)=="undefined")
{
	var defCinta=true;

//=============================================================================
// CLASS.CINTA.JS
//=============================================================================
	/**
	 * csCinta
	 *
	 * @param string varName Nombre de la variable a la que se asigna
	 * @param string objName Nombre del objeto div contenedor de la cinta
	 */
	function csCinta(varName,objName,jsonConfig,objCache,objPaginado)
	{
	//===========================================================================
	// Configuracion
	//===========================================================================
		this.hookOnDom = function(conf) //DOM creation and hook
			{
				//--[config]-----------------------------------------------------------
					this.CONFIG=conf;
					if(typeof(this.CONFIG.totalItems)=="undefined") this.CONFIG.totalItems=0;
					if(typeof(this.CONFIG.pageItems)=="undefined") this.CONFIG.pageItems=3;
					if(typeof(this.CONFIG.rows)=="undefined") this.CONFIG.rows=1; //cinta Multirow
					if(typeof(this.CONFIG.cachePages)=="undefined") this.CONFIG.cachePages=this.CONFIG.totalItems/this.CONFIG.pageItems;
				//--[dom]--------------------------------------------------------------
					this.DOM={		"cinta" : document.getElementById(objName),
												"antes" : document.getElementById(objName+"_antes"),
												"despues" : document.getElementById(objName+"_despues"),
												"marco" : document.getElementById(objName+"_marco"),
												"tr" : null, //pendientes de calculo
												"td" : null //pendientes de calculo
												};
					if(this.DOM.marco)
					{
						this.DOM.tr=this.DOM.marco.getElementsByTagName("TR")[0];
						if(this.DOM.tr) this.DOM.td=this.DOM.tr.getElementsByTagName("TD")[0];
					}
					else
					{
						this.autodestruccion=true;
						return;
					}
					//for(var aux in this.DOM) if(this.DOM[aux]==null) this.raise("Imposible encontrar "+aux);
				//--[cinta]------------------------------------------------------------
					this.CINTA={	'currItem' : 1,
												'currPage' : 1,
												'currDesde' : 1,
												'currHasta' : this.CONFIG.pageItems,
												'totalMinItem' : 1,
												'totalMinPage' : 1,
												'totalMaxItem' : this.CONFIG.totalItems,
												'totalMaxPage' : Math.ceil(this.CONFIG.totalItems/this.CONFIG.pageItems),
												'cacheMinItem' : 1,
												'cacheMinPage' : 1,
												'cacheMaxItem' : this.CONFIG.cachePages*this.CONFIG.pageItems,
												'cacheMaxPage' : this.CONFIG.cachePages
												};
				//--[calculos]---------------------------------------------------------
					if(this.flow=="h")
					{
						//--[size]-----------------------------------------------------------
							if(typeof(this.DOM.cinta.getBoundingClientRect)!='undefined')
							{
								var sizeMarco=this.DOM.marco.getBoundingClientRect();
								var sizeItem=this.DOM.td.getBoundingClientRect();
								this.SIZE={ "marcoWidth" : sizeMarco.right-sizeMarco.left,
														"itemWidth" : sizeItem.right-sizeItem.left
														};
							}
							else if(typeof(document.getBoxObjectFor)!='undefined')
							{
								this.SIZE={ "marcoWidth" : document.getBoxObjectFor(this.DOM.marco).width,
														"itemWidth" : document.getBoxObjectFor(this.DOM.td).width
														};
							}
							else
							{
								this.SIZE={ "marcoWidth" : this.DOM.marco.offsetWidth,
														"itemWidth" : this.DOM.marco.offsetWidth
														};
							}
					}
					else
					{
						//--[size]-----------------------------------------------------------
						if(typeof(this.DOM.cinta.getBoundingClientRect)!='undefined')
						{
							var sizeMarco=this.DOM.marco.getBoundingClientRect();
							var sizeItem=this.DOM.td.getBoundingClientRect();
							if(this.debug) this.say(this.DOM.td.outerHTML);
							this.SIZE={ "marcoWidth" : sizeMarco.bottom-sizeMarco.top,
													"itemWidth" : sizeItem.bottom-sizeItem.top
													};
						}
						else if(typeof(document.getBoxObjectFor)!='undefined')
						{
							this.SIZE={ "marcoWidth" : document.getBoxObjectFor(this.DOM.marco).height,
													"itemWidth" : document.getBoxObjectFor(this.DOM.td).height
													};
						}
						else
						{
							this.SIZE={ "marcoWidth" : this.DOM.marco.offsetHeight,
													"itemWidth" : this.DOM.td.offsetHeight
													};
						}
					}
				//--[hooks]------------------------------------------------------------
					this.agregarEventoObjeto(this.DOM.antes,"click",this.nombre,"antes")
					this.agregarEventoObjeto(this.DOM.despues,"click",this.nombre,"despues")
				//--[delegates]--------------------------------------------------------
					this.DELEGATES={ cache: objCache, paginado: objPaginado };
					for(var aux in this.DELEGATES)
					{
						if(typeof(this.DELEGATES[aux])=="undefined") this.DELEGATES[aux]=null;
						if(typeof(this.DELEGATES[aux])!="object") this.DELEGATES[aux]=null;
					}
					if(this.DELEGATES.cache!=null)
					{
						this.DELEGATES.cache.CINTA=this.CINTA;
						this.DELEGATES.cache.CONFIG=this.CONFIG;
						this.DELEGATES.cache.PARENT=this;
					}
					if(this.DELEGATES.paginado!=null)
					{
						this.DELEGATES.paginado.CINTA=this.CINTA;
						this.DELEGATES.paginado.CONFIG=this.CONFIG;
						this.DELEGATES.paginado.PARENT=this;
					}
			};
	//===========================================================================
	// Operacion
	//===========================================================================
		this.alertClick = function(source)
			{
				alert("Hiciste click en algo ["+source.id+"]");
			};
		this.antes = function(source)
			{
				if(this.autodestruccion) return;
				this.jumpToPage(this.CINTA.currPage-1);
				if(this.debug) this.showData();
			};
		this.despues = function(source)
			{
				if(this.autodestruccion) return;
				this.jumpToPage(this.CINTA.currPage+1);
				if(this.debug) this.showData();
			};
		this.reload = function(nro,cinta,config)
			{
				if(this.autodestruccion) return;
				this.CINTA=cinta;
				this.CONFIG=config;
				if(nro!=this.CINTA.currItem) this.jumpToItem(nro);
			}
	//===========================================================================
	// Manejo Interno
	//===========================================================================
		this.jumpToPixel = function(pixel)
			{
				var p1 = parseInt(this.DOM.marco.scrollLeft);
				var p2 = parseInt(pixel);
				var paso = 0.5/this.pasos; //delta PI
				for(t=1.5; t<=2; t+=paso) this.DOM.marco.scrollLeft=p1+(p2-p1)*Math.cos(t*Math.PI);
			}
		this.jumpToItem = function(itemNro,directo)
			{
				if(typeof(directo)=="undefined") directo=false;
				if(itemNro!=this.CINTA.currItem)
				{
					if(itemNro>=this.CINTA.totalMinItem && itemNro<=this.CINTA.totalMaxItem)
					{
						if(itemNro>=this.CINTA.cacheMinItem && itemNro<=this.CINTA.cacheMaxItem)
						{
							itemActual=itemNro-(this.CINTA.cacheMinItem-1);
							if(directo) this.DOM.marco.scrollLeft=(itemActual-1)*this.SIZE.itemWidth/this.CONFIG.rows;
							else
							{
								if(this.realtime)
								{
									if(this.DOM.marco.getAttribute("jumperT")=="" || this.DOM.marco.getAttribute("jumperT")==null)
									{
										//__cinta__jumpToPixel(this.DOM.marco.id,(itemActual-1)*this.SIZE.itemWidth,this.flow,true);
										if(this.debug) this.say("("+itemActual+"-1)*"+this.SIZE.itemWidth+"/"+this.CONFIG.rows+" >> ");
										__cinta__jumpToPixel(this.DOM.marco.id,(itemActual-1)*this.SIZE.itemWidth/this.CONFIG.rows,this.flow,true);
									}
									else
									{
										this.say("NO NESTING (!) >> STILL ON ["+this.DOM.marco.getAttribute("jumperT")+"]");
										return(false); //no hay nested
									}
								}
								else this.jumpToPixel((itemActual-1)*this.SIZE.itemWidth/this.CONFIG.rows);
							}
							//--[seteos]--
							this.CINTA.currItem=itemNro;
							if(this.CONFIG.pageItems==1)
								this.CINTA.currPage=this.CINTA.currItem;
							else
								this.CINTA.currPage=Math.floor(this.CINTA.currItem/this.CONFIG.pageItems)+1;
							this.CINTA.currDesde=(this.CONFIG.pageItems*(this.CINTA.currPage-1))+1;
							this.CINTA.currHasta=this.CONFIG.pageItems*(this.CINTA.currPage);
							if(this.DELEGATES.paginado!=null)
								if(typeof(this.DELEGATES.paginado.refresh)!="undefined")
									this.DELEGATES.paginado.refresh(itemNro);
						}
						else //manejo por fuera de cache
						{
							// TODO: llamar al ICache
							if(this.DELEGATES.cache!=null)
								if(typeof(this.DELEGATES.cache.load)!="undefined")
									this.DELEGATES.cache.load(itemNro);
						}
					}
					else //manejo por fuera de totales
					{
						if(itemNro>this.CINTA.totalMaxItem) return(this.jumpToItem(this.CINTA.totalMaxItem-this.CONFIG.pageItems+1));
						if(itemNro<this.CINTA.totalMinItem) return(this.jumpToItem(this.CINTA.totalMinItem))
					}

				}
				else return; //es el mismo, no hago nada.
			}
		this.jumpToPage = function(pageNro)
			{
				this.jumpToItem(((pageNro-1)*this.CONFIG.pageItems)+1);
			}
	//===========================================================================
	// Utilidades
	//===========================================================================
		this.agregarEventoObjeto=function(objeto,evento,nombreObjeto,nombreMetodo)
			{
				eval("var funcion=function() { "+nombreObjeto+"."+nombreMetodo+"(this); };");
				if(typeof(objeto.attachEvent)!="undefined")
				{
					if(evento=='click') evento='onclick';
					if(evento=='mouseover') evento='onmouseover';
					if(evento=='mouseout') evento='onmouseout';
					objeto.attachEvent(evento,funcion);
				}
				else
				{
					if(evento=='onclick') evento='click';
					if(evento=='onmouseover') evento='mouseover';
					if(evento=='onmouseout') evento='mouseout';
					objeto.addEventListener(evento,funcion,false);
				}
			};
		this.raise = function(msg)
			{
				alert(msg);
			}
		this.say = function(msg)
			{
				if(this.debug) alert("["+this.nombre+"] >> "+msg);
			}
		this.showData = function()
			{
				msg="Contenido de la cinta ["+this.nombre+"]\n------------------\n";
				for(var i in this.CINTA) msg+=i+"::"+this.CINTA[i]+"\n";
				msg+="\n--------------------------";
				alert(msg);
			}
		this.showCinta = function()
			{
				alert(	"\n -==-==- ["+this.nombre+"] -==-==- "+
								"\nTotal de Items = "+this.CONFIG.totalItems+
								"\nItems por Row = "+this.CONFIG.rows+
								"\nItems por Pagina = "+this.CONFIG.pageItems+
								"\nPaginas por cache = "+this.CONFIG.cachePages+
								"\n -==-==- SiZe -==-==- "+
								"\nAncho del marco = "+this.SIZE.marcoWidth+
								"\nAncho del Item = "+this.SIZE.itemWidth+
								"\nAncho de "+this.CONFIG.pageItems+" items = "+(this.CONFIG.pageItems*this.SIZE.itemWidth)+
								"\n -==-==- ***** -==-==- "+
								"\nInicializacion terminada"
								);
			}
	//===========================================================================
	// Inicializacion
	//===========================================================================
		//--[Propiedades]---------------------------------------------------------
		this.className="csCinta";
		this.nombre=varName;
		this.div=objName;
		this.debug=false;
		this.pasos=500;
		this.realtime=true;
		this.flow="h";
		this.autodestruccion=false;

		//--[Funcionamiento]-------------------------------------------------------
		this.hookOnDom(jsonConfig,objCache,objPaginado);
		if(this.debug) this.showCinta();
		if(this.debug) this.showData();
	}

	function __cinta__jumpToPixel(ob,pixel,flow,entrada) //asyncronic jumper
		{
			var miob=document.getElementById(ob);
			var p1 = parseInt((flow=="h")?miob.scrollLeft:miob.scrollTop); var p2 = parseInt(pixel); var paso = 0.5/(200); //delta PI
			t=parseFloat(miob.getAttribute("jumperT")); if(t==null || isNaN(t)) t=1.5; if(entrada && t==3) t=1.5;
			if(t<=2)
			{
				t+=paso;
				anterior=((flow=="h")?miob.scrollLeft:miob.scrollTop);
				if(flow=="h") { miob.scrollLeft=p1+(p2-p1)*Math.cos(t*Math.PI); miob.setAttribute("jumperT",t); }
				else					{ miob.scrollTop=p1+(p2-p1)*Math.cos(t*Math.PI); miob.setAttribute("jumperT",t); }
				if(anterior!=((flow=="h")?miob.scrollLeft:miob.scrollTop))
					setTimeout("__cinta__jumpToPixel('"+ob+"',"+pixel+",'"+flow+"',false);",10);
				else
				{
					if(flow=="h") miob.scrollLeft=pixel; else miob.scrollTop=pixel;
					t=null; miob.setAttribute("jumperT",t);
				}
			}
			else
			{
				t=null; miob.setAttribute("jumperT",t);
			}
		}
	//alert("class.cinta.js");
}