var slideInUse = new Array();

function ajaxFunction(file, title, objId)
{
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      document.getElementById(objId + "Title").innerHTML = title;
      document.getElementById(objId + "Body").innerHTML = xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET", file, true);
  xmlHttp.send(null);
}


//function slideTest(objId, options, file) {
function revealBox(objId, file, title, options) {
  //populate the div using AJAX
  if(typeof file != 'undefined') 
  { 
    ajaxFunction(file, title, objId);
  } 

  
  // start the slider
  this.obj = document.getElementById(objId + "Box");
  this.duration = 1;
  this.height = parseInt(this.obj.style.height);

  if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
  if(this.options.duration) { this.duration = this.options.duration; }
    
  // close the slider; hide
  this.up = function() {
    this.curHeight = this.height;
    this.newHeight = '1';
    if(slideInUse[objId] != true) {
      var finishTime = this.slide();
      window.setTimeout("revealBox('"+objId+"').finishup("+this.height+");",finishTime);
    }
  }
  
  // open the slider; show
  this.down = function() {
    this.newHeight = this.height;
    this.curHeight = '1';
    if(slideInUse[objId] != true) {
      this.obj.style.height = '1px';
      this.obj.style.display = 'block';
      this.slide();
    }
  }
  
  this.slide = function() {
    slideInUse[objId] = true;
    var frames = 30 * duration; // Running at 30 fps

    var tIncrement = (duration*1000) / frames;
    tIncrement = Math.round(tIncrement);
    var sIncrement = (this.curHeight-this.newHeight) / frames;

    var frameSizes = new Array();
    for(var i=0; i < frames; i++) {
      if(i < frames/2) {
        frameSizes[i] = (sIncrement * (i/frames))*4;
      } else {
        frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
      }
    }
    
var mainHeight = document.getElementById('wrap').style.height;
    
    for(var i=0; i < frames; i++) {
      this.curHeight = this.curHeight - frameSizes[i];
      window.setTimeout("document.getElementById('"+objId+"Box').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
    }
    
    window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
    
    if(this.options.onComplete) {
      window.setTimeout(this.options.onComplete, tIncrement * (i-2));
    }
    
    return tIncrement * i;
  }
  
  this.finishup = function(height) {
    this.obj.style.display = 'none';
    this.obj.style.height = height + 'px';
  }
  
  return this;
}
