// copyright EEOC.com 2006 (requires express written permission)
//makes tabbed content boxes (can handle multiple navBoxes and tabbed ajax pages)
//requires setCookie,getCookie,classExists,ALTgetElementById,addOnloadEvent in library
var enableNavboxCookie	= true; //use a cookie to remember last tab clicked?
var navboxIDlist		= new Object();//array to hold list of ids for each content box ('boxid') in a given navBox id (separate list for each navBox)
//ajax only
var enableAjaxCache		= true; //stop potential caching of external pages after initial request
var ajaxInProgress		= "<img src='loading.gif' /> Requesting content...";
var ajaxExtrasLoaded="";var ajaxCacheKiller="";

var navBox={//navBox div id is called 'id', the content box for a given tab is called 'boxid'
	init: function () {//ex. call via addOnloadEvent(function(){navBox.init('navBox1','navBox2')});
		if (!document.getElementById&&!document.all){return;}//must use these to handle this navBox function
		for (var i=0; i<arguments.length; i++){ //loop through each navBox div 'id' (ex.navBox1) - handles multiple navBoxes on one page
			var id=arguments[i];var boxidLoaded=false;var init_url,init_boxid,init_tabLink;

			//check cookie
			var boxidCookie=getCookie(id) //if cookie exists for this navBox id, get 'boxid' of last tab clicked
			if (!enableNavboxCookie && boxidCookie!="") setCookie(id, "") //if not enabled, delete cookie

			//look thru each 'tabLink', find all 'boxid's in LI link 'rel', save to array
			//add onclick to each 'tabLink' LI A tag with a 'rel' attribute
			//show an initial tab:(1st choice - last tab clicked per cookie)(2nd - use li class="navBoxOn")(3rd - first tab)
			var tabUL=ALTgetElementById(id).getElementsByTagName("ul")[0];
			var tabLIs=tabUL.getElementsByTagName("li") //array with LIs within this UL
			for (var x=0; x<tabLIs.length; x++){ //look thru each tab LI
				var tabLink=tabLIs[x].getElementsByTagName("a")[0];
				if (tabLink.getAttribute("rel")){
					var boxid=tabLink.getAttribute("rel");//alert('id:'+id+' boxid:'+boxid);
					navBox.addToIDList(id,boxid) //save boxid of each tab content as loop runs

					tabLink.onclick=function(){
						id=this.parentNode.parentNode.parentNode.id;
						navBox.showBox(id,this.getAttribute("rel"),this);
						return false;
					}
					//show default tab box (use li class='navBoxOn') if no cookie exists
					if (classExists(tabLIs[x],'navBoxOn') && boxidCookie==""){
						init_boxid=boxid;init_tabLink=tabLink;
					}
				}
			}
			 //see if a tab has been previously clicked on per the cookie 'box'id', then show the 'boxid' content
			if (enableNavboxCookie&&boxidCookie!=""){
				//verify boxid is valid for this id's link rel, then show boxid from cookie
				init_boxid=navBox.splitCookie(boxidCookie)[0];
				init_url=navBox.splitCookie(boxidCookie)[1];//alert('boxid'+boxid+' url '+url);
				init_tabLink=navBox.tabLink(id,init_boxid,init_url);
			}
			if (init_boxid){// show content if from cookie or using 'navBoxOn' class
				navBox.showBox(id,init_boxid,init_tabLink); //load tab content selected initially
				boxidLoaded=true;//this should probably be inside the showBox function with error checking
			}
			if (!boxidLoaded){navBox.showBox(id,'',tabLIs[0].getElementsByTagName("a")[0]);}//just load first tab
		} 
	},
	tabUL: function (id) {//returns the tab UL element
		var UL=ALTgetElementById(id).getElementsByTagName("ul")[0];return UL;
	},
	tabLink: function (id,boxid,url){//returns the tab link element based on a match for ' href' url and 'boxid' of content
		var UL=navBox.tabUL(id);
		var LIs=UL.getElementsByTagName("li");
		for (var i=0; i<LIs.length; i++){
			var tabLink=LIs[i].getElementsByTagName("a")[0];
			if (tabLink.getAttribute("rel")==boxid){
				if (url){if (tabLink.getAttribute("href")==url){return tabLink;break;}}
				else {return tabLink;break;}
			}
		}
	},
	addToIDList: function(id,boxid){// save 'boxid's of tab content divs for this TabBox div 'id'
		if (typeof navboxIDlist[id]=="undefined") navboxIDlist[id]=new Array();//if this array doesn't exist yet
		navboxIDlist[id][navboxIDlist[id].length]=boxid;
	},
	showBox: function(id,boxid,tabLinkObj){//display one content box for a given TabBox div
		if (typeof tabLinkObj=="undefined"){navBox.showTab(id,0);return;}
		var url;
		if (tabLinkObj.getAttribute("href")) {url=tabLinkObj.getAttribute("href");}
		else {url='';}
		//alert('showme boxid:'+boxid+' url '+url+' id:'+id);
		if (boxid.indexOf("ajax")!=0)url='';//if rel begins with 'ajax' sign, its a real url
		if (boxid&&url){navBox.saveCookie(id,boxid,url);}
		navBox.hideBoxes(id,tabLinkObj);
		tabLinkObj.parentNode.className="navBoxOn";  //make clicked LI tab active
		if (boxid) {ALTgetElementById(boxid).style.display="block"; }//show this tab content box
		if (url!="") {// if ajax, get content and any accompanying js/css files
			navBox.ajaxGetPage(url,boxid);
			if (tabLinkObj.getAttribute("rev")){navBox.ajaxGetExtras(tabLinkObj.getAttribute("rev"));}
		}
	},
	hideBoxes: function(id,tabLinkObj){//hide all content boxes in a TabBox div, and make all tabs inactive (off)
		var tabUL=tabLinkObj.parentNode.parentNode //get UL for this tabLink
		var tabLIs=tabUL.getElementsByTagName("li") //get list of LIs corresponding to the tab contents
		for (var i=0; i<tabLIs.length; i++){tabLIs[i].className="";} //make all tabs (LI) inactive
		for (i=0;i<navboxIDlist[id].length;i++){
			ALTgetElementById(navboxIDlist[id][i]).style.display="none" //hide all tab content boxes for this id
		}
	},
	splitCookie: function(value){
			var tmp=value.split("||");var boxid=tmp[0];var url=tmp[1];return [boxid,url];tmp="";
	},
	saveCookie: function(id,boxid,url){//save cookie with TabBox div 'id' and the content div 'boxid' and the url, if any
		var value=boxid+'||';if (url)value+=url;
		if (enableNavboxCookie)setCookie(id,value);//boxidCookie=id;
	},
	showTab: function(id,tabnumber){ //allow hyperlink to show a tab/content box
		var tabLink=ALTgetElementById(id).getElementsByTagName("a")[tabnumber];
		var boxid=tabLink.getAttribute("rel");
		if (boxid){navBox.showBox(id,boxid,tabLink);boxidLoaded=true;}
		else {return false;}
	},
	ajaxGetPage: function(url,boxid){//get/load the ajax page
		var ajaxhttp = false;
		if (window.XMLHttpRequest){ajaxhttp=new XMLHttpRequest();} // preferred IE7up,FF/Mozilla, Safari etc
		else if (window.ActiveXObject){ // if IE6 and prior (wont work if ActiveX has been disabled)
			try {ajaxhttp=new ActiveXObject("Msxml2.XMLHTTP");} 
			catch (e){try{ajaxhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){}}
		}
		else {return false;}

		if (ajaxInProgress) ALTgetElementById(boxid).innerHTML=ajaxInProgress;//show 'in progress' while waiting
		ajaxhttp.onreadystatechange=function(){navBox.ajaxInsertPage(ajaxhttp,boxid);}
		if (!enableAjaxCache){ajaxCacheKiller=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();}
		ajaxhttp.open('GET', url+ajaxCacheKiller, true);
		ajaxhttp.send(null);
	},
	ajaxInsertPage: function(ajaxhttp,boxid){//insert ajax page into content box on this page
		if (ajaxhttp.readyState == 4 && (ajaxhttp.status==200 || window.location.href.indexOf("http")==-1))
		ALTgetElementById(boxid).innerHTML=ajaxhttp.responseText;
	},
	ajaxGetExtras: function (revattribute){//load external .js or .css files (if any) for a given ajax content box
		if (revattribute==null||revattribute==""){return;} //if "rev" attribute is defined (load external .js or .css files)
		var ajaxExtrasList=revattribute.split(/\s*,\s*/) //split the files and store as array
		for (var i=0; i<ajaxExtrasList.length; i++){
			var file=ajaxExtrasList[i];var fileref="";
			if (ajaxExtrasLoaded.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
				if (file.indexOf(".js")!=-1){ //If object is a js file
					fileref=document.createElement('script')
					fileref.setAttribute("type","text/javascript");
					fileref.setAttribute("src", file);
				}
				else if (file.indexOf(".css")!=-1){ //If object is a css file
					fileref=document.createElement("link")
					fileref.setAttribute("rel", "stylesheet");
					fileref.setAttribute("type", "text/css");
					fileref.setAttribute("href", file);
				}
			}
			if (fileref!=""){
				document.getElementsByTagName("head").item(0).appendChild(fileref)
				ajaxExtrasLoaded+=file+" " //Add to extras loaded list - has been loaded into ajax content box already
			}
		}
	}
};

