// JavaScript Document


function email_enkoder(username, subject, linktext){ 
	if (linktext){
		document.write('<a href="mailto:' + username + '@swarthmore.edu?subject=' + subject + '">' + linktext + '</a>' ); 
	}
	else{
		document.write('<a href="mailto:' + username + '@swarthmore.edu?subject=' + subject + '">' + username + '@swarthmore.edu' + '</a>' ); 
	}
}


function jumpTo(selObj){

  var index = selObj.selectedIndex;
  selObj.selectedIndex=0;
  window.location= selObj.options[index].value;

}

	


function init() {

	if (!document.getElementById) { return; }
		
	// linkify spam-resistant email addresses
	iTagsArray = document.getElementById("Content").getElementsByTagName("i"); //grab all addresses in the content div
	for (i = 0; i < iTagsArray.length; i++){
		iTag = iTagsArray[i];
		
		if (iTag.className == 'email'){  //make sure they aren't just regular address tags
			spanTagsArray = iTag.getElementsByTagName("span");  //grab the first span. it has the username in it.				
		
			switch (spanTagsArray.length){
				case 1: //just username
					username = spanTagsArray[0].innerHTML.replace(/\s+/g,'');
					domain = 'swarthmore';
					tld = 'edu';
					linkText  = username + '@' + domain + '.' + tld;
					break;
				case 2: //username and title
					username = spanTagsArray[1].innerHTML.replace(/\s+/g,'');
					domain = 'swarthmore';
					tld = 'edu';
					linkText  = spanTagsArray[0].innerHTML;
					break;
				case 3: //username and non-swat email address
					username = spanTagsArray[0].innerHTML.replace(/\s+/g,'');
					domain = spanTagsArray[1].innerHTML.replace(/\s+/g,'');
					tld = spanTagsArray[2].innerHTML;
					linkText  = username + '@' + domain + '.' + tld;
					break;
				case 4: //username, title and non-swat email address
					username = spanTagsArray[1].innerHTML.replace(/\s+/g,''); //grab text and strip out whitespace
					domain = spanTagsArray[2].innerHTML.replace(/\s+/g,'');
					tld = spanTagsArray[3].innerHTML.replace(/\s+/g,'');
					linkText = 	spanTagsArray[0].innerHTML;
					break;
				default:
					username = 'webdev';
					domain = 'swarthmore';
					tld = 'edu';
					linkText = 'Error!';
					break;
			}
			
			iTag.innerHTML = '<a href="mailto:' + username + '@' + domain + '.' + tld + '">' + linkText + '</a>';
		}
	}

	//highlight "current" sub-navigation links
	liArray = document.getElementById("PrimaryNav").getElementsByTagName("li");
	for (i = 0; i < liArray.length; i++){
		liElement = liArray[i];
		
		if (liElement.className == "currentShowNestedNav"){ // if sublinks are being shown
			liLinkArray = liElement.getElementsByTagName("a");
			
			for (j = 1; j < liLinkArray.length; j++){ //for all subnav links
				indexOfLastPeriod = liLinkArray[j].href.lastIndexOf(".");
				linkSubstring = liLinkArray[j].href.substring(0, indexOfLastPeriod); // spanish/alumni.html -> spanish/alumni
				isParent = (location.href.indexOf(linkSubstring) != -1) // search for spanish/alumni in spanish/alumni.html#marc or spanish/alumni-details.html or spanish/alumni/gallery/
				if (isParent) //if you are on the same page as one of the links, or a subpage of one of the links
					liLinkArray[j].parentNode.className = "currentNestedLink"; //set the class on the li for greater CSS flexibility 
			}
		}
		
		if (liElement.className == ""){ // if it is an "AdditionalPrimayNav" item (otherwise class=none)
			liLink = liElement.getElementsByTagName("a")[0];
			if (liLink.href == location.href) //if you are on the same page as one of the links
					liLink.parentNode.className = "currentAdditionalPrimaryNavLink"; //set the class on the li for greater CSS flexibility. use a new class name to avoid issues with .imagereplacedText .current
		}
		
		
		/*
		if (liElement.className == "hideNestedNav" || liElement.className == "currentShowNestedNav"){ //if the menu item has nested elements
			liLinkArray = liElement.getElementsByTagName("a");
			liLink = liLinkArray[0];
			liLink.onclick = function () {
				//alert("class: " + this.parentNode.className + " id: " + this.parentNode.id);
				if (this.parentNode.className=="hideNestedNav"){
					this.parentNode.className="showNestedNav";
				}
				else if (this.parentNode.className=="showNestedNav") {
					this.parentNode.className="hideNestedNav";
				}
				else if (this.parentNode.className=="currentHideNestedNav"){
					this.parentNode.className="currentShowNestedNav";
				}
				else if (this.parentNode.className=="currentShowNestedNav") {
					this.parentNode.className="currentHideNestedNav";
				} 
				return false;
			}
		}
		*/
		
	}
	

}