/////////////////////////////////////////////////////////////////////////
// ACCENT-WHADS S.L. 2001
// http://www.whads.com

var isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
var carregant=false;
var mapas = new Array();
var imagenes=0;

function getMousePos(e) {	
	
	if (!e) e = window.event;
	
	// Manera raonable
	/*if ("clientX" in e && "clientY" in e) {
		return {
			x: e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
			y: e.clientY + document.body.scrollTop + document.documentElement.scrollTop
		};		
	}
	else {
		return {
			x: e.pageX,
			y: e.pageY
		};
	}*/

	// Mètode Dynapi
	return {
		x: e.pageX + (isIE ? 0 : document.body.scrollLeft),
		y: e.pageY + (isIE ? 0 : document.body.scrollTop)
	}
}

function getElementPos(element) {

	var pos = {
		x: 0,
		y: 0
	}

	while (element) {
		pos.x += element.offsetLeft;
		pos.y += element.offsetTop;
		element = element.offsetParent;
	}

	return pos;
}

var debugLabel = null;

function showMousePos(e) {
		
	var mouse = getMousePos(e);
	var coords = getElementPos(document.getElementById("eventosMapa"));
	
	if (!debugLabel) {
		debugLabel = document.createElement("div");
		with (debugLabel.style) {
			border = "1px solid red";
			padding = "4px";
			fontSize = "20px";
		}
		document.body.appendChild(debugLabel);
	}

	debugLabel.innerHTML = "mouse: " + mouse.x + ", " + mouse.y + " scroll: " + document.body.scrollLeft + ", " + document.body.scrollTop
		+ " client: " + e.clientX + ", " + e.clientY + " page: " + e.pageX + ", " + e.pageY;
	//debugLabel.innerHTML = "rel-dynapi: " + e.getX() + "," + e.getY() + " mouse: " + mouse.x + "," + mouse.y + " rel: " + (mouse.x - coords.x) + "," + (mouse.y - coords.y);
}

function mapa(lletra,ini0,ini1,ini2,ini3,maxX,maxY,escala) {
	this.DynLayer = DynLayer;
	this.DynLayer();
	this.setZIndex(150);
	//this.setBgColor('#112211')

	//------variables per controlar limits de mapilla
	this.numcuadresX=0;
	this.numcuadresY=0;
	this.maximX=0;
	this.minimX=100;
	this.minimY=100;
	//------------------------------
		
	this.ruta_imatges='../images/mapa/'
	this.lletra=lletra;
	this.escala = escala;

	this.limit = new Array(50)	
	for (i=0; i < 50; i++) {   
		this.limit[i] = new Array(50)   
		for (j=0; j < 1; j++) {      
			this.limit[i][j] = -1 
			}
		}

	this.mapas = new Array();

	this.setSize(700,430);
	//marge
	this.marge = this.addChild(new DynLayer(null,188,18,454,390,"#121212"));
	
	//variables per controlar els gifs de limits
	this.gif_sup=false;
	this.gif_inf=false;
	this.gif_dret=false;
	this.gif_esq=false;	
	//--------
	this.venga=false;
	//--Variables de distancia
	this.moure=false;
	//Carregar imatges
	var imagenes = new Array();

	var img = this.ruta_imatges+this.lletra+ini0+'.GIF';
	imagenes[0]=DynImage.getImage(img)
	img = this.ruta_imatges+this.lletra+ini1+'.GIF';
	imagenes[1]=DynImage.getImage(img)
	img = this.ruta_imatges+this.lletra+ini2+'.GIF';
	imagenes[2]=DynImage.getImage(img)
	img = this.ruta_imatges+this.lletra+ini3+'.GIF';
	imagenes[3]=DynImage.getImage(img)
	
	
	//Flechas-----------	
	this.sup = this.addChild(new DynLayer(null,395,0,20,20,"") );
	this.sup.setHTML('<a href="javascript:void()" ><img name="sup" src="../images/mapa/sup.gif" width="19" height="19" border="0"></a>');
	this.inf = this.addChild(new DynLayer(null,395,410,20,20,""));
	this.inf.setHTML('<a href="javascript:void()" ><img name="inf" src="../images/mapa/inf.gif" width="19" height="19" border="0"></a>');
	this.esq = this.addChild(new DynLayer(null,170,190,20,20,""));
	this.esq.setHTML('<a href="javascript:void()" ><img name="esq" src="../images/mapa/esq.gif" width="19" height="19" border="0"> </a>');
	this.dret = this.addChild(new DynLayer(null,647,190,20,20,""));
	this.dret.setHTML('<a href="javascript:void()" ><img name="dret" src="../images/mapa/dret.gif" width="19" height="19" border="0"></a>');
	
	this.sup_dret = this.addChild(new DynLayer(null,647,0,20,20,""));
	this.sup_dret.setHTML('<a href="javascript:void()" ><img name="sup_dret" src="../images/mapa/sup_dret.gif" width="19" height="19" border="0"></a>');
	this.sup_esq = this.addChild(new DynLayer(null,170,0,20,20,""));
	this.sup_esq.setHTML('<a href="javascript:void()" ><img name="sup_esq" src="../images/mapa/sup_esq.gif" width="19" height="19" border="0"></a>');
	this.inf_esq = this.addChild(new DynLayer(null,170,410,20,20,""));
	this.inf_esq.setHTML('<a href="javascript:void()" ><img name="inf_esq" src="../images/mapa/inf_esq.gif" width="19" height="19" border="0"></a>');
	this.inf_dret = this.addChild(new DynLayer(null,647,410,20,20,""));
	this.inf_dret.setHTML('<a href="javascript:void()" ><img name="inf_dret" src="../images/mapa/inf_dret.gif" width="19" height="19" border="0"></a>');

	//events --------
	var dret_listener = new EventListener(this);
	dret_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;	
		o.move0(1,3,0,2,1)			
	};
	
	dret_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	
	dret_listener.onmousedown = function(e) {
		var o = e.getTarget();	
	//	alert('no click')		
		
	};
	
	this.dret.addEventListener(dret_listener);

	var esq_listener = new EventListener(this);
	esq_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;				
		o.move0(2,0,1,3,-1)
	};
	esq_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	esq_listener.onmousedown = function(e) {
		var o = e.getTarget();	
	//	alert('no click')		
		
	};
	
	this.esq.addEventListener(esq_listener);

	var sup_listener = new EventListener(this);
	sup_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;
		o.move1(0,1,2,3,1)		
	};
	sup_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	sup_listener.onmousedown = function(e) {
		var o = e.getTarget();	
//		alert('no click')		
		
	};
	this.sup.addEventListener(sup_listener);

	var inf_listener = new EventListener(this);
	inf_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;
		o.move1(2,3,0,1,-1)		
	};
	inf_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	inf_listener.onmousedown = function(e) {
		var o = e.getTarget();	
//		alert('no click')		
		
	};
	this.inf.addEventListener(inf_listener);

	var sup_esq_listener = new EventListener(this);
	sup_esq_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;
		o.move0(2,0,1,3,-1)		
		o.move1(0,1,2,3,1)
		
	};
	sup_esq_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	sup_esq_listener.onmousedown = function(e) {
		var o = e.getTarget();	
//		alert('no click')		
		
	};
	
	this.sup_esq.addEventListener(sup_esq_listener);

	var sup_dret_listener = new EventListener(this);
	sup_dret_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;
		o.move0(1,3,0,2,1)		
		o.move1(0,1,2,3,1)
		
	};
	sup_dret_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	sup_dret_listener.onmousedown = function(e) {
		var o = e.getTarget();	
//		alert('no click')		
		
	};
	this.sup_dret.addEventListener(sup_dret_listener);

	var inf_esq_listener = new EventListener(this);
	inf_esq_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;
		o.move0(2,0,1,3,-1)		
		o.move1(2,3,0,1,-1)
		
	};
	inf_esq_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	inf_esq_listener.onmousedown = function(e) {
		var o = e.getTarget();	
//		alert('no click')		
		
	};
	this.inf_esq.addEventListener(inf_esq_listener);

	var inf_dret_listener = new EventListener(this);
	inf_dret_listener.onmouseover = function(e) {
		var o = e.getTarget();
		o.venga=true;
		o.move0(1,3,0,2,1)		
		o.move1(2,3,0,1,-1)
		
	};
	
	
	inf_dret_listener.onmouseout = function(e) {
		var o = e.getTarget();
		o.venga=false;	
	};
	inf_dret_listener.onmousedown = function(e) {
		var o = e.getTarget();	
//		alert('no click')		
		
	};
	this.inf_dret.addEventListener(inf_dret_listener);

	//mapa----------------
	this.pare = this.addChild(new DynLayer(null,190,20,450,386,"#636363",null,0));	
	this.visor= this.addChild(new DynLayer("eventosMapa",190,20,450,386,"",null,30));	
	
	
	this.info1 = this.addChild(new DynLayer(null,190,20,140,60,"#114444",null,500));
	this.text = this.info1.addChild(new DynLayer(null,1,1,138,58,"#FFFFFF"));
	
	this.info1.setVisible(false);
	this.origen = this.visor.addChild(new DynLayer(null,0,0,10,10,""));
	this.origen.setVisible(false);
	this.origen.setHTML('<img src="../images/mapa/punt_vermell.gif" border="0">');
	
	
		// *************** jordi 28/02/2006
	mapas[0]=  this.pare.addChild(new DynLayer(null,0,0,560,400,"#fff111"));
	mapas[0].setHTML('<img name="myImg0" src="'+this.ruta_imatges+this.lletra+ini0+'.GIF" width="560" height="400" border="0">');
		
	mapas[1]= this.pare.addChild(new DynLayer(null,560,0,560,400,"#fff111"));
	mapas[1].setHTML('<img name="myImg1" src="'+this.ruta_imatges+this.lletra+ini1+'.GIF" width="560" height="400" border="0">');
	
	mapas[2]=  this.pare.addChild(new DynLayer(null,0,400,560,400,"#fff111"));
	mapas[2].setHTML('<img name="myImg2" src="'+this.ruta_imatges+this.lletra+ini2+'.GIF" width="560" height="400" border="0">');
	
	mapas[3]=  this.pare.addChild(new DynLayer(null,560,400,560,400,"#ffffff"));
	mapas[3].setHTML('<img name="myImg3" src="'+this.ruta_imatges+this.lletra+ini3+'.GIF" width="560" height="400" border="0">');
	// ***************
	
	//punts
	this.num_punts=20;
	this.punts = new Array();
	for(var i=0;i<this.num_punts-1;i++){
		this.punts[i] = this.visor.addChild(new DynLayer(null,100,100,3,3,"#111111",null,500));
		this.punts[i].setVisible(false);
	}
	this.punts[this.num_punts-1] = this.visor.addChild(new DynLayer(null,100,100,4,4,"#111111",null,500));
	this.punts[this.num_punts-1].setVisible(false);
	//---------

	

	this.inix=0;
	this.inix=0;
	this.moure=false;
	//-events pare------
	
	var visor_listener = new EventListener(this.visor);

	visor_listener.onmousedown = function(e) {

		var o = e.getTarget().parent;
		
		// Iniciar un recorregut
		if(o.origen.visible==false) {
		
			var BALL_OFFSET = -5;
			var mouse = getMousePos(e);
			var ref = getElementPos(document.getElementById("eventosMapa"));

			//o.origen.moveTo(e.getX() + BALL_OFFSET, e.getY() + BALL_OFFSET);
			o.origen.moveTo(mouse.x - ref.x + BALL_OFFSET, mouse.y - ref.y + BALL_OFFSET);
			o.origen.setVisible(true);
			o.info1.setVisible(true);
			o.text.setHTML("<table class='distancia'><tr><td>"+getText({ca:"Distancia",es:"Distancia",en:"Distance"})+": 0 "+getText({ca:"metres",es:"metros",en:"meters"})+"</td></tr></table>");	
			
			for (var i = 0; i < o.num_punts; i++) {
				o.punts[i].moveTo(0,0);
				o.punts[i].setVisible(true);
			}
		//o.moure=true;
		}
		// Finalitzar un recorregut
		else {			
			o.info1.setVisible(false);
			o.origen.setVisible(false);
			o.moure=false;
			o.venga=false;

			for(var i=0;i<o.num_punts;i++) {
				o.punts[i].setVisible(false);
			}
		}
	}

	visor_listener.onmouseup = function(e) {
		var o = e.getTarget();		
	}

	visor_listener.onmousemove = function(e) {
		var o = e.getTarget().parent;
		var st ='';
		var temps=0;
		var vel=1.32;
		//var de punt
		
		var mouse = getMousePos(e);
		var ref = getElementPos(document.getElementById("eventosMapa"));
		var x = (mouse.x - ref.x);
		var y = (mouse.y - ref.y);

		
		//var dist_x = (e.getX() - o.origen.x) / o.num_punts;
		//var dist_y = (e.getY() - o.origen.y) / o.num_punts;
		var dist_x = (x - o.origen.x) / o.num_punts;
		var dist_y = (y - o.origen.y) / o.num_punts;
		var distx=dist_x
		var disty=dist_y
		//------------	
		
		for(var i=0;i<o.num_punts;i++){
	
			o.punts[i].moveTo(o.origen.x+distx+2,o.origen.y+disty+2);
			
			distx +=dist_x
			disty +=dist_y		
			if(o.origen.visible==true) {
					o.punts[i].setVisible(true);					
				}		
			
		}
		
		var dist=Math.abs(((o.origen.x+170)-(e.getPageX()-o.x-25)))+Math.abs((o.origen.y-(e.getPageY()-o.y-25)));	
		
		dist=Math.abs(dist*o.escala);//metros		
		dist=Math.round(dist);
		var t1=Math.round(0.8*((dist/vel)/60));//min
		var t2=Math.round(1.2*((dist/vel)/60));
		st= '<table  class="distancia" border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;</td><td><b>'+getText({ca:"Distancia",es:"Distancia",en:"Distance"})+':</b>'+dist+getText({ca:"metres",es:"metros",en:"meters"})+'</td></tr>'
		st += '<tr><td>&nbsp;</td><td  nowrap><b>'+getText({ca:"Temps aprox",es:"Tiempo aprox",en:"Time aprox"})+'.:</b></td></tr>';
		st += '<tr><td>&nbsp;</td><td >&nbsp;&nbsp;de&nbsp;'+t1+'&nbsp;'+getText({ca:"a",es:"a",en:"to"})+'&nbsp;'+t2+'&nbsp;min.</td></tr></table>';
		o.text.setHTML(st);
		
		
		//per moures mentres calcules
		if(o.moure==true){	
		if(e.getPageX()-o.x>370) {o.venga=true;o.move0(1,3,0,2,1);}
		if(e.getPageX()-o.x<20) {o.venga=true;o.move0(2,0,1,3,-1);}
		if((e.getPageX()-o.x<370)&&(e.getPageX()-o.x>20)){o.venga=false}

		if(e.getPageY()-o.y>330) {o.venga=true;o.move1(2,3,0,1,-1);}
		if(e.getPageY()-o.y<20) {o.venga=true;o.move1(0,1,2,3,1);}
		if((e.getPageY()-o.y<540)&&(e.getPageX()-o.x>20)){o.venga=false}
		 }
	
		
		
	};
	visor_listener.onmouseout = function(e) {
		var o = e.getTarget();
		for(var i=0;i<o.num_punts;i++){
			o.punts[i].setVisible(false);		
		}
	
		
		}
	this.visor.addEventListener(visor_listener);
	
	//-------------------


	
		//variables--------
	ini0++;ini0--;ini1++;ini1--;ini2++;ini2--;ini3++;ini3--;
	mapas[0].imatge="myImg0";	
	mapas[0].nom=ini0;

	mapas[1].imatge="myImg1";	
	mapas[1].nom=ini1;

	mapas[2].imatge="myImg2";	
	mapas[2].nom=ini2;

	mapas[3].imatge="myImg3";	
	mapas[3].nom=ini3;
	

	//---------------
	//mapa lateral
	//tamany real del mapa
	this.movcurX=(maxX)*540;
	this.movcurY=(maxY)*390;
	//tamany cuadre petit mapilla
	this.cuadreX=100/maxX;
	this.numcuadresX=maxX
	this.cuadreY=72/maxY;
	this.numcuadresY=maxY
	
	//Factor de transformacio vel mapilla
	this.factvelX=((20*100)/this.movcurX)/20;
	this.factvelY=((20*70)/this.movcurY)/20;
	
	//Factor de transformacio distancia mapilla
	this.factX=((100)/this.movcurX);
	this.factY=((70)/this.movcurY);
	
	this.mapa_marge = this.addChild(new DynLayer(null,0,300,102,74,"#111111",null,400));
	this.mapa = this.mapa_marge.addChild(new DynLayer(null,1,1,100,72,"",null,400));
	this.mapa.setHTML('<img  src="'+this.ruta_imatges+'bcn_petit.gif" width="100" height="72" border="0">');
	
	if(this.cuadreX>8){
	this.cursor = this.mapa.addChild(new DynLayer(null,70,10,this.cuadreX,this.cuadreY,""));
	this.cursor1 = this.cursor.addChild(new DynLayer(null,0,0,this.cuadreX-2,1,"#111111"));
	this.cursor2 = this.cursor.addChild(new DynLayer(null,0,0,1,this.cuadreY-2,"#111111"));
	this.cursor3 = this.cursor.addChild(new DynLayer(null,this.cuadreX-2,0,1,this.cuadreY-2,"#111111"));
	this.cursor4 = this.cursor.addChild(new DynLayer(null,0,this.cuadreY-2,this.cuadreX-2,1,"#111111"));
	}
	else{
	this.cursor = this.mapa.addChild(new DynLayer(null,70,10,10,10,""));
	this.cursor1 = this.cursor.addChild(new DynLayer(null,2.5,0,1,5,"#cc0000"));
	this.cursor2 = this.cursor.addChild(new DynLayer(null,0,2.5,5,1,"#cc0000"));
	}
	//------------
	var mapapetit_listener = new EventListener(this.cursor);
	this.cursor.mapax=0;
	this.cursor.mapay=0;
	this.cursor.origenX=0;
	this.cursor.origenY=0;
	this.cursor.esmou=false;
	
	
	mapapetit_listener.onmousedown = function(e) {
		var o = e.getTarget();
		o.mapax=o.x;
		o.mapay=o.y;
		o.origenX=o.x;
		o.origenY=o.y;
		o.esmou=true;
		
		
	};
	mapapetit_listener.onmouseup = function(e) {
		var o = e.getTarget();		
		o.mapax=o.mapax-o.x;
		o.mapay=o.mapay-o.y;		
		o.parent.parent.parent.carrega(o.mapax,o.mapay);
		o.esmou=false;				
	};	
	
	this.cursor.addEventListener(mapapetit_listener);
	
	
	DragEvent.setDragBoundary(this.cursor);
	DragEvent.enableDragEvents(this.cursor);
	
	mapa_listener=new EventListener(this)
	mapa_listener.onmouseover=function(e){
		var o = e.getTarget();
	
		if(o.origen.visible){
			o.info1.setVisible(false);
			o.origen.setVisible(false);
			o.moure=false;
			o.venga=false;
			for(var i=0;i<o.num_punts;i++){				
				o.punts[i].setVisible(false);
				}
			}
		
	}
	mapa_listener.onmouseout=function(e){
		var o = e.getTarget();
		if(o.cursor.esmou){
		o.cursor.moveTo(o.cursor.origenX,o.cursor.origenY)
		}
		
		
		
	}
	this.mapa_marge.addEventListener(mapa_listener)
	
	//----------
	DynAPI.document.addChild(this)
	
	

};

mapa.prototype = new DynLayer;

mapa.prototype.limitadd = function(fila,esq,dret) {
	var a=fila;
	if (a<this.minimY) this.minimY=a;
	a=dret
	if (a>this.maximX) this.maximX=a;
	a=esq
	if (a<this.minimX) this.minimX=a;		
	
	for(var i = esq;i<=dret;i++){
		this.limit[fila][i] =1
		
		}	
}
mapa.prototype.mapilla_origen = function() {
	this.cursor.moveTo(this.cursor.origenX,this.cursor.origenY)
	}
mapa.prototype.carrega = function(X,Y) {
	var num_casillasX=0;
	var num_casillasY=0;	
	num_casillasX=-Math.round(X/this.cuadreX);
	num_casillasY=Math.round(Y/this.cuadreY);	
	var nom='';
	var cero=0;
	cero = this.colum(mapas[0].nom);
	nom='E'+cero+mapas[0].nom;
	
	var numactualX=''
	var numactualY=0;	
	
	numX=nom.substr(3,4);
	numY=nom.substr(1,2);	
	numX++;numX--;numY++;numY--;
	numX=numX+num_casillasX;
	
	numY=numY+num_casillasY;
	//S'ha de posar maco	
	
	if((this.limit[numY][numX]==1)&&(this.limit[numY][numX+1]==1)&&(this.limit[numY+1][numX]==1)&&(this.limit[numY+1][numX+1]==1)){	
	if (((this.maximX==numX)||(this.minimY==numY)) ){	
	//Limitssss
		if(((this.maximX==numX)&&(this.minimY!=numY)) )	{
				this.mapilla_moviment_mapes(1,0,3,2,numX,numY,-1,1);
				}
		if(((this.maximX!=numX)&&(this.minimY==numY)) ){
			this.mapilla_moviment_mapes(2,3,0,1,numX,numY,1,-1);
		}
		if(((this.maximX==numX)&&(this.minimY==numY)) )	{
			this.mapilla_moviment_mapes(3,2,1,0,numX,numY,-1,-1);	
		}
	}	
	else{
	//Cuadros normals
			this.mapilla_moviment_mapes(0,1,2,3,numX,numY,1,1);
	}
	mapas[0].moveTo(0,0);
	mapas[1].moveTo(560,0);
	mapas[2].moveTo(0,400);
	mapas[3].moveTo(560,400);
	this.mapilla_recolocar(numX,numY);
	}else{
	
	this.cursor.moveTo(this.cursor.origenX,this.cursor.origenY)
	
	}
	
	
}
mapa.prototype.mapilla_moviment_mapes = function(a0,a1,a2,a3,numX,numY,sumX,sumY) {
	
	//imatge 0
	var cero='0';
	if(numX>=10) cero='';
	var futur=numY+cero+numX;
	//alert('futur:'+futur)
	var cero = this.colum(futur);				
	var imatge =this.ruta_imatges+this.lletra+cero+(futur)+".GIF" ;	
	this.imatge_per_imatge(mapas[a0].imatge,imatge,0); 
	mapas[a0].nom=futur;	
	//imatge 1
	cero='0';
	if((numX+sumX)>=10) cero='';
	var futur=numY+cero+(numX+sumX);
	var cero = this.colum(futur);				
	var imatge =this.ruta_imatges+this.lletra+cero+(futur)+".GIF" ;	
	this.imatge_per_imatge(mapas[a1].imatge,imatge,1); 
	mapas[a1].nom=futur;	
	//imatge 2
	cero='0';
	if(numX>=10) cero='';
	var futur=(numY-sumY)+cero+(numX);
	var cero = this.colum(futur);				
	var imatge =this.ruta_imatges+this.lletra+cero+(futur)+".GIF" ;	
	this.imatge_per_imatge(mapas[a2].imatge,imatge,2); 
	mapas[a2].nom=futur;	
	//imatge 3
	cero='0';
	if((numX+sumX)>=10) cero='';
	var futur=(numY-sumY)+cero+(numX+sumX);
	var cero = this.colum(futur);				
	var imatge =this.ruta_imatges+this.lletra+cero+(futur)+".GIF" ;	
	this.imatge_per_imatge(mapas[a3].imatge,imatge,3); 
	mapas[a3].nom=futur;	
	}
mapa.prototype.mapilla_recolocar = function(numX,numY) {
	this.cursor.moveTo((numX-this.minimX)*this.cuadreX,(this.numcuadresY*this.cuadreY)-(numY*this.cuadreY));
		if(((this.maximX==numX)&&(this.minimY!=numY)) )	{
				this.cursor.moveBy(-this.cuadreX,0);}
		if(((this.maximX!=numX)&&(this.minimY==numY)) ){
			this.cursor.moveBy(0,-this.cuadreY);}
		if(((this.maximX==numX)&&(this.minimY==numY)) )	{
			this.cursor.moveBy(-this.cuadreX,-this.cuadreY);}
	}
mapa.prototype.mapilla = function(col,fila) {
	var posX=0;
	var posY=72-((72*400)/this.movcurY);
	
	for(var i = 1;i<=col;i++){
		posX += (100*560)/this.movcurX;		
	}
	for(var i = 1;i<=fila;i++){
		posY -= (72*400)/this.movcurY;		
	}
	
	this.cursor.moveTo(posX,posY);
	
}


mapa.prototype.move0 = function(a0,a1,a2,a3,direccio) {

	
if((this.venga==false)||carregant==true){return;}

var nom=""+mapas[a1].nom;
var fila0 =nom.substr(0,nom.length-2);
fila0++;fila0--;
var columna0 = nom.substr(nom.length-2,nom.length);
columna0++;columna0--;
columna0 = columna0 + direccio;

nom=""+mapas[a0].nom;

var fila1 =nom.substr(0,nom.length-2);

fila1++;fila1--;
var columna1 = nom.substr(nom.length-2,nom.length);
columna1++;columna1--;
columna1 = columna1 + direccio;


if(fila0>=0 && columna0 >=0 && fila1>=0 && columna1 >=0){
if(((this.limit[fila0][columna0]==1)&&(this.limit[fila1][columna1]==1))||(mapas[a1].x!=0)){
	this.cursor.moveBy((20*this.factvelX)*direccio,0);
	

this.start('esq');
this.cantonades_start();
for (i in mapas) {	
	if(mapas[a1].x==0){this.change0(2*direccio,a2,a3);}								
	}	
	this.origen.moveBy(-20*direccio,0);
	for (i in mapas) {
			mapas[i].moveBy(-20*direccio,0);}	
		}else{var imatge='dret';
		      if (direccio<0) imatge='esq' 
			this.stop(imatge);}
	}

setTimeout(this+".move0("+a0+","+a1+","+a2+","+a3+","+direccio+")",20);
}

mapa.prototype.move1 = function(a0,a1,a2,a3,direccio) {
	
			
	if((this.venga==false)||(carregant==true)){return;}		
	var nom=""+mapas[a0].nom;
	var fila0 =nom.substr(0,nom.length-2);	
	fila0--;fila0++;	
	fila0 = fila0 + direccio;	
	var columna0 = nom.substr(nom.length-2,nom.length);
	columna0++;columna0--;		
	nom=""+mapas[a1].nom;
	var fila1 =nom.substr(0,nom.length-2);
	fila1--;fila1++;
	fila1 = fila1 + direccio;
	var columna1 = nom.substr(nom.length-2,nom.length);
	columna1++;columna1--;
	


if(fila0>=0 && columna0>=0 && fila1>=0 && columna1>=0){
if(((this.limit[fila0][columna0]==1)&&(this.limit[fila1][columna1]==1))||(mapas[a0].y!=0)){
	
	this.cursor.moveBy(0,-(20*this.factvelY)*direccio);
	this.start('inf');
	this.start('sup');	
	this.cantonades_start();
		
		this.origen.moveBy(0,20*direccio);		
			if(mapas[a0].y==0){this.change1(200*direccio,a2,a3);}											
		for (i in mapas) {
		mapas[i].moveBy(0,20*direccio);
			}	
		}else{var imatge='sup';
		      if (direccio<0) imatge='inf' 
		      this.stop(imatge);
		      this.cantonades_stop();	
			}
	}
	
	setTimeout(this+".move1("+a0+","+a1+","+a2+","+a3+","+direccio+")",20);
}

mapa.prototype.change0 = function(suma,a0,a1) {
	
	mapas[a0].nom=this.char_a_int(mapas[a0].nom);
	var cero = this.colum(mapas[a0].nom+suma);
	

	

	var imatge =this.ruta_imatges+this.lletra+cero+(mapas[a0].nom+suma)+".GIF" ;
	
	this.imatge_per_imatge(mapas[a0].imatge,imatge,a0); 	
	
	mapas[a1].nom=this.char_a_int(mapas[a1].nom);		
	cero = this.colum(mapas[a1].nom+suma);
	imatge =this.ruta_imatges+this.lletra+cero+(mapas[a1].nom+suma)+".GIF" ;
	this.imatge_per_imatge(mapas[a1].imatge,imatge,a1); 		
	
	//----Reorganitzacio--------------------	
	mapas[a0].nom=mapas[a0].nom+suma;mapas[a1].nom=mapas[a1].nom+suma;	
	//---------------------------------------
	
	var mov = 1120;
	if (suma<0) { mov=-1120;}
	mapas[a0].moveBy(mov,0);
	mapas[a1].moveBy(mov,0);
	this.cambio(0,1);
	this.cambio(2,3);	
	}

mapa.prototype.change1 = function(suma,a0,a1) {			
	//-----------------------------
	mapas[a1].nom=this.char_a_int(mapas[a1].nom);
	
	var cero = this.colum(mapas[a1].nom+suma);	
	
	var imatge =this.ruta_imatges+this.lletra+cero+(mapas[a1].nom+suma)+".GIF" ;
	this.imatge_per_imatge(mapas[a1].imatge,imatge,a1); 	
	
	mapas[a0].nom=this.char_a_int(mapas[a0].nom);
	cero = this.colum(mapas[a0].nom+suma);
	
	imatge =this.ruta_imatges+this.lletra+cero+(mapas[a0].nom+suma)+".GIF" ;
	this.imatge_per_imatge(mapas[a0].imatge,imatge,a0); 	
	
	//----Reorganitzacio--------------------	
	mapas[a0].nom=mapas[a0].nom+suma;mapas[a1].nom=mapas[a1].nom+suma;
	var mov=-800;
	if (suma<0) {mov=800 ;}
	mapas[a0].moveBy(0,mov);
	mapas[a1].moveBy(0,mov);
	this.cambio(0,2);
	this.cambio(1,3);
}
mapa.prototype.char_a_int = function(a) {
	a++;a--;
	return a;
}
mapa.prototype.cambio = function(a,b) {
	var capa,gen,nom;
	capa=mapas[a];	
	nom=mapas[a].nom;	
	mapas[a]=mapas[b];	
	mapas[a].nom=mapas[b].nom;	
	mapas[b]=capa;	
	mapas[b].nom=nom;		
	}	
mapa.prototype.colum = function(a){
	var b='0';
	
	if(a>=1000) b='';	
	
	return b;
	}
mapa.prototype.imatge_per_imatge = function(nom,imatge,a){	
	var img =DynImage.getImage(imatge,100,100);	
	if (document.layers)  eval('mapas['+a+'].doc.images["'+nom+'"].src = "'+img.src+'"'); 
	else document.images[nom].src =img.src;
	}
	
mapa.prototype.stop = function(a){	
	if(a=='sup') this.gif_sup=true;
	if(a=='inf') this.gif_inf=true;
	if(a=='dret') this.gif_dret=true;
	if(a=='esq') this.gif_esq=true;
	
	var imatge ="../images/mapa/"+a+"_roig.gif" ;		
	
	if (document.layers)  eval('this.'+a+'.doc.images["'+a+'"].src = "'+imatge+'"'); 
	else document.images[a].src =imatge;	
	}
mapa.prototype.cantonades_stop = function(){	
	if((this.gif_sup==true)&&(this.gif_dret==true)) this.stop('sup_dret');
	if((this.gif_inf==true)&&(this.gif_dret==true)) this.stop('inf_dret');
	if((this.gif_sup==true)&&(this.gif_esq==true))  this.stop('sup_esq');
	if((this.gif_inf==true)&&(this.gif_esq==true)) this.stop('inf_esq');			
	}
	
mapa.prototype.cantonades_start = function(){	
	if((this.gif_sup==true)&&(this.gif_dret==false)) this.start('sup_dret');
	if((this.gif_sup==false)&&(this.gif_dret==true)) this.start('sup_dret');
	if((this.gif_inf==true)&&(this.gif_dret==false)) this.start('inf_dret');
	if((this.gif_inf==false)&&(this.gif_dret==true)) this.start('inf_dret');
	if((this.gif_sup==true)&&(this.gif_esq==false))  this.start('sup_esq');
	if((this.gif_sup==false)&&(this.gif_esq==true))  this.start('sup_esq');
	if((this.gif_inf==true)&&(this.gif_esq==false)) this.start('inf_esq');	
	if((this.gif_inf==false)&&(this.gif_esq==true)) this.start('inf_esq');		
	}

mapa.prototype.start = function(a){
	if(a=='sup') this.gif_sup=false;
	if(a=='inf') this.gif_inf=false;
	if(a=='dret') this.gif_dret=false;
	if(a=='esq') this.gif_esq=false;
	var imatge ="../images/mapa/"+a+".gif" ;	
	
	if (document.layers)  eval('this.'+a+'.doc.images["'+a+'"].src = "'+imatge+'"'); 
	else document.images[a].src =imatge;			
	}



function getLanguage () {
	var ruta = '' + document.location;
	var indexarxiu = ruta.lastIndexOf("/")+1;
	var indexidioma = ruta.substring(0,indexarxiu-1).lastIndexOf("/")+1;
	var IdiomaActual= ruta.substring(indexidioma,indexarxiu-1);

	return IdiomaActual;
}

function getText(text) {
	return text[getLanguage()];
}




