//################################################################################
//#    crossfader - JavaScript element crossfader
//#    Copyright (C) 2005  Mike Kornelson
//#
//#    This program is free software; you can redistribute it and/or modify
//#    it under the terms of the GNU General Public License as published by
//#    the Free Software Foundation; either version 2 of the License, or
//#    (at your option) any later version.
//#
//#    This program is distributed in the hope that it will be useful,
//#    but WITHOUT ANY WARRANTY; without even the implied warranty of
//#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//#    GNU General Public License for more details.
//#
//#    You should have received a copy of the GNU General Public License
//#    along with this program; if not, write to the Free Software
//#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
//#
//#    You can find the GPL license here: http://www.gnu.org/copyleft/gpl.html
//################################################################################

//# -----------------------------------
//#  Instructions:
//#
//#    <todo>
//#
//# -----------------------------------
//#  Contact:
//#    Mike Kornelson
//#    mike@windmillcomputers.ca
//#    http://www.windmillcomputers.ca
//# -----------------------------------

//%%%%%%%%%% YOU DO NOT NEED TO EDIT THIS FILE %%%%%%%%%%%%

/*
	FADER CLASS
*/
function Fader(id, holdTime, RunOnLoad)
{
  //## CONSTRUCTOR ##
  var m_id = id;		//id of the element list
  var m_holdTime = holdTime;	//time the pictures stay solid, in MS

  //## VARIABLES ##
  var m_gallery;		//object reference to the list
  var m_galleryImages;		//child elements of the list
  var m_currentImage;		//which image should current be showing
  var m_previousImage;
  
  //## FUNCTION ASSIGNMENTS ##
  this.preInit = preInit;
  this.fadeInit = fadeInit;
  this.crossfade = crossfade;

  //## FUNCTION DEFINITIONS ##
  function preInit() {
    if ((document.getElementById) && (m_gallery=document.getElementById(m_id))) {
      m_gallery.style.visibility = "hidden";
    }
  }
  
  function fadeInit() {
    if (document.getElementById) {
      preInit();
      
      if (!detectIE()) fnCleanTree(m_gallery);
    
      m_galleryImages = m_gallery.childNodes;
      
		for(i=0;i<m_galleryImages.length;i++) {
			m_galleryImages[i].style.position = 'absolute';
			m_galleryImages[i].style.top = 0;
			m_galleryImages[i].style.zIndex = 0;
			fader(i,0);
		}
      
      m_gallery.style.visibility = 'visible';      
      m_currentImage = 0;
      m_previousImage = m_galleryImages.length-1;
      opacity = 100;
      fader(m_currentImage, 100);
      window.setTimeout(m_id+".crossfade(100)", m_holdTime);
      //window.setTimeout("crossfade(100)", faderTime);
    }
  }
  
  function fader(imageNumber, opacity) {
    var obj=m_galleryImages[imageNumber];
    if (obj.style.MozOpacity != null) {
      obj.style.MozOpacity = (opacity/100) - .001;
    } else if (obj.style.opacity != null) {
      obj.style.opacity = (opacity/100) - .001;
    } else if (obj.style.filter != null) {
      obj.style.filter = "alpha(opacity="+opacity+")";
    }
  }
  
  function crossfade(opacity) {
    if (opacity < 100) {
      fader(m_currentImage, opacity);
      opacity += 10;
      window.setTimeout(m_id+".crossfade("+opacity+")", 30);
      //window.setTimeout("crossfade("+opacity+")", 30);
    } else {
      fader(m_previousImage, 0);
      m_previousImage = m_currentImage;
      m_currentImage += 1;
      
      if (m_currentImage >= m_galleryImages.length) {
        m_currentImage = 0;
      }
      
      m_galleryImages[m_previousImage].style.zIndex = 0;
      m_galleryImages[m_currentImage].style.zIndex = 100;
      opacity = 0;
      window.setTimeout(m_id+".crossfade("+opacity+")", m_holdTime);
      //window.setTimeout("crossfade("+opacity+")", faderTime);
    }
  }

  // ## CONSTRUCTOR CALLS ##
  this.preInit();
  if (RunOnLoad == true) { addLoadEvent(this.fadeInit); }
  //fnAddEvent(window,'load',this.fadeInit);
}

/*
	SUPPORT FUNCTIONS
*/
/*
function fnAddEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) {
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  }
}
*/

function detectIE() {
  function checkIt(string) {
    place = navigator.userAgent.toLowerCase().indexOf(string) + 1;
    return place;
  }

  if (checkIt('msie')) return true;
  return false;
}

function fnCleanTree(node) {
  var notspace = /\S/;
  var i=0;
  var cNodes = node.childNodes
  var t;

  while ((t=cNodes.item(i++))) {
    switch (t.nodeType) {
      case 1: //Element node
        fnCleanTree(t);
        break;
      case 3: //Text node
        if (notspace.test(t.nodeValue)) break;
      case 8: //Comment node
        node.removeChild(t);
        i--;
    }
  }	
}