var today = new Date();
var expiry = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);

function ExpandingPanel_Load( expanderId, contracterId, expanderContainerId, contracterContainerId, hiddenFieldId, cookiePath, cookieExpires, cookieKey ) {
	var expander = document.getElementById( expanderId );
	var expanderContainer = document.getElementById( expanderContainerId );

	var contracter = document.getElementById( contracterId );
	var contracterContainer = document.getElementById( contracterContainerId );
	
	var hiddenField = document.getElementById( hiddenFieldId );
	
	if ( typeof( CookieJar ) != "undefined" ) {
		CookieJar.setPath( cookiePath );
	}

	expander.switchView = ExpandingPanel_Switch;
	expander.container = contracterContainer;
	expander.container.otherPanel = expanderContainer;
	expander.hiddenField = hiddenField;
	expander.cookieKey = cookieKey;
	expander.cookieExpires = expiry;

	contracter.switchView = ExpandingPanel_Switch;
	contracter.container = expanderContainer;
	contracter.container.otherPanel = contracterContainer;
	contracter.hiddenField = hiddenField;
	contracter.cookieKey = cookieKey;
	contracter.cookieExpires = expiry;

	expander.onclick = expander.switchView;
	contracter.onclick = contracter.switchView;
	
	//contracterContainer.onclick = function(){expander.click();};
	//expanderContainer.onclick = function(){contracter.click();};
}
function ExpandingPanel_Switch() {

	this.container.otherPanel.style.display = "block";
	this.container.style.display = "none";
	if ( this.hiddenField.value == "True" ) {
		this.hiddenField.value = "False";
	} else {
		this.hiddenField.value = "True";
	}
	
	
	if(get_cookie("ExpandingPanel") == null)
	{
		set_cookie("ExpandingPanel", null, expiry);
	}
	
	//alert(this.cookieKey + " " + this.hiddenField.value);
	var stateCookie = CookieJar.getCookie("ExpandingPanel");
	stateCookie.expires = this.cookieExpires;
	stateCookie.setValue( this.cookieKey, this.hiddenField.value );
	CookieJar.setCookie( stateCookie );	
	
//	if ( typeof( CookieJar ) != "undefined" ) {
//		var stateCookie = CookieJar.getCookie("ExpandingPanel");
//		if (stateCookie == null) {
//			alert("Cookie is null");
//			stateCookie = new Cookie();
//			stateCookie.name = "ExpandingPanel";
//		}
//		stateCookie.expires = this.cookieExpires;
//		stateCookie.setValue( this.cookieKey, this.hiddenField.value );
//		CookieJar.setCookie( stateCookie );
//	}
	
	return false;
}



/*This function allows you to create a cookie called name and store value in it. 
You specify the expiry date for this cookie in the expires field. If you don't specify an expiry date then 
the function will use the current date and time and the cookie will expire at the end of your visitor's browser session.*/
function set_cookie(name,value,expires) 
{
	if (dbug) 
		alert('set_cookie'); 
	if (!expires) 
		expires = new Date();
	document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString();
} 

// This function allows you to retrieve the content of a previously saved cookie called name into a field called value.	
function get_cookie(name) 
{
	if (dbug) 
		alert('get_cookie'); 
	var dcookie = document.cookie; 
	var cname = name + "="; 
	var clen = dcookie.length; 
	var cbegin = 0; 
	while (cbegin < clen) 
	{
		var vbegin = cbegin + cname.length;
		if (dcookie.substring(cbegin, vbegin) == cname) 
		{
			var vend = dcookie.indexOf (";", vbegin);
			if (vend == -1) 
				vend = clen; 
			return unescape(dcookie.substring(vbegin, vend));
		} 
		cbegin = dcookie.indexOf(" ", cbegin) + 1; 
		if (cbegin == 0) 
			break;
	} 
	return null;
} 
