var menu_curent=getCookie("menu_curent");
var temp_curent;
function showScroll(){
	$("#pan").jScrollPane({showArrows:true, scrollbarWidth: 15,dragMaxHeight:20,scrollbarMargin:12});
}

function menu(){
	getState();
	//showScroll();
	$("#menu a").click(function(){
		//showScroll();
		//alert($("#menu ul").height());
		/*$("#nav").attr("style","height:"+$("#container").height());*/
		switchMenu($(this));
		if(temp_curent){
			$(temp_curent).css({color:'#ccc',border:'none'});
			//$("#"+temp_curent).next(".child").css({background:"#fff"});
		}
		if(menu_curent) {
			$("#"+menu_curent).css({color:'#ccc',border:'none'});
			//$("#"+menu_curent).next(".child").css({background:"#fff"});
		}
		temp_curent=$(this);
		createCookie("menu_curent",$(this).attr("id"));
		$(this).css({color:"#ff8247",border:"1px solid #99ccff"});
		//$(this).next(".child").css({background:"#90a1ac"});
	});
	
	$('#'+menu_curent).css({color:"#ff8247",border:"1px solid #99ccff"});
	//$('#'+menu_curent).next(".child").css({background:"#90a1ac"});
	$(window).unload(saveState);
}
function saveState(){
	var out="";
	$("#menu a.expanded").each(function(i){
			if(i!=0) out += ",";
      		out += $(this).attr("id");
	}
	);
	createCookie("rxc_menu",out);
}
function getState(){
	var state=getCookie("rxc_menu");
	$("#menu a").not(".leaf").each(function(){
		$(this).next(".child").addClass("remove");
		$(this).addClass("closed");
		
	});
	//alert(state);
	if(state!=""){
		var cookieList = state.split(",");
    	for (var i = 0; i < cookieList.length; i++) {
    		var id=cookieList[i];
      		$("#menu a").not(".leaf").each(function(i){
      			if($(this).attr("id")==id){
      				$(this).addClass("expanded");
      				$(this).next(".child").show();
      				$(this).next().show();
      			}
      		}
      		);	
    	}
	}
}
function switchMenu(el){
	var e=$(el).attr("id");
	var status=0;//closed
	if($(el).is(".expanded")){ 
		status=1;
	}
	if($(el).is(".leaf")){
		status=2;
	}
	switch(status){
		case 0:
			$(el).removeClass();
			$(el).addClass("expanded");
			/**$(el).next().slideDown("slow");*/
			$(el).next(".child").show();
			break;
		case 1:
			/**$(el).next().slideUp("slow");*/
			$(el).next(".child").hide();
			$(el).removeClass();
			$(el).addClass("closed");
			break;
		case 2:
			break;
		default:
			alert($(el).attr("class"));
	}
	
	saveState();
}

