$(document).ready(function() {
	$("#menu").load("./menu3.html", function(text, status) {

		var url = window.location.href;

		$("#menu_list").children("ul").children("li").each(function (event){
			var index = $(this).attr('id').split("_")[1];
			if ($.cookie("open_ul") && $.inArray("1-" + index, $.cookie("open_ul").split(",")) != -1) {
				$("#menu_list li#li1_" + index).children("ul").show();
				$("#menu_list li#li1_" + index).attr('class', 'level1_on');
			} else {
				$("#menu_list li#li1_" + index).children("ul").hide();
				$("#menu_list li#li1_" + index).attr('class', 'level1');
			}
		});

		$("#menu_list").children("ul").children("li").children("ul").children("li").each(function (event){
			var index = $(this).attr('id').split("_")[1];
			if ($.cookie("open_ul") && $.inArray("2-" + index, $.cookie("open_ul").split(",")) != -1) {
				$("#menu_list li#li2_" + index).children("ul").show();
				$("#menu_list li#li2_" + index).attr('class', 'level2_on');
			} else {
				$("#menu_list li#li2_" + index).children("ul").hide();
				$("#menu_list li#li2_" + index).attr('class', 'level2');
			}
		});

		$("#menu_list").children("ul").children("li").children("a").click(function(event){
			event.preventDefault();
			var index = $(event.target).parent().attr('id').split("_")[1];

			$("#menu_list li#li1_" + index).children("ul").toggle();
			if ($("#menu_list li#li1_" + index).children("ul").css('display') == 'block') {
				$("#menu_list li#li1_" + index).attr('class', 'level1_on');
			} else {
				$("#menu_list li#li1_" + index).attr('class', 'level1');
			}

			var name = "open_ul";
			var value = "1-" + index;
			var openlist = $.cookie(name) ? $.cookie(name).split(",") : new Array();
			if ($.cookie(name)) {
				if ($.inArray(value, $.cookie("open_ul").split(",")) != -1) {
					$.cookie(name, $.grep(openlist, function (a) { return a != value; }), { expires: 7 });
				} else {
					openlist.push(value);
					$.cookie(name, openlist, { expires: 7 });
				}
			} else {
				$.cookie(name, value, { expires: 7 });
			}
			window.focus();

		}).css("cursor","pointer");
		$("#menu_list").children("ul").children("li").children("ul").children("li").children("a").click(function(event){
			var index = $(event.target).parent().attr('id').split("_")[1];
			if ($(this).parent().children("ul").length) {
				event.preventDefault();
				$("#menu_list li#li2_" + index).children("ul").toggle();

				if ($("#menu_list li#li2_" + index).children("ul").css('display') == 'block') {
					$("#menu_list li#li2_" + index).attr('class', 'level2_on');
				} else {

					$("#menu_list li#li2_" + index).attr('class', 'level2');
				}

				var name = "open_ul";
				var value = "2-" + index;
				var openlist = $.cookie(name) ? $.cookie(name).split(",") : new Array();
				if ($.cookie(name)) {
					if ($.inArray(value, $.cookie("open_ul").split(",")) != -1) {
						$.cookie(name, $.grep(openlist, function (a) { return a != value; }), { expires: 7 });
					} else {
						openlist.push(value);
						$.cookie(name, openlist, { expires: 7 });
					}
				} else {
					$.cookie(name, value, { expires: 7 });
				}
			}

			window.focus();
		}).css("cursor","pointer");

		$("#menu_list").css('visibility', 'visible');
	});
});

