//touchless javascript (no event handlers in html)
window.onload = kickOff;
var currentNav;
var currentSub;
var allA;
var mBox;
var eDomain;
var mDomain = "liveoaksf.org";

function kickOff() {
    //detect nav area and subnav section from body id
    var bodyId = document.getElementsByTagName("body")[0].id;
    currentNav = bodyId.substr(0,4);
    currentSub = bodyId.charAt(8);
    //set current subnav section to "here" state and disable link for current
    if (document.getElementById("subnav")) {
        var subnavObj = document.getElementById("subnav");
        subnavObj.getElementsByTagName("a")[currentSub].style.color = "#cc6600";
        subnavObj.getElementsByTagName("a")[currentSub].style.borderBottom = "0";
        subnavObj.getElementsByTagName("a")[currentSub].onclick = doDisable;
    }
    //remove outlines
    allA = document.getElementsByTagName("a");
    for (var i=0; i<allA.length; i++) {
        allA[i].onfocus = doBlur;
        //protect email address from spambots
		// new code
		if (allA[i].href.indexOf("mailto:") != -1) {
        	mBox = allA[i].href.substr(7);	
			if (allA[i].getAttribute('title')) {  
				eDomain = allA[i].getAttribute('title');
       			allA[i].href = "mailto:" + mBox + "@" + eDomain;
       			allA[i].firstChild.nodeValue = mBox + "@" + eDomain;
	    	}
			else {     
           	 allA[i].href = "mailto:" + mBox + "@" + mDomain;
           	 allA[i].firstChild.nodeValue = mBox + "@" + mDomain;
        	}
		}
		
		
		// end new code

//        if ((allA[i].href.indexOf("mailto:") != -1) && (allA[i].href.indexOf("@") == -1)) {
//            mBox = allA[i].href.substr(7);
//            allA[i].href = "mailto:" + mBox + "@" + mDomain;
//            allA[i].firstChild.nodeValue = mBox + "@" + mDomain;
//        }



		// automatically inserts external link or pdf icon
		if (allA[i].firstChild && allA[i].firstChild.nodeName == "IMG") {
			continue;
		}
		if (allA[i].href.indexOf(".pdf") != -1) {
			linkImg(i,"url(/images/pdflink.gif)","100% 3px","no-repeat","13px","5px");
		}		
		else if (allA[i].href.indexOf("npo.networkforgood.org") == -1 && allA[i].href.indexOf("liveoaksf.org") == -1 || allA[i].href.indexOf("library.liveoaksf.org") != -1)  {
			linkImg(i, "url(/images/extlink.gif)","100% 3px","no-repeat","13px","5px");
		}





    }
    //set current side column subsection to "here" and disable link for current
    if (document.getElementById("sidecol")) {
        var sidecolObj = document.getElementById("sidecol");
        var sidecolA = sidecolObj.getElementsByTagName("a") ;
        for (var i=0; i<sidecolA.length; i++) {
            if (sidecolA[i] == window.location.href) {
                sidecolA[i].style.borderBottom = "none";
                sidecolA[i].style.color = "#883b00";
                sidecolA[i].onclick = doDisable;
                sidecolObj.style.backgroundColor = "#ffdd55";
                /* sidecolObj.style.borderBottom = "1px solid #ffffff"; */
            }
        }
    }
    //find all img srcs with off in their names -- they have a corresponding overs
    var allImg = document.getElementsByTagName("img");
    for (var i=0; i<allImg.length; i++) {
        if (allImg[i].src.indexOf("_off") != -1) {
            //if body nav id = img id, set tab image to indicate "here" status
            if (allImg[i].id == currentNav) {
                allImg[i].src = allImg[i].src.replace("_off","_over");
                allImg[i].onclick = doDisable;
                //allImg[i].onfocus = doBlur;
            }
            //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;
        }
    }
    //set rollover behavior for go button
    document.getElementById("submit").onmouseover = doMouseover;
    document.getElementById("submit").onmouseout = doMouseout;
    //load extra js files for specialty pages
    if (bodyId == "nav0_sub0") {
        goHome();
    }
    if (this.location.href.indexOf("faculty_staff") != -1) {
        goFaculty();
    }
    if (this.location.href.indexOf("academics/curriculum") != -1) {
        goMatrix();
    }
    if (this.location.href.indexOf("schoollife/art") != -1) {
        goArt();
    }
    if (this.location.href.indexOf("academics/camp") != -1) {
        goSlide("academics","camp",7);
    }
    if (this.location.href.indexOf("look/lowerschool") != -1) {
        goSlide("look","ls",13);
    }
    if (this.location.href.indexOf("look/middleschool") != -1) {
        goSlide("look","ms",11);
    }
	if (this.location.href.indexOf("look/summeroaks") != -1) {
		goSlide("look","so",17);
    }
    if (this.location.href.indexOf("calendar") != -1) {
        goCalendar();
    }
    if (this.location.href.indexOf("cmaint") != -1) {
        goCalendar();
    }
    if (this.location.href.indexOf("class_reps") != -1) {
        goCalendar();
    }
    if (this.location.href.indexOf("lopa") != -1) {
        goLopa();
    }
}

function doMouseover() {
    //disable mouseover for current nav (area)
    if (this.id == currentNav) {
        return false;
    }
    //rollover
    this.src = this.src.replace("_off","_over");
    //was used when contact menu changed on rollover
	//if (this.id == "nav6") {
    //    document.getElementById("contact").style.zIndex = 5;
    //}
    return true;
}

function doMouseout() {
    //disable mouseout for current nav (area)
    if (this.id == currentNav) {
        return false;
    }
    //rolloff
    this.src = this.src.replace("_over","_off");
    //was used when contact menu changed on rollover
	//if (this.id == "nav6") {
    //    document.getElementById("contact").style.zIndex = 3;
    //}
	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 linkImg (i,url,pos,rep,pad,mar) {
	allA[i].style.backgroundImage = url;
	allA[i].style.backgroundPosition = pos;
	allA[i].style.backgroundRepeat = rep;
	allA[i].style.paddingRight = pad;
	allA[i].style.marginRight = mar;
}
