// -------------------------------------------------------------------
// Switch Content Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: Jan 5th, 2007
// -------------------------------------------------------------------

function switchcontent(className, filtertag){
	this.className=className
	this.collapsePrev=false // Collapse previous content each time
	this.persistType="none" // Disable persistence
	this.filter_content_tag=(typeof filtertag!="undefined")? filtertag.toLowerCase() : ""
}

switchcontent.prototype.setStatus=function(openHTML, closeHTML){ // Set open/closing HTML indicator. Optional
	this.statusOpen=openHTML
	this.statusClosed=closeHTML
}

switchcontent.prototype.setPersist=function(bool){ // Enable/disable persistence. Default is false.
	if (bool==true){
		this.persistType="session"
	}
	else
		this.persistType="none"
}

switchcontent.prototype.collapsePrevious=function(bool){ // Enable/disable collapse previous content. Default is false.
	this.collapsePrev=bool
}

// Sets status indicator HTML of switch header.

switchcontent.prototype.togglestatus=function(header, status){
	if (typeof this.statusOpen!="undefined")
		header.firstChild.innerHTML=status
}

// Contracts a content based on its corresponding header entered

switchcontent.prototype.contractcontent=function(header){
	var innercontent=document.getElementById(header.id.replace("-title", "")) //Reference content container for this header
	innercontent.style.display="none"
	this.togglestatus(header, this.statusClosed)
}

// Expands content based on its corresponding header entered

switchcontent.prototype.expandcontent=function(header){
	var innercontent=document.getElementById(header.id.replace("-title", ""))
	innercontent.style.display="block"
	this.togglestatus(header, this.statusOpen)
}

// Toggledisplay(header)- Toggles between a content being expanded or contracted

switchcontent.prototype.toggledisplay=function(header){
	var innercontent=document.getElementById(header.id.replace("-title", "")) // Reference content container for this header
	if (innercontent.style.display=="block")
		this.contractcontent(header)
	else{
		this.expandcontent(header)
	}
}

// CollectElementbyClass()- Searches and stores all switch contents (based on shared class name) and their headers in two arrays

switchcontent.prototype.collectElementbyClass=function(classname){ // Returns an array containing DIVs with specified classname
	var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i") // Regular expression to screen for classname within element
	this.headers=[], this.innercontents=[]
	if (this.filter_content_tag!="") // If user defined limit type of element to scan for to a certain element 
		var allelements=document.getElementsByTagName(this.filter_content_tag)
	else // else, scan all elements on the page!
		var allelements=document.all? document.all : document.getElementsByTagName("*")
	for (var i=0; i<allelements.length; i++){
		if (typeof allelements[i].className=="string" && allelements[i].className.search(classnameRE)!=-1){
			if (document.getElementById(allelements[i].id+"-title")!=null){ // If header exists for this inner content
				this.headers[this.headers.length]=document.getElementById(allelements[i].id+"-title") // Store reference to header intended for this inner content
				this.innercontents[this.innercontents.length]=allelements[i] // Store reference to this inner content
			}
		}
	}
}

// init()- Initializes Switch Content function (collapse contents by default unless exception is found)

switchcontent.prototype.init=function(){
	var instanceOf=this
	this.collectElementbyClass(this.className) // Get all headers and its corresponding content based on shared class name of contents
	if (this.headers.length==0) // If no headers are present (no contents to switch), just exit
		return
	var opencontents_ids=(this.persistType=="session" && switchcontent.getCookie(this.className)!="")? ','+switchcontent.getCookie(this.className)+',' : (this.persistType=="days" && switchcontent.getCookie(this.className+"_d")!="")? ','+switchcontent.getCookie(this.className+"_d")+',' : (this.expandedindices)? ','+this.expandedindices+',' : ""
	for (var i=0; i<this.headers.length; i++){ // BEGIN FOR LOOP
		if (typeof this.statusOpen!="undefined") // If open/closing HTML indicator is enabled/set
			this.headers[i].innerHTML='<span class="status"></span>' // Add a span element to original HTML to store indicator
		if (opencontents_ids.indexOf(','+i+',')!=-1){ // if index "i" exists within cookie string or default-enabled string (i=position of the content to expand)
			this.expandcontent(this.headers[i]) // Expand each content per stored indices (if ""Collapse Previous" is set, only one content)
		}
		else // else if no indices found in stored string
			this.contractcontent(this.headers[i]) // Contract each content by default
		this.headers[i].onclick=function(){instanceOf.toggledisplay(this)}
	} // END FOR LOOP
}