var gPromos = [];

function Promo(id, promo_tipo,cargar)
{
	this.DEFAULT_ANCHO_IMAGEN = 106;
	this.DEFAULT_ANCHO_TOTAL = 130;
	this.DEFAULT_ALTO_IMAGEN = 53;
	this.DEFAULT_MARGEN_PROMO = 21;

	this.id = id;

	this.promos = null;
	//this.promos = eval(xslTranform);
	this._iA = null;
	this._iS = null;
	this._v = null;
	this._c = null;
	this._cP = null;

	this._aV = null;
	this.anchoImagenPromo = this.DEFAULT_ANCHO_IMAGEN;
	this.anchoTotalPromo = this.DEFAULT_ANCHO_TOTAL;
	this.altoImagenPromo = this.DEFAULT_ALTO_IMAGEN;
	this.margenPromo = this.DEFAULT_MARGEN_PROMO;
	this.imgDefault = "/imgs/varios/cargando109x53.gif";
	this.dominio = "http://www.zonacinemania.com";
	this._t = null;	

	this.anchoBotonSig = 0;
	this.sentidoRotacion = -1;
	this.desplazado = null;
	this.vel = 1;
	this.step = Math.round(this.anchoTotalPromo / 2);
	this.pos = 0;
	this.promoActual = 0;
	this.cantidadPromos = 0;
	this.promosVisibles = 0;
	this.botonAnterior = "/imgs/GaleriaFotos/btnGaleria1.png";
	this.botonSiguiente = "/imgs/GaleriaFotos/btnGaleria2.png";
	
	this.xml = null;
	this.xsl = null;
	this.xmlTranform = null;
	this.promoEnHtml = null;
	
	this.mouseover = false;
	
	// Generan estadisticas al ver la imagen de la promo
	this.promoConEstadistica = false;

	this.cargar = function(t)
	{	
	
		this.cargarSrv();
	
		if (t == null) t = "default";
				
		this.urlJsPromos = this._aUJsP(t);
				
		this.js = this._js('var _'+this.id+' = new Promo("'+this.id+'","'+t+'", null);');
		this.js += this._js('',this.urlJsPromos);
		

        if (this.xmlTranform == null)
        {
            this.js += this._js('if(_'+this.id+'.promos == null);_'+this.id+'.promos = gPromos["'+t+'"];',null,false);
        }
        else
        {
            this.js += this._js('if(_'+this.id+'.promos == null);_'+this.id+'.promos = '+ this.xmlTranform+'', null, false);
        };
			
		this.html = '<div id="'+this.id+'" class="ln_promoComp floatFix">';
		
		if (this.mouseover == false)
		{
		    this.html +='<div onclick="javascript:_'+this.id+'.rotacion(1);" id="'+this.id+'_ant" class="ln_promoAnt">';
		}
		else
		{
		    this.html += '<div onclick="javascript:_'+this.id+'.rotacion(1);" onmouseover="javascript:_'+this.id+'.rotacion(1);" id="'+this.id+'_ant" class="ln_promoAnt">';
		}		
		
		this.html += '<img src="'+ this.dominio + this.botonAnterior + '" alt="Anterior" title="Anterior"/>' + '</div>' + '<div id="'+this.id+'_ventana" class="ln_promoVent floatFix"'+ ((this._aV != null)?' style="width:'+this._aV+'px;background-color:transparent;"':' style="background-color:transparent;"')+'>\n';
		
		this.cantidadPromos = 0;
						
		if (gPromos!=null)
		{
			this.promos = gPromos[t];

			this.html += '<ul id="'+this.id+'_list" class="ln_promoList">'
			var tmp = "";
			for (i in this.promos)
			{
				tmp += this.armarPromo(i,this.promos[i]);
				this.cantidadPromos++;
			}
			this.html += tmp;
			this.html += '</ul>'
		}	
		
		this.js += '_'+this.id+'.cantidadPromos = '+this.cantidadPromos+';';
		
		if(this.anchoImagenPromo != this.DEFAULT_ANCHO_IMAGEN)
			this.js += '_'+this.id+'.anchoImagenPromo = '+this.anchoImagenPromo+';';
		
		if(this.anchoTotalPromo != this.DEFAULT_ANCHO_TOTAL)
			this.js += '_'+this.id+'.anchoTotalPromo = '+this.anchoTotalPromo+';';
		
		if(this.altoImagenPromo != this.DEFAULT_ALTO_IMAGEN)
			this.js += '_'+this.id+'.altoImagenPromo = '+this.altoImagenPromo+';';
		
		if(this.margenPromo != this.DEFAULT_MARGEN_PROMO)
			this.js += '_'+this.id+'.margenPromo = '+this.margenPromo+';';

		if(this.anchoBotonSig != 0)
			this.js += '_'+this.id+'.anchoBotonSig = '+this.anchoBotonSig+';';

		this.js += '_'+this.id+'.promoConEstadistica = '+this.promoConEstadistica+';';

		if(this["onCargoPromo"])
			this.js += '_'+this.id+'.onCargoPromo = '+this.onCargoPromo+';';
		
		this.js += '_'+this.id+'.iniciar();</script>';
		this.html += '</div>';		
		
		if (this.mouseover == false)
		{
		    this.html += '<div onclick="javascript:_'+this.id+'.rotacion(-1);" id="'+this.id+'_sig" class="ln_promoSig">';
		}
		else
		{
		    this.html +='<div onclick="javascript:_'+this.id+'.rotacion(-1);" onmouseover="javascript:_'+this.id+'.rotacion(-1);" id="'+this.id+'_sig" class="ln_promoSig">';
		}		
		
		this.html +='<img src="'+ this.dominio + this.botonSiguiente +' " alt="Siguiente" title="Siguiente"/></div>' +'</div>';        
		return this.js;
	};	
	
	this._armarIdPromo = function(id)
	{
		return 'promo_'+(this.id)+'_'+(id);
	};
	
	this._aUJsP = function(n)
    {
        return this.dominio + "/scripts/promos."+n+".js";		
    };
   
   this._js = function(t,s,c)
    {	    
        var x = '<script type="text/javascript" language="javascript"';
        if (s!=null && this.xml == null && this.xsl == null && promo_version != null) x+=' src="'+s+'?v=' + promo_version + '"';
        x+='>';
        x+=t;
        if (c==null || c!=false)
        x+='</script>';
        return x;
    }; 
		
	this.armarPromo = function(id,promo)
	{
		var txt="";
		txt += '<li id="'+this._armarIdPromo(id)+'" style="width:'+ (this.anchoTotalPromo - this.margenPromo) * (promo.a?promo.a:1)+'px;visibility:hidden;" class="floatFix">';
		if (promo.h)
		{
			txt += '<a href="' + promo.h+ '"';
			
			//Funcion onclick por Sebastian
			if (promo.o != null && promo.o.length != 0)
			{
			    txt += 'onclick="' + promo.o + '"';
			}				
			
			if (promo.t != null)
			{
				switch(promo.t)
				{
					case '':
						break;
					case 'n':
						txt += ' target="_blank"';
						break;
					default:
						txt += ' target="'+promo.t+'"';
				}
			}
			txt += '>';
		}
		
		txt += '<img id="'+this._armarIdPromo(id)+'_img"';
				
		if ((promo.H != null) && (promo.W != null))
		{
			var altoP = promo.H;
			var anchoP = promo.W;
			var ajustarPor="alto";

			altoP = (this.anchoImagenPromo * promo.H ) / promo.W;
			anchoP = (this.altoImagenPromo * promo.W ) / promo.H;

			//response.write(" ALTO: " + promo.H + " ANCHO: " + promo.W + "<br/>" + " altoP: " + altoP + " anchoP: " + anchoP + "<br/>" + "-------------" + "<br/>");

			if (altoP < this.altoImagenPromo)
			{
				ajustarPor="ancho";
			}
			else if(anchoP > this.anchoImagenPromo)
			{
				if (anchoP > altoP) // && (altoP <= this.altoImagenPromo)
				{
					ajustarPor="ancho";
				}
				else
				{
					ajustarPor="alto";
				}
			}
			else
			{
				ajustarPor="alto";
			}

			if (ajustarPor=="alto")
			{
				if (this.altoImagenPromo>0)
					txt += ' height="' + this.altoImagenPromo + '"';
					txt += ' style="margin-top: 0px; margin-bottom:0px;"';
			}
			else
			{
				
				if (altoP < this.altoImagenPromo && promo.H == this.altoImagenPromo)
				{
					txt += ' width="' + this.anchoImagenPromo + '"';
					txt += ' style="margin-top:'+(this.altoImagenPromo-altoP)/2 +'px; ' + 'margin-bottom:'+(this.altoImagenPromo-altoP)/2 + 'px;"';
				}
				
				if (this.anchoImagenPromo>0) 
					txt += ' width="' + this.anchoImagenPromo + '"';

				if (this.altoImagenPromo-promo.H > 0)
					txt += ' style="margin-top:'+(this.altoImagenPromo-promo.H)/2+'px;' + 'margin-bottom:' + (this.altoImagenPromo-promo.H)/2 + 'px;"';				
			}
		}		
		else
		{
			if (this.anchoImagenPromo>0)
				txt += ' width="' + this.anchoImagenPromo + '"';
			if (this.altoImagenPromo>0)
				txt += ' height="' + this.altoImagenPromo + '"';
		}
		
		txt += ' src="" class="cargandoImagen" />';// '+ this.imgDefault +'"/>';

		if (promo.c != null && promo.c.length != 0)  
        {
            txt += '<span class="nroFoto" style="width:70px;">'+promo.c+'</span>'; 
        } 

        if (promo.e != null && promo.e.length != 0)  
        {
            txt += '<span class="tituloFoto">'+promo.e+'</span>'; 
        } 
		
		if (promo.h)
			txt += '</a>';
		
		txt += '</li>';

		return txt;
	};
	
	this.getElements = function()
	{
		this._iA = document.getElementById(this.id+"_ant");
		this._iS = document.getElementById(this.id+"_sig");
		this._v = document.getElementById(this.id+"_ventana");
		this._c = document.getElementById(this.id);
		this._cP = document.getElementById(this.id+"_list");
	};

	this.ajustarVentana = function()
	{
		var o1 = this._iA.offsetLeft;
		var o2 = this._iS.offsetLeft + this.anchoBotonSig;

		this.promosVisibles = Math.floor((o2-o1) / (this.anchoTotalPromo));
		if (this.promosVisibles<1)
		{
		    // vuelvo a poner x Seba
		    this.promosVisibles = 1;
		}

		var r = Math.floor(((o2-o1) % (this.anchoTotalPromo))/2);

		this._aV = (this.promosVisibles*this.anchoTotalPromo) - Math.floor(this.margenPromo/2);
		//Por Sebastian, bug imagen 0
		//if (this._aV == 0) this._aV = this.anchoTotalPromo
		//
		this._v.style.width = this._aV+"px";

		for (var i=0;i<=this.promosVisibles;i++)
			this.cargarPromo(i);
	};
		
	this.cargarPromo = function(n,contar)
	{    
	    if (this.promos == null && this.promos == undefined) this.promos = gPromos;
	         	
		if (this.promos[n] != null)
		{
		    if (this.promos[n].l == 0)
		    {
			    var p = document.getElementById(this._armarIdPromo(n));
			    if (p!=null)
			    {
				    p.style.visibility = "visible";               
				    var img = document.getElementById(this._armarIdPromo(n) + "_img");
				    img.src = this.promos[n].u;							
				    img.className = "";
				    if(n == 0 & this.promos[n].l != true)this.contarEstadistica(this.promos[n]);
				    this.promos[n].l = true;				    
			    }			
		    }	  
		}
	};

	this.rotacion  = function(n)
	{
		this.step = Math.round(this.anchoTotalPromo / 2);
		
		var cant = 0;
		cant = this.promosVisibles;
		this.sentidoRotacion = n;
		this.r(this.sentidoRotacion*cant);
	};

	this.r = function(n)
	{
		if (this.sentidoRotacion>0 && this.promoActual<=0)
		{
			this._onPrincipio();			
			return;
		}

		if (this.sentidoRotacion<0 && (this.promoActual >= (this.cantidadPromos-this.promosVisibles)))
		{
			this._onFinal();
			return;
		}
		this._onRotando();

		if(this.desplazado == null) this.desplazado =  this.anchoTotalPromo;
		
		clearTimeout(this._t);
		this.desplazado -= this.step;
        var s  = - this.sentidoRotacion;
        var p;
        
        if (s>0)
            p = this.promoActual + (this.promosVisibles*s);
        else
            p = this.promoActual + s;

            
		this.cargarPromo(p,1);
		this.cargarPromo(p+s,2);

		if (this.desplazado >= 0)
		{
			this.pos += (this.sentidoRotacion * this.step);

			var str = '_'+this.id+'.r('+(n)+');';
			this._t = setTimeout(str, this.vel);
		}
		else
		{
			this.promoActual -= this.sentidoRotacion;

			this.desplazado = null;

			if (n * this.sentidoRotacion > 1)
			{
				n-=this.sentidoRotacion;
		
				var str = '_'+this.id+'.r('+(n)+');';
				this._t = setTimeout(str, this.vel);
			}
			this._onCargoPromo(this.promos[p]);
			
		}        
		this._cP.style.left = this.pos + "px";
	}

	this.contarEstadistica = function(p)
	{
	   if(this.promoConEstadistica)
	   {
	         var imagen_id = "imagen_id:" + p.imgId;
	         var fechacompleta = new Date();	         
	         var servidor = document.getElementById('servidorEST');
	         var usuario = document.getElementById('usuarioEST');
	         var host_id = document.getElementById('hostEST');
	         var pagina = document.getElementById('paginaEST');
	         var nrorandom = p.imgId + fechacompleta.getHours() + fechacompleta.getMinutes() + fechacompleta.getSeconds() + fechacompleta.getMilliseconds();
	         
	         var gifEst = document.getElementById('checkGifEST');
	         
	         var usr;
	         
	         if (usuario != null)
	         {
	            usr = usuario.value;
	         }
	         else
	         {
	            usr = "";
	         }            
	        	         
	         //gifEst.src = "http://estadisticas.lanacion.com.ar/diario/default.gif?pagina="+pagina.value+"/GaleriaFotos&servidor="+servidor.value+"&nro="+nrorandom+"&origen=diario&nota_id=&otros="+imagen_id+"&usuario="+usr+"&host_id="+host_id.value	         	       	        
	   } 	        
	};
		
	//Eventos
	this.onCargoPromo =this.contarEstadistica;
	this._onCargoPromo = function(p){if(this["onCargoPromo"]!=null)this.onCargoPromo(p);};
	this._onRotando = function(){if(this["onRotando"])this.onRotando();};
	this._onPrincipio = function(){if(this["onPrincipio"])this.onPrincipio();};
	this._onFinal = function(){if(this["onFinal"])this.onFinal();};	
	
	this.iniciar = function()
	{
		this.getElements();
		this.ajustarVentana();

		this._c.style.visibility = "visible";
		//this._c.style.display = "block";	
	};
	if (cargar != null && cargar == true)
		this.cargar(promo_tipo);
}

function NewPromo(id)
{
	return new Promo(id);
}