// gr.js
// Global javaScript routines for the GolfReizen.nl site.

// W3CDOM (level 1 DOM) indicator.
var W3CDOM = (document.createElement && document.getElementsByTagName);

// Add Init to the Window.onload chain.
AddOnLoad(window, Init);


//*********************************************************
// Initialise.
//*********************************************************
function Init() {
	InitMO('button');InitMO('buttonleft');InitMO('buttonleft1');
	InitMO('leftmenu');
}

//*********************************************************
// Image Mouseover Javascript.
//*********************************************************
function InitMO (id_div) {
	// This only works in "modern" level 1 DOMs...
	if (!W3CDOM) 
		return;

	// Only handle the IMG's in the 'buttons' div.
//	var div = document.getElementById('buttons');
	var div = document.getElementById(id_div);
	if(div==null)
		return;
	var imgs = div.getElementsByTagName('img');

	// Check all the img's...
	for (var i=0; i<imgs.length; i++) {
		// If the name of the image ends with a '1', then 
		// replace that '1' with a '2' for the mouseover image.
		var dot = imgs[i].src.lastIndexOf('.');
		if (dot>0) {
			var name = imgs[i].src.substring(0, dot);
			var ext  = imgs[i].src.substring(dot, imgs[i].src.length);
			if (name.charAt(name.length-1) == '1') {
				imgs[i].mosrc = new Image();
				imgs[i].mosrc = name.substring(0, name.length-1) + '2' + ext;
				imgs[i].onmouseover = SwitchSrc;
				imgs[i].onmouseout  = SwitchSrc;
			}
		}
	}
}

//*********************************************************
// SwitchSrc switches the two SRC's in an IMG element.
//*********************************************************
function SwitchSrc() {
	var tempsrc = this.src;
	this.src = this.mosrc;
	this.mosrc = tempsrc;
}

//*********************************************************
// AddOnLoad
// To add an init function to the onload chain.
// Target = window, etc.
//*********************************************************
function AddOnLoad (target, onload_fn) {
	if (target.addEventListener) // W3CDOM / DOM-1
		target.addEventListener("load", onload_fn, false)
	else 
		if (target.attachEvent) // MS IE5+/Win only
			target.attachEvent("onload", onload_fn)
		else 
			if (document.getElementById) { // We're down to Jurassic browsers here...
				var PrevOnLoad = target.onload; // DOM-0 ?
				if ((PrevOnLoad != NULL) && (typeof PrevOnLoad == 'function'))
					target.onload = function() { PrevOnLoad(); onload_fn; }
				else
					target.onload = onload_fn;
			}
}
