/*
 * Image Rollover
 * 
 * Needs Prototype.js - http://prototypejs.org
 * 
 * Created By Vincent Limjap 
 * Last Edited 12/07/06
 * 
 * Instructions: The script tag to include this file must be located at the end of the HTML file. All element ID names must be unique.
 * 
 * Ex.
 * 	Offstate: image.jpg
 *  Onstate: image_on.jpg
 * 
 *	<img src="img.jpg" id="img1" class="imgRollover" />
 * 
 * dynoRollovers
 * 
 * 	Offstate: image.gif *Must be a gif file
 *  Onstate: image_on.gif *Must be a gif file
 *  TargetID: image_target.jpg *Target Image MUST be a jpg
 *  
 * <img id="dyno0" class="dynoRollover" src="/dynoimg.gif" />
 * <img id="dyno1" class="dynoRollover" src="/dynoimg1.gif" />
 * <img id="dyno2" class="dynoRollover" src="/dynoimg2.gif" />
 * <img id="dynoTarget" src="/dynoimg_target.jpg" />
 * 
 */

//Initialize Script
var Prototype;
if(!Prototype) {
	alert('Prototype framework missing. - http://prototypejs.org');
	window.open('http://prototypejs.org','prototypewindow');
}

ImgRollover = Class.create();

ImgRollover.prototype = {
	initialize:function(){
		this.docImg=document.getElementsByTagName('img');
		for (x=0;x<this.docImg.length;x++) {
			fileExt = '';
			fileExt_on = '';
			if(this.docImg[x].src.indexOf('_on.')==-1) {
				fileExt = this.docImg[x].src.substring(this.docImg[x].src.length-4,this.docImg[x].src.length);
				fileExt_on ='_on'+fileExt;
			}
			if(this.docImg[x].className.indexOf('imgRollover')>=0) {
				eval(this.docImg[x].id+'_on = new Image()');
				eval(this.docImg[x].id+'_on.src = \''+this.docImg[x].src.replace(fileExt,fileExt_on)+'\'');

				eval(this.docImg[x].id+'_off = new Image()');
				eval(this.docImg[x].id+'_off.src = \''+this.docImg[x].src+'\'');

				this.docImg[x].onmouseover = this.imgMouseOver.bindAsEventListener(this.docImg[x],this.docImg[x].id);
				this.docImg[x].onmouseout = this.imgMouseOut.bindAsEventListener(this.docImg[x],this.docImg[x].id);
			}
			if(this.docImg[x].className.indexOf('dynoRollover')>=0) {
				eval(this.docImg[x].id+'_on = new Image()');
				eval(this.docImg[x].id+'_on.src = \''+this.docImg[x].src.replace('.gif','_on.gif')+'\'');
				
				eval(this.docImg[x].id+'_off = new Image()');
				eval(this.docImg[x].id+'_off.src = \''+this.docImg[x].src+'\'');
				
				eval(this.docImg[x].id+'_target = new Image()');
				eval(this.docImg[x].id+'_target.src = \''+this.docImg[x].src.replace('.gif','_target.jpg')+'\'');

				for(y=0;y<this.docImg[x].id.length;y++) {
					if (/(^-?\d\d*$)/.test(this.docImg[x].id.substr(y,1))) {
						dynotarget=this.docImg[x].id.substr(0,y);
						eval('orig_'+dynotarget+'Target =new Image()');
						eval('orig_'+dynotarget+'Target.src=$(\''+dynotarget+'Target\').src');
					}
				}
				this.docImg[x].onmouseover = this.dynoMouseOver.bindAsEventListener(this.docImg[x],this.docImg[x].id, dynotarget);
				this.docImg[x].onmouseout = this.dynoMouseOut.bindAsEventListener(this.docImg[x],this.docImg[x].id, dynotarget);
			}
		}
	},
	
	dynoMouseOver:function(evt, elID, elTarget) {
		try{
			if ($('imgRolloverFlag').value==elID) { //PRESERVES CLICK STATE
				return;	
			}
		} catch(err) {}
		$(elID).src=eval(elID+'_on.src');
		$(elTarget+'Target').src=eval(elID+'_target.src');
	},
	
	dynoMouseOut:function(evt, elID, elTarget) {
		$(elID).src=eval(elID+'_off.src');
		$(elTarget+'Target').src=eval('orig_'+elTarget+'Target.src');
	},
	
	imgMouseOver:function(evt, elID) {
		try{
			$(elID).src=eval(elID+'_on.src');
		} catch(err) {alert('This Element Needs an ID attribute.');}
		
	},
	
	imgMouseOut:function(evt, elID) {
		try{
			//PRESERVES CLICK STATE
			if ($('imgRolloverFlag').value==elID) {
				return;	
			}
		} catch(err) {}
		try {
			$(elID).src=eval(elID+'_off.src');
		} catch(err){}
	},
	
	clearAll:function(divContainer) {
		divEl = $(divContainer).getElementsByTagName('img');
		for (i=0;i<divEl.length;i++) {
			if(divEl[i].className.indexOf('imgRollover')>=0 && divEl[i].id!=$('imgRolloverFlag').value) {
				divEl[i].src = eval(divEl[i].id+'_off.src');
			}
		}
	}
}

ImgRollover = new ImgRollover();