/*   dw_banners.js   version date: April 2005  */

/*************************************************************************
  This code is from Dynamic Web Coding at dyn-web.com
  Copyright 2001-5 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

dw_Banner.restartDelay = 500; // delay onmouseout to restart rotation
dw_Banners = {}; // globally accessible reference to object 
dw_Banner.idList = []; // numerically indexed list for removing event handlers onunload 

function dw_Banner(id, delay, bMouse) {
    this.id = id; this.delay = delay; this.items = []; this.ctr = 0; this.timer = null;
    dw_Banners[this.id] = this; this.animString = "dw_Banners." + this.id;
    if (bMouse) dw_Banner.setMouseEvents(this.id); // set up pause/resume onmouseover/out
}

dw_Banner.prototype.addItem = function(sHtml) {
 	this.items[this.items.length] = sHtml;
}

dw_Banner.prototype.rotate = function() {
    clearTimeout(this.timer); this.timer = null;
    var el = document.getElementById(this.id);
    if ( el && typeof el.innerHTML != "undefined" ) {
        el.innerHTML = this.items[this.ctr];
        if (this.ctr < this.items.length-1) this.ctr++;
        else this.ctr = 0;
        this.timer = setTimeout(this.animString + ".rotate()", this.delay);
    }
}

dw_Banner.setMouseEvents = function(id) {
    var el = document.getElementById(id);
    if (el) {
        el.onmouseover = dw_Banner.pause;
        el.onmouseout = dw_Banner.resume;
        dw_Banner.idList[dw_Banner.idList.length] = id;   
    }
}

// these 2 functions called onmouseover/out of banner el
dw_Banner.pause = function() { 
    var curObj = dw_Banners[this.id];
    if (curObj) { clearTimeout(curObj.timer); curObj.timer = null; }
}
 
dw_Banner.resume = function(e) {
    e = e? e: window.event;
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( this != toEl && !dw_contained(toEl, this) ) {
        var curObj = dw_Banners[this.id];
        if (curObj)
            curObj.timer = setTimeout(curObj.animString + ".rotate()", dw_Banner.restartDelay);
    }
} 

// avoid memory leak (tiny in code like this, but anyway ...)
dw_Banner.unHook = function() {
    var id, el;
    for (var i=0; id = dw_Banner.idList[i]; i++) {
        el = document.getElementById(id);
        if (el) { 
            el.onmouseover = null;
            el.onmouseout = null;
            el = null;
        }
    }
}

if ( window.addEventListener ) window.addEventListener( "unload", dw_Banner.unHook, true);
else if ( window.attachEvent ) window.attachEvent( "onunload", dw_Banner.unHook );

// returns true of oNode is contained by oCont (container)
function dw_contained(oNode, oCont) {
    if (!oNode) return; // in case alt-tab away while hovering (prevent error)
    while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
    return false;
}

var imageHandler = { 
    path:"", imgs:[], preload:function() { for(var i=0;arguments[i];i++) {
    var img=new Image(); img.src=this.path+arguments[i]; this.imgs[this.imgs.length]=img;}}
}
