//Menu acordion

(function($){
	$.fn.qaccordion = function(options){
		var
		options = $.extend({
		'fxtime':400,
		'multiple':false
		},options),
		$container = $(this),
		maxHeight = 0,
		count = 0;
		

		$container.find('.item').each(function(){
			var $this = $(this),
			$subItems = $this.find('.subitems').first();
			$this.data('rheight',$subItems.height());
			if($subItems.height() > maxHeight) maxHeight = $subItems.height();
			count++;
			$this.attr('refid','menuitem'+count);
			$this.find('a').first().click(function(e){
				e.preventDefault();
				if($this.data('open')){
					close($this);
				}else{
					open($this);
				}
			});
		});

		var
			open = function($item){
				var $subItems = $item.find('.subitems').first();
				$subItems.show().height(0);
				$subItems.animate({'height':$item.data('rheight')+'px'},options.fxtime);
				$item.data('open',true);
				if(!options.multiple){
					$container.find('.item').each(function(){
						if(this!=$item[0] && !$item.data('closed') ) close($(this));
					});
				}
			},
			close = function($item){
				var $subItems = $item.find('.subitems').first();
				if(!$item.data('rheight'))$item.data('rheight',$subItems.height());
				$subItems.animate({'height':'0'},options.fxtime,function(){
					$subItems.hide();
				});
				
				$item.data('open',false);

			},
			setCookie = function(name,value,expiredays, path){
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+expiredays);
				document.cookie=name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+((path)?";path=" + path : "" );
			},
			getCookie = function(name){
				if (document.cookie.length) {
					  var c_start = document.cookie.indexOf(name + "=");
					  if (c_start!=-1)  {
						c_start=c_start + name.length+1;
						c_end=document.cookie.indexOf(";",c_start);
						if (c_end==-1) c_end = document.cookie.length;
						return unescape(document.cookie.substring(c_start,c_end));
					   }
					}
					return "";
			};
	}
})(jQuery);



