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

var Bolt_PreloadedImages = [];
function Bolt_SetClassName (n,c) {
	var s = String(n.className).replace(/\s*(actual|current|over)\s*/gi, '');
	if (c) s+=' '+c;
	n.className = s;
}
function Bolt_TestClassName(n,c) {
	var r = new RegExp("(^| )"+c+"($| )","gi");
	return r.test(String(n.className));
}
function Bolt_Highlight () {
	
	this.QuoteMeta = function (str) {
		str = str.replace( /([^A-Za-z0-9])/g , "\\$1" );
		return str;
	}
	
	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\.{2,4}($|\?)/gi,'');
			var href_noextension = href.replace(/\.[a-z]{2,4}($|\?)/gi,'');
			if (((href == location.substr(0,href.length) 
				|| href_noindex == location.substr(0,href_noindex.length)
				|| href_noextension == location.substr(0,href_noextension.length)
				)
				/*&& (href != this.UrlFormatting('/index.html') || (href == location || href_noindex == location))*/
				&& (as[i].id != 'homeLink' || (href == location || href_noindex == location || href_noextension == location) ))
				
				// hard coded exceptions
				
				) {
				// places the arrow next to current file
				if ((href == location || href_noindex == location)
					 
					 // hard coded exceptions
					 
					 ) {
					Bolt_SetClassName(as[i], 'actual');
				} else {
					Bolt_SetClassName(as[i], '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 (!Bolt_TestClassName(this, '(current|actual|over)')) {
			Bolt_SetClassName(this, '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 (Bolt_TestClassName(this,'over')) {
			Bolt_SetClassName(this, '');	
			var img = this.firstChild;
			if (img.tagName && img.tagName == 'IMG') {
				img.src = String(img.src).replace(/_current\.(jpg|jpeg|png|gif)$/gi, ".$1");
			}
		}
	}
	
}