/********************************************************************************
 *
 *	imageflip.js
 *	
 *	usage : 
 *		Change OFF_SUFFIX and ON_SUFFIX to match image names.
 *		
 *		For each image which is to be flipped, document.onload should call
 *			flip.myFlipName = new Flip( myImageName, myImageSource )
 *		To turn image on, call 
 *			flip.myFlipName.on()
 *		To turn image off, call
 *			flip.myFlipName.off()
 *
 ********************************************************************************/

isLoaded=false
function init(){
	getAllImages()
	isLoaded=true
}

//mouseovers
function mouseon(name){
	if ( ! isLoaded ) return;
	flip[name].on()
}

function mouseoff(name){
	if ( ! isLoaded ) return;
	flip[name].off()
}


var IMAGE_OFF_SUFFIX = "_off.gif"
var IMAGE_ON_SUFFIX = "_over.gif"

var flip = new Array();
function Flip( imgName, imgSrc )
{
	this.docImg = getDocImg(imgName);
	this.imgOff = new Image( this.docImg.width, this.docImg.height )
	this.imgOffsrc = this.imgOff.src = this.docImg.src;
	
	this.imgOn = new Image ( this.docImg.width, this.docImg.height )
	if ( imgSrc )
		this.imgOnsrc = this.imgOn.src = imgSrc
	else
	{ var s = this.docImg.src
		this.imgOnsrc = this.imgOn.src = s.substr( 0, s.indexOf(IMAGE_OFF_SUFFIX) ) + IMAGE_ON_SUFFIX
	}
	
	this.on = Flip_on
	this.off = Flip_off
}

function Flip_on()
{	this.imgOn.src = "";
	this.docImg.src = this.imgOnsrc;

	if ( this.status )
		window.status = this.status;
	return true;
}

function Flip_off()
{	this.imgOn.src = "";
	this.docImg.src = this.imgOffsrc
	window.status = "";
	return true;
}

/*
 *	function getDocImg( name )
 *
 *	returns the named element in document.images
 *		searches recursively through document.layers for the named image
 */
function getDocImg(name, d)
{
	d = ( d == null ) ? document : d; //set d to be the document if empty
	var img = d.images[name];
	if (img) return img; //found it
	
	if ( ! document.layers ) return null; //in ie, we die here
	
	for ( var i=0; i < d.layers.length; i++ ) 
		if ( d.layers[i].id )
		{
			img = getDocImg( name, d.layers[i].document )  //recursive call
				if (img) return img; //found it
		}
	return null; //did not find it
}

function getAllImages( d ){
	d = ( d == null ) ? document : d; //set d to be the document if empty
	for ( var i = 0; i < d.images.length; i++ ){
		var src = d.images[i].src
		if ( d.images[i].name ){
			if ( src.indexOf( IMAGE_OFF_SUFFIX ) != -1 ){
				flip[ d.images[i].name ] = new Flip( d.images[i].name )
			}
		}
	}
	if ( !document.layers ) return;
	for ( var i=0; i < d.layers.length; i++ ) 
		getAllImages( d.layers[i].document )  //recursive call
}
