/*----------------------------------------------------------------------------
 * AllMenuView
 * ---------------------------------------------------------------------------
 * Date		: 2011-02-08
 * Author	: PLANI Web Standardization Team (http://plani.co.kr/) kimkikwan
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).AllMenuView({'target':selector});
 *----------------------------------------------------------------------------
 */
$.fn.AllMenuView	= function (option)
{
	var option		= option || {};
	var btn_close	= '<div class="btn-all-menu-close"><a href="#btn-menu-all" title="CLOSE"></a></div>';

	if ($(option.target).length<1) { return; }


	$(option.target).children('div.menu-all-wrap').hide().css({'overflow':'visible'}).each(function()
	{
		$(this).append(btn_close);

		var id	= $(this).attr('id');

		$(this).children('ul.menu-all-navigation').find('a').each(function()
		{
			var target	= $(this).attr('href');
			var	target_id	= target.split('#');
			var target_object	= $('div' + target);
			var is_target	= $(target_object).length>0 ? true : false;

			$(this).click(function()
			{
				if (id!=target_id[1] && is_target==true)
				{
					$(option.target).children('div.menu-all-wrap').hide();
					$(target_object).show();
					
					$(target_object).children('ul.menu-all-navigation').find('a').each(function()
					{
						if ($(this).attr('href')==target)
						{
							$(this).focus();
						}
					});
				}

				return false;
			});
		});
	});

	$(option.target).css({'position':'absolute', 'top':0, 'right':0, 'z-index':100, 'overflow':'visible', 'height':'auto'});

	$(this).click(function()
	{
		$('div#menu-all-view').show();

		if ($(option.target + ' div.menu-all-wrap:visible').length==0)
		{
			$(option.target + ' div.menu-all-wrap:first').show();
		}
		else
		{
			$(option.target).children('div.menu-all-wrap').hide();
		}

		return false;
	});

	$(option.target).find('div.btn-all-menu-close').click(function()
	{
		$(option.target).children('div.menu-all-wrap').hide();
		return false;
	});
};

/*----------------------------------------------------------------------------
 * Top Menu
 * ---------------------------------------------------------------------------
 * Date		: 2011-02-15
 * Author	: PLANI Web Standardization Team (http://plani.co.kr/) kimkikwan
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).TopMenu();
 *----------------------------------------------------------------------------
 */
$.fn.TopMenu	= function (option)
{
	var option		= option || {};
	var interval	= option.interval ? option.interval : 500;
	
	if (!option.hover) { option.hover	= {};}

	var changeImage	= function (obj, ext, action)
	{
		var img	= $(obj).find('img');
		var src	= $(img).attr('src');

		if (action=='over')
		{
			src	= src.replace(ext, '_on' + ext);
		}
		else
		{
			src	= src.replace('_on' + ext, ext);
		}
		
		$(img).attr('src', src);
	};

	$(this).find('ul.lnb-sub').hide();

	var lnb	= $(this);
	var timeout	= null;
	
	var dept1_index	= $(lnb).children('ul').children('li.current:first').index();
	var dept2_index	= $(lnb).children('ul').children('li:eq('+ dept1_index + ')').find('ul.lnb-sub').children('li.current').index();

	var init	= function ()
	{
		if (dept1_index>=0)
		{
			if (option.hover.dept1)
			{
				$(lnb).children('ul').children('li').children('a:first').each(function()
				{
					changeImage($(this), option.hover.dept1);
				});
				$(lnb).children('ul').children('li').find('a').each(function()
				{
					changeImage($(this), option.hover.dept1);
				});
			}

			//$(lnb).children('ul').children('li.current').removeClass('current');
		}

		if (dept2_index>=0)
		{
			if (option.hover.dept2)
			{
				changeImage($(lnb).children('ul').find('ul.lnb-sub').children('li:eq('+ dept2_index + ')'), option.hover.dept2);
			}

			//$(lnb).children('ul').find('ul.lnb-sub').children('li').removeClass('current');
		}

		$(lnb).find('li.current').removeClass('current');
	};

	var setCurrent	= function ()
	{
		$(lnb).find('ul.lnb-sub').hide();
		
		if (dept1_index>=0)
		{
			if (option.hover.dept1)
			{
				$(lnb).children('ul').children('li').children('a:first').each(function()
				{
					changeImage($(this), option.hover.dept1);
				});

				changeImage($(lnb).children('ul').children('li:eq('+ dept1_index + ')').children('a:first'), option.hover.dept1, 'over');
			}

			$(lnb).children('ul').children('li:eq('+ dept1_index + ')').addClass('current');
		}
		
		if (dept2_index>=0)
		{
			if (option.hover.dept2)
			{
				dept2_reset();
				changeImage($(lnb).children('ul').children('li:eq('+ dept1_index + ')').find('ul.lnb-sub').children('li:eq(' + dept2_index + ')'), option.hover.dept2, 'over');
			}

			$(lnb).children('ul').children('li:eq('+ dept1_index + ')').find('ul.lnb-sub').children('li:eq(' + dept2_index + ')').addClass('current');

		}
		
		if (dept1_index>=0)
		{
			$(lnb).children('ul').children('li:eq('+ dept1_index + ')').find('ul.lnb-sub').show();
		}
	};

	var dept1_reset	= function ()
	{
		if (option.hover.dept1)
		{
			$(lnb).children('ul').children('li').find('a').each(function()
			{
				changeImage($(this), option.hover.dept1);
			});

			dept2_reset();
		}
	};

	var dept2_reset	= function ()
	{
		if (option.hover.dept2)
		{
			$(lnb).find('ul.lnb-sub').find('a').removeClass('focus-in').each(function()
			{
				changeImage($(this), option.hover.dept2);
			});
		}
	};

	init();
	setCurrent();

	$(this).children('ul').children('li').each(function()
	{
		$(this).children('a:first').hover
		(
			function()
			{
				dept1_reset();				
				$(lnb).find('ul.lnb-sub').hide();
				$(this).parent().children('ul.lnb-sub').show();
				
				clearTimeout(timeout); 
	
				init();

				if (option.hover.dept1)
				{
					changeImage($(this), option.hover.dept1, 'over');
				}
			},
			function()
			{
				timeout	= setTimeout(setCurrent, interval);

				if (option.hover.dept1)
				{
					changeImage($(this), option.hover.dept1);
				}
			}
		)
		.focus(function()
		{
			dept1_reset();
			$(lnb).find('ul.lnb-sub').hide();
			$(this).parent().children('ul.lnb-sub').show();

			clearTimeout(timeout);
			init();

			if (option.hover.dept1)
			{
				changeImage($(this), option.hover.dept1, 'over');
			}
		})
		.blur(function()
		{
			if (option.hover.dept1)
			{
				changeImage($(this), option.hover.dept1);
			}
			timeout	= setTimeout(setCurrent, interval);
		});

		$(this).children('ul.lnb-sub').hover
		(
			function()
			{
				$(this).show();
				clearTimeout(timeout);

				init();

				if (option.hover.dept1)
				{
					changeImage($(this).parent().children('a:first'), option.hover.dept1, 'over');
				}

			},
			function()
			{
				if (option.hover.dept1)
				{
					changeImage($(this).parent().children('a:first'), option.hover.dept1);
				}

				timeout	= setTimeout(setCurrent, interval);
			}
		);

		$(this).children('ul.lnb-sub').find('a').hover
		(
			function()
			{
				dept2_reset();
				$(this).addClass('focus-in');
				changeImage($(this), option.hover.dept2, 'over');
			},
			function()
			{
				dept2_reset();
				$(this).removeClass('focus-in');
				changeImage($(this), option.hover.dept2);
			}
		)
		.focus(function()
		{
			dept2_reset();

			$(this).addClass('focus-in');
			changeImage($(this), option.hover.dept2, 'over');

			clearTimeout(timeout);
			init();

			if (option.hover.dept1)
			{
				changeImage($(this).parent().parent().parent().children('a:first'), option.hover.dept1, 'over');
			}
		})
		.blur(function()
		{
			$(this).removeClass('focus-in');
			timeout	= setTimeout(setCurrent, interval);
			changeImage($(this), option.hover.dept2);

			if (option.hover.dept1)
			{
				changeImage($(this).parent().parent().parent().children('a:first'), option.hover.dept1);
			}
		});

	});
};

/*----------------------------------------------------------------------------
 * Visual Close
 * ---------------------------------------------------------------------------
 * Date		: 2011-02-08
 * Author	: PLANI Web Standardization Team (http://plani.co.kr/) kimkikwan
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).AllMenuView();
 *----------------------------------------------------------------------------
 */
$.fn.VisualClose	= function ()
{
	var btn_close	= '<a href="#sub-visual" id="btn-visual-close" title="close"></a>';

	$(this).append(btn_close);

	$('a#btn-visual-close').click(function()
	{
		$('div#sub-visual').hide();
		$('div#layout-container').css('background', 'none');
		return false;
	});
};


/*----------------------------------------------------------------------------
 * Left Menu
 * ---------------------------------------------------------------------------
 * Date		: 2011-02-08
 * Author	: PLANI Web Standardization Team (http://plani.co.kr/) kimkikwan
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).LeftMenu();
 *----------------------------------------------------------------------------
 */
$.fn.LeftMenu	= function ()
{

	$(this).children('ul.snb-root').find('ul.snb-sub').hide();
	$(this).children('ul.snb-root').children('li.current').find('ul.snb-sub').show();
	
	/*
	var btn_close	= '<a href="#sub-visual" id="btn-visual-close" title="close"></a>';

	$(this).append(btn_close);

	$('a#btn-visual-close').click(function()
	{
		$('div#sub-visual').hide();
		$('div#layout-container').css('background', 'none');
		return false;
	});
	*/
};

/*----------------------------------------------------------------------------
 * Article Tab
 * ---------------------------------------------------------------------------
 * Date		: 2011-02-08
 * Author	: PLANI Web Standardization Team (http://plani.co.kr/) kimkikwan
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).ArticleTab();
 *----------------------------------------------------------------------------
 */

$.fn.ArticleTab	= function ()
{
	var element	= $(this);

	$(element).css('overflow', 'hidden');

	$(element).each(function()
	{
		var obj	= $(this);

		$(this).children('div.article-tab-wrap').each(function(i)
		{
			if (i>0) { $(this).hide(); }

			$(this).children('ul.article-tab-header').children('li').find('a:first').each(function()
			{
				$(this).click(function()
				{
					try
					{
						var list	= $(obj).find('ul' + $(this).attr('href')).parent('div.article-tab-wrap');
						var photo	= $(obj).find('div' + $(this).attr('href')).parent('div.article-tab-wrap');
						var target	= $(this).attr('href');

						if ($(list).length>0)
						{
							$(obj).children('div.article-tab-wrap').hide();
							$(list).show();

							$(list).children('ul.article-tab-header').find('a').each(function()
							{
								if ($(this).attr('href')==target) { $(this).focus(); }
							});
						}
						else if ($(photo).length>0)
						{
							$(obj).children('div.article-tab-wrap').hide();
							$(photo).show();
							$(photo).children('ul.article-tab-header').find('a').each(function()
							{
								if ($(this).attr('href')==target) { $(this).focus(); }
							});
						}

						return false;
					}
					catch (e) {}
				});

				$(this).mouseover(function()
				{
					try
					{
						var list	= $(obj).find('ul' + $(this).attr('href')).parent('div.article-tab-wrap');
						var photo	= $(obj).find('div' + $(this).attr('href')).parent('div.article-tab-wrap');
						var target	= $(this).attr('href');

						if ($(list).length>0)
						{
							$(obj).children('div.article-tab-wrap').hide();
							$(list).show();

							$(list).children('ul.article-tab-header').find('a').each(function()
							{
								if ($(this).attr('href')==target) { $(this).focus(); }
							});
						}
						else if ($(photo).length>0)
						{
							$(obj).children('div.article-tab-wrap').hide();
							$(photo).show();
							$(photo).children('ul.article-tab-header').find('a').each(function()
							{
								if ($(this).attr('href')==target) { $(this).focus(); }
							});
						}

						return false;
					}
					catch (e) {}
				});

			});
		});
	});
};

/*----------------------------------------------------------------------------
 * Tab
 * ---------------------------------------------------------------------------
 * Date		: 2011-02-23
 * Author	: PLANI Web Standardization Team (http://plani.co.kr/) kimkikwan
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 * $(selector).Tab();
 *----------------------------------------------------------------------------
 */

$.fn.Tab	= function ()
{
	var element	= $(this);

	$(element).each(function()
	{
		var obj	= $(this);

		$(this).children('div.tab-wrap').each(function(i)
		{
			if (i>0) { $(this).hide(); }

			$(this).children('ul.tab-header').children('li').find('a:first').each(function()
			{
				$(this).click(function()
				{
					try
					{
						var list	= $(obj).find('div' + $(this).attr('href'));
						var target	= $(this).attr('href');

						if ($(list).length>0)
						{
							$(obj).children('div.tab-wrap').hide();
							$(list).show();

							$(list).children('ul.tab-header').find('a').each(function()
							{
								if ($(this).attr('href')==target) { $(this).focus(); }
							});
						}

						return false;
					}
					catch (e) {}
				});
			});
		});
	});
};

