// Rollovers class to handle image rollovers with multiple MIME types, and to light up the current page without additional configuration.
// Additional changes will be needed to handle URLs with hashes or queries, or designs with a current-page light up that is different from the mouseover light up
// -RQ 8/26/10

	function Rollovers() {
		
		// Create a private alias to avoid scope issues
		var self = this;
		
		// Change this configuration as necessary
		this.config = {
			off   : '_off', // The conventional suffix for images before mouseover, i.e., imageName_off.png
			on    : '_on',  // The suffix after mouseover
			index : 'index.aspx' // The file name, with extension, for the index/default file
		}
		
		// Most of the time there will be no need to edit past this point
		
		// Flips an image from on to off. The direction arg is optional -- if it's set to 'on' or 'off' it will always switch to that regardless of it's current state.
		this.flip = function(button, direction)
		{
			
			var button = button
			  , rPngFilter = /[\w\/\.\:]+\.png/ // Matches a .png file type
			  // If you're using the Unit png fix in ie6, you need to access the :filter css property instead of the src attribute.
			  , imgPath = (typeof(pngfix) === 'function' && button.style.filter) ? button.style.filter.match(rPngFilter)[0] : button.src // Grab the src of the image
			  , rExt = /(.\w+)$/
			  , imgType = imgPath.match(rExt)[0]
			  , offExt = self.config.off + imgType
			  , onExt = self.config.on + imgType
			  , rOffExt = endReg(offExt)
			  , rOnExt = endReg(onExt);
			
			// Creates a reg exp that matches the end of a string
			function endReg(string) {
				return new RegExp('(' + string + ')$');
			}
			// Tests whether the argument string contains the "off" extension created above
			function isOff() {
				return rOffExt.test(imgPath);
			}
			// Uses a reg exp to change an image from on to off
			function turn(reg, ext) {
				imgPath = imgPath.replace(reg, ext);
				button.src = imgPath;
				// again, with Unit png fix we need to access the :filter property
				if (typeof(pngfix) === 'function' && button.style.filter) {
					button.style.filter = button.style.filter.replace(rPngFilter, imgPath);
				};
			}
			
			if ((isOff() && direction !== 'off') || direction === 'on')
				turn(rOffExt, onExt);
			else
				turn(rOnExt, offExt);
		}
		
		this.lightUp = function() {
			var locPath = window.location.pathname
			  , rFileName = /(\w+.\w+)$/
			  , thisFileName = getFile(locPath).toLowerCase()
			  , anchors = document.getElementsByTagName('a')
			  , menu = [];
			
			function getFile(str) {
					return str.match(rFileName) ? str.match(rFileName)[0] : self.config.index;
			};
			
			for (var i=0; i<anchors.length; i++) {
				aFileName = getFile(anchors[i].href);
				if (aFileName === thisFileName && !/#$/.test(anchors[i].href)) {
					img = anchors[i].getElementsByTagName('img');
					img = img ? img[0] : null;
					if (img) {
						img.onmouseover = null;
						img.onmouseout = null;
						self.flip(img, 'on')
					}
				}
			}
		}
		
		this.ready = function(callback) {
			var callbacks = callback;
			if (document.body) {
				if (typeof(callbacks) === 'array') {
					for (f in callbacks) {
						self.ready(callbacks[f]);
					}
				}
				else {
					callbacks();
				}
			}
			else {
				setTimeout(function() {self.ready(callbacks);}, 300)
			}
		}
		
		// When the document is ready, call the lightUp method
		this.ready(self.lightUp);

	};

	rollovers = new Rollovers();

