//** Ajax Tabs Content script v2.0- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
//** Updated Oct 21st, 07 to version 2.0. Contains numerous improvements
//** Updated Feb 18th, 08 to version 2.1: Adds a public "tabinstance.cycleit(dir)" method to cycle forward or backward between tabs dynamically. Only .js file changed from v2.0.
//** Updated April 8th, 08 to version 2.2:
//   -Adds support for expanding a tab using a URL parameter (ie: http://mysite.com/tabcontent.htm?tabinterfaceid=0) 
//   -Modified Ajax routine so testing the script out locally in IE7 now works 

var ddajaxtabssettings={}
ddajaxtabssettings.bustcachevar=1  //bust potential caching of external pages after initial request? (1=yes, 0=no)
ddajaxtabssettings.loadstatustext="<img src='ajaxtabs/loading.gif' /> Requesting content..." 


////NO NEED TO EDIT BELOW////////////////////////

function ddajaxtabs(tabinterfaceid, contentdivid){
	this.tabinterfaceid=tabinterfaceid //ID of Tab Menu main container
	this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container
	this.revcontentids=[] //Array to store ids of arbitrary contents to expand/contact as well ("rev" attr values)
	this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link")
}

ddajaxtabs.prototype={

	expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers
		var tabref=""
		try{
			if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr
				tabref=document.getElementById(tabid_or_position)
			else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr
				tabref=this.tabs[tabid_or_position]
		}
		catch(err){alert("Invalid Tab ID or position entered!")}
		if (tabref!="") //if a valid tab is found based on function parameter
			this.expandtab(tabref) //expand this tab
	},
	
	expanditEx:function(category, tabid){ //PUBLIC function to select a tab either by its ID or position(int) within its peers
		var tabref=""
		try{
			if (typeof tabid=="string" && document.getElementById(tabid).getAttribute("rel")) //if specified tab contains "rel" attr
				tabref=document.getElementById(tabid)
			else if (parseInt(tabid)!=NaN && this.tabs[tabid].getAttribute("rel")) //if specified tab contains "rel" attr
				tabref=this.tabs[tabid]
		}
		catch(err){alert("Invalid Tab ID entered!")}
		if (tabref!="") //if a valid tab is found based on function parameter
			this.expandtabEx(tabref) //expand this tab
	},	

	setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link")
		this.selectedClassTarget=objstr || "link"
	},

	getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to
		return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref
	},

	urlparamselect:function(tabinterfaceid){
		var result=window.location.search.match(new RegExp(tabinterfaceid+"=(\\d+)", "i")) //check for "?tabinterfaceid=2" in URL
		return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index
	},

	expandtab:function(tabref){
		//Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easy searching through
		this.framedisplay(tabref.getAttribute("href"), tabref.getAttribute("rel"))
		
		for (var i=0; i<this.tabs.length; i++)
		{ //Loop through all tabs, and assign only the selected tab the CSS class "selected"
			if (this.tabs[i].getAttribute("href")==tabref.getAttribute("href")) {
				this.getselectedClassTarget(this.tabs[i]).className = "selected"	
				if (tabref.getAttribute("toggle")=="1"){
					var href = "subhead.php?toggle=1&tab=" + tabref.getAttribute("id") + "&cat=" + tabref.getAttribute("category")
					this.framedisplay(href,"subhead")
				}					
			}
			else {
				this.getselectedClassTarget(this.tabs[i]).className = ""
			}
		}		
	},
	
	expandtabEx:function(tabref){
		//Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easy searching through
		this.framedisplay(tabref.getAttribute("href"), tabref.getAttribute("rel"))
		
		for (var i=0; i<this.tabs.length; i++)
		{ //Loop through all tabs, and assign only the selected tab the CSS class "selected"
			if (this.tabs[i].getAttribute("href")==tabref.getAttribute("href")) {
				this.getselectedClassTarget(this.tabs[i]).className = "selected"				
			}
			else {
				this.getselectedClassTarget(this.tabs[i]).className = ""
			}
		}		
	},	

	framedisplay:function(pageurl, frameName){
		parent[frameName].location.href=pageurl
	},	

	init:function(){
		var selectedtab=-1 //Currently selected tab index (-1 meaning none)
		for (var i=0; i<this.tabs.length; i++){		
			this.tabs[i].tabposition=i //remember position of tab relative to its peers
			if (this.tabs[i].getAttribute("rel")){
				var tabinstance=this
				this.tabs[i].onclick=function(){
					tabinstance.expandtab(this)
					return false
				}
				if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element
					this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/))
				}
				if (selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){
					selectedtab=i //Selected tab index, if found
				}
			}
		} //END for loop			
		//if (selectedtab!=-1) { //if a valid default selected tab index is found
		//	this.expandtab(this.tabs[selectedtab]) //expand selected tab (either from URL parameter, persistent feature, or class="selected" class)
		//}
	} //END int() function

} //END Prototype assignment
