$(function() {
	/* navigation */
	$('div.lnb-gnb').TopMenu({'hover':{'dept1':'.gif'}});
});

$.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')
		{
			n	= src.replace('_on' + ext, ext);
			src	= n.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').find('a:first').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:first').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);
			}
		});

	});
};
