//border: solid #ff0000 3px;
	//document.getElementById("sc").style.borderColor = "#0000ff";
	//la ultima la obviamos
	var exceso=200;
	var nubestam=100;
	var contfondo=0;
	var paso=8;
	var ed=19;
	var pos=new Array(ed);
	var posfinal=new Array(ed);
	var tam=500;
	var alto=400;
	var separacion=60;
	//var cont=10;
	var altura=-45;
	var debug_on=false;
	var mouseX=40;
	var mouseY=60;
	
	//para el banner xzepeellin
	var zeppelin=true;
	var zepX=20;
	var zepY=15;
	var zepFondoX=-200;
	var zepFondoY=-150;
	var izqdrc=true;
	var posb2=60;
	
	
	function scroll(){
		document.getElementById("fondorasca").style.backgroundPosition = (contfondo)+"px "+(altura+alto-180)+"px";
		document.getElementById("nubes").style.backgroundPosition = ((contfondo*2))+"px "+(altura/4-10)+"px";
		contfondo=contfondo-2;
		moveZep();
		setTimeout("scroll()",100);
	}
	
	function edificar(){
		for (var i=0;i<=ed;i++){
			var alt=-alto*i;
			pos[i]=tam;
			posfinal[i]=0;
			document.getElementById("ed"+i).style.top=(alt-10)+"px";
			document.getElementById("ed"+i).style.left=(tam+10)+"px";
			//document.getElementById("ed"+i).style.border="solid 1px green";
			construye(i);
		}
	}
	
	function getNext(i){
		var x=0;
		if (i<ed){
			x=i+1;
		}else{
		}
		return x;
	}

	//scrool del div
	function step(){
		for (var i=0;i<=ed;i++){
			if (pos[i]<tam-separacion){
				//si estoy empezando a moverme
				if (pos[getNext(i)]>tam-separacion){
					//compruebo si el sigte esta en tam  si no lo pongo lo redifico
					if (pos[getNext(getNext(i))]!=tam){
						//Realimentación
						//alert("realimento");
						//alert("REALIM "+i +"- - >"+getNext(getNext(i)));
						reedificar(getNext(getNext(i)));
					}
				}
				//Si el anterior a pasado este pto comeiza a moverte
				pos[getNext(i)]=pos[getNext(i)]-paso;
			}
			if (i==0){
				//El primero tira de los demas
				pos[i]=pos[i]-paso;
				if (pos[i]<=0){
					//lo mantego hasta que sea realimentado
					pos[i]=0;
					//y mientras voy haciendo el scroll de su fondo
					stepback(i);
				}
			}else{
				if (pos[i]<=0){
					//lo mantego hasta que sea realimentado
					pos[i]=0;
					//y mientras voy haciendo el scroll de su fondo
					stepback(i);
				}
			}
			//ALTURA FONDO
			//document.getElementById("ed"+i).style.backgroundPosition = posfinal[i]+"px "+ (altura-40)+"px";
			//document.getElementById("ed"+(getNext(i))).style.backgroundPosition = posfinal[getNext(i)]+"px "+ (altura-40)+"px";
			
			document.getElementById("ed"+i).style.backgroundPosition = posfinal[i]+"px bottom";
			document.getElementById("ed"+(getNext(i))).style.backgroundPosition = posfinal[getNext(i)]+"px bottom";
			//document.getElementById("ed"+i).style.height=(altura+alto)+"px";
			//LINEAL
			/*document.getElementById("ed"+i).style.backgroundPosition = posfinal[i]+"px bottom";
			document.getElementById("ed"+getNext(i)).style.backgroundPosition = posfinal[getNext(i)]+"px bottom";*/
			
			//ALTURA DIV
			document.getElementById("ed"+i).style.top=(-alto*i)+(altura+40)+"px";
			document.getElementById("ed"+getNext(i)).style.top=(-alto*getNext(i))+(altura+40)+"px";
			//document.getElementById("ed"+i).style.marginBottom=(-alto*i)+altura+"px";
			//document.getElementById("ed"+getNext(i)).style.marginBottom=(-alto*getNext(i))+altura+"px";
			
			document.getElementById("ed"+i).style.left=(pos[i])+"px";
			document.getElementById("ed"+getNext(i)).style.left=(pos[getNext(i)])+"px";
			//document.getElementById("ed"+i).style.border="solid 1px green";
			//document.getElementById("ed"+i).style.marginTop=-i+"px";
			document.getElementById("txtdebug").value=pos+"\n"+posfinal;
		}
		setTimeout("step()",100);
	}
	
	function reedificar(i){
		pos[i]=tam;
		posfinal[i]=paso;
		construye(i);
	}
	
	function construye(i){
		document.getElementById("cargando").style.display="";
		var n=parseInt(Math.random() * 50160);
		//var n=getNumber();
		//var u="url('http://skyscraperpage.com/diagrams/images/"+n+".gif')";
		var u="url('rascas/rascacielos/"+n+".gif')";
		//document.getElementById("sgte").src="http://skyscraperpage.com/diagrams/images/"+n+".gif";
		//u="url('30160.gif')";
		document.getElementById("ed"+i).style.backgroundImage = u;
		document.getElementById("ed"+i).style.backgroundPosition = posfinal[i]+"px bottom";
		setTimeout('document.getElementById("cargando").style.display="none";',3000);
	}
	
	function delay(m){
		date = new Date();
		var curDate = null;
		do { 
			var curDate = new Date(); 
		}while(curDate-date < m);
} 
	
	function getNumber(){
		var n=parseInt(Math.random() * 50160);
		var ok=false;
		while (!ok){
			var u="http://skyscraperpage.com/diagrams/images/"+n+".gif";
			document.getElementById("sgte").src=u;
			delay(500);
			if (document.getElementById("sgte").width>50){
				ok=true;
			}else{
				n=parseInt(Math.random() * 50160);
			}
		}
		return n;
	}
	
	//scrool del bckground del div, no del div
	function stepback(num){
		//alert(posfinal[num]);
		if (pos[num]<=0)
		posfinal[num]=posfinal[num]-paso;
		//document.getElementById("ed"+num).style.backgroundPosition = posfinal[num]+"px "+(altura+40)+"px";
		//document.getElementById("ed"+num).style.left=posfinal[num]+"px";
	}
	
	function updateAxis(e){
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY){
			posx = e.pageX;
			posy = e.pageY;
		}else if (e.clientX || e.clientY){
			posx = e.clientX + document.body.scrollLeft;
			posy = e.clientY + document.body.scrollTop;
			}
		mouseX=posx;
		mouseY=posy;
		setValores();
	}
	
	
	function setValores(){
		if (mouseX<tam){
			paso=parseInt(mouseX/40);
			if (paso<2) {
				paso=2;
			}else{
				if (paso>8) {
					paso=8;
				}
			}
			altura=parseInt(mouseY-300/1.2);
			if (altura<=-35) altura=-35;
			if (altura>10) altura=10;
		}
	}
	
	function actualizaPanel(){
		var h=0;
		h=4-parseInt((altura/3)-(altura/2))+1;
		if (h<0){
		h=0;
		}
		var c="white";
		if (h<2){
			c="green";
		}else{
			if (h<4){
				c="orange";
			}else{
				c="red";
			}
		}
		for (var i=0;i<7;i++){
			if (h<i){
				document.getElementById("he"+i).style.backgroundColor="white";
			}else{
				document.getElementById("he"+i).style.backgroundColor=c;
			}
		}
		var p=0;
		p=paso-2;
		var c="white";
		if (p<2){
			c="green";
		}else{
			if (p<4){
				c="orange";
			}else{
				c="red";
			}
		}
		for (var i=0;i<7;i++){
			if (p<i){
				document.getElementById("sp"+i).style.backgroundColor="white";
			}else{
				document.getElementById("sp"+i).style.backgroundColor=c;
			}
		}
		
		setTimeout("actualizaPanel();",300);
	}
	
	function setText(n){
		for (var i=0;i<7;i++){
			if (i!=n){
				document.getElementById("txt"+i).style.display="none";
			}else{
				document.getElementById("txt"+i).style.display="";
			}
		}
	}
	
	function start(){
	document.getElementById("fondorasca").style.width=tam+"px";
	document.getElementById("marco").style.width=tam+"px";
	document.getElementById("nifty").style.width=(tam+20)+"px";
	document.getElementById("nifty").style.height=(alto+320)+"px";
	document.getElementById("fondorasca").style.height=(alto-8)+"px";
	document.getElementById("tapador1").style.left=(tam+10)+"px";
	document.getElementById("tapador1").style.width="10px";
	document.getElementById("tapador1").style.top=-(alto+87)+"px";
	document.getElementById("tapador3").style.width="248px";
	document.getElementById("tapador3").style.top=-(alto+alto+205)+"px";
	document.getElementById("tapador3").style.left=(tam+20)+"px";
	document.getElementById("tapador2").style.left="10px";
	//document.getElementById("tapador2").style.top=-((alto*2)+20+100)+"px";
	document.getElementById("tapador2").style.top=-(alto+alto+alto)+"px";
	document.getElementById("nubes").style.left="10px";
	document.getElementById("nubes").style.width=tam+"px";
	document.getElementById("nubes").style.height=nubestam+"px";
	document.getElementById("nubes").style.top=-(alto-10)+"px";
	//<div id="nubes" style="border:1px solid;position:relative;left:0px;top:-180px;width:400px;height:100px;
	document.getElementById("marco").onmousemove =  updateAxis;
	//document.getElementById("z3").onclick =  quitarZep;
	document.getElementById("zep").onclick =  quitarZep;
	//document.getElementById("cuerpo").addEventListener("click", quitarZep, false);
	document.getElementById("tapador1").style.height=(alto+100)+"px";
	for (var i=0;i<=ed;i++){
		document.getElementById("ed"+i).style.height=alto+"px";
		document.getElementById("ed"+i).style.width="264px";
	}
	
	
	//segun navegador
	if (isIE()){
		//document.getElementById("marco").style.display="none";
		document.getElementById("fondorasca").style.display="none";
		document.getElementById("nubes").style.display="none";
		document.getElementById("tapador4").style.display="none";
		document.getElementById("tapador1").style.display="none";
		document.getElementById("tapador3").style.top="-14px";
		//document.getElementById("capturas").style.left="-6px";
		document.getElementById("iebanner").style.display="";
		document.getElementById("zep").style.display="none";
		document.getElementById("z1").style.display="none";
		document.getElementById("z2").style.display="none";
	}else{
		scroll();
		edificar();
		setValores();
		actualizaPanel();
		setTimeout("step()",5000);
	}
	document.getElementById("cuerpo").style.display='';
}

function quitarZep(event){
	if (zepFondoX==0 && zepFondoY==0){
		zeppelin=false;
		//alert("Hacer Click y Quitar el zeppelin");
	}
}

function moveZep(){
	var paszep=1;
	var pasban=2;
	if (zeppelin){
		if (zepFondoX==0 && zepFondoY==0){
			if (document.getElementById("banner2").style.display!=""){
				document.getElementById("banner2").style.display="";
				document.getElementById("z1").style.display="";
				document.getElementById("z2").style.display="";
			}
			//Movimiento del div
			document.getElementById("zep").style.left = zepX+"px";
			document.getElementById("zep").style.top = zepY+"px";
			if (zepY<120){
				zepY=zepY+paszep;
				if (zepY>90){
					zepX=zepX+paszep;
				}
			}else{
				if (zepX>220){
					//Legamos al final movemos de new el background
					zepFondoX=zepFondoX+1;
				}else{
					zepX=zepX+paszep;
				}
			}
			if (izqdrc){
				if (posb2<113){
					posb2=posb2+pasban;
				}else{
					izqdrc=false;
				}
			}else{
				if (posb2>70){
					posb2=posb2-pasban;
				}else{
					izqdrc=true;
				}
			}
			//movimiento del banner en si
			document.getElementById("banner2").style.left=posb2+"px";
		}else{
			//Movimiento background
			if (document.getElementById("banner2").style.display==""){
				document.getElementById("banner2").style.display="none";
				document.getElementById("z1").style.display="none";
				document.getElementById("z2").style.display="none";
			}
			if (zepFondoX>0){
				zepFondoX=zepFondoX+(paszep*5);
				//zepFondoY=zepFondoY-2;
				document.getElementById("zep").style.backgroundPosition = zepFondoX+"px "+zepFondoY+"px";
				if (zepFondoX>900){
					zepX=20;
					zepY=15;
					zepFondoX=-200;
					zepFondoY=-150;
					document.getElementById("zep").style.left = zepX+"px";
					document.getElementById("zep").style.top = zepY+"px";
					document.getElementById("zep").style.backgroundPosition = zepFondoX+"px "+zepFondoY+"px";
				}
			}else{
			zepFondoY=zepFondoY+(paszep*3);
			zepFondoX=zepFondoX+(paszep*3);
			if (zepFondoY>0){
				zepFondoY=0;
			}
			if (zepFondoX>0){
				zepFondoX=0;
			}
			document.getElementById("zep").style.backgroundPosition = zepFondoX+"px "+zepFondoY+"px";
			}
		}
	}else{
		//quitamos el zep
		if (document.getElementById("banner2").style.display==""){
				document.getElementById("banner2").style.display="none";
				document.getElementById("z1").style.display="none";
				document.getElementById("z2").style.display="none";
		}
		if (zepY>15){
			zepY=zepY-paszep*2;
			document.getElementById("zep").style.top = zepY+"px";
		}else{
			zepFondoY=zepFondoY-paszep*2;
			document.getElementById("zep").style.backgroundPosition = zepFondoX+"px "+zepFondoY+"px";
			if (zepFondoY<-130){
				document.getElementById("zep").style.display="none";
			}
		}
	}
}

//TODO
/*
Precarga http://www.gamarod.com.ar/javascript/precarga_de_imagenes_con_javascript_rutina_javascript.asp
boton de pausa
HECHO quitar zeppelin con click
boton de captura de pantalla
*/


function isIE(){
	var version;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		var temp=navigator.appVersion.split("MSIE")
		version=parseFloat(temp[1])
	}

	if (version>0){
		return true;
		}else{
	return false;
	}
}
