var velocidadv = 15;
var velocidadh = 5;
var cuantospixelesy = 10;
var cuantospixelesx = 16;
var inicioy = -800;
var finy = 0;
var iniciox = 0;//Pixel donde comienza el movimiento
var finx = 288; //Pixel donde termina el movimiento
var posicionactualy = 0;

function alCargar(){
	this.capa=document.getElementById("tab");
	this.capa1=document.getElementById("LayBoton1");
	this.boton1=document.getElementById("boton1");
	this.boton1.onclick=function(){intervalo1('derecha1();');}
	this.posicionactualx1 = 16;

	this.capa2=document.getElementById("LayBoton2");
	this.boton2=document.getElementById("boton2");
	this.boton2.onclick=function(){intervalo2('derecha2();');}
	this.posicionactualx2 = 16;

	this.capa3=document.getElementById("LayBoton3");
	this.boton3=document.getElementById("boton3");
	this.boton3.onclick=function(){intervalo3('derecha3();');}
	this.posicionactualx3 = 16;
}
function intervalo(funcion){
	this.IntervalID= setInterval(funcion,velocidadv);
}
function intervalo1(funcion){
	this.IntervalID1= setInterval(funcion,velocidadh);
	if (this.ultimo_boton=="boton2" && this.ultima_funcion=="derecha2();"){
		this.IntervalID2= setInterval('izquierda2();',velocidadh)
		boton2.onclick=function(){intervalo2('derecha2();');}
	}else if (this.ultimo_boton=="boton3" && this.ultima_funcion=="derecha3();"){
		this.IntervalID3= setInterval('izquierda3();',velocidadh)
		boton3.onclick=function(){intervalo3('derecha3();');}
	}
	this.ultimo_boton="boton1";
	this.ultima_funcion=funcion;
}
function intervalo2(funcion){
	this.IntervalID2= setInterval(funcion,velocidadh);
	if (this.ultimo_boton=="boton1" && this.ultima_funcion=="derecha1();"){
		this.IntervalID1= setInterval('izquierda1();',velocidadh)
		boton1.onclick=function(){intervalo1('derecha1();');}
	}else if (this.ultimo_boton=="boton3" && this.ultima_funcion=="derecha3();"){
		this.IntervalID3= setInterval('izquierda3();',velocidadh)
		boton3.onclick=function(){intervalo3('derecha3();');}
	}
	this.ultimo_boton="boton2";
	this.ultima_funcion=funcion;
}
function intervalo3(funcion){
	this.IntervalID3= setInterval(funcion,velocidadh);
	if (this.ultimo_boton=="boton1" && this.ultima_funcion=="derecha1();"){
		this.IntervalID1= setInterval('izquierda1();',velocidadh)
		boton1.onclick=function(){intervalo1('derecha1();');}
	}else if (this.ultimo_boton=="boton2" && this.ultima_funcion=="derecha2();"){
		this.IntervalID2= setInterval('izquierda2();',velocidadh)
		boton2.onclick=function(){intervalo2('derecha2();');}
	}
	this.ultimo_boton="boton3";
	this.ultima_funcion=funcion;
}

function bajar(){
	posicionactualy+=cuantospixelesy;
	if (posicionactualy >= inicioy && posicionactualy <= finy){
		this.capa.style.top=posicionactualy+"px";
	}else{
//		boton.onclick=function(){intervalo('subir();');}
		clearInterval(this.IntervalID);
	}
}
function subir(){
	posicionactualy-=cuantospixelesy;
	if (posicionactualy >= inicioy && posicionactualy <= finy){
		this.capa.style.top=posicionactualy+"px";
	}else{
//		boton.onclick=function(){intervalo('bajar();');}
		clearInterval(this.IntervalID);
	}
}

function derecha1(){
	posicionactualx1+=cuantospixelesx;
	if (posicionactualx1 >= iniciox && posicionactualx1 <= finx){
		capa1.style.left=posicionactualx1+"px";
	}else{
		boton1.onclick=function(){intervalo1('izquierda1();');}
		clearInterval(this.IntervalID1);
	}
}
function izquierda1(){
	posicionactualx1-=cuantospixelesx;
	if (posicionactualx1 >= iniciox && posicionactualx1 <= finx){
		capa1.style.left=posicionactualx1+"px";
	}else{
		boton1.onclick=function(){intervalo1('derecha1();');}
		clearInterval(this.IntervalID1);
	}
}


function derecha2(){
	posicionactualx2+=cuantospixelesx;
	if (posicionactualx2 >= iniciox && posicionactualx2 <= finx){
		capa2.style.left=posicionactualx2+"px";
	}else{
		boton2.onclick=function(){intervalo2('izquierda2();');}
		clearInterval(this.IntervalID2);
	}
}
function izquierda2(){
	posicionactualx2-=cuantospixelesx;
	if (posicionactualx2 >= iniciox && posicionactualx2 <= finx){
		capa2.style.left=posicionactualx2+"px";
	}else{
		boton2.onclick=function(){intervalo2('derecha2();');}
		clearInterval(this.IntervalID2);
	}
}


function derecha3(){
	posicionactualx3+=cuantospixelesx;
	if (posicionactualx3 >= iniciox && posicionactualx3 <= finx){
		capa3.style.left=posicionactualx3+"px";
	}else{
		boton3.onclick=function(){intervalo3('izquierda3();');}
		clearInterval(this.IntervalID3);
	}
}
function izquierda3(){
	posicionactualx3-=cuantospixelesx;
	if (posicionactualx3 >= iniciox && posicionactualx3 <= finx){
		capa3.style.left=posicionactualx3+"px";
	}else{
		boton3.onclick=function(){intervalo3('derecha3();');}
		clearInterval(this.IntervalID3);
	}
}
