//timer de moviment
Moviment.prototype.mytime = null;
//coordenadas del cursor
Moviment.prototype.mouseX = 0;
Moviment.prototype.mouseY = 0;
Moviment.prototype.self;

//Definició de variables locals

//Moviment.prototype.sheight = 100; //altura de l'espai per a fotografies

Moviment.prototype.imgwidth; //amplada de fotografies

//Moviment.prototype.imgheight = 100; //altura de fotografies

//Moviment.prototype.titleheight = 20; //altura del titol

//Moviment.prototype.margen = 10; //marge del titol

function Moviment(contenedor, imagenes, tam, img){
	this.cont = contenedor;
	this.img = imagenes;
	this.swidth = tam;
	this.imgwidth = img;
	
	var self;
	
	this.onHorizMouseOver = function(num)
	{
		document.getElementById(this.img).style.width=(num * this.imgwidth) + "px";
		if (this.mytime==null)
		{	
			self = this;
			this.mytime=setInterval(function(){ self.moverHorizontal(); },45);
		}
		
	};

	this.onUpdateMouseCoordX = function(e)
	{				
		if (!e) var e = window.event||window.Event;
		if (e)
		{
			// Detect if the browser is IE or not.
			// If it is not IE, we assume that the browser is NS.
			var IE = document.all?true:false
			
			if (IE) { // grab the x-y pos.s if browser is IE
				var aux = e.clientX + document.body.scrollLeft;
				if (this.mouseX != aux){
					this.mouseX = aux;
				}
			} else {  // grab the x-y pos.s if browser is NS
				this.mouseX = e.pageX;
			}
		}
	};

	this.onHorizMouseOut = function()
	{
		//borram el timer
		if (this.mytime!=null)
		{
			clearInterval(this.mytime);
			this.mytime=null;
			self=null;
			
			//borram les coordenades del cursor
			document.getElementById(this.cont).onmousemove=null;
		}

	};

	this.moverHorizontal = function()
	{
		var localX=0;
		var hPos=0;
		var desplazamientoH=10;

		//calculamos el factor de velocidad
		var imgsLeft= new Number(document.getElementById(self.img).style.left.replace("px",""));
		var imgsWidth=new Number(document.getElementById(self.img).style.width.replace("px",""));
		
		var esquerra = (screen.width - 984) / 2;
		
		var fueraLeft=new Number(document.getElementById(self.cont).style.left.replace("px",""));
		var fueraWidth=new Number(document.getElementById(self.cont).style.width.replace("px",""));
		
		//alert("imgs: left, " + fueraLeft + " - width, " + fueraWidth);
		
		//var ancho=document.getElementById(self.cont).style.width.replace('px','');
		//ancho = ancho.replace('pt','');
		ancho = self.swidth;
		
		//alert(imgsLeft);
		
		var speed=1;
		
		
		//cogemos la posición local del ratón
		localX = self.mouseX - esquerra;
		
		//alert(localX);
		
		speed=(localX/ancho - .5) * 2;	//velocidad es la posición relativa del puntero (0..1), trasladado a (-.1,.1)
		//alert(speed)
		
		//alert (document.getElementById('imgs').style.left + "   " + document.getElementById('imgs').style.width + "   " + document.getElementById('fuera').style.left + "   " + document.getElementById('fuera').style.width);
		
		//alert("left: " + imgsLeft + " width: " + imgsWidth + "ancho: " + ancho + "speed: " + speed)
		
		//si hemos llegado al final de la lista de fotos, no hacemos nada
		if(((imgsLeft + imgsWidth < ancho+9) && (speed > 0)) || 
			((imgsLeft > 0) && (speed <0)))
		{
			//alert("return 1")
			return;
		}
		
		//Si no shemos salido de las fotos, no hacemos nada
		if (localX > (new Number(document.getElementById(self.img).style.left.replace("px","")) + new Number(document.getElementById(self.img).style.width.replace("px",""))) ||
			localX < (new Number(document.getElementById(self.img).style.left.replace("px",""))))
		{
			//alert("return 2")
			return;
		}
		
		hPos = document.getElementById(self.img).style.left.replace('px','');
		hPos = hPos.replace('pt','');
		hPos = new Number(hPos);
		hPos-= desplazamientoH * speed;
		
		//alert(hPos + " : " + localX + " : " + hlocalGlobalX + " : " + (new Number(document.getElementById('imgs').style.left.replace("px","")) + new Number(document.getElementById('imgs').style.width.replace("px","")) ))
		
		if(hPos < 0.0){
			document.getElementById(self.img).style.left=+hPos + "px";
		}
		
		//actualizamos la var de posición horizontal
		//document.getElementById('fPosiciones').posicionHoriz.value=hPos
		
		//alert(document.getElementById('fPosiciones').hPos.tetx)

		
	};
	
	
}