(function($){			
	$.cookie = function (key, value, options){
		if(arguments.length > 1 && (value === null || typeof value !== "object")){
			options = jQuery.extend({}, options);

			if(value === null)	options.expires = -1;

			if(typeof options.expires === 'number'){
				var days = options.expires, t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}

			return (document.cookie = [
				encodeURIComponent(key), '=',
				options.raw ? String(value) : encodeURIComponent(String(value)),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path ? '; path=' + options.path : '',
				options.domain ? '; domain=' + options.domain : '',
				options.secure ? '; secure' : ''
			].join(''));
		}

		// key and possibly options given, get cookie...
		options = value || {};
		var result, decode = options.raw ? function (s){ return s; } : decodeURIComponent;
		return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
	};
					
	$.fn.serialexpand = function(){
		var rels = [];
		
		var toggle = function(){
			if(!this.content.length) return;
			
			var img = $('img', this);
			var imageSrc = img.length ? $('img', this).attr('src') : $(this).css('background-image');
			var toHide = this.content.is(':visible');
			
			if(toHide){
				this.content.hide();	
				imageSrc = imageSrc.replace('-on', '-off');
				if(this.rel) $.data(document.body, this.rel, false);
			} else {
				this.content.show();	
				imageSrc = imageSrc.replace('-off', '-on');
				if(this.rel){
					var current = $.data(document.body, this.rel);
					if(current && $(current).not(this).length == 1){ $(current).click(); }
					
					//set current
					$.data(document.body, this.rel, this);
				}
			}
			
			if(this.rel) $.cookie(this.rel, toHide ? undefined : this.content.attr('id'));
			
			(img.length) ? img.attr('src', imageSrc) : $(this).css('background-image', imageSrc);
			return false;
		} 
		
		this.each(function(i){
			var href = $(this).attr('href');
			
			if(this.rel && $.inArray(this.rel, rels) < 0){
				rels.push(this.rel);
				var first = $(this).addClass('open');
			}
			
			var relCoockie = $.cookie(this.rel);
			if("#"+relCoockie == href){
				if(first && first.length) first.removeClass('open');
				$(this).addClass('open');		
			}
			
			this.content = $(href);
			if(href == '#next'){ 
				if($(this).next().length){
					this.content = $(this).next();
				} else if($(this).parent().next().length){
					this.content = $(this).parent().next();
				} else if($(this).parent().parent().next().length){
					this.content = $(this).parent().parent().next();
				}
			}
			this.content.hide();
		}).bind('click', toggle).filter('.open').trigger('click');
	};
})(jQuery);
