/*
* @author lks
*/
// gnb menu
try{ document.execCommand("BackgroundImageCache", false, true); }catch(e){}

$(function(){

	// 상단 검색 bg img 처리 (검색어가 없을 경우 show, 있을 경우 hide)
	$(".newSearchForm input:eq(0)").focus(function(){
		$(this).css({"background-image":"none"});
	}).focusout(function(){
		if( $(this).val() == "" || $(this).val() == undefined )
		{
			$(this).css({"background-image":"url(/en/common/images/text/inputSearch.gif)"});
		}
	});

	$("form[name=totalSearch]").submit(function(){
		if($('#Query').val() == ''){
			alert('Please enter search words.');
			return false;
		}
	});

	// 상단 다국어 선택 layer popup
	if( isPC )
	{
		$(".newSearchLang").mouseenter(function(){
			
			$(this).find("span").addClass("on");
			$(".nlist").slideDown(200);
	
		}).mouseleave(function(){
	
			$(this).find("span").removeClass("on");
			$(".nlist").slideUp(200);
		});
	}
	else
	{
		$(".newSearchLang").click(function(){

			if( $(".nlist").css("display") == "block" )
			{
				$(this).find("span").removeClass("on");
				$(".nlist").slideUp(200);
			}
			else
			{
				$(this).find("span").addClass("on");
				$(".nlist").slideDown(200);
			}
		}).mouseleave(function(){
	
			$(this).find("span").removeClass("on");
			$(".nlist").slideUp(200);
		});
	}
});

(function($){

	// URL.MAIN : GNB LOGO 클릭 시 이동 될 경우
	// LIMIT_NO : GNB 메뉴 갯수 제한 (SITEMAP.XML에서 LIMIT_NO 이상은 XML NODE 파싱 안함)
	var URL = {
		MAIN : "/en/index.jsp"
	};
	var LIMIT_NO = 4;

	$(function(){

		// 팝업창 등 GNB가 필요 없는 페이지에서 <body class="noMenu"> 로 줄 경우 GNB 동작 안함
		if($("body.noMenu").size() > 0)
			return false;

		// XML 경로
		var xmlURL = "/en/common/xml/sitemap.xml";
		$.get(xmlURL, function(data){
			// XML 로딩 완료 시 GNB FUNCTION 실행
			xmlLoadComplete(data);
		});
	});

	// XML 로딩 완료 시 현재 페이지의 URL과 PARAMETER를 기준으로 XML node의 location, locationMap, posMap 비교
	function xmlLoadComplete(data)
	{
		var selected;
		var _locationInfo = getURL();
		var _location = _locationInfo.fileName;

		try
		{
			// url의 parameter 중 ?posMap="[xxxxx명]" 같은 형식으로 들어갈 경우
			// 강제로 선택 된 메뉴를 지정
			if( (_locationInfo.paramMap).indexOf("posMap") > -1 )
			{
				var posMap = "null";
				var nArrParamMap = _locationInfo.paramMap.split("&");

				for( var i=0; i < nArrParamMap.length; i++ )
				{
					if( (nArrParamMap[i]+"").indexOf("posMap") > -1 )
					{
						posMap = (nArrParamMap[i]).replace("posMap=", "");
					}
				}

				selected = $(data).find("*[posMap*=" + posMap + "]");
			}
			else
			{
				selected = $(data).find("*[locationMap*=" + _location+"?"+_locationInfo.paramMap + "]");
				selected = (isNULL(selected) || selected.size() == 0) ? $(data).find("*[locationMap*=" + _location + "]") : selected;
				selected = (isNULL(selected) || selected.size() == 0) ? $(data).find("*[location*=" + _location+"?"+_locationInfo.paramMap + "]") : selected;
				selected = (isNULL(selected) || selected.size() == 0) ? $(data).find("*[location*=" + _location + "]") : selected;
			}
		}
		catch(e)
		{
			//alert(e);
		}

		// 현재 url과 xml node를 매칭 될 경우 해당 node와 xml 전체를 인자값으로 전달
		create( selected, data );
	}

	// design : css
	// action : javascript
	function create( selected, data )
	{
		var html = "";

		// depth2 inserHTML 관련 script
		html = getGnbDep1HTML( data );
		$(".newgnbMenu:eq(0)").html( html );

		// depth2 inserHTML 관련 script
		html = getGnbDep2HTML( data );
		$(".subgnbmenu:eq(0)").html( html );

		// gnb motion 관련 script
		setGnbAction();
	}

	// 가독성을 위한 innerHTML depth1
	function getGnbDep1HTML( data )
	{
		// logoIndex : logo과 depth과 같이 들어가기 때문에 logo가 들어가는 위치 지정
		var oMenu;
		var logoIndex = Math.ceil( $(data).find("depth1[over!=#]").size() / 2 );
		var html = '<ul>\n';

		// subject, over, out, lnbTitleImg, target, firstChild-url
		$(data).find("depth1[over!=#]").each(function(index){

			if( index > LIMIT_NO )
			{
				return;
			}

			oMenu = {
				"subject"	: $(this).attr("subject")
			,	"target"	: $(this).attr("target")
			,	"href"		: $(this).find("depth2:eq(0)").attr("url")
			};

			var isPCURL = (isPC ? (oMenu.href == undefined ? "#" : oMenu.href) : "#void");
			html += '	<li class="gnbmenu'+ ++index +'"><a href="'+ isPCURL +'" title="'+ oMenu.subject +'" target="'+ oMenu.target +'"></a></li>\n';

			if(index == logoIndex)
			{
				html += '	<li class="gnbmenulogo"><a href="'+ URL.MAIN +'" title="hynix home"></a></li>';
			}
		});

		html += '</ul>\n';

		return html;
	}

	// 가독성을 위한 innerHTML depth2
	function getGnbDep2HTML( data )
	{
		var html	=	'<div class="subgnbBg"></div>\n'
					+	'<div class="subgnblist">\n';

		$(data).find("depth1[over!=#]").each(function(index){

			// LIMIT_NO : 상단에 정의 된 menu 갯수 제한
			if( index > LIMIT_NO )
			{
				return;
			}

			html	+=	'<dl class="sublist0'+ ++index +'">\n'
					+	'	<dt>'+ $(this).attr("subject") +'</dt>\n'
					+	'	<dd>\n'
					+	'		<ul>\n';

			$(this).find("depth2").each(function(){
				html+=	'			<li>\n'
					+	'				<a href="'+ $(this).attr("url") +'" target="'+ $(this).attr("target") +'">\n'
					+	'					'+ $(this).attr("subject") +'\n'
					+	'				</a>\n'
					+	'			</li>\n';
			});

			html	+=	'		</ul>\n'
					+	'	</dd>\n'
					+	'</dl>\n';
		});

		html	+= '</div>\n';
		
		return html;
	}

	// motion 부분
	function setGnbAction()
	{
		var $subgnbmenu		= $(".subgnbmenu");
		var selectedAction	= null;

		var $subgnbBg		= $(".subgnbBg:eq(0)");
		var $subgnblist		= $(".subgnblist:eq(0)"); 

		// 224 | 240(224+16) | 300(224+76)
		var autoHeight		= $subgnblist.height() + 20;
		
		$subgnblist.css({
			"margin-top" : function(){
				return -1 * (autoHeight + 16);
			}
		});

		$subgnbBg.height( autoHeight + 76 );

		var speed 	= {
			slideUp 	: isPC ? 400 : 1000
		,	slideDown 	: isPC ? 500 : 600
		,	delay		: isPC ? 400 : 600
		};

		var subObtion = {
			over	: 0
		,	out		: -($subgnbmenu.height() + 20)
		};

		$subgnbmenu.css({ "top":subObtion.out });

		if( isPC )
		{
			$(".newgnbMenu li[class!=gnbmenulogo] a, .subgnbmenu").mouseenter(function(){
	
				if( selectedAction )
				{
					clearTimeout( selectedAction );
				}
	
				$subgnbmenu.stop();
				$subgnbmenu.animate({ "top":subObtion.over }, speed.slideDown);
	
			}).mouseleave(function(){
	
				selectedAction = setTimeout(function(){
					$subgnbmenu.animate({ "top":subObtion.out }, speed.slideUp);
				}, speed.delay);
	
			});
		}
		else
		{
			$(".newgnbMenu li[class!=gnbmenulogo] a, .subgnbmenu").click(function(){

				if( parseInt( $subgnbmenu.css("top") ) != 0 )
				{
					$subgnbmenu.stop();
					$subgnbmenu.animate({ "top":subObtion.over }, speed.slideDown);					
				}
				else
				{
					$subgnbmenu.animate({ "top":subObtion.out }, speed.slideUp);
				}

			}).mouseleave(function(){
				$subgnbmenu.animate({ "top":subObtion.out }, speed.slideUp);
			});
		}
	}

	// 현재 페이지 url을 object 형식으로 return
	function getURL()
	{
		var rtn = '';
		var tmpUrl = location.href;
		var tmpParam = location.href;

		if( tmpUrl.indexOf("?") > -1 )
		{
			tmpUrl = tmpUrl.substring( 0, tmpUrl.indexOf("?") );
		}

		if( tmpUrl.indexOf("#") > -1 )
		{
			tmpUrl = tmpUrl.substring( 0, tmpUrl.indexOf("#") );
		}

		var url = tmpUrl.split("/");
		var page = url[ url.length - 2 ] + "/" + url[ url.length - 1 ];
		var param = "";

		if( tmpParam.indexOf("?") > -1 )
		{
			param = tmpParam.substring( tmpParam.indexOf("?")+1, tmpParam.length );
		}

		if( param.indexOf("#") > -1 )
		{
			var tmp = param.split("#");
			param = tmp[0] + (tmp[1].substring( tmp[1].indexOf("&"), tmp[1].length ));
		}

		rtn = { "fileName":page, "paramMap":param };
		return rtn;
	}

	// 문자열 체크
	function isNULL(obj)
	{
		if(obj == null || obj == "" || obj == undefined || obj == "undefined")
		{
			return true;
		}

		return false;
	}

})(jQuery);
