//Animated Collapsible DIV- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated Aug 1st, 07'. Fixed bug with "block" parameter not working when persist is enabled
//Updated June 27th, 07'. Added ability for a DIV to be initially expanded.

/******************** This goes in the <body> directly after the div *********************
<script type="text/javascript">
  //Syntax: var uniquevar=new toggle("DIV_id", animatetime_milisec, enablepersist(true/fase), [initialstate] )
  var watchedTopics=new toggle("watchedTopicsToggle", 0, true, "block")
</script>
******************************************************************************************/

//var uniquepageid=window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, "") //get current page path and name, used to uniquely identify this page for persistence feature

function toggle(divId){
	var persistexpand=true
//	var initstate="block"
	
	this.divId=divId
	this.divObj=document.getElementById(divId)
	this.divToggle=document.getElementById(divId.replace(/Toggle/, "Heading"))
	this.divObj.style.overflow="hidden"
	this.timelength=0
//	this.initstate=(typeof initstate!="undefined" && initstate=="block")? "block" : "contract"
	this.initstate="block"
//	this.isExpanded=toggle.getCookie(uniquepageid+"-"+divId) //"yes" or "no", based on cookie value
	this.isExpanded=toggle.getCookie(divId) //"yes" or "no", based on cookie value
	if (this.isExpanded=="")
		this.isExpanded="yes"
	this.contentheight=parseInt(this.divObj.style.height)
	var thisobj=this
	if (isNaN(this.contentheight)){ //if no CSS "height" attribute explicitly defined, get DIV's height on window.load
		toggle.dotask(window, function(){thisobj._getheight(persistexpand)}, "load")
		if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes" && this.isExpanded!="") //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
			this.divObj.style.visibility="hidden" //hide content (versus collapse) until we can get its height
	}
	else if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes" && this.isExpanded!="") //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
		this.divObj.style.height=0 //just collapse content if CSS "height" attribute available
	if (persistexpand){
//		toggle.dotask(window, function(){toggle.setCookie(uniquepageid+"-"+thisobj.divId, thisobj.isExpanded)}, "unload")
		toggle.dotask(window, function(){toggle.setCookie(thisobj.divId, thisobj.isExpanded)}, "unload")
		if (this.isExpanded=="yes")
			this.divToggle.style.background="#3E5F7F url(http://wealthadvisorconnect.com/templates/MorpheusX/images/sblue/arrow_open.gif) no-repeat center right"
		else
			this.divToggle.style.background="#3E5F7F url(http://wealthadvisorconnect.com/templates/MorpheusX/images/sblue/arrow_closed.gif) no-repeat center right"
	}
	else{
		if (this.initstate=="block")
			this.divToggle.setAttribute('src',"http://wealthadvisorconnect.com/templates/MorpheusX/images/sblue/arrow_open.gif");
		else
			this.divToggle.setAttribute('src',"http://wealthadvisorconnect.com/templates/MorpheusX/images/sblue/arrow_closed.gif");
	}		
}

toggle.prototype._getheight=function(persistexpand){
	this.contentheight=this.divObj.offsetHeight
	if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes"){ //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
		this.divObj.style.height=0 //collapse content
		this.divObj.style.visibility="visible"
	}
	else //else if persistence is enabled AND this content should be expanded, define its CSS height value so slideup() has something to work with
		this.divObj.style.height=this.contentheight+"px"
}

toggle.prototype._slideengine=function(direction){
	var elapsed=new Date().getTime()-this.startTime //get time animation has run
	var thisobj=this
	if (elapsed<this.timelength){ //if time run is less than specified length
		var distancepercent=(direction=="down")? toggle.curveincrement(elapsed/this.timelength) : 1-toggle.curveincrement(elapsed/this.timelength)
	this.divObj.style.height=distancepercent * this.contentheight +"px"
	this.runtimer=setTimeout(function(){thisobj._slideengine(direction)}, 10)
	}
	else{ //if animation finished
		this.divObj.style.height=(direction=="down")? this.contentheight+"px" : 0
		this.isExpanded=(direction=="down")? "yes" : "no" //remember whether content is expanded or not
		this.runtimer=null
	}
}


toggle.prototype.slidedown=function(){
	if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running
		if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
			alert("Please wait until document has fully loaded then click again")
		else if (parseInt(this.divObj.style.height)==0){ //if content is collapsed
			this.startTime=new Date().getTime() //Set animation start time
			this._slideengine("down")
			this.divToggle.style.background="#3E5F7F url(http://wealthadvisorconnect.com/templates/MorpheusX/images/sblue/arrow_open.gif) no-repeat center right"
		}
	}
}

toggle.prototype.slideup=function(){
	if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running
		if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
			alert("Please wait until document has fully loaded then click again")
		else if (parseInt(this.divObj.style.height)==this.contentheight){ //if content is expanded
			this.startTime=new Date().getTime()
			this._slideengine("up")
			this.divToggle.style.background="#3E5F7F url(http://wealthadvisorconnect.com/templates/MorpheusX/images/sblue/arrow_closed.gif) no-repeat center right"
		}
	}
}

toggle.prototype.slideit=function(){
	if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
		alert("Please wait until document has fully loaded then click again")
	else if (parseInt(this.divObj.style.height)==0)
		this.slidedown()
	else if (parseInt(this.divObj.style.height)==this.contentheight)
		this.slideup()
}

// -------------------------------------------------------------------
// A few utility functions below:
// -------------------------------------------------------------------

toggle.curveincrement=function(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}


toggle.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.addEventListener)
		target.addEventListener(tasktype, functionref, false)
	else if (target.attachEvent)
		target.attachEvent(tasktype, functionref)
}

toggle.getCookie=function(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return ""
}

toggle.setCookie=function(name, value){
		var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		
		document.cookie = name+"="+value+expires
}

// Checkbox function
function select_switch(status){
	if ( !document.getElementById('privmsg_list') )
	{
		target = document.getElementByName('privmsg_list');
	}
	else
	{
		target = document.getElementById('privmsg_list')
	}
	
	for (i = 0; i < target.length; i++)
	{
		target.elements[i].checked = status;
	}
}

// Disable return submit
function disableEnterKey(e){
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

/* cssjs
* written by Christian Heilmann (http://icant.co.uk)
* eases the dynamic application of CSS classes via DOM
* parameters: action a, object o and class names c1 and c2 (c2 optional)
* actions: swap exchanges c1 and c2 in object o
*			add adds class c1 to object o
*			remove removes class c1 from object o
*			check tests if class c1 is applied to object o
* example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
*/

function cssjs(a,o,c1,c2)
{
	switch (a){
		case 'swap':
			o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
		case 'add':
			if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className)
		break;
	}
}
