jQuery(function($){

/*
	ie 6 이하 처리
*/
	if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 6)
	{
		try{ document.execCommand("BackgroundImageCache", false, true); }catch(e){}
		$('.container > hr').hide().width(0).height(0);
		$('div.container > div.center-box > hr').hide().width(0).height(0);

		var sm = $('body.sub nav#sub-nav'),
			cc = $('body.sub section.content');
		if(sm.height() > cc.height())
		{
			var h = sm.height();
			h = h > 500 ? h : 500;
			cc.height(h);
		}
	}


/*
object, embed, apple 처리
*/
	$('object, embed, applet').each(function(index)
	{
		var t = $(this),
			c = t.get(0).cloneNode(true);
		t.after(c).remove();
	});





	// Top Menu
	var $topmenu = $('nav#main-nav');
	if($topmenu.get(0))
	{
		var $nowmenuno = ($menuno.split('|'))[0],
			$selector = '#topmenu_'+ $nowmenuno.toString(),
			$nowmenu = $topmenu.find($selector),
			$nowmenu_depth1 = $nowmenu.parents('.cd1'),
			$menu_depth1 = $topmenu.find('.ld1>.cd1:visible'),
			$menu_depth1_a = $menu_depth1.find('>a'),
			$menu_depth2 = $menu_depth1.find('.ld2-dumybox'),
			$menu_now_active = null;

		// 2차 메뉴 외곽에 박스 삽입
		$topmenu.find('.ld2').each(function(index)
		{
			var $this = $(this),
				$parent = $this.parent();

			$this.show();
			$parent.append('<div class="ld2-dumybox"></div>');
			$parent.find('.ld2-dumybox').append($this);
		});

		$menu_depth2 = $menu_depth1.find('.ld2-dumybox');

		$menu_depth1.each(function(index)
		{
			var $this = $(this),
				$index = index;

/*
			// 하위 메뉴 위치 조정
			$this.find('.ld2-dumybox').each(function(index)
			{
				var $this = $(this),
					$parent_width = $this.parent().width(),
					$width = $this.width(),
					$value = $parent_width - $width;

				if($width < ($parent_width + 10))
					$this.width($parent_width + 10);
				else
					$this.width($width + 10);

				$width = $this.width();
				$value = $parent_width - $width;

				$value = parseInt($value / 2, 10);
				$this.css('margin-left', $value);

				// 더미 자식 추가
				$this.append('<div class="arrow"></div>');
			});
*/

			// 기본 숨김, 보이기 처리
			if($this.get(0) == $nowmenu_depth1.get(0))
			{
				$menu_now_active = $this.find('>a');
				$menu_now_active.addClass('active');
				$this.find('.ld2-dumybox').hide();
			}
			else
			{
				$this.find('.ld2-dumybox').hide();
			}
		});

/*
		$menu_depth1.bind('mouseleave', function(event)
		{
			$menu_depth1_a.removeClass('active');
			$menu_depth2.hide();

			$menu_now_active.addClass('active');
		});
*/

		$menu_depth1_a.bind('mouseover focus', function(event)
		{
			var $this = $(this);
			$menu_depth1_a.removeClass('active');
			$menu_depth2.hide();

			$this.addClass('active');
			$this.parents('.cd1').find('.ld2-dumybox').show();
		});
	}


	// SubMenu
	var $submenu = $('nav#sub-nav');
	if($submenu.get(0))
	{
		var $nowmenunoarray = ($menuno.split('|')),
			$selector,
			$menu,
			$submenu_event = null;

		$submenu.find('.cd2').each(function(index)
		{
			var $this = $(this);

			if($this.find('.ld3').length > 0)
			{
				$this.addClass('close');
				$this.find('.ld3').hide();

				$this.find('>a').bind('click focus', function(event)
				{
					event.preventDefault();

					if($submenu_event == this) return;
					$submenu_event = this;
					setTimeout(function(){ $submenu_event=null }, 200);

					var $this = $(this),
						$parent = $this.parents('.cd2');

					if($parent.hasClass('open'))
					{
						$parent.removeClass('open');
						$parent.addClass('close');
						$parent.find('.ld3').slideUp('fast');
					}
					else
					{
						$parent.removeClass('close');
						$parent.addClass('open');
						$parent.find('.ld3').slideDown('fast');
					}
				});
			}
		});

		for(var i=0; i<$nowmenunoarray.length; ++i)
		{
			$selector = '#submenu_'+ $nowmenunoarray[i].toString();
			$menu = $submenu.find($selector);
			$menu.addClass('active');
			$menu.parents('li').removeClass('close').addClass('open').find('>ul').show();
		}
	}

	
	// 메인 - 게시판
	$('article#main section.board').each(function(index)
	{
		var t = $(this),
			h2 = t.find('h2'),
			ul = t.find('ul'),
			h2a = h2.find('a');

		h2.each(function(idx)
		{
			var tt = $(this),
				u = tt.next('ul');

			if(idx == 0)
			{
				tt.find('a').addClass('active');
				u.show();
			}
			else
			{
				tt.find('a').removeClass('active');
				u.hide();
			}
		});

		h2a.each(function(idx)
		{
			var tt = $(this);

			tt.mouseover(function(event)
			{
				h2a.removeClass('active');
				ul.hide();

				h2a.eq(idx).addClass('active');
				ul.eq(idx).show();
			});
		});
	});


	// 메인 - 일정
	$('article#main section.schdule').each(function(index)
	{
		var t = $(this),
			h3 = t.find('h3'),
			p = t.find('p');

		h3.each(function(idx)
		{
			var tt = $(this),
				pp = tt.next('p');

			if(idx == 0)
			{
				tt.addClass('active');
				pp.show();
			}
			else
			{
				tt.removeClass('active');
				pp.hide();
			}

			tt.mouseover(function(event)
			{
				h3.removeClass('active');
				p.hide();

				h3.eq(idx).addClass('active');
				p.eq(idx).show();
			});
		});
	});
	



	// 베너 처리
	$('footer#footer aside.footer section.banner').navigation(
	{
		showcount : 5,
		control : true,
		navigation : false,
		controlinfo : 
		{
			previous : true,
			next : true
		},
		intervaltime : 3000,
		changetime : 1000,
		canvastag : 'ul',
		listtag : 'li',
		changetype : 'slide'
	});
});

