//Timer y Cursor
var macacos = new Array();
var currentTick=0;
var xc=0;
var yc=0;
var dt=1;

function onTick() {
	currentTick++;
	for (x=0; x<macacos.length; x++) {
		macacos[x].doTick();
	}
	setTimeout('onTick()', dt);
}


//Objeto Macaco ------------------------------------------------
function Macaco(x0, y0, xi, xd, lap, imageId,id,title,url) {
	this.x0 = x0;
	this.y0 = y0;
	this.xi = xi;
	this.xd = xd;
	this.id=id;
	this.title=title;
	this.url=url;
	this.lap = lap;
	this.imageId = imageId;
	this.image = document.getElementById(imageId);	
	this.drawed=false;
	this.showed=false;
	this.go=false;
	this.nextTick=0;
	this.x = x0;
	this.y = y0;
	
	this.doTick = macaco_doTick;
	this.move = macaco_move;
	this.turn = macaco_turn;
	this.draw_bubble=draw_bubble;
	this.show_bubble=show_bubble;
	this.hide_bubble=hide_bubble;
	this.move_bubble = move_bubble;
	this.href=macaco_href;	
	this.image.style.left = x0;
	this.image.style.top = y0; 
}
function macaco_href(){
	if (this.go){
	window.location=this.url;
	}
}

function macaco_turn(value) {
	if (value==true) {
		this.image.style.display='block';
	} else {
		this.image.style.display="none";
	}
}

function macaco_move() {
	this.image.style.left=this.x;
	this.image.style.top=this.y;
	this.move_bubble(); 
}

function macaco_doTick() {
	if (this.nextTick<=currentTick) {
		//Ingresar aquí signfica que es turno.
		
		//Estalezco los límites y la dirección en función de la posición del curso.
		var dir;
		var minX;
		var maxX;
		if (this.x > xc) {
			minX = (xc > this.xi)?xc:this.xi;
			maxX = this.xd;
			dir = -1;
		} else if (this.x < xc) {
			minX = this.xi;
			maxX = (xc > this.xd)?this.xd:xc;
			dir = 1;
		} else {
			minX = this.xi;
			maxX = this.xd;
			dir = 0;
		}
		minX = this.xi;
		maxX = this.xd;
		//Compruebo si hay que hacer algo
		if ( (dir==-1 && this.x > minX) || (dir==1 && this.x < maxX)) {
			this.x = this.x + dir;
			if (dir==1) {
				this.nextTick = currentTick + Math.floor(10 /(maxX - minX) * (this.x-minX) + 1) +this.lap;
			} else {
				this.nextTick = currentTick + Math.floor(9/(minX - maxX) * (this.x-maxX) + 1) + this.lap;
			}
			this.move();
		}
	}
}

function draw_bubble(){
	var aux = document.getElementById("divmacaco"+this.id);
	if (!this.drawed){	
		aux.innerHTML="<table class='tableBubble' background='images/bubbleb.gif' id='tableBubble"+this.id+"'><tr><td>&nbsp;</td></tr><tr><td valign='top'><a href='_+this.url+_' class='bubbleTitle'>"+this.title+"</a></td></tr><tr><td>&nbsp;</td></tr></table>";
		this.drawed=true;
	}	
	this.go=true;	
	document.getElementById("macaco"+this.id).style.cursor="pointer";
			
}

function move_bubble(){
	if (this.drawed){
		var aux = document.getElementById("divmacaco"+this.id);
		aux.style.top=(this.y-90)+"px";
		aux.style.left=(this.x+80)+"px";
	}
}

function show_bubble(){
	if (this.drawed){
		clearTimeout(b);
		if (!this.showed){
			document.getElementById("divmacaco"+this.id).style.visibility='visible'; 
			this.showed=true;		
			var b = setTimeout('hideBubble('+this.id+')',2000);
		}	
	}
}

function hide_bubble(){ 
if (this.drawed){  
		document.getElementById("divmacaco"+this.id).style.visibility='hidden'; 
		this.showed=false;	
		clearTimeout(b);
}	
}
//Fin de objeto Macaco -----------------------------------------

function setMouseCoord(event) {
	xc=event.clientX;
	yc=event.clientY;
}
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y)
		curtop += obj.y;
	return curtop;
}		

function onLoad(event) {
		
	//document.getElementById('homeanimation').scrollTop=370;
	var i = document.getElementById('overlay');				
		
	var x0 = 160;
	var y0 = findPosY(i)-12;		

	//var y0 = 160;
	//var x0 = 335;
	
	var x = x0 + 10;
	var y = y0 - 13;
	macacos[0] = new Macaco(x, y, x-50, x+50, 5, 'macaco1',1,"tit y algo 1","http://www.google.com");
	macacos[0].turn(true);
	
	
	x= x0 + 70;
	y= y0 + 10;
	macacos[1] = new Macaco(x, y, x-100, x + 130, 0, 'macaco2',2,"Promociones y Eventos","http://www.google.com");
	macacos[1].turn(true);
	//macacos[1].draw_bubble();

	x= x0 + 250;
	y= y0 + 0;
	macacos[2] = new Macaco(x, y, x-50, x + 39, 3, 'macaco3',3,"tit y algo 3","http://www.google.com");
	macacos[2].turn(true);
	
	x= x0 + 570;
	y= y0 + 0; 
	macacos[3] = new Macaco(x, y, x-30, x + 27, 5, 'macaco4',4,"tit y algo 4","http://www.google.com");
	macacos[3].turn(true);

	
	var x = x0 - 90;
	var y = y0 + 375;
	macacos[4] = new Macaco(x, y, x, x+100, 0, 'macaco5',5,"tit y algo 5","http://www.google.com");
	macacos[4].turn(true);
	
	var x = x0 + 50;
	var y = y0 + 398;
	macacos[5] = new Macaco(x, y, x-30, x+30, 3, 'macaco6',6,"San Valentin","http://www.google.com");
	macacos[5].turn(true);
	//macacos[5].draw_bubble();
	
	var x = x0 + 210;
	var y = y0 + 376; 
	macacos[6] = new Macaco(x, y, x, x+50, 0, 'macaco7',7,"Camping y Exteriores","http://www.google.com");
	macacos[6].turn(true);
	//macacos[6].draw_bubble();
	
	var x = x0 + 300;
	var y = y0 + 374;
	macacos[7] = new Macaco(x, y, x-10, x+10, 0, 'macaco8',8,"tit y algo 8","http://www.google.com");
	macacos[7].turn(true);
	
	var x = x0 + 500;
	var y = y0 + 358;
	macacos[8] = new Macaco(x, y, x-70, x+70, 0, 'macaco9',9,"tit y algo 9","http://www.google.com");
	macacos[8].turn(true);
	
	var x = x0 + 350;
	var y = y0 + 398;
	macacos[9] = new Macaco(x, y, x-70, x+70, 0, 'macaco10',10,"Encomiendas","shopsite/tc/text.jsp?titleon=off&pictureon=on&contentid=3725&version=1&site=1&channel=trescruces");
	macacos[9].turn(true);
	macacos[9].draw_bubble();
	
	setTimeout('onTick()', dt);
}
function goHref(i){
	macacos[i-1].href();
}
function showBubble(i){
	macacos[i-1].show_bubble();
} 

function hideBubble(i){
	macacos[i-1].hide_bubble();
} 


//FUNCIONES DEL CAMBIO DE PLANTA
     
  var scrollTop=0;	
  var scrollTop2=0;
  var stop1 = false;
  var stop2 = false;
  var maxScroll=294;
  function move_down() {
	 /* for (x=0; x<4; x++) {
		macacos[x].turn(false);
		macacos[x].hide_bubble();
	  }*/
	  var aux = document.getElementById("homeanimation");
	  var aux2 = document.getElementById("overlay");
	  if (loadedMacacos){
   	  	scrollTop=scrollTop+5;
   	  }
   	  oldScroll = aux.scrollTop;
      aux.scrollTop =scrollTop ;
    
   		   aux2.style.top =scrollTop ;
  	  
      if (!stop1 && aux.scrollTop<maxScroll){     	
        	setTimeout("move_down()",5);         
      }else{
		reset();
	 /*   for (x=4; x<9; x++) {
		 macacos[x].turn(true);
	    }*/				
	  }			  
  }
  
  function move_up() {
	 /* for (x=4; x<9; x++) {
		macacos[x].turn(false);
		macacos[x].hide_bubble();
	  }*/
	  for (y=0;y<terminal.length;y++){
	  	document.getElementById(terminal[y]).style.visibility='hidden';
	  }
  
	var aux = document.getElementById("homeanimation");
    var aux2 = document.getElementById("overlay");
  	if (loadedMacacos){
  		scrollTop=scrollTop-5;
  	} 
  	if (scrollTop<0){
  		scrollTop=0;
  	}
  	oldScroll=aux.scrollTop;
    aux.scrollTop = scrollTop;
  
  	aux2.style.top = scrollTop;
  	
    if (!stop1 && scrollTop>0){
    setTimeout("move_up()",5);
    }else{
		reset();	
	   /* for (x=0; x<4; x++) {
		 	macacos[x].turn(true);
	    }	*/							
	}
  }
  function cancel(){
  	stop1 = true;
  }
  function reset(){
  	stop1 = false;
  }	
  
  //Bubbles
  
  var timeoutb=0;
  var b;	
  var terminal = new Array();
  terminal[0]="bubbledest";
  
  var h=69;
	var h2=149;
	var timeout=0;
	var ok = true;
	var ti=null;
	function AppearLR(h) {				
		if (ok){
		document.getElementById('tdOverlayTable').style.visibility='hidden';
		   var w = document.getElementById('overlay');	   
		   w.style.width=h;	   
		   h = h + 10;
		   if (h<149){
			 window.setTimeout('AppearLR('+h+')', 20);
		   }else{
			ok=false;   	
			document.getElementById('overlayTable').style.visibility='visible';
		
		   }
		}
	}
	
	function clearF(){
		timeout=0;
		clearTimeout(ti);
	}

	function startTime(){
		timeout=timeout+1;
		if (timeout>=3){
			DisAppearRL(139);			
			clearTimeout(h);
			timeout=0;
		}else{
			ti= setTimeout('startTime()',1000);
		}
	}
	
	function DisAppearRL(h2) {	 
	if (!ok){
	   var w = document.getElementById('overlay');	
	   document.getElementById('overlayTable').style.visibility='hidden';   
	   w.style.width=h2;	   
	   h2 = h2 - 10;
	   if (h2>=69){
	  	 window.setTimeout('DisAppearRL('+h2+')', 20);
	   }else{
	   	// reset();
		 ok=true;	
	 	 document.getElementById('tdOverlayTable').style.visibility='visible';
	   }
	}
	 
	}
	function bubble(top,left,id){
	    var aux = document.getElementById("m"+id);
		i = findPosY(aux);
		j = findPosX(aux);
		 if (document.getElementById(id).style.visibility!="visible"){	    
			document.getElementById(id).style.top=top;
			document.getElementById(id).style.left=j+left;	
			document.getElementById(id).style.visibility='visible';
		}		
	}
	function bubble_destinos(id){	
		var aux = document.getElementById("m"+id);
		i = findPosY(aux);
		j = findPosX(aux);
		var left = 360;
		if (navigator.appName!="Microsoft Internet Explorer"){
	    	left= left-260;
	    }
	    if (document.getElementById(id).style.visibility!="visible"){	   
			document.getElementById(id).style.top=100;
			document.getElementById(id).style.left=j+left;
			document.getElementById(id).style.visibility='visible';	
		}	
	}
	
	function disappear_bubble(id){	
		if (document.getElementById(id).style.visibility=="visible"){
				document.getElementById(id).style.visibility='hidden';
		}		
	}
	var loadedMacacos = false;
	function loadMacacos(){
	
		var i=1;
		var num=0;
		while (num<10){		
			if (document.getElementById("macaco"+i).complete){
				num++;
				i++;				
			}
		}
		loadedMacacos=true;
	}	

