/*
	Orange Automated Image Caching Script

	This script will after pageload, run through all Anchor tags on a page, and find any and all images within them.
	Once there is a list of all the anchored images, this script will add a mouseover and mouseout event to the anchor,
	and also create a cache image for the standard state, and the rollover state, which will reside in an ImageCache Hash

	Written by:
		Stephen Brown, SDB, stephen.brown@akqa.com
		AKQA
		February 2003
*/

var hImages = new Array; // Hash Image library

/**
 * Prototype Methods:
 * both apply to ALL objects (there is no Anchor Object in some browsers)
 *
 *		hasChildImage returns whether a child exists in child Node 0
 *		getChildImage returns that child Node
 */
 function hasChildImage(anyObject) {
	if (document.getElementById) {
		if (anyObject.childNodes[0] && anyObject.childNodes[0].src) {
			return true;
		}
	} else if (document.all) {
		if (anyObject.children[0] && anyObject.children[0].src) {
			return true;
		}
	} else if (document.links) {
		/**
		 * No support for Moz2 compliant browsers
		 */
	}
	return false;
}
function getChildImage(anyObject) {
	if (document.getElementById) {
		return anyObject.childNodes[0];
	} else if (document.all) {
		return anyObject.children[0];
	} else if (document.links) {
		/**
		 * No Support for Moz2 compliant browsers
		 */
	}
	return false;
}

agressiveImageCache = function () {

	this.createList = function() {
		var outStr = '';
		for(img in hImages) {
			outStr += hImages[img].outerHTML+"<BR>\n";
		}
	}

	/**
	 * Adds the current image URL to the HASH
	 */
	this.addToHash = function (parseName, addOverState) {

		parseName = this.convertName(parseName, addOverState);

		if (hImages[parseName] && typeof hImages[parseName].src != "undefined") {

			/**
			 * This HAS Image already Exists
			 * Skipping
			 */

		} else {
			hImages[parseName] = new Image(1,1);
			hImages[parseName].src = parseName;
		}
	}
	/**
	 * Retrieves from the HASH the image URL
	 */
	this.getFromHash = function (parseName, addOverState) {

		var removeOverState = !addOverState;
		var outputSrc;

		parseName = this.convertName(parseName, addOverState, removeOverState);

		if (hImages[parseName] && typeof hImages[parseName].src != "undefined") {
			outputSrc = hImages[parseName].src;
			return (addOverState) ? outputSrc.replace(/_over/,'-over') : outputSrc;
		}
		return parseName;
	}

	/**
	 * Converts the image name, and returns (with Over State when necessary
	 */
	function convertName (parseName, addOverState, removeOverState) {
		if (addOverState) { parseName = parseName.replace(/\.gif/,'-over.gif'); }
		if (removeOverState) { parseName = parseName.replace(/-over\.gif/,'.gif'); }
		//if (parseName.indexOf("file:///C:/") != -1) { parseName = parseName.substring(parseName.lastIndexOf(":")+1); }
		//if (parseName.charAt(0) == '/') { parseName = parseName.substring(1); }

		return parseName;
	}
	this.convertName = convertName;

	/**
	 * Searches through the pages Anchors object to find ALL images, and set their rollover states
	 */
	this.searchAnchors = function (skipTracking) {

		/**
		 * Loop through all Links tags, and adds the Anchors with Image found
		 * to the LinksWithImages Array object
		 */
		var LinksWithImages = (document.getElementById) ? this.getAIMGs_DOM(skipTracking) :
			(document.all) ? this.getAIMGs_IE(skipTracking) :
			(document.links) ? this.getAIMGs_Moz2(skipTracking) :
			new Array;

		/**
		 * Loops through all Links with Images, and adds the MouseOver and MouseOut events
		 */
		var imageChildNums;
		for (var eachL=0; eachL<LinksWithImages.length; eachL++) {
			/**
			 * Add the MouseOver and MouseOut events
			 */
			if (LinksWithImages[eachL].className != "dynamicrollover") {
				this.addRolloverTo (LinksWithImages[eachL]);
			}
		}
	}

	this.getAIMGs_IE = function(skipTracking) {
		/**
		 * Using This code for MSIE as it works quicker in MSIE6 when tested
		 * It also is compatible with MSIE (using children object)
		 */
		var allLinks = document.all.tags["A"];
		var LinksWithImages = new Array;
		var foundImage;
		for (eachL in allLinks) {
			foundImage = false;
			if (allLinks[eachL].className != "norollover") {
				for (eachChild in allLinks[eachL].children) {
					if (allLinks[eachL].children[eachChild].src) {
						this.addToHash(allLinks[eachL].children[eachChild].src);
						this.addToHash(allLinks[eachL].children[eachChild].src, true);
						foundImage = true;
					}
				}
				if (foundImage) LinksWithImages[LinksWithImages.length] = allLinks[eachL];
			}
		}
		return LinksWithImages;
	}
	this.getIIMGs_IE = function() {
		/**
		 * MSIE code block: retrieves all inputs where type="image" OR type="submit"
		 */
		var allInputs = document.all.tags["INPUT"];
		var InputTypeImages = new Array;
		for (eachType in allInputs) {
			if (allInputs[eachType].type == "image" || allInputs[eachType].type == "submit") {
				this.addToHash(allInputs[eachType].src);
				this.addToHash(allInputs[eachType].src, true);
				InputTypeImages[InputTypeImages.length] = allInputs[eachType];
			}
		}
		return InputTypeImages;
	}

	this.getAIMGs_DOM = function(skipTracking) {
		/**
		 * DOM does not support children, as this is an IE property, so we use
		 * childNodes instead, we also are using old "for (var x=y; x<z.length; x++)"
		 * as DOM does not support the more java like "for (x in z)"
		 */
		var allLinks = document.getElementsByTagName("A");
		var LinksWithImages = new Array;
		var foundImage;
		for (var eachL=0; eachL<allLinks.length; eachL++) {
			foundImage = false;
			if (allLinks[eachL].className != "norollover") {
				for (var eachChild=0; eachChild<allLinks[eachL].childNodes.length; eachChild++) {
					if (allLinks[eachL].childNodes[eachChild].src) {
						this.addToHash(allLinks[eachL].childNodes[eachChild].src);
						this.addToHash(allLinks[eachL].childNodes[eachChild].src, true);
						foundImage = true;
					}
				}
				if (foundImage) LinksWithImages[LinksWithImages.length] = allLinks[eachL];
			}
		}
		return LinksWithImages;
	}
	this.getIIMGs_DOM = function(skipTracking) {
		/**
		 * DOM code block: retrieves all inputs where type="image" or "type="submit"
		 */
		var allInputs = document.getElementsByTagName("INPUT");
		var InputTypeImages = new Array;
		for (var eachType=0; eachType<allInputs.length; eachType++) {
			if (allInputs[eachType].type == "image" || allInputs[eachType].type == "submit") {
				this.addToHash(allInputs[eachType].src);
				this.addToHash(allInputs[eachType].src, true);
				InputTypeImages[InputTypeImages.length] = allInputs[eachType];
			}
		}
		return InputTypeImages;
	}

	function getAIMGs_Moz2(skipTracking) {
		/**
		 * Not sure how to code this function as it needs to work in Netscape!
		 * I don't believe netscape can do what I want to do here, but i'm going
		 * to try anyway.
		 * You may notice this function is also written differently, this is so
		 * that older browsers, which this function is created for, can read it.
		 * Moz3 and earlier do not support local this.name = function(), so we
		 * we function name(), this.name = name;
		 *
		 * This function returns a blank array, to skip the above code, as the
		 * Browsers that run this code are required to run differently to that
		 * of the above code
		 */
		/**
		 * So far, attemps have been very unsuccessful
		 *
		 * I will leave this function blank for now
		 */
		return new Array;
	}
	this.getAIMGs_Moz2 = getAIMGs_Moz2;
	function getIIMGs_Moz2(skipTracking) {
		/**
		 * Mozilla 2 compliant code block: retrieves all inputs where type="image" OR type="submit"
		 * Again, Mozilla support is not necessary at this point
		 * will leave blank 'till support is required.
		 */
		 return new Array;
	}
	this.getIIMGs_Moz2 = getIIMGs_Moz2;

	/**
	 * These Methods are used for Dynamic Rollover Enable/Disable functionality
	 * However, I will be using addRolloverTo for adding rollovers in the first place
	 */
	function addRolloverTo (AnchorObject) {
		var sMouseOver = '';
		var sMouseOut = '';

		ImageArrayList = new Array;
		/**
		 * Count the number of children images, and take their numbers
		 * so that ALL images within a Single Anchor will rollover simultaneously
		 */
		if (AnchorObject != null) {
			for (var eachChild=0; eachChild<AnchorObject.childNodes.length; eachChild++) {
				if (AnchorObject.childNodes[eachChild].src) {
					ImageArrayList[ImageArrayList.length] = eachChild;
				}
			}

			for (var eachChild=0; eachChild<ImageArrayList.length; eachChild++) {
				sMouseOver += 'oMouseOver(this.childNodes['+ImageArrayList[eachChild]+']);';
				sMouseOut += 'oMouseOut(this.childNodes['+ImageArrayList[eachChild]+']);';
			}

			AnchorObject.onmouseover = function() { eval(sMouseOver) }
			AnchorObject.onmouseout = function() { eval(sMouseOut) }
		}
	}
	this.addRolloverTo = addRolloverTo;

	function addInputOverTo (InputObject) {
		var sMouseOver = 'oMouseOver(this);';
		var sMouseOut = 'oMouseOut(this);';

		InputObject.onmouseover = function() { eval(sMouseOver) }
		InputObject.onmouseout = function() { eval(sMouseOut) }
	}
	this.addInputOverTo = addInputOverTo;

	function removeRolloverFrom (AnchorObject) {
		if (AnchorObject != null) {
			AnchorObject.onmouseover = function() {}
			AnchorObject.onmouseout = function() {}
		}
	}
	this.removeRolloverFrom = removeRolloverFrom;

	return this;
}
var aIC = agressiveImageCache();

// Mouse Rollover function (Using Object Parse, instead of name)
function oMouseOver(childobject) {
	if(childobject) {
		/**
		 * Support for Missing ALT or VALUE
		 */
		if (childobject.alt && childobject.alt != '') {
			setTimeout('javascript:window.status="'+childobject.alt+'";',1);
		} else if (childobject.value && childobject.value != '') {
			setTimeout('javascript:window.status="'+childobject.value+'";',1);
		} else {
			// Blank Status
			setTimeout('javascript:window.status="";',1);
		}
		childobject.src = aIC.getFromHash(childobject.src, true);
	}
}

// Mouse Rollout function (Using Object Parse, instead of name)
function oMouseOut(childobject) {
	if(childobject) {
		setTimeout("javascript:window.status='';",1);
		childobject.src = aIC.getFromHash(childobject.src);
	}
}
