// JavaScript Document
var slideElement = null;
var pos_x=0;
var pos_y=0;
var sl_wi = 0;
var sl_he = 0;
var sp=150;

function slide(elementId,p_x,p_y,wi,he,clas)
{
	pos_x=parseInt(p_x);
   	pos_y=((w_size.scrollSize.x/2)-parseInt(p_y));
	sl_wi = parseInt(wi);
	sl_he = parseInt(he);
   	slideElement = document.getElementById(elementId);
  	if (clas=='out')
   	{
      	slideUpStep1();
		slideElement.height = 0;
   	}
   	else if (clas=='in')
   	{
		slideDownStep1();
      	slideElement.height = sl_he;
   	}
}

function slideUpStep1()
{
   animate(slideElement.id, pos_x, pos_y, sl_wi, 0, sp, null);
}

function slideUpStep2()
{
   animate(slideElement.id, pos_x, pos_y, 0, 0, sp, null);
}

function slideDownStep1()
{
   animate(slideElement.id, pos_x, pos_y, sl_wi, sl_he, sp, null);
}

function slideDownStep2()
{
   animate(slideElement.id, pos_x, pos_y, sl_wi, sl_he, sp, null);
}

function animate(elementID, newTop, newLeft, newWidth, newHeight, time, callback)
{
  var el = document.getElementById(elementID);
  if(el == null)
    return;
 
  var cLeft = parseInt(el.style.left);
  var cTop = parseInt(el.style.top);
  var cWidth = parseInt(el.style.width);
  var cHeight = parseInt(el.style.height);
  
  var totalFrames = 1;
  if(time > 0)
    totalFrames = time/100;

  var fLeft = newLeft - cLeft;
  if(fLeft != 0)
    fLeft /= totalFrames;
  
  var fTop = newTop - cTop;
  if(fTop != 0)
    fTop /= totalFrames;
  
  var fWidth = newWidth - cWidth;
  if(fWidth != 0)
    fWidth /= totalFrames;
  
  var fHeight = newHeight - cHeight;
  if(fHeight != 0)
    fHeight /= totalFrames;
    
  doFrame(elementID, cLeft, newLeft, fLeft, cTop, newTop, fTop, 
      cWidth, newWidth, fWidth, cHeight, newHeight, fHeight, callback);
}

function doFrame(eID, cLeft, nLeft, fLeft, cTop, nTop, fTop, 
    cWidth, nWidth, fWidth, cHeight, nHeight, fHeight, callback)
{
  var el = document.getElementById(eID);
  if(el == null)
    return;

  cLeft = moveSingleVal(cLeft, nLeft, fLeft);
  cTop = moveSingleVal(cTop, nTop, fTop);
  cWidth = moveSingleVal(cWidth, nWidth, fWidth);
  cHeight = moveSingleVal(cHeight, nHeight, fHeight);

  el.style.left = Math.round(cLeft) + 'px';
  el.style.top = Math.round(cTop) + 'px';
  el.style.width = Math.round(cWidth) + 'px';
  el.style.height = Math.round(cHeight) + 'px';
  
  if(cLeft == nLeft && cTop == nTop && cHeight == nHeight 
    && cWidth == nWidth)
  {
    if(callback != null)
      callback();
    return;
  }
    
  setTimeout( 'doFrame("'+eID+'",'+cLeft+','+nLeft+','+fLeft+','+cTop+','
      +nTop+','+fTop+','+cWidth+','+nWidth+','+fWidth+','+cHeight+','
      +nHeight+','+fHeight+','+callback+')', 40);
}

function moveSingleVal(currentVal, finalVal, frameAmt)
{
  if(frameAmt == 0 || currentVal == finalVal)
    return finalVal;
  
  currentVal += frameAmt;
  if((frameAmt > 0 && currentVal >= finalVal) 
      || (frameAmt < 0 && currentVal <= finalVal))
  {
    return finalVal;
  }
  return currentVal;
}
