// -------------------------------------------------------------------
// Switch Content Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: Jan 5th, 2007
// April 5th: Added ability to persist content states by x days versus just session only
// -------------------------------------------------------------------

function switchcontent(className, filtertag){
	this.className=className
	this.persistType="days"
	this.persistDays=parseInt(30)
	this.filter_content_tag= filtertag.toLowerCase()
	this.statusOpen='<img src="img/fechar.png" style="vertical-align:middle" />  '
	this.statusClosed='<img src="img/abrir.png" style="vertical-align:middle" />  '
	this.classOpen="aberto"
	this.classClosed="fechado"

}

switchcontent.prototype.sweepToggle=function(setting){
	if (typeof this.headers!="undefined" && this.headers.length>0){
		for (var i=0; i<this.headers.length; i++){
			if (setting=="expand")
				this.expandcontent(this.headers[i])
			else if (setting=="contract")
				this.contractcontent(this.headers[i])
		}
	}
}

switchcontent.prototype.defaultExpanded=function(){
	var expandedindices=[]
	for (var i=0;i<arguments.length; i++)
		expandedindices[expandedindices.length]=arguments[i]
	this.expandedindices=expandedindices.join(",")
}

switchcontent.prototype.togglecolor=function(header, statusClass){
	header.className=statusClass
}

switchcontent.prototype.togglestatus=function(header, status){
	header.firstChild.innerHTML=status
}

switchcontent.prototype.contractcontent=function(header){
	var innercontent=document.getElementById(header.id.replace("-title", ""))
	innercontent.style.display="none"
	this.togglestatus(header, this.statusClosed)
	this.togglecolor(header, this.classClosed)
}

switchcontent.prototype.expandcontent=function(header){
	var innercontent=document.getElementById(header.id.replace("-title", ""))
	innercontent.style.display="block"
	this.togglestatus(header, this.statusOpen)
	this.togglecolor(header, this.classOpen)
}

switchcontent.prototype.toggledisplay=function(header){
	var innercontent=document.getElementById(header.id.replace("-title", ""))
	if (innercontent.style.display=="block")
		this.contractcontent(header)
	else
		this.expandcontent(header)
}

switchcontent.prototype.collectElementbyClass=function(classname){
	var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i")
	this.headers=[], this.innercontents=[]
	if (this.filter_content_tag!="")
		var allelements=document.getElementsByTagName(this.filter_content_tag)
	else 
		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){ 
				this.headers[this.headers.length]=document.getElementById(allelements[i].id+"-title")
				this.innercontents[this.innercontents.length]=allelements[i]
			}
		}
	}
}

switchcontent.prototype.init=function(){
	var instanceOf=this
	this.collectElementbyClass(this.className)
	if (this.headers.length==0)
		return

	var opencontents_ids=(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++){
		this.headers[i].innerHTML='<span class="status"></span>'+this.headers[i].innerHTML
		if (opencontents_ids.indexOf(','+i+',')!=-1)
			this.expandcontent(this.headers[i])
		else
			this.contractcontent(this.headers[i])
		this.headers[i].onclick=function(){instanceOf.toggledisplay(this)}
	}
	switchcontent.dotask(window, function(){instanceOf.rememberpluscleanup()}, "unload")
}

switchcontent.prototype.rememberpluscleanup=function(){
	var opencontents=new Array("none")
	for (var i=0; i<this.innercontents.length; i++){
		if (this.innercontents[i].style.display=="block" )
			opencontents[opencontents.length]=i
		this.headers[i].onclick=null
	}
	
	if (opencontents.length>1)
		opencontents.shift()

	this.statusOpen=this.statusClosed=null

	switchcontent.setCookie(this.className+"_d", opencontents.join(","), this.persistDays) //populate cookie with indices of open contents
	switchcontent.setCookie(this.className+"_dtrack", this.persistDays, this.persistDays) //also remember number of days to persist (int)
}

switchcontent.dotask=function(target, functionref, tasktype){
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.addEventListener)
		target.addEventListener(tasktype, functionref, false)
	else if (target.attachEvent)
		target.attachEvent(tasktype, functionref)
}

switchcontent.getCookie=function(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i");
	if (document.cookie.match(re))
		return document.cookie.match(re)[0].split("=")[1]
	return ""
}

switchcontent.setCookie=function(name, value, days){
	var expireDate = new Date()
	var expstring=expireDate.setDate(expireDate.getDate()+days)
	document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()
}
