var IntSession = function() 
{
	var cookies = document.cookie.split('; ');

	var i, name, value, split;
	for(i = 0; i < cookies.length; i++)
	{
		split = cookies[i].split('=');
		name = $.trim(split[0].toString());
		value = $.trim(split[1].toString());
		this.cookies.push({'name': name, 'value': value}); 
	}
	
	//ustawiamy opcje
	var options = this.getVar('intOptions');
	if(!options)
		return true;
	
	var options_arr = [];
	if(options.match('&'))
		options_arr = options.split('&');
	else
		options_arr.push(options);
	
	
	for(i = 0; i < options_arr.length; i++)
	{
		split = options_arr[i].split('=');
		name = split[0];
		value = split[1];
		
		if((name != undefined) && (value != undefined))
			this.options[name] = value;
		
	}
};

IntSession.prototype.cookies = [];
IntSession.prototype.options = new Array();

IntSession.getInstance = function()
{
	if(IntSession.getInstance.IntSessionInstance == undefined)
	{
		IntSession.getInstance.IntSessionInstance = new IntSession();
	}
	
	return IntSession.getInstance.IntSessionInstance;
};

IntSession.prototype.deleteVar = function(name)
{
	var i;
	for(i = 0; i < this.cookies.length; i++)
	{
		if(this.cookies[i].name == name)
		{
			var expire = new Date();
			expire.setTime(0);
			return this.setCookie(name, null, expire);
		}
	}
};

IntSession.prototype.setVar = function(name, value)
{
	var expire = new Date();
	
	// na pół roku
	expire.setTime(expire.getTime() + 15552000);

	return this.setCookie(name, value, expire);
};

IntSession.prototype.setCookie = function(name, value, expire)
{
	document.cookie = name + "=" + escape(value) + "; expires=" + expire.toGMTString() +  "; path=/";
	
	this.cookies.push({'name': name, 'value': escape(value)});
};

IntSession.prototype.getVar = function(name, def)
{
	var i;
	for(i = 0; i < this.cookies.length; i++)
	{
		if(this.cookies[i].name == name)
			return unescape(this.cookies[i].value);
	}
	
	return def || false;
};

IntSession.prototype.setOption = function(name, value)
{
	this.options[name] = value;
	this.rewriteOptions();
};

IntSession.prototype.deleteOption = function(name)
{
	if(this.optionExists(name))
	{
		delete this.options[name];
		this.rewriteOptions();
	}
};

IntSession.prototype.clearOptions = function()
{
	this.deleteVar('intOptions');
	this.options = new Array();
};

IntSession.prototype.rewriteOptions = function()
{
	var options = "";
	var name;
	
	for(name in this.options)
	{
		options += name + '=' + this.options[name] + '&';
	}
	
	options = options.substring(0, options.length - 1);
	
	this.setVar('intOptions', options);
};

IntSession.prototype.getOption = function(option_name, def)
{
	if(this.optionExists(option_name))
	{
		return this.options[option_name];
	}
	
	return def || false;
};


IntSession.prototype.optionExists = function(name)
{
	return (this.options[name] != undefined); 
}
