/**
* @author J.S. van Zanden <svz@yes-co.nl> 
* @version 1
* @version $Id: objectRotator.js,v 1.5 2007/04/20 12:54:26 stefanz Exp $
* @description:  This script adds the ability to fade out a object for another.
* 		 Using this script you are required to call "or_startRotating();" after the include.
* @currentNotes: This script does not yet allow configuration of fade in or fade out effects,
*		 could be a nice feature for the future.
**/

// BEGIN configuration
  orc_objectBlocks = new Array();
  orc_useBlock = 1;
  orc_currentDisplayedBlock = 'objectRotatorCurrentContainer';
  orc_nextDisplayedBlock = 'objectRotatorNextContainer';
  orc_rotateItemsBlock = 'objectRotatorItemsContainer';
  orc_timeToGoNext = 4; 
  orc_timeSpanToFade = 3;
// END configuration

// BEGIN Configuration functions
function or_createCurrentAndNextContainers() {
  html = '<div id="' + orc_currentDisplayedBlock + '" style="position:absolute; z-index: 99;"></div>';
  html += '<div id="' + orc_nextDisplayedBlock + '" style="position:absolute; z-index: 98;"></div>';
  document.getElementById(orc_rotateItemsBlock).innerHTML = html;
}

function or_initializeConfig() {
  orc_numRotateItems = or_getNumRotateItems(); 
  or_getRotateItemBlocks(); 
  or_createCurrentAndNextContainers();
}

function or_initializeFirstCall() {
   or_setContainer(orc_currentDisplayedBlock, orc_useBlock-1);    
   or_setContainer(orc_nextDisplayedBlock, orc_useBlock);
   orc_useBlock += 2;

   if (orc_useBlock > orc_numRotateItems) {
      orc_useBlock = 1;
   }  
}

function or_getNumRotateItems() {
  items = document.getElementById(orc_rotateItemsBlock).childNodes;
  numItems = items.length;
  numRotateItems = 0;
  for (i = 0; i < numItems; i++) {
    if (items[i].nodeName.toLowerCase() == "div") {
      numRotateItems++;
    }   
  }
  return numRotateItems;
}

function or_getRotateItemBlocks() {
  items = document.getElementById(orc_rotateItemsBlock).childNodes;
  numItems = items.length;
  rotateItem = 0;
  for (i = 0; i < numItems; i++) {
    if (items[i].nodeName.toLowerCase() == "div") { 
      itemAttributes = items[i].attributes;
      newContainer = "<div ";
      for(j = 0; j < itemAttributes.length; j++) {
	      if(itemAttributes[j].nodeValue != null && itemAttributes[j].nodeValue != '') {
	        newContainer += itemAttributes[j].nodeName + "=\"" + itemAttributes[j].nodeValue + "\" ";
  	    }
      }
      
      newContainer += ">";
      newContainer += items[i].innerHTML; // outerHTML is nicer to use, but Firefox does not support this.
      newContainer += "<\/div>";
      orc_objectBlocks[rotateItem] = newContainer;
      rotateItem++;
    }  
  }   
}

function or_setRotateItemsBlock(rotateItemsBlock)
{
  orc_rotateItemsBlock = rotateItemsBlock;
}

function or_setTimeToGoNext(time)
{
  orc_timeToGoNext = time;
}
// END Configuration functions

// Main call of the imageRotater
function or_startRotating() {
  or_initializeConfig();
  or_initializeFirstCall();
  if(1 < orc_numRotateItems)
  {
    setTimeout('or_fadeOut(\'' + orc_currentDisplayedBlock + '\')', (orc_timeToGoNext * 1000)); 
  }
}

// By Stefan on 02-05-2007: This function sets the opacity for all browsers.
function or_setOpacity(id, opacity) {
  if (opacity == 10) {
    document.getElementById(id).style.display = 'block';
  }
  document.getElementById(id).style.opacity = opacity / 10; // Call for Mozilla
  document.getElementById(id).style.filter = 'alpha(opacity=' + opacity * 10 + ')'; // Call for IE
  if (opacity == 0) {
    document.getElementById(id).style.display = 'none';
  }
}

function or_setContainer(containerId, blockIndex) {
  document.getElementById(containerId).innerHTML = orc_objectBlocks[blockIndex];
  document.getElementById(containerId).innerHTML += "<div class=\"clear\" style=\"clear: both;\" />";	
}

function or_fadeIn(id) {
  for (var i = 0; i < 11; i++) {
    setTimeout('or_setOpacity(\'' + id + '\', '+i+')' , 100 * i); // 100
  }
  return false;
}

function or_fadeOut(id) {
  j = 10;
  for (var i = 0; i < 11; i++) { 
    setTimeout('or_setOpacity(\'' + id + '\', '+(i+j)+')' , 100 * i);  // 100
    j-=2;
  }
  
  setTimeout('or_goNext(true)', (orc_timeToGoNext * 1000));  
  
  return false;
}

function or_switchIndex() {
  tempIndex = document.getElementById(orc_currentDisplayedBlock).style.zIndex;
  document.getElementById(orc_currentDisplayedBlock).style.zIndex = document.getElementById(orc_nextDisplayedBlock).style.zIndex;
  document.getElementById(orc_nextDisplayedBlock).style.zIndex = tempIndex;
} 

function or_switchBlocks() {
  tempCurrentDisplayedBlock = orc_currentDisplayedBlock;
  orc_currentDisplayedBlock = orc_nextDisplayedBlock;
  orc_nextDisplayedBlock = tempCurrentDisplayedBlock;
}

function or_goNext(firstCall) {
  or_setOpacity(orc_nextDisplayedBlock, 10);
  or_setOpacity(orc_currentDisplayedBlock, 10);
  or_switchBlocks(); 
  or_switchIndex();
  or_setContainer(orc_nextDisplayedBlock, orc_useBlock-1);    
  if (orc_useBlock == orc_numRotateItems) {
    orc_useBlock = 1;
  }
  else {
    orc_useBlock++;
  }
  setTimeout('or_fadeOut(\'' + orc_currentDisplayedBlock + '\')', 900);
  
}
