var bLoaded = false;
var cNav;
var cSubNav = cLocalImgs = false;

function mouseOver(obj) {
	if(!bLoaded) return false;
	var sPath = obj.src.substring( 0, obj.src.lastIndexOf('/') +1 );
	var sExt = (obj.src.indexOf('.jpg') !=-1) ? '.jpg' : '.gif';
	var sImg = obj.src.substring(obj.src.lastIndexOf('/') +1, obj.src.lastIndexOf('.'));
	if(sImg.indexOf('_on') == -1) obj.src = sPath + sImg + "_on" + sExt;
}

function mouseOut(obj) {
	if(!bLoaded) return false;
	var sPath = obj.src.substring( 0, obj.src.lastIndexOf('/') +1 );
	var sExt = (obj.src.indexOf('.jpg') !=-1) ? '.jpg' : '.gif';
	var sImg = obj.src.substring(obj.src.lastIndexOf('/') +1, obj.src.lastIndexOf('.'));
	if(sImg.indexOf('_on') != -1) obj.src = sPath + sImg.replace('_on', '') + sExt;
}

function attachMouseEvents() {
	for(var n=0; n<cNav.length; n++) {
		// no mouse events on active buttons
		if(cNav[n].src.indexOf('_on')!=-1) continue;
		cNav[n].onmouseover = function() { mouseOver(this) }
		cNav[n].onmouseout = function() { mouseOut(this) }
	}
	
	if(cSubNav) {
		for(var u=0; u<cSubNav.length; u++) {
			// no mouse events on active buttons
			if(cSubNav[u].src.indexOf('_on')!=-1) continue;
			cSubNav[u].onmouseover = function() { mouseOver(this) }
			cSubNav[u].onmouseout = function() { mouseOut(this) }
		}
	}
	
	if(cLocalImgs.length > 0) {
		for(var i=0; i<cLocalImgs.length; i++) {
			// no mouse events on active buttons
			if(cLocalImgs[i].src.indexOf('_on')!=-1) continue;
			cLocalImgs[i].onmouseover = function() { mouseOver(this) }
			cLocalImgs[i].onmouseout = function() { mouseOut(this) }
		}
	}	
}

function init() {
	cNav = document.getElementById('nav').getElementsByTagName('IMG');
	
	if(document.getElementById('subnav')) {
		cSubNav = document.getElementById('subnav').getElementsByTagName('IMG');
	}

	oRollovers = document.getElementById('main').getElementsByTagName('IMG') || false;
	cLocalImgs = new Array();
	for(var i=0; i<oRollovers.length; i++) {
		if(oRollovers[i].className == 'rollover') {
			cLocalImgs.push(oRollovers[i]);
		}
	}	
	
	attachMouseEvents();
	bLoaded = true;
	
	if(window.location.search.length > 1) {
		color = window.location.search.split("=")[1]
		document.body.style.backgroundColor = "#" + color;
	}
}

var newWin = null;
function openWin(url, w, h) {
	var props = "width=" + w + ",height=" + h + ",toolbar=no,menubar=no,personalbar=no,copyhistory=no,scrollbars=yes";
	var handle = "oNewWin";
	if(newWin && !newWin.closed) {
		newWin.focus();
	}
	newWin = window.open(url, handle, props);
}

// written by Dean Edwards, 2005
// with input from Tino Zijdel

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	// assign each event handler a unique ID
	if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	// create a hash table of event types for the element
	if (!element.events) element.events = {};
	// create a hash table of event handlers for each element/event pair
	var handlers = element.events[type];
	if (!handlers) {
		handlers = element.events[type] = {};
		// store the existing event handler (if there is one)
		if (element["on" + type]) {
			handlers[0] = element["on" + type];
		}
	}
	// store the event handler in the hash table
	handlers[handler.$$guid] = handler;
	// assign a global event handler to do all the work
	element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	// delete the event handler from the hash table
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$guid];
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(window.event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

addEvent(window, 'load', init);
