 /*
 * Copyright (c) 1997-2009, Ron Lussier. All rights reserved.
 *
 * This software is an unpublished work subject to a confidentiality agreement
 * and protected by copyright and trade secret law. Unauthorized copying,
 * redistribution or other use of this work is prohibited. All copies must
 * retain this copyright notice. Any use or exploitation of this work without
 * authorization could subject the perpetrator to criminal and civil liability.
 *
 * The above copyright notice does not indicate actual or intended publication
 * of this source code.
 */

/*---------------------------------------------------------------------------------------------------------
 * This file contains code allowing rollovers 
 *
 * This file requires the following Earthenberm files to be included:
 *      eb-common.js
 *		eb-dom.js
 *      eb-effects.js
 *---------------------------------------------------------------------------------------------------------*/

/*
 * Functions allowing rollover & mousedown effects for any <img> or <input type='image'>
 * on a page.
 *
 * Use is simple:
 *
 * Rollover image:
 *      <img src="original.jpg" srchover="over.jpg" />
 *
 * Rollover image & mousedown image:
 *      <img src="original.jpg" srchover="over.jpg" srcclick="down.jpg" />
 *
 * Also works for submit buttons:
 *      <input type="image" name="submitit"
 *             src="original.jpg" srchover="over.jpg" srcclick="down.jpg" border="0" />
 */

if (typeof EB == "undefined") {
	/**
	 * The EARTHENBERM global namespace object.
	 */
	var EB = {};
	}

if (typeof EB.rollover == "undefined") {
	/**
	 * The EARTHENBERM rollover namespace object.
	 */
	EB.rollover = {};
	}


/*---------------------------------------------------------------------------------------------------------
 *	UTILITY CLASS
 *---------------------------------------------------------------------------------------------------------*/

EB.rollover.imgHolderClass = function() {
	this.hover = new Array();
	this.click = new Array();
	this.src = new Array();

	function store( src, srcClick, srcHover ) {
		var		arrayLen = this.src.length;

		if ((src != null) && (src.length > 0)) {
			this.src[arrayLen] = new Image();
			this.src[arrayLen].src = src;
		}
		else {
			this.src[arrayLen] = null;
		}

		if ((srcHover != null) && (srcHover.length > 0)) {
			this.hover[arrayLen] = new Image();
			this.hover[arrayLen].src = srcHover;
		}
		else {
			this.nover[arrayLen] = null;
		}

		if ((srcClick != null) && (srcClick.length > 0)) {
			this.click[arrayLen] = new Image();
			this.click[arrayLen].src = srcClick;
		}
		else {
			this.click[arrayLen] = null;
		}
	}

	this.store = store;
}

/*---------------------------------------------------------------------------------------------------------
 *	PACKAGE VARIABLES
 *---------------------------------------------------------------------------------------------------------*/

EB.rollover.imgHolder = new EB.rollover.imgHolderClass();


/*---------------------------------------------------------------------------------------------------------
 *	PACKAGE FUNCTIONS
 *---------------------------------------------------------------------------------------------------------*/

EB.rollover.preloader = function( tagList ) {
	for (i = 0; i < tagList.length; i++) {
		if (tagList[i].getAttribute('srchover') || tagList[i].getAttribute('srcclick')) {
			EB.rollover.storeImgs( tagList[i] );

			/* Initialize the event handler(s) for the button */

			var		checker = '';

			checker = (tagList[i].getAttribute('srchover')) ? checker+'A' : checker+'';
			checker = (tagList[i].getAttribute('srcclick')) ? checker+'B' : checker+'';
			
			switch(checker) {
				case 'A':
					EB.rollover.mouseover( tagList[i] );
					EB.rollover.mouseout( tagList[i] );
					break;

				case 'B':
					EB.rollover.mousedown( tagList[i] );
					EB.rollover.mouseup2( tagList[i] );
					break;

				case 'AB':
					EB.rollover.mouseover( tagList[i] );
					EB.rollover.mouseout( tagList[i] );
					EB.rollover.mousedown( tagList[i] );
					EB.rollover.mouseup( tagList[i] );
					break;

				default:
					return;
			}
			
			if (tagList[i].src) {
				tagList[i].setAttribute( "oldsrc", tagList[i].src );
			}
		}
	}
}

EB.rollover.mouseup = function( tag ) {
	var		newMouseup;

	if (tag.onmouseup) {
		tag.oldmouseup = tag.onmouseup;
		newMouseup =
			function() {
				this.src = this.getAttribute( "srchover" );
				this.oldmouseup();
			}
	}
	else {
		newMouseup =
			function() {
					this.src = this.getAttribute( "srchover" );
				}
	}

	tag.onmouseup = newMouseup;
}

EB.rollover.mouseup2 = function( tag ) {
	var		newMouseup;

	if (tag.onmouseup) {
		tag.oldmouseup = tag.onmouseup;

		newMouseup =
			function() {
				this.src = this.getAttribute( "oldsrc" );
				this.oldmouseup();
			}
	}
	else {
		newMouseup =
			function() {
			this.src = this.getAttribute( "oldsrc" );
			}
	}
	
	tag.onmouseup = newMouseup;
}

EB.rollover.mousedown = function( tag ) {
	var		newMousedown;

	if (tag.onmousedown) {
		tag.oldmousedown = tag.onmousedown;
		newMousedown =
			function() {
				this.src = this.getAttribute( "srcclick" );
				}
	}
	else {
		newMousedown =
			function() {
				this.src = this.getAttribute( "srcclick" );
			}
	}

	tag.onmousedown = newMousedown;
}

EB.rollover.mouseover = function( tag ) {
	var		newMouseover;

	if (tag.onmouseover) {
		tag.oldmouseover = tag.onmouseover;
		newMouseover =
			function() {
				this.src = this.getAttribute( "srchover" );
				this.oldmouseover();
			}
	}
	else {
		newMouseover =
			function() {
				this.src = this.getAttribute( "srchover" );
			}
	}

	tag.onmouseover = newMouseover;
}

EB.rollover.mouseout = function( tag ) {
	var		newMouseout;

	if (tag.onmouseout) {
		tag.oldmouseout = tag.onmouseout;
		newMouseout =
			function() {
				this.src = this.getAttribute( "oldsrc" );
				this.oldmouseout();
			}
	}
	else {
		newMouseout =
			function() {
				this.src = this.getAttribute( "oldsrc" );
			}
	}

	tag.onmouseout = newMouseout;
}

EB.rollover.storeImgs = function( tag ) {
	var		src = (tag.getAttribute( 'src' )) ? tag.getAttribute('src') : '';
	var		srcClick = (tag.getAttribute( 'srcclick' )) ? tag.getAttribute( 'srcclick' ) : '';
	var		srcHover = (tag.getAttribute( 'srchover' )) ? tag.getAttribute( 'srchover' ) : '';

	EB.rollover.imgHolder.store( src, srcClick, srcHover );
}

EB.rollover.preLoadImgs = function() {
	if (!document.getElementById) {
		return;
		}

	var		tagList = document.getElementsByTagName( 'IMG' );
	var		tagList2 = document.getElementsByTagName( 'INPUT' );

	EB.rollover.preloader( tagList );
	EB.rollover.preloader( tagList2 );
}

/*---------------------------------------------------------------------------------------------------------
 *	RUN ON LOAD LISTENER
 *---------------------------------------------------------------------------------------------------------*/

	if (window.addEventListener) {
		window.addEventListener( "load", EB.rollover.preLoadImgs, false );
	} 
	else {
		if (window.attachEvent) { 
			window.attachEvent( "onload", EB.rollover.preLoadImgs );
		}
		else { 
			if (document.getElementById) {
				window.onload = EB.rollover.preLoadImgs;
			}
		}
	}
