/************************************
/
/  Tab Box Widget
/  version 1.02
/  (c)2004 Yahoo!
/  author: Eric Miraglia
/  dependencies: ygBrowser [http://l.yimg.com/a/lib/g/yg_browser.js]
/ 				 css declarations (see documentation)
/  support: 	 DOM-cabable browsers; should degrade gracefully on older browsers or when errors occur
/
/  versions: 	 1.01 converted to LSM
			     1.02 added interstitial element (for pipe-separated tabs); added option "none" to hide main title
/************************************/
function oTabBox (elParent,aContentEls,sTitlePos,nCurrentTab) {
	this.elParent = (document.getElementById(elParent)) ? document.getElementById(elParent) : false;
	this.elParentHTML = this.elParent.innerHTML;
	this.nCurrentTab = nCurrentTab;
	this.aContentEls = aContentEls;
	this.sTitlePos = ((sTitlePos=="top")||(sTitlePos=="left")||(sTitlePos=="none")) ? sTitlePos : "top";
	this.isDrawn = false;
	this.verified = false;
	this.elTabs = new Array();
	this.aTabTitles = new Array();
	this.quiet = true;
	this.warnings = "";

	this.verify = function verify() {
		/*check for title div and get title; check arrays and lengths*/
		try {
			if(this.sTitlePos!="none") {
				this.titleDiv = (this.getFirstEl(this.elParent,"yspTabBoxTitle"));
				if (this.titleDiv) {
						this.sTitle=this.titleDiv.innerHTML;
				} else {
						this.sTitle="";
						this.logError("<b>No title div</b>: No title div was found.  A div with the class name yspTabBoxTitle should be a child of the main tab box div; we couldn't find it.")
						this.restoreOriginalState();
				}
			}

			if ((nCurrentTab<aContentEls.length)&&(nCurrentTab>-1)) {
				/*check that we can get the content els*/
				for (ce=0;ce<aContentEls.length;ce++) {
					this.elTabs[ce]=document.getElementById(aContentEls[ce]);
					var t=this.elTabs[ce].id; //forces error if we failed to get the element
				}
				return true;
			} else {
				this.logError(" Invalid input; the current tab index (" + nCurrentTab + ") must be a number between zero and the number of content elements minus one.");
				this.restoreOriginalState();
				return false
			}
		} catch (e) {
			this.logError(" An unexpected error was encountered while verifying input (JS Error: " + e +")");
			this.restoreOriginalState();
			return false
		}
	}

	this.checkBrowser = function checkBrowser() {
		try {
			if(oBw.dom) {
				if ((oBw.mac)&&(!oBw.op)&&(oBw.agt.indexOf("msie")!=-1)) {
					//MacIE 5.0 is buggy; no support
					this.browserSupport=(oBw.agt.indexOf("5.0")>0)?"none":"full";
				} else {
					//all other DOM compliant browsers get full
					this.browserSupport="full";
				}
			} else {
				this.browserSupport="none";
			}
		} catch (e) {
			this.logError(" There was an error checking for browser support; remember that yg_browser needs to be included prior to creating a new oTabBox object (JS Error: " + e +")");
			this.restoreOriginalState();
		}
	}

	this.checkBrowser();

	this.drawTabBox = function drawTabBox() {
		if (this.browserSupport=="full") {
		this.verified=this.verify();
		if(!this.verified) {return false};
		try {
			if (this.isDrawn) {
				this.elTabCanvas.innerHTML = "";
			} else {
				if (this.sTitlePos=="top") {
					if (this.sTitle!="") {
						this.elTitle = makeElement("div","yspTabBoxTitle","<div class='yspTabBoxTitleText'>" + this.sTitle + "</div	>",this.elParent);
						this.elTitle.style.display = "block";
					}
				}
				this.elTabCanvas=makeElement("div","yspTabBoxCanvas","",this.elParent);
			}
			this.interstice=(this.getFirstEl(this.elParent,"yspTabBoxInterstice"))?(this.getFirstEl(this.elParent,"yspTabBoxInterstice").innerHTML):false; /*interstitial element -- if present, place the contents of this el, styled as yspTabBoxInterstice, between each tab*/
			this.elTabLSpace=makeElement("div","yspTabBoxLspace","<spacer type=block height=1>",this.elTabCanvas);
			if (this.sTitlePos=="left") {this.elTabLSpace.innerHTML=this.sTitle;}
			for(t=0;t<this.aContentEls.length;t++) {
				if((this.interstice)&&(t>0)) {
					var newInterstice=makeElement("div","yspTabBoxInterstice",this.interstice,this.elTabCanvas);
				}
				var thisTabTitle = (this.getFirstEl(this.elTabs[t],"yspTabBoxTabTitle")) ? this.getFirstEl(this.elTabs[t],"yspTabBoxTabTitle").innerHTML : "[tab title]";
				var onOrOff = (t==this.nCurrentTab) ? "yspTabBoxTabOn" : "yspTabBoxTabOff";
				this.elTabs[t]=makeElement("div",onOrOff,"",this.elTabCanvas);
				var textChild=makeElement("span","",thisTabTitle,this.elTabs[t]);
				if (t!=this.nCurrentTab) {
					document.getElementById(this.aContentEls[t]).style.display="none";
					this.elTabs[t].id = "tabBox-" + t + "-" + elParent;
					textChild.id = "tabBox-" + t + "-" + elParent + ".child";
					textChild.className = "yspTabBoxTabOffText";
					this.elTabs[t].cursor = "hand";
					this.elTabs[t].tabBoxObject = this;
					if ((oBw.mac&&oBw.ie)&&(!oBw.op)) {
						this.elTabs[t].innerHTML="<a onclick='tabClick()'>" + thisTabTitle + "</a>";
					} else {
						this.addTabEvents(this.elTabs[t]);
					}
				} else {
					textChild.className = "yspTabBoxTabOnText";
					if ((oBw.mac&&oBw.ie)&&(!oBw.op)&&(!this.isDrawn)) {
						this.elBr=makeElement("br","","",this.elParent);
						this.elBr.clear="all";
					}
					this.elParent.appendChild(document.getElementById(this.aContentEls[t]));/*moves content to bottom of box*/
					document.getElementById(this.aContentEls[t]).style.display="block";
				}

			}

			/*turn off display of the semantic header elements from which we've drawn box and tab titles*/
			if(this.titleDiv) {this.titleDiv.style.display = "none";}
			for (ce=0;ce<this.elTabs.length;ce++) {
				this.getFirstEl(document.getElementById(aContentEls[ce]),"yspTabBoxTabTitle").style.display = "none";
			}
			/*finish*/
			this.isDrawn = true;
			return true;
		} catch (e) {
			this.logError(" There was an error drawing the tab box (JS Error: " + e +")");
			this.restoreOriginalState();
		}
	}
	}

	this.addTabEvents = function addTabEvents(elTarget) { //used to attach event listeners
		if (document.all) { //IE uses a slightly different syntax to attach events
			elTarget.attachEvent("onclick", tabClick);
		} else { //this is the W3C syntax for attaching events
			elTarget.addEventListener("click",tabClick,false);
		}
	}

	this.logError = function logError(msg) {
		this.warnings += msg;
		if (!this.quiet) {document.write("<b>Tab Box</b>: " + this.warnings);}
	}

	this.getFirstEl = function getFirstEl(elPar,sClassname) {
		try {
			oChildren=elPar.childNodes;
			for(gfe=0;gfe<oChildren.length;gfe++) {
				//alert(oChildren[gfe].className);
				if (oChildren[gfe].className==sClassname) {return oChildren[gfe];}
			}
			return false
		} catch(e) {
			this.logError(" There was an error finding the title element for one of the tabs (JS Error: " + e +")")
		}
	}

	this.restoreOriginalState = function restoreOriginalState() {
		try {
			/*tab creation failed; try to put things right for semantic fallback display*/
			this.browserSupport="none"; //no longer try to execute tab actions unless page is reloaded
			this.elParent.innerHTML = this.elParentHTML; //restore original HTML
			this.getFirstEl(this.elParent,"yspTabBoxTitle").style.display = "block"; //turn on main title
			/*ensure that content els and their titles aren't left hidden*/
			for (ce=0;ce<this.elTabs.length;ce++) {
				document.getElementById(aContentEls[ce]).style.display = "block";
				this.getFirstEl(document.getElementById(aContentEls[ce]),"yspTabBoxTabTitle").style.display = "block";
			}
		} catch (e) {
			this.logError(" Restoration of original state (in response to an error) failed, generating another error (JS Error: " + e +")");
		}
	}
}

function makeElement(sType,elClass,elHTML,elAppendTo) {
	var newEl = document.createElement(sType);
	elAppendTo.appendChild(newEl);
	if (elClass!="") {newEl.className=elClass;}
	if (elHTML!="") {newEl.innerHTML=elHTML;}
	return newEl;
}

function tabClick(evt) {
	try {
		if(document.all) {
			var sTabId=window.event.srcElement.id;
			var sParentId=window.event.srcElement.parentNode.id;
		} else if (evt.target) {
			var sTabId=evt.target.id;
			var sParentId = evt.target.parentNode.id;
		}
		if (sParentId) {//may have gotten text node instead of div
			if (sParentId.indexOf('tabBox-') != -1) {
				sTabId = sParentId;
			}
		}
		//alert(sTabId);
		if((sTabId)&&(sTabId.charAt(7))) {
			var nTab=sTabId.charAt(7);
			if (sTabId.indexOf(".child")>-1) {
				aTabId=sTabId.split(".ch");
				sTabId=aTabId[0];
				//alert(sTabId);
			}
			document.getElementById(sTabId).tabBoxObject.nCurrentTab=nTab;
			document.getElementById(sTabId).tabBoxObject.drawTabBox();
		}
	} catch (e) {
		alert(" There was an error processing the tab click (JS Error: " + e +")");
	}
}