//please note, that my SpryDOMUtils.js is packed so i have a the extra onUNLOAD listener in the code below.
//  * Declare vars;
var Spry;
if (!Spry.Utils) Spry.Utils = {};
var cp1;var cp2;var cp3;var cp4;var cp5;var cp6;var cp7; //declare vars outside the function so u can use then collap panel everywhere

//  * This is a addition to the SpryDomUtils.js U can include it under line #262
// Or leave it in a javascript external / internal
// This function will fire anything when u leave the current page.

Spry.Utils.addUnLoadListener = function(handler)
{
if (typeof window.addEventListener != 'undefined')
	window.addEventListener('unload', handler, false);
else if (typeof document.addEventListener != 'undefined')
	document.addEventListener('unload', handler, false);
else if (typeof window.attachEvent != 'undefined')
	window.attachEvent('onunload', handler);
};

//This function will be used by cookie to check if the value is allready in the cookie, if so it returns it position
Spry.Utils.CheckArray = function(a, s){
	for (i=0; i<a.length; i++){if (a[i] == s)return i;}return null;
};
/*  -------------------------
 *  SPRY COOKIE; HOW TO USE;
 *  -------------------------
 *  constructor: Spry.Utils.Cookie(type,string,{name:'cookie_name',path:'/',days:'number'};
 *  
 *  TYPE:
 *  - create : this creates *saves* the cookie
 *  		 : Spry.Utils.Cookie('create','string or array',{name:'Spry_Cookie'});
 *  - get	 : this will return the cookie in array format
 *  		 : Spry.Utils.Cookie('get','',{name:'Spry_Cookie'});
 *  - destory: this will destroy the cookie
 *  		 : Spry.Utils.Cookie('destroy','',{name:'Spry_Cookie'});
 *  - add	 : this allows u to add value to the cookie with out creating a whole new string
 *  		   it will place the add value behind the older cookie values, it checks if the value is allready in the cookie,
 *  		   if it is, it will NOT add it in the cookie.
 *  
 *  STRING:
 *  This is the data what u want to store in to the cookie, it can be an Array or a normal string / var
 *  
 *  OPTIONS:
 *  - name:	 : this is the name of the cookie so u can identify it
 *  - path:	 : optional path for the cookie;
 *  - days:  : the amount of days for the cookie to be saved.
 */

Spry.Utils.Cookie = function(type,string,options){
	if(type == 'create'){
		var expires='';
		if(options.days != null){
			var date = new Date();
			var UTCString;
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toUTCString();
		}
		var thePath = '; path=/';
		if(options.path != null){
			thePath = '; path='+options.path;
		}
		document.cookie = options.name+'='+escape(string)+expires+thePath;
	}else if(type == 'get'){
		var nameEQ = options.name + '=';
		var ca = document.cookie.split(';');
		for (var i=0; i<ca.length; i++){
			var c = ca[i];
			while (c.charAt(0)==' '){
			c = c.substring(1,c.length);
		}if (c.indexOf(nameEQ)==0) return unescape(c.substring(nameEQ.length,c.length)).split(",");
		}return null;
	}else if(type == 'destory'){
		Spry.Utils.Cookie('create','',{
			name: options.name
		});
	}else if(type == 'add'){
		var c = Spry.Utils.Cookie('get','',{name:options.name});
		if (typeof string == 'object') {
			for (i = 0, str; str = string[i], i < string.length; i++) {
				if (Spry.Utils.CheckArray(c, str) == null)c.push(str);
			}
		}else{
			if (Spry.Utils.CheckArray(c, string) == null) c.push(string)
		}
		Spry.Utils.Cookie('create',c,{name:options.name});		
	}
};

// * check if we have a panel to set from cookie value
// * becouse we are using spry dom we can also include the tab construction within this function.
Spry.Utils.addLoadListener(function(){
	//becouse cookie GET returns a array we have to add a var before it to store the data
	var col = Spry.Utils.Cookie('get','',{name:'col_history'});
	if(col)//checks if acc contains data
		  {
			if(col[0] == 'false')cp1 = new Spry.Widget.CollapsiblePanel("cp1",{contentIsOpen:false});
			else cp1 = new Spry.Widget.CollapsiblePanel("cp1");
			
			if(col[1] == 'false')cp2 = new Spry.Widget.CollapsiblePanel("cp2",{contentIsOpen:false});
			else cp2 = new Spry.Widget.CollapsiblePanel("cp2");
			
			if(col[2] == 'false')cp3 = new Spry.Widget.CollapsiblePanel("cp3",{contentIsOpen:false});
			else cp3 = new Spry.Widget.CollapsiblePanel("cp3");
			
			if(col[3] == 'false')cp4 = new Spry.Widget.CollapsiblePanel("cp4",{contentIsOpen:false});
			else cp4 = new Spry.Widget.CollapsiblePanel("cp4");
			
			if(col[4] == 'false')cp5 = new Spry.Widget.CollapsiblePanel("cp5",{contentIsOpen:false});
			else cp5 = new Spry.Widget.CollapsiblePanel("cp5");
			
			if(col[5] == 'false')cp6 = new Spry.Widget.CollapsiblePanel("cp6",{contentIsOpen:false});
			else cp6 = new Spry.Widget.CollapsiblePanel("cp6");
			
			if(col[6] == 'false')cp7 = new Spry.Widget.CollapsiblePanel("cp7",{contentIsOpen:false});
			else cp7 = new Spry.Widget.CollapsiblePanel("cp7");
			
			if(col[7] == 'false')cp8 = new Spry.Widget.CollapsiblePanel("cp8",{contentIsOpen:false});
			else cp8 = new Spry.Widget.CollapsiblePanel("cp8");		
			
		  }else{
	cp1 = new Spry.Widget.CollapsiblePanel("cp1",{contentIsOpen:false});
	cp2 = new Spry.Widget.CollapsiblePanel("cp2");
	cp3 = new Spry.Widget.CollapsiblePanel("cp3",{contentIsOpen:false});
	cp4 = new Spry.Widget.CollapsiblePanel("cp4");
	cp5 = new Spry.Widget.CollapsiblePanel("cp5",{contentIsOpen:false});
	cp6 = new Spry.Widget.CollapsiblePanel("cp6");
	cp7 = new Spry.Widget.CollapsiblePanel("cp7",{contentIsOpen:false});
	
	}
 });

Spry.Utils.addUnLoadListener(function(){
	//Create a array where all panel statuses are
	//want more than 5? just add construtorname.isOpen() to it :)
	var panel = new Array(cp1.isOpen(),cp2.isOpen(),cp3.isOpen(),cp4.isOpen(),cp5.isOpen(),cp6.isOpen(),cp7.isOpen());
	//destroy old cookie (i want a clean cookie)
	Spry.Utils.Cookie('destory','',{name:'col_history'});
	//setting the new cookie value
	Spry.Utils.Cookie('create',panel,{name:'col_history'});
});