var Spry;
if (!Spry.Utils) Spry.Utils = {};
var tp1; //declare vars outside the function so u can use then tab 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.onDOMReady(function(){
	//becouse cookie GET returns a array we have to add a var before it to store the data
	var tab = Spry.Utils.Cookie('get','',{name:'tabs_history'});
	tp1 = new Spry.Widget.TabbedPanels("tp1");
	if(tab)//checks if tab contains data
		  {
		  	//tp1 beein our tab panel constructor variable name. tab is the location of the saved tab location
			//and by substracting -0 from it it will become a number instead of string
		  	tp1.showPanel((tab-0));
		  };
 });
Spry.Utils.addUnLoadListener(function(){
	//get active (last used tab position)
	var tab = tp1.getCurrentTabIndex();
	
	//destroy old cookie (i want a clean cookie)
	Spry.Utils.Cookie('destory','',{name:'tabs_history'});
	
	//setting the new cookie value
	Spry.Utils.Cookie('create',tab,{name:'tabs_history'});
});
