/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact.

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/

window.addEventListener?window.addEventListener("load",fx_init,false):window.attachEvent("onload",fx_init);

var d=document, imgs = new Array(), zInterval = 5000, current=0, pause=false, fadeRate=40;
var lastObj;
n = (document.layers) ? 1:0;
ie = (document.all) ? 1:0;

function fx_init() {

  if(!d.getElementById || !d.createElement)return;
  //imgs = d.getElementById("imageContainer").getElementsByTagName("img");
  imgs = d.getElementById("imageContainer").getElementsByTagName("div");
  if(imgs.length!=0)
  {
    for(i=0;i<imgs.length;i++) imgs[i].xOpacity = 0;
    current = 0; //Math.round((imgs.length-1)*Math.random())
    //removed the random feature since we randomize in php
    imgs[current].style.display = "block";
    imgs[current].xOpacity = .99;
    if (imgs.length == 1)
    {
      setOpacity(imgs[current]);
    }
    else
    {
      setTimeout(fx_crossFade,zInterval);
    }
  }
}

function fx_crossFade() {
  var nIndex;
  cOpacity = imgs[current].xOpacity;
  nIndex = imgs[current+1]?current+1:0;
  nOpacity = imgs[nIndex].xOpacity;

  cOpacity-=.05;
  nOpacity+=.05;

  imgs[current].style.zindex = -1;
  imgs[nIndex].style.zindex = 1;
  imgs[nIndex].style.display = "block";
  imgs[current].xOpacity = cOpacity;
  imgs[nIndex].xOpacity = nOpacity;

  if (nIndex ==0 ) {setOpacity(imgs[current]);}
  setOpacity(imgs[nIndex]);

  if(cOpacity<=0) {

    setOpacity(imgs[current]);
    imgs[current].style.display = "none";
    current = nIndex;
    setTimeout(fx_crossFade,zInterval);
    //alert('current = '+ current);
  } else {

    setTimeout(fx_crossFade,fadeRate);

  }
}

function setOpacity(obj) {
  if(obj.xOpacity>.99) {
    obj.xOpacity = .99;
  }
  obj.style.opacity = obj.xOpacity;
  obj.style.MozOpacity = obj.xOpacity;
  obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

function fx_fadeObjInInit(pObj, pMaxFade, pFadeRate, pShowLast)
{
  if (!pObj) return;
  if (lastObj== pObj)  return ;

  if (lastObj!=null && !pShowLast) { fx_hide(lastObj.id); }

  obj = pObj;
  obj.style.display = "block";
  obj.xOpacity = 0.0;
  obj.xCancelled=false;
  lastObj = obj;
  setOpacity(obj);
  fx_fadeObjIn(obj, pMaxFade, pFadeRate);
}

function fx_fadeObjIn(pObj, pMaxFade, pFadeRate, pRemoveFilter)
{
  if (!pObj) return;

  obj = pObj;
  if (!obj.xCancelled)
  {
    cOpacity = obj.xOpacity;
    cOpacity+=.10;
    obj.style.display = "block";
    obj.xOpacity = cOpacity;
    setOpacity(obj);
    if(cOpacity<pMaxFade) {
      setTimeout(create_fx_fadeObjIn(obj, pMaxFade, pFadeRate),pFadeRate);
    }
    else
    {
      obj.xCancelled = false;
      if (arguments.length<4)
      {
        pRemoveFilter = true;
      }

      if (pRemoveFilter)
      {
        obj.style.removeAttribute("filter");
      }
    }
  }
}

function create_fx_fadeObjIn(pObj, pMaxFade, pFadeRate)
{
  var lObj = pObj;
  var lMaxFade = pMaxFade;
  var lFadeRate = pFadeRate;
  pObj=null;
  pMaxFade=null;
  pFadeRate=null;
  return (function(){ fx_fadeObjIn(lObj, lMaxFade, lFadeRate)});

}


function fx_fadeObjOutInit(pObj, pMaxFade, pFadeRate, pShowLast)
{
  if (!pObj) return;

  obj = pObj;
  obj.xCancelled=false;
  lastObj = obj;
  setOpacity(obj);
  fx_fadeObjOut(obj, pMaxFade, pFadeRate);
}

function fx_fadeObjOut(pObj, pMaxFade, pFadeRate)
{
  if (!pObj) return;

  obj = pObj;
  if (!obj.xCancelled)
  {
    cOpacity = obj.xOpacity;
    cOpacity-=.05;
    obj.xOpacity = cOpacity;
    setOpacity(obj);
    if(cOpacity>pMaxFade) {
      setTimeout(create_fx_fadeObjOut(obj, pMaxFade, pFadeRate),pFadeRate);
    }
    else
    {
      obj.xCancelled = false;
      obj.style.display = "none";
      obj.xOpacity = 0.0;
    }
  }
}

function create_fx_fadeObjOut(pObj, pMaxFade, pFadeRate)
{
  var lObj = pObj;
  var lMaxFade = pMaxFade;
  var lFadeRate = pFadeRate;
  pObj=null;
  pMaxFade=null;
  pFadeRate=null;
  return (function(){ fx_fadeObjOut(lObj, lMaxFade, lFadeRate)});

}


function fx_show(pId)
{
  lObj = document.getElementById(pId);
  if (lObj)
  {
    lObj.style.display="block";
    lObj.xOpacity=0.99;
    lObj.xCancelled=false;
  }
}

function fx_hide(pId)
{
  lObj = document.getElementById(pId);
  if (lObj)
  {
    lObj.xOpacity=0.00;
    setOpacity(obj);
    lObj.style.display="none";
    lObj.xCancelled=true;
  }
  lastObj = null;
}


//--------------------------------------------------------------------SLIDE

function fx_slide(which, pX, pY, pSteps, pInterval)
{
    slideoutNew = which;
    slideoutNew.xpos = (!slideoutNew.xpos ? parseInt(slideoutNew.style.marginLeft) : slideoutNew.xpos);
    slideoutNew.ypos = (!slideoutNew.ypos ? parseInt(slideoutNew.style.marginTop) : slideoutNew.ypos);
    slideoutNew.xposOriginal = slideoutNew.xpos;
    slideoutNew.xpos =pX;
    slideoutNew.yposOriginal = slideoutNew.ypos;
    slideoutNew.ypos =pY;

    slideObject(slideoutNew, pSteps, pInterval);
}

function slideObject(pObj, pSteps, pInterval)
{

  var doTimeout = false;
  var increment;
  var steps = pSteps;
  if (Math.abs(pObj.xpos - parseInt(pObj.style.marginLeft)) >steps )
  {
    increment= (pObj.xpos > pObj.xposOriginal ? 1 : -1)*steps;
    pObj.style.marginLeft = (parseInt(pObj.style.marginLeft)+increment) +'px';
    doTimeout = true;
  }
  if (Math.abs(pObj.ypos - parseInt(pObj.style.marginTop)) >steps )
  {
    increment = (pObj.ypos > pObj.yposOriginal ? 1 : -1)*steps;
    pObj.style.marginTop = (parseInt(pObj.style.marginTop)+increment) +'px';
    doTimeout = true;
  }

  if (doTimeout)
  {
    setTimeout(create_slideObject(pObj, steps, pInterval),pInterval);
  }
}


function create_slideObject(pObj, pSteps, pInterval)
{
  var lObj = pObj;
  var lSteps = pSteps;
  var lInterval = pInterval;
  pObj=null;
  pSteps = null;
  pInterval = null;
  return (function(){ slideObject(lObj, lSteps, lInterval)});
}


//--------------------------------------------------------------------RESIZE


function fx_resize(which, pWidth, pHeight, pSteps, pInterval)
{
    slideoutNew = which;
    slideoutNew.x_width = (!slideoutNew.x_width ? parseInt(slideoutNew.style.width) : slideoutNew.x_width);
    slideoutNew.x_height = (!slideoutNew.x_height ? parseInt(slideoutNew.style.height) : slideoutNew.x_height);
    slideoutNew.x_widthOriginal = slideoutNew.x_width;
    slideoutNew.x_width =pWidth;
    slideoutNew.x_heightOriginal = slideoutNew.x_height;
    slideoutNew.x_height =pHeight;
    resizeObject(slideoutNew, pSteps, pInterval);
}


function resizeObject(pObj, pSteps, pInterval)
{

  var doTimeout = false;
  var increment;
  var steps = pSteps;
  if (Math.abs(pObj.x_width - parseInt(pObj.style.width)) >steps )
  {
    increment= (pObj.x_width > pObj.x_widthOriginal ? 1 : -1)*steps;
    pObj.style.width = (parseInt(pObj.style.width)+increment) +'px';
    doTimeout = true;
  }
  else
  {
    pObj.style.width = pObj.x_width +'px';
  }

  if (Math.abs(pObj.x_height - parseInt(pObj.style.height)) >steps )
  {

    increment = (pObj.x_height > pObj.x_heightOriginal ? 1 : -1)*steps;
    pObj.style.height = (parseInt(pObj.style.height)+increment) +'px';
    doTimeout = true;
  }
  else
  {
    pObj.style.height = pObj.x_height +'px';
  }

  if (doTimeout)
  {
    setTimeout(create_resizeObject(pObj, steps, pInterval),pInterval);
  }
}


function create_resizeObject(pObj, pSteps, pInterval)
{
  var lObj = pObj;
  var lSteps = pSteps;
  var lInterval = pInterval;
  pObj=null;
  pSteps = null;
  pInterval = null;
  return (function(){ resizeObject(lObj, lSteps, lInterval)});
}