//This code was created by the fine folks at Switch On The Code - http://blog.paranoidferret.com
//This code can be used for any purpose

function animate(elementID, newLeft, newTop, newWidth,
      newHeight, time, callback, checkForCloseSame)
{
  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 cOpacity = el.style.opacity;
  
  var totalFrames = 1;
  if(time> 0)
    totalFrames = time/40;

  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;

  var fOpacity = 0;
  var nOpacity = 0;
  if(isPositiveInteger(fHeight)==true)
  {
    fadeIn = true;
    fadeOut = false;
    fOpacity = 1/totalFrames;
    nOpacity = 1;
  }
  else
  {
    fadeIn = false;
    fadeOut = true;
    fOpacity = -1/totalFrames;
    nOpacity = 0;
  }
	
  doFrame(elementID, cLeft, newLeft, fLeft,
      cTop, newTop, fTop, cWidth, newWidth, fWidth,
      cHeight, newHeight, fHeight, callback, 
	  cOpacity, nOpacity, fOpacity, checkForCloseSame);
}

function isPositiveInteger(val){
      if(val==null){return false;}
      if (val.length==0){return false;}
      for (var i = 0; i < val.length; i++) {
            var ch = val.charAt(i)
            if (ch < "0" || ch > "9") {
            return false
            }
      }
      return true;
}



function doFrame(eID, cLeft, nLeft, fLeft,
      cTop, nTop, fTop, cWidth, nWidth, fWidth,
      cHeight, nHeight, fHeight, callback, 
	  cOpacity, nOpacity, fOpacity, checkForCloseSame)
{
   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);
  cOpacity= moveSingleVal(cOpacity, nOpacity, fOpacity);
  
  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';
  el.style.opacity += fOpacity;
  el.style.filter = 'alpha(opacity = ' + (el.style.opacity*100) + ')';
  

  if(cLeft == nLeft && cTop == nTop && cHeight == nHeight && cWidth == nWidth)
  {
	el.style.opacity = isPositiveInteger(fOpacity) == true ? '1' : '0';
	el.style.filter = 'alpha(opacity = ' + (isPositiveInteger(fOpacity) == true ? '100' : '0') + ')';
	if (checkForCloseSame==true)
	{
	 if (EsameContentClick==true)
	  {
		EKillFireEvents=true;
	  }
	}
    if(callback != null)
      callback();
    return;
  }
   
  setTimeout( 'doFrame("'+eID+'",'+cLeft+','+nLeft+','+fLeft+','
    +cTop+','+nTop+','+fTop+','+cWidth+','+nWidth+','+fWidth+','
    +cHeight+','+nHeight+','+fHeight+','+callback+','+cOpacity+',' 
	+nOpacity+','+fOpacity+','+checkForCloseSame+')', 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;
}
