// Copyright El País Digital
// Autor: Damian Ares
// Fecha: 2007-04-14

if(typeof(window.gSlide)=="undefined"){window.gSlide=new Array()};

if(typeof(elpdSlide)=="undefined"){
	
	function elpdSlide()
	{
		this.Slide			=	gSlide.length;
		this.slides			=	new Array();
		this.slides_text	=	new Array();
		this.slides_alto	=	new Array();
		this.timerID		=	null;
		this.waitID			=	null;
		this.activeIndex	=	1;
		this.auto			=	true;
		this.hasSlides		=	false;
		this.direction		=	1
		this.slideTime		=	5000;
		this.checkTime		=	15000;
		this.slideHeight	=	"auto";
		this.SlideID		=	"";
		this.SlideStrict	=	true;
	

		this.Add = function(pSlide,pAlto, pText){
			var oCapa  =  MM_findObj("elpdSlideObj_" + pSlide,document);
			if(oCapa){
				
				if(pAlto>this.slideHeight || this.slideHeight=="auto"){
					this.slideHeight = pAlto;
				}
				
				this.slides[this.slides.length] = oCapa;
				this.slides_text[this.slides_text.length] = (pText)?pText:"";
				this.slides_alto[this.slides_alto.length] = pAlto;				
				
				if(!this.hasSlides && this.slides.length>0) this.hasSlides=true;
			}
			
		}

		this.Next = function(o){
			var n;
			
			try{
				
				if(gSlide[o].hasSlides){					

					//ocultar todas las imagenes
					n = (gSlide[o].activeIndex-1)
					if(n>=0){
						if(gSlide[o].slides[n]!=null && gSlide[o].slides[n]!=undefined){
							gSlide[o].slides[n].style.overflow ="hidden";
							gSlide[o].slides[n].style.display='none';
							gSlide[o].slides[n].style.visibility = "hidden";
							gSlide[o].slides[n].style.zIndex = 1;
							gSlide[o].slides[n].style.position = "relative";
							gSlide[o].slides[n].style.border = "0px solid";
							gSlide[o].slides[n].style.top = "0px";
							gSlide[o].slides[n].style.left = "0px";
							gSlide[o].slides[n].style.height = "0px";
							
						}
					}
					
					if(gSlide[o].direction==1){
						gSlide[o].activeIndex++;
						if(gSlide[o].activeIndex>gSlide[o].slides.length) gSlide[o].activeIndex=1;
					}else{
						gSlide[o].activeIndex--;
						if(gSlide[o].activeIndex<1) gSlide[o].activeIndex=gSlide[o].slides.length;
					}
					
					//mostrar la imagen correspondiente al indice
					gSlide[o].slides[gSlide[o].activeIndex-1].style.overflow ="hidden";
					gSlide[o].slides[gSlide[o].activeIndex-1].style.display='block';
					gSlide[o].slides[gSlide[o].activeIndex-1].style.visibility = "visible";
					gSlide[o].slides[gSlide[o].activeIndex-1].style.zIndex = 1;
					gSlide[o].slides[gSlide[o].activeIndex-1].style.position = "relative";
					gSlide[o].slides[gSlide[o].activeIndex-1].style.border = "0px solid #cc0000";
					gSlide[o].slides[gSlide[o].activeIndex-1].style.top = "0px";
					gSlide[o].slides[gSlide[o].activeIndex-1].style.left = "0px";					
					
					try{
					
						if(gSlide[o].SlideStrict)
							gSlide[o].slides[gSlide[o].activeIndex-1].style.height = gSlide[o].slideHeight+"px";
						else{
							gSlide[o].slides[gSlide[o].activeIndex-1].style.height = gSlide[o].slides_alto[gSlide[o].activeIndex-1]+"px";
						}

					}catch(e){}

					
					if(typeof(gSlide[o].slides_text.length)!="undefined"){
						
						var spanText = MM_findObj("elpdSlideObj_leyenda"+gSlide[o].SlideID,document);	
						
						if(spanText){
							spanText.innerHTML = gSlide[o].slides_text[gSlide[o].activeIndex-1];						
						}
					}				

					//alert(gSlide[o].activeIndex);
					if(gSlide[o].timerID!=null)
						clearInterval(gSlide[o].timerID);
				
					
					if(gSlide[o].auto){
						var _index = o;
						gSlide[o].timerID = setTimeout(function(){ window.gSlide[o].Next(o) },gSlide[o].slideTime);
					}

				}else{
					if(gSlide[o].timerID!=null)
						clearInterval(gSlide[o].timerID);
					
				}
			}catch(e){
				alert("ERROR:" + e.description);
			}
			
		}

		this.setFirst = function(o){
			gSlide[o].auto=false;		//pasarlo a manual
			gSlide[o].activeIndex=0;	//no hay ninguna activa
			gSlide[o].direction=1;		//setear para que avance
			gSlide[o].Next(o);			//setear datos de la primer imagen
			gSlide[o].auto=true;		//pasarlo a automatico de nuevo
		}

		this.Check = function(o){
			//alert("volviendo a automatico");
			gSlide[o].auto = true;
			gSlide[o].direction=1; //setear para que avance
			gSlide[o].Next(o);
		}

		this.PrevImage = function(o){

			if(typeof(o)=="undefined")
				o = this.Slide;

			if(gSlide[o].timerID!=null)
				clearInterval(gSlide[o].timerID);
			
			if(gSlide[this.Slide].waitID!=null)
				clearTimeout(gSlide[o].waitID);

			//setear que ya no es automatico el desplazamiento
			gSlide[o].auto = false;

			//usar como control para saber si ya dejo de clickear y asi volver a manual
			var _index = o;
			gSlide[o].waitID = setTimeout(function(){ window.gSlide[o].Check(o) },gSlide[o].checkTime);

			gSlide[o].direction=-1;//setear para que retroceda
			gSlide[o].Next(o);
		}

		this.NextImage = function(o){

			if(typeof(o)=="undefined")
				o = this.Slide;


			if(gSlide[o].timerID!=null)
					clearInterval(gSlide[o].timerID);
			

			if(gSlide[o].waitID!=null)
				clearTimeout(gSlide[o].waitID);

			//setear que ya no es automatico el desplazamiento
			gSlide[o].auto = false;

			//usar como control para saber si ya dejo de clickear y asi volver a manual
			var _index = o;
			gSlide[o].waitID = setTimeout(function(){ window.gSlide[o].Check(o) },gSlide[o].checkTime);

			gSlide[o].direction=1; //setear para que avance
			gSlide[o].Next(o);
		}

		this.Start  = function(){
			if(gSlide[this.Slide].hasSlides){
				gSlide[this.Slide].setFirst(this.Slide); //setear datos de la primer imagen
				//disparar el timer
				var _index = this.Slide;
				if(gSlide[this.Slide].slides.length>1)
					gSlide[this.Slide].timerID = setTimeout(function(){ window.gSlide[_index].Next(_index) },gSlide[this.Slide].slideTime);
			}
		}

		this.Pause  = function(){
			if(gSlide[this.Slide].hasSlides){

				if(gSlide[this.Slide].timerID!=null)
					clearInterval(gSlide[this.Slide].timerID);
			

				//disparar el timer
				var _index = this.Slide;

				//gSlide[this.Slide].timerID = setTimeout(function(){ window.gSlide[_index].Next(_index) },gSlide[this.Slide].slideTime);
			}
		}

		

	}
}

//gSlide[gSlide.length] = new elpdSlide();