
$(document).ready(function() {

	
	
	// menu scroll
	$("#menu li a").each(function() {
		var next = $(this).next("ul");
		
		// ha van almenu
		if( typeof next[0] != 'undefined') {
		
			var id   = $(this).attr("id");
			var itemsLength = $(this).next("ul").children("li").length;
			
			$(this).attr("href", "javascript:void(null)");
			$(this).click(function() {
			
				eraseCookie("openmenu_"+id);
				
				if(next.css("display") == 'none') {
					next.slideDown();
					
					var i = 0;
				    
					$(this).next("ul").children("li").each(function() {
						$(this).animate({ 
        					left: '0px'
      					}, 500 + i*100, "swing" );
						i++;
					});
					
					createCookie("openmenu_"+id, "true", 7)
				}
				else {
					var i = itemsLength;
				    
					$(this).next("ul").children("li").each(function() {
						$(this).animate({ 
        					left: '-300px'
      					}, 500 + i*100, "swing" );
						i--;
					});
					next.slideUp("slow");
				}
					
					
			});
			
			

		}
		
	});
	
	// nyitott menuk megjelenitese
	// vegigmegy minden almenun, es megnezi van e ilyen cookie
	$("#menu li a").each(function() {
	
		var next = $(this).next("ul");
		// havan almenu
		if( typeof next[0] != 'undefined') {
		
			var id = $(this).attr("id");
			
			if(readCookie("openmenu_"+id)) {
				next.show();
				$(this).next("ul").children("li").each(function() { $(this).css("left", '0px'); });
			}
			
		}
		
	});


});



function createCookie(name,value,days) {
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = 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 c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
