// Bolt_Highlight
// Auto menu highlight and rollover class
// (c) Copyright 2008 ChristopherBolt.Com

var bolt_preloadedImages = [];
function Bolt_Highlight () {
	
	this.quoteMeta = function (str) {
		str = str.replace( /([^A-Za-z0-9])/g , "\\$1" );
		return str;
	}

	this.swapImage = false;
	
	this._location = String(document.location);
	this._location2 = this._location.replace(/^([\s\S]*\/)[^\/]*$/i, "$1");
	this._domain = this._location.replace(/(^(http|https):\/\/[^\/]*)[\s\S]*/i, '$1');
	
	this._locationRegex = new RegExp('^'+this.quoteMeta(this._location).replace(/[\\]&/gi,'(\\&|\\&amp\\;)')+'#', 'gi');
	
	this.urlFormatting = function (url) {
		url = String(url);
		if (url.match(/^javascript:/gi)) {
			return url;
		}
		if ( /^www\./i.test(url)) {
			url = 'http://'+url;	
		}
		
		url = url.replace(this._locationRegex, '#');
	
		var location = this._location2;
		
		url = url.replace(/^\//gi, this._domain+'/');
		url = url.replace(/^([^#][^:"]*)$/gi, location+'$1');
		while(url.match(/([^:][^\/])\/[^\/]*\/\.\.\//i)) {
			url = url.replace(/([^:][^\/])\/[^\/]*\/\.\.\//i, '$1/');
		}
		url = url.replace(/\/\.\.\//gi, '/');
		
		// delete everything after #
		url = url.replace(/(^[^#]*#)[\s\S]+$/g, "$1");
		//url = url.replace(/^[^#]*#[\s\S]+$/g, function (x){s=x.split('#');return s[0]+'#'+unescape(s[1]);});
		
		url = unescape(url);
		
		return url;
	}
	
	this.highlight = function (id) {
		//var id = 'mainMenu';
		var as = document.getElementById(id).getElementsByTagName('A');
		var location = this.urlFormatting(this._location);
			
		for (var i=0;i<as.length;i++) {
			var href = String(as[i].href);
			href = this.urlFormatting(href);
			var href_noindex = href.replace(/index\.html/,'');
			if (((href == location.substr(0,href.length) || href_noindex == location.substr(0,href_noindex.length))
				/*&& (href != this.urlFormatting('/index.html') || (href == location || href_noindex == location))*/
				&& (as[i].id != 'homeLink' || (href == location || href_noindex == location)))
				
				// hard coded exceptions
				
				) {
				// places the arrow next to current file
				if ((href == location || href_noindex == location)
					 
					 // hard coded exceptions
					 
					 ) {
					as[i].className = 'actual';
				} else {
					as[i].className = 'current';
				}
				
				var img = as[i].firstChild;
				if (img.tagName && img.tagName == 'IMG') {
					img.src = String(img.src).replace(/\.(jpg|jpeg|png|gif)$/gi, "_current.$1");
				}
			} else {
				// do image pre-load
				var img = as[i].firstChild;
				if (img.tagName && img.tagName == 'IMG') {
					bolt_preloadedImages[bolt_preloadedImages.length] = new Image();
					bolt_preloadedImages[bolt_preloadedImages.length-1].src = String(img.src).replace(/\.(jpg|jpeg|png|gif)$/gi, "_current.$1");
				}
			}
			as[i].onmouseover = this.onmouseover;
			as[i].onmouseout = this.onmouseout;
		}		
	}
	
	this.onmouseover = function (e) {
		if (this.className != 'current' && this.className != 'actual' && this.className != 'over') {
			this.className = 'over';	
			var img = this.firstChild;
			if (img.tagName && img.tagName == 'IMG') {
				img.src = String(img.src).replace(/\.(jpg|jpeg|png|gif)$/gi, "_current.$1");
			}
		}
	}
	
	this.onmouseout = function (e) {
		if (this.className == 'over') {
			this.className = '';	
			var img = this.firstChild;
			if (img.tagName && img.tagName == 'IMG') {
				img.src = String(img.src).replace(/_current\.(jpg|jpeg|png|gif)$/gi, ".$1");
			}
		}
	}
	
}
