var min_w=1280;
var m_speed=500;
var m_ease="Power3.easeOut";
var is_show=false;
var kind='p';
$(window).resize(function(){
	page_menu();
});

$(document).ready(function(){
	$(".sub-menu").each(function(){
		var target_id=$(this).attr("data-menu");
		var target=$(this).parent().find("#"+target_id);

		if(target.hasClass("selected")){
			$(this).slideDown(0);
		}else{
			$(this).slideUp(0);
		}
	});
	$("#menu-content > li").click(function(){
		var obj_id=$(this).attr("data-target");

		$(".sub-menu").slideUp(200);
		$("#menu-content > li").removeClass("active");

		if($("."+obj_id).is(":visible")==false){
			$(this).addClass("active");
			$("."+obj_id).slideDown(200);
		}
	});

	var overlay_offset=$('.adm-left-overlay').offset();
	$(window).scroll(function(){
		if ($(document).scrollTop()>overlay_offset.top){
			$('.adm-left-overlay').css({"top":0,"position":"fixed"});
		}else{
			$('.adm-left-overlay').css({"top":$(".adm-content").position().top+"px","position":"absolute"});
		}
	});

	//사이드메뉴 토글
	$(".toggle-side-menu").click(function(){
		page_menu_toggle();
		is_show=true;
	});

	$(".adm-left-overlay").click(function(){
		page_menu_hide();
	});

	page_menu_init();
});

function page_menu_init(){
	var menu=$(".adm-left");
	var page=$(".adm-content");
	var win_w=$(window).width();
	var win_h=$(window).height();
	var m_h=win_h-$(".adm-top-menu").height();

	if(g5_is_mobile==1) m_h=$(window).height()+50;
	//if(navigator.userAgent.toLowerCase().indexOf('trident') !== -1) m_h=$(window).height()+250;

	var m_size=$(".nav-side-menu").width();

	if(win_w>min_w){
		menu.css({"width":m_size+"px"}).addClass("open");
		if(navigator.userAgent.toLowerCase().indexOf('trident') !== -1){
			menu.find(".nav-side-menu").css({"height":m_h+"px","left":0});
		}else{
			menu.find(".nav-side-menu").css({"min-height":m_h+"px","left":0});
		}
		kind='p';
		$(".toggle-side-menu").addClass("active");
	}else{
		menu.css({"width":0}).removeClass("open");
		if(navigator.userAgent.toLowerCase().indexOf('trident') !== -1){
			menu.find(".nav-side-menu").css({"height":m_h+"px","left":"-"+m_size+"px"});
		}else{
			menu.find(".nav-side-menu").css({"min-height":m_h+"px","left":"-"+m_size+"px"});
		}
		kind='m';
		$(".toggle-side-menu").removeClass("active");
	}
}

function page_menu(){
	var menu=$(".adm-left");
	var page=$(".adm-content");
	var win_w=$(window).width();
	var win_h=$(window).height();
	var m_h=win_h-$(".adm-top-menu").height();
	var m_size=$(".nav-side-menu").width();

	if(win_w>min_w){
		if(kind=='m'){
			is_show=false;
			menu.removeClass("open");
		}
		kind='p';
		$(".toggle-side-menu").addClass("active");
	}else{
		if(kind=='p'){
			is_show=false;
		}
		kind='m';
		$(".toggle-side-menu").removeClass("active");
	}
	
	if(!is_show){
		if(win_w>min_w){
			if(menu.hasClass("open")==false){
				menu.css({"height":"100%"}).addClass("open").stop().animate({"width":m_size+"px"},m_speed,m_ease);
				menu.find(".nav-side-menu").css({"height":m_h+"px"}).stop().animate({"left":0},m_speed,m_ease);
				$(".adm-left-overlay").css({"width":page.width()+"px","height":page.height()+"px"}).animate({"opacity":0},m_speed,m_ease,function(){
					$(this).hide();
				});
			}
		}else{
			if(menu.hasClass("open")){
				menu.removeClass("open").stop().animate({"width":0},m_speed,m_ease);
				menu.find(".nav-side-menu").css({"height":m_h+"px"}).stop().animate({"left":"-"+m_size+"px"},m_speed,m_ease);
				$(".adm-left-overlay").css({"width":page.width()+"px","height":page.height()+"px"}).animate({"opacity":0},m_speed,m_ease,function(){
					$(this).hide();
				});
			}
		}
	}
}

function page_menu_toggle(){
	var menu=$(".adm-left");
	var page=$(".adm-content");
	var m_size=$(".nav-side-menu").width();
	var win_w=$(window).width();
	var win_h=$(window).height();
	var m_h=win_h-$(".adm-top-menu").height();

	if(win_w>min_w){
		if(menu.hasClass("open")==false){
			menu.css({"width":0,"height":"100%"}).addClass("open").stop().animate({"width":m_size+"px"},m_speed,m_ease);
			menu.find(".nav-side-menu").stop().animate({"left":0},m_speed,m_ease);
			$(".toggle-side-menu").addClass("active");
		}else{
			menu.css({"width":m_size+"px","height":"100%"}).removeClass("open").stop().animate({"width":0},m_speed,m_ease);
			menu.find(".nav-side-menu").stop().animate({"left":"-"+m_size+"px"},m_speed,m_ease);
			$(".toggle-side-menu").removeClass("active");
		}
	}else{
		if(menu.hasClass("open")==false){
			$(".adm-left-overlay").css({"width":page.width()+"px","height":page.height()+"px","top":page.position().top+"px","opacity":0}).show().stop().animate({"opacity":0.5},m_speed,m_ease);
			menu.addClass("open");
			menu.find(".nav-side-menu").css({"height":m_h+"px"}).stop().animate({"left":0},m_speed,m_ease);
			$(".toggle-side-menu").addClass("active");
		}else{
			$(".adm-left-overlay").stop().animate({"opacity":0},m_speed,m_ease,function(){
				$(this).hide();
			});
			menu.removeClass("open");
			menu.find(".nav-side-menu").css({"height":m_h+"px"}).stop().animate({"left":"-"+m_size+"px"},m_speed,m_ease);
			$(".toggle-side-menu").removeClass("active");
		}
	}
}

function page_menu_hide(){
	var menu=$(".adm-left");
	var m_size=$(".nav-side-menu").width();

	$(".adm-left-overlay").stop().animate({"opacity":0},m_speed,m_ease,function(){
		$(this).hide();
	});
	menu.removeClass("open");
	menu.find(".nav-side-menu").stop().animate({"left":"-"+m_size+"px"},m_speed,m_ease);
	$(".toggle-side-menu").removeClass("active");
}
