/*
* by @mosf (http://www.mosf.go.kr)
*/

$(function(){
	var $screenW = $(window).width(),
		$screenH = $(window).height(),
		$scrollHeight = $('body').scrollTop();
	
	// 전체 메인메뉴 시작
	var	$mainNav = $('#mainNav'),
		$menu1m = $mainNav.find('.depth1m'),
		$menu2m = $mainNav.find('.depth2m'),
		$menu3m = $mainNav.find('.depth3m');	
	
	
	// level2 메뉴 클래스 값 리셋
	$menu2m.find('> li').attr('class','');

	// 새창열림 링크
	$('.depth2m li a[target^="_blank"]').each(function() {
		var	blank = '_blank';
		var Title = $(this).attr('title');
		var NewTabs = Title + ' (새창열림)';
		if(Target = blank){
			$(this).append('<i>새창</i>');
			$(this).attr('title', NewTabs);
		}
	});	
	// 좌ㆍ우측 퀵메뉴 - 우선 잠시 해당 기능을 kill(서브 콘텐츠의 문제로 인한 처리)
//	$(window).scroll(function(){
//		var $scrollTop = $(this).scrollTop();
//		if($scrollTop > 200){
//			$('.quickNav').addClass('on');
//		} else {
//			$('.quickNav').removeClass('on');
//		}
//	});	
	
	// 자주찾는 나의 메뉴
	var $mymenuinit = $('.favReset'),
		$mymenusave = $('.favSave'),
		$favCheck = $('.favLayer .favMenu input[type=checkbox]');

	// 자주찾는 나의 메뉴 초기화
	$mymenuinit.click(function() {
		$(".favLayer .favMenu input[type=checkbox]:checked").each(function(){
			$(this).attr("checked", false);
		});
	});
	// 자주찾는 나의 메뉴 선택 후 저장 시 메뉴 등록부분
	$mymenusave.click(function() {
		var menuNo = ""; 
		if($(".favLayer .favMenu input[type=checkbox]:checked").length > 0){
			$(".favLayer .favMenu input[type=checkbox]:checked").each(function(){
				menuNo = menuNo + $(this).val() + ",";
				});
			menuNo = menuNo.substring(0, menuNo.length -1);
			$.ajax({
	        	url:  G_contextPath + '/com/sessionMyMenuMng.do',
	            dataType: "json",
	            data : { "myMenus" : menuNo },
	            success : function(data){
	            	if (data.success == true) {
	            		$(".favLayer .favList").children().remove();
	            		$(data.data).each(function(idx, obj) {
	            			var menunopre = obj.etc1;
	            			var iconno = obj.etc2;
	            			var classNm = "";
	            			if(menunopre == "3"){
	            				classNm = "fav_id" + iconno;
	            			} else if(menunopre == "4"){
	            				classNm = "fav_nw" + iconno;
	            			} else if(menunopre == "5"){
	            				classNm = "fav_pl" + iconno;
	            			} else if(menunopre == "6"){
	            				classNm = "fav_st" + iconno;
	            			} else if(menunopre == "7"){
	            				classNm = "fav_lw" + iconno;
	            			} else if(menunopre == "8"){
	            				classNm = "fav_cv" + iconno;
	            			} else if(menunopre == "9"){
	            				classNm = "fav_mi" + iconno;
	            			}
	            			$(".favLayer .favList").append('<li><a href="#" onclick="fn_moveMenuAjax(' + obj.etc3 + ', ' + obj.searchKeyword + ', ' + obj.menuUrl + ', ' + obj.stsfdgAt + ')"><span class="icoFav ' + classNm + '"></span>' + obj.menuNm +'</a></li>');
		           		});
	            		$(".favLayer .favMenu input[type=checkbox]:checked").each(function(){
	            			$(this).attr("checked", false);
	            		});
	            		alert("나만의 즐겨찾기가 저장되었습니다.");
	            	}
	            }
	        	});
		} else {
			alert("1개 이상의 메뉴를 선택해야 합니다.");
		}
	});
	// 자주찾는 나의 메뉴 5개 선택 채크
	$favCheck.click(function() {
		var cnt = $(".favLayer .favMenu input[type=checkbox]:checked").length;
		if(cnt > 5){
			alert("메뉴 선택은 5개 까지 가능합니다.");
			$(this).attr("checked", false);
		}
	});
	
	var m2mName = function(){ 
		if($(window).width() <= 768) {
			$menu2m.find('> li').each(function() {
				var m3m = $(this).find('ul').length;
				if( m3m >= 1 ){
					$(this).addClass('down');
				}
			});		
		}else{		
			$menu2m.find('> li').attr('class','');
			$menu3m.removeClass('on');
		}
	};
	m2mName();
	
	$(window).resize(function(){
		m2mName();
		if($(window).width() <= 768) {
			// level1 메뉴 클릭 시 level2 메뉴 노출 - toggle 사용
			$menu1m.each(function(){
				$(this).find('> button').bind('click',function(e){
					e.stopPropagation();	
					$(this).siblings('ul').addClass('on');			
					$(this).parent('li').siblings().find('ul').removeClass('on');	
					$(this).parent('li').siblings().find('> ul > li').removeClass('open');
				});	
			});
			// level2 메뉴 클릭 시 level3 메뉴 노출 - toggle 사용
			if($menu2m.find('> li').hasClass('down')){
				$menu2m.find('> li > a').each(function(){
					$(this).bind('click', function(event){
						event.preventDefault();
						var $menu3m = $(this).siblings('ul');
						$menu3m.addClass('on');
						$(this).parent('li').siblings().children('ul').removeClass('on');
						$(this).parent('li').addClass('open');
						$(this).parent('li').siblings().removeClass('open');
					});
				});
			};
		}else{
			$menu1m.find('> button').unbind('click');
			$menu2m.find('> li > a').unbind('click');
			$menu2m.find('> li.down > a').unbind('click');
		}
	}).resize();
	
	
});
