// animation object holds numerous properties related to motion
var anime;

// initialize default anime object
function comienzo(num) {
	anime = new Array();
	altura = 0;
	alt = 7;
	for (i=1;i<=num;i++) {
		elID = "despacho"+i;
		document.getElementById(elID).style.top = alt+"px";	
		if (i==1) {
			alt += $("div#despacho"+i).height();
		}
		else {
			alt += 24;
		}
	}
	alt = $("div#despacho1").height();
	alt_tot = 7 + alt + 24*(num-1);
	$("div#despachos_cont").height(alt_tot);	
	document.getElementById('despachos_cont').style.overflow = "hidden";
}

function initAnime(num) {
    anime[num] = {elemID:"", 
             xCurr:0, 
             yCurr:0, 
             xTarg:0, 
             yTarg:0, 
             xStep:0, 
             yStep:0,
             xDelta:0,
             yDelta:0,
             xTravel:0,
             yTravel:0,
             vel:1, 
             pathLen:1, 
             interval:null
            };
}

function mueve(num, tot) {
	altura = 0;
	alt = 7;
	alt = $("div#despacho"+num).height();
	alt_tot = 7 + alt + 24*(tot-1);
	$("div#despachos_cont").height(alt_tot);	
	document.getElementById('despachos_cont').style.overflow = "hidden";

	alt = $("div#despacho"+num).height();
	for (i=1;i<=tot;i++){
		if (i <= num) {
			posY = 7 + (i-1)*24;	
		}
		else {
			posY = 7 + alt + (i-2)*24;
		}
		initSLAnime(i,'despacho'+i, posY, 10);
	}
}

// stuff animation object with necessary explicit and calculated values
function initSLAnime(num,elemID, endY, speed) {
    initAnime(num);
    anime[num].elemID = elemID;
	startY = parseInt(document.getElementById(anime[num].elemID).style.top);
    anime[num].yCurr = startY;
    anime[num].yTarg = endY;
    anime[num].yDelta = Math.abs(endY - startY);
    anime[num].vel = (speed) ? speed : 1;
    // set element's start position
    document.getElementById(elemID).style.top = startY + "px";
    // the length of the line between start and end points
    anime[num].pathLen = Math.sqrt((Math.pow((startY - endY), 2)));
    // how big the pixel steps are along each axis
    anime[num].yStep = parseInt(((anime[num].yTarg - anime[num].yCurr) / anime[num].pathLen) * anime[num].vel);
    // start the repeated invocation of the animation
    anime[num].interval = setInterval("doSLAnimation("+num+")", 10);
}

// calculate next steps and assign to style properties
function doSLAnimation(num) {
//	alert(anime[num].yTravel + anime[num].yStep);
//	alert(anime[num].yDelta);
    if ((anime[num].yTravel + anime[num].yStep) <= anime[num].yDelta - 10) {
        var y = anime[num].yCurr + anime[num].yStep;
        document.getElementById(anime[num].elemID).style.top = y + "px";
        anime[num].yTravel += Math.abs(anime[num].yStep);
        anime[num].yCurr = y;
    } else {
        document.getElementById(anime[num].elemID).style.top = anime[num].yTarg + "px";
        clearInterval(anime[num].interval);
    }
}
