// 사이트업 전용 jQuery 프로그램

$(function() {

	// 새글등록버튼 클릭 시
	$('.btn_new_article').css('cursor', 'pointer');	
	$('.btn_new_article').click(		
		function() {
			$('#global_dialog').attr('title', "새 글을 등록할 코너를 선택하세요");
			$('#global_dialog').load('insiter.php?design_file=article_new_pop.php').dialog(
				{
					modal: true,
					width:400,
					height:500
				/*buttons: [
						 {
							  text: "Ok",
							  click: function() { $(this).dialog("close"); }
						 }
					]*/
				}
			);
		}
	);

	// gnb, lnb 마크업 추가
	$('.ABA-gnb a').append('<span class="AB-lleft"></span><span class="AB-rleft"></span>');
	$('.ABA-lnb-box a').append('<span class="AB-lleft"></span><span class="AB-rleft"></span>');
	$('.ABA-head-lnb').append('<div class="AB-lleft"></div><div class="AB-rleft"></div>');
	$('.ABA-body-lnb').after('<div class="ABA-foot-lnb"><div class="AB-lleft"></div><div class="AB-rleft"></div></div>');

	// gnb
	var gnb_wrap = $('.ABA-gnb-wrap');
	var gnb_wrap_width = gnb_wrap.width();		
	var gnb_li = $('.ABA-gnb > li'), gnb_sub_1 = $('.ABA-gnb-sub-1'), gnb = gnb_li.parent();
	var gnb_width = gnb.width();
	var gnb_li_all = $('.ABA-gnb-sub-1 li');
	var gnb_sub_2 = $('.ABA-gnb-sub-2');	
	var gnb_v = 'N', gnb_slide = 'N';
	var gnb_current = $('.AB-current-gnb');
	var gnb_current_ul = gnb.find('.AB-current-gnb>ul');
	if (gnb_wrap.hasClass('GNB-v')) gnb_v = 'Y';
	if (gnb_wrap.hasClass('GNB-slide')) gnb_slide = 'Y';
	if (browser_type == 'IE6') gnb_slide = 'N';

	// GNB 메뉴 위치 설정
	gnb_li.each(function() {																													// GNB <li> 개수 만큼
		var gnb_sub_1_ul = $('ul', $(this)).eq(0);
		if ($(this).position().left + gnb_sub_1_ul.width() >= gnb_width) {						// GNB <li> 좌측위치 + GNB 서브 가로폭 >= GNB 가로폭 (GNB 서브메뉴가 GNB 우측 영역을 벗어나는 경우)
			var xy = $(this).position().left + gnb_sub_1_ul.width() - gnb_width;				// 우측 끝 선에 맞춤
			gnb_sub_1_ul.css('left', $(this).position().left-xy);
		} else {
			gnb_sub_1_ul.css('left', $(this).position().left);
		}
	});
	if (gnb_v == 'Y') {										// 수직으로 펼쳐지는 GNB인 경우
		var gnb_sub_ul_parent_li;					// 서브2차 노출 위치 설정
		gnb_sub_2.each(function() {
			gnb_sub_ul_parent_li = $(this).parent().parent();
			$(this).css('left', gnb_sub_ul_parent_li.width());
			$(this).css('top', '0');
		});
		gnb.find('ul').addClass('AB-live-menu');		// 펼쳐질 서브메뉴(ul) 들에 클래스 주입, 이미 펼쳐진 서브메뉴는 제외
	} else {																											// 수평으로 펼쳐지는 GNB인 경우
		gnb_current_ul.removeClass('AB-hidden').addClass('AB-show');
		gnb.find('ul:not(.AB-show)').addClass('AB-live-menu');		// 펼쳐질 서브메뉴(ul) 들에 클래스 주입, 이미 펼쳐진 서브메뉴는 제외
	}

	// 마우스 오버/아웃시 메뉴노출 설정
	var gnb_a_all = gnb.find('li>a');	
	if (gnb_slide == 'Y') {																			// 슬라이드 메뉴인 경우
		gnb_a_all.mouseover(m_over_slide).focus(m_over_slide);	// 각 메뉴의 링크에 마우스를 올렸을 때	 m_over_slide 함수 호출
		gnb.mouseleave(function() {														// gnb 영역에서 마우스가 벗어났을 때
			if (gnb_v == 'Y') gnb.find('ul').hide('fast').addClass('AB-live-menu');
			else gnb.find('ul:not(.AB-show)').hide('fast').addClass('AB-live-menu');			
			if (gnb_v != 'Y' && gnb_current_ul.hasClass('AB-show')) gnb_current_ul.show('fast').removeClass('AB-live-menu');
		});
	} else {
		gnb_a_all.mouseover(m_over_display).focus(m_over_display);
		gnb.mouseleave(function() {
			if (gnb_v == 'Y') gnb.find('ul').css('display', 'none').addClass('AB-live-menu');
			else gnb.find('ul:not(.AB-show)').css('display', 'none').addClass('AB-live-menu');
			if (gnb_v != 'Y' && gnb_current_ul.hasClass('AB-show')) gnb_current_ul.css('display', 'block').removeClass('AB-live-menu');
		});
	}

	// 마우스오버/아웃시 현재 GNB 복원
	gnb_li.mouseover(function() {
		if (gnb_current.hasClass('TCGNB')) return;
		gnb_current.addClass('TCGNB');
		gnb_current.removeClass('AB-current-gnb');
		$(this).addClass('AB-current-gnb');
	});
	gnb_li.mouseleave(function() {
		$(this).removeClass('AB-current-gnb');
		gnb_current = $('.TCGNB');
		gnb_current.addClass('AB-current-gnb');
		gnb_current.removeClass('TCGNB');		
	});

	// lnb
	var lnb_li = $('.ABA-lnb > li'), lnb_sub = $('.ABA-lnb-sub');
	lnb_li.mouseover(function() {
		$('ul', $(this)).eq(0).show('fast');
	});

	function m_over_slide(){
		var gnb_a = $(this);
		if (gnb_a.next().hasClass('AB-live-menu')) {
			gnb_a.parent().parent().children().children('ul').hide('fast').addClass('AB-live-menu');
			gnb_a.next('ul').show('fast').removeClass('AB-live-menu');
		} else if (!gnb_a.next('ul').length) {
			gnb_a.parent().parent().children().children('ul').hide('fast').addClass('AB-live-menu');
		}
	}
	
	function m_over_display(){
		var gnb_a = $(this);
		if (gnb_a.next().hasClass('AB-live-menu')) {
			gnb_a.parent().parent().children().children('ul').css('display', 'none').addClass('AB-live-menu');
			gnb_a.next('ul').css('display', 'block').removeClass('AB-live-menu');
		} else if (!gnb_a.next('ul').length) {
			gnb_a.parent().parent().children().children('ul').css('display', 'none').addClass('AB-live-menu');
		}
	}
});
