//touchless javascript (no event handlers in html)

var whichArt = "headerart";
var allImg;
var allA;

window.onload = startGlobal;
window.onunload = restorePage;

function startGlobal() {
    allImg = document.getElementsByTagName("img");
    allA = document.getElementsByTagName("a");
	pngFix(); // make transparent pngs work in IE
	// any divs hidden until postload (pngs, scrollbox) are made visible:
	var allDiv = document.getElementsByTagName("DIV");
	for(var i=0; i<allDiv.length; i++) {
		allDiv[i].style.visibility = "visible";
		if (allDiv[i].id == "homebase") {
			whichArt = "homeart";
			startScroll();
		}
	}
	artSwap(); // randomly choose homepage or hearder art
    //find all img srcs with off in their names -- they have a corresponding overs:
    for (var i=0; i<allImg.length; i++) {
        if (allImg[i].src.indexOf("_off") != -1) {
            //preload over images:
            var imgObj = new Image(); 
            imgObj.src = allImg[i].src.replace("_off","_over");
            //set rollover behaviors:
            allImg[i].onmouseover = doMouseover;
            allImg[i].onmouseout = doMouseout;
        }
    }
    // remove outlines from clicked nav elements:
    for (var i=0; i<allA.length; i++) {
        allA[i].onfocus = doBlur;
    }
    // set current global section to "here" highlight and disable reload click:
	var globalA = document.getElementById("global").getElementsByTagName("a");
    for (var i=0; i<globalA.length; i++) {
    	var aSplit = String(globalA[i]).split("_");
		if (window.location.href.indexOf(aSplit[0]) != -1) {
            if (globalA[i].firstChild.nodeName == "IMG") {
                globalA[i].firstChild.src = globalA[i].firstChild.src.replace("_off","_over");
                globalA[i].onclick = doDisable;
            }
        }
    }
    // set home image to on whether location is www.imbee.com or www.imbee.com/index.html
    if (whichArt == "homeart") {
		for (var i=0; i<allImg.length; i++) {
			if (allImg[i].src.indexOf("home_o") != -1) {
				allImg[i].src = allImg[i].src.replace("_off","_over");
			}
		}
	}
	// set current sub and sub global sections to "here" highlight and disable reload click:
    for (var i=0; i<allA.length; i++) {
		if (allA[i] == window.location.href) {
            if (allA[i].firstChild.nodeName == "IMG") {
                allA[i].firstChild.src = allA[i].firstChild.src.replace("_off","_over");
            }
            allA[i].style.borderBottom = "1px";
            allA[i].style.color = "#cc6600";
            allA[i].style.cursor = "text";
            allA[i].onclick = doDisable;
        }
    }
    if (this.location.href.indexOf("namegame") != -1) {
        startNamegame();
    }
    if (this.location.href.indexOf("signup") != -1) {
    	if (window.location.href.indexOf("?") != -1) {
    		document.getElementById("screenname").value = window.location.href.split("?")[1];
        }
    }
}

function pngFix() {	
	// use MSIE alpha image loader filter to achieve PNG transparency in Win IE 5.5 & 6.
	// DOModified version by http://www.industrious.com, based on http://homepage.ntlworld.com/bobosola.
	// if not MSIE, do nothing:
	if (navigator.appVersion.indexOf("MSIE") == -1) {
		return false;
	}
	// detect version number:
	var thisVersion = navigator.appVersion.split("MSIE")
	var versionNum = parseFloat(thisVersion[1])
	// apply MSIE alpha image loader filter:
	if ((versionNum >= 5.5 && versionNum < 7) && (document.body.filters)) {
		var allImg = document.getElementsByTagName("img");
		for(var i=0; i<allImg.length; i++) {
			if (allImg[i].src.indexOf(".png") != -1) {
				var thisImg = allImg[i];
				var imgTitle = "title='" + thisImg.alt + "' ";
				var imgStyle = "display:inline-block";
				if (thisImg.parentElement.href) {
					imgStyle = "cursor:pointer;" + imgStyle;
				}
				var strNewHTML = "<span " + imgTitle + " style=\"" + "width:" + thisImg.width + "px; height:" + thisImg.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + thisImg.src + "\', sizingMethod='image');\"></span>" 
				thisImg.outerHTML = strNewHTML;
				i = i-1;
			}
		}
	}
	return true;
}

// artSwap variables:
var artTotal = 3;
var randomNum = -1;
var prevNum = -1;
var artNum;

function artSwap() {
	// randomly choose home page or header art and show its associated number:
	randoMeter();
	document.getElementById(whichArt).firstChild.src = "images/" + whichArt + artNum + ".jpg";
	document.getElementById("artno").firstChild.src = "images/artno" + artNum + ".gif";
	// change alt attribute for number image on the fly
	var artnoAlt = "no. " + artNum;
	document.getElementById("artno").firstChild.setAttribute("alt",artnoAlt);
	if (whichArt == "homeart") {
		// set behavior for easteregg
		document.getElementById("easteregg").onclick = artSwap;
		document.getElementById("easteregg").style.cursor = "pointer";
		// will get rid of this when scroller is tidied up (doesn't fire in firefox anyway)
		window.onresize = reloadPage;
	}
}

function randoMeter() {
	// if random number is in use, rerandomize to prevent duplicates:
    while (randomNum == prevNum) {
        randomNum = Math.floor((Math.random() * artTotal));
    }
    prevNum = randomNum;
	artNum = randomNum + 1;
}

function doMouseover() {
    // disable mouseover for current nav:
    if (this.parentNode.href) {
		var locationSplit = window.location.href.split("_");
		var thisSplit = this.parentNode.href.split("_");
		if (locationSplit[0] == thisSplit[0] || (whichArt == "homeart" && this.src.indexOf("home_o") != -1)) {
			this.style.cursor = "default";
			return false;
		}
    }
    // rollover:
    this.src = this.src.replace("_off","_over");
    return true;
}

function doMouseout() {
    // disable mouseout for current nav: 
    if (this.parentNode.href) {
		var locationSplit = window.location.href.split("_");
		var thisSplit = this.parentNode.href.split("_");
		if (locationSplit[0] == thisSplit[0] || (whichArt == "homeart" && this.src.indexOf("home_o") != -1)) {
			this.style.cursor = "default";
			return false;
		}
    }
    // rolloff:
    this.src = this.src.replace("_over","_off");
    return true;
}

function doDisable() {
    // get rid of focus highlight from clicked link and disable link for current nav and subnav:
    if (this.blur) {
        this.blur();
    }
    return false;
}

function doBlur() {
    // get rid of focus highlight from clicked link and disable link for current nav and subnav:
    this.blur();
}

function restorePage() {
	// reset all images to off to unstick for back button events:
	for (var i=0; i<allImg.length; i++) {
    	allImg[i].src = allImg[i].src.replace("_over","_off")
    }
}
