// To make the html page less busy, all javascript, including mouse tricks, has been moved here.
// This script, the HTML, and all of the functions you see here, could be
// Copyright 2004 by Digitolle, but I prefer you use them for yourself! -John A. Tolle

// Note: This script depends on functions contained in scripts/generalfunctions.js

window.onload = initWindow; // Note: This will override any onload event defined in the html BODY object

function initWindow() {	// Define Event Handlers (so we don't have to uglify the HTML code!)
	//document.all.aHREF.onclick = doSomething;
	//document.all.aHREF.onmouseover = setWindowStatus;
	//document.all.aHREF.onmouseout = resetWindowStatus;
	//document.all.aHREF.onclick = showNewDesignAlert;
	//document.all.iIMG.onmouseover = swapImg;
	//document.all.iIMG.onmouseout = swapImg;
	//document.all.selectBox.onchange = doSomethingElse; // Note: We must NOT add empty brackets ()!
	if (document.images) { // This only works for browsers that support image manipulation.
		preloadImages();
		
		// We only want to allow the mouse-over image affect when we're NOT on the selected page!
		
		if (document.location.href.indexOf("index.htm") == -1 && document.location.href.indexOf(".htm") != -1) {
			document.all.btnhome.onmouseover = swapImg;
			document.all.btnhome.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("aboutus.htm") == -1) {
			document.all.btnaboutus.onmouseover = swapImg;
			document.all.btnaboutus.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("successstories.htm") == -1) {
			document.all.btnsuccessstories.onmouseover = swapImg;
			document.all.btnsuccessstories.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("photogallery.htm") == -1) {
			document.all.btnphotogallery.onmouseover = swapImg;
			document.all.btnphotogallery.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("faq.htm") == -1) {
			document.all.btnfaq.onmouseover = swapImg;
			document.all.btnfaq.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("donations.htm") == -1) {
			document.all.btndonations.onmouseover = swapImg;
			document.all.btndonations.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("applyforhelp.htm") == -1) {
			document.all.btnapplyforhelp.onmouseover = swapImg;
			document.all.btnapplyforhelp.onmouseout = swapImg;
		}
		if (document.location.href.indexOf("contactus.htm") == -1) {
			document.all.btncontactus.onmouseover = swapImg;
			document.all.btncontactus.onmouseout = swapImg;
		}
	}
}

// The following functions are in ALPHABETICAL ORDER to make visual scanning easier ----------------------

function swapImg() {
	/* Swap images during mouse events.  We add or remove "_over" as needed.
		This makes for very little code on both the html and javascript sides,
		although it is admittedly limited, requiring an image naming convention. 
		As an added bonus, text in a "status" tag will appear in the status bar.*/
	var overLoc, dotLoc, imgId, imgObj
	if (this.id.slice(0, 1) == "a") {	// This is an anchor, so deal with it's image
		imgId = this.iref;				// (image id grabbed from required "iref" custom attribute)
		eval("imgObj = document.all." + imgId);
		//imgObj = document.all.eval(imgId);
	} else { // This is an img, so deal with it directly!
		imgObj = this;
	}
	overLoc = imgObj.src.lastIndexOf("-over");
	dotLoc = imgObj.src.lastIndexOf(".");
	if (overLoc >= 0) {	// If we found "-over", then remove it from the image name:
		imgObj.src = imgObj.src.slice(0, overLoc) + imgObj.src.slice(dotLoc);
		window.status = "";
	} else {			// otherwise, add "-over" into the image name:
		imgObj.src = imgObj.src.slice(0, dotLoc) + "-over" + imgObj.src.slice(dotLoc);
		if (this.status) {
			window.status = this.status; // Show whatever is in the "status" custom attribute
		}
	}
	return true;
}

function preloadImages() { // Preloads over images for specified images on the page
	preload(document.all.btnhome);
	preload(document.all.btnaboutus);
	preload(document.all.btnsuccessstories);
	preload(document.all.btnphotogallery);
	preload(document.all.btnfaq);
	preload(document.all.btndonations);
	preload(document.all.btnapplyforhelp);
	preload(document.all.btncontactus);
}

function preload(obj) {	// Preloads a particular over image into browser cache for faster image swaps
	var preloader = new Image();
	dotLoc = obj.src.lastIndexOf(".");
	if (obj.src.slice(- 9, -4) != "-down") // Skip images that are in the down state (we're on their page)
		preloader.src = obj.src.slice(0, dotLoc) + "-over" + obj.src.slice(dotLoc);
}