/******************************
* Nate GNB
* @sample			NateGNB.addAddition();
* @sample			NateGNB.addAddition();
* @sample			NateGNB.addPromotion();
* @sample			NateGNB.write();
******************************/

// object name
var objName = "NateGNB";
// nGnbType
var GNB_MENU_ONLY = 0;
var GNB_DEFAULT = 1;
var GNB_MINI = 2;
var GNB_SEARCH_RESULT = 3;
var GNB_SEARCH_MAIN = 4;
// frame info
var GNB_FRAME_NAME = "";
// define GNB class
function Class_NateGNB()
{
	// member
	var nGnbType = 1;
	var nIsIE = null;
	var nUserMinWidth = null;
	var sUserWidth = null;
	var rGnbTypeData = new Array();
	var rGnbMenu = new Array();
	var rGnbSelect = new Array();
	var rAddition = new Array();
	var oPromotion = null;
	var sPromotionHTML = null;

	// method
	var init;

	var writeStyle;
	var writeGNB;
	var getCookie;
	var getVersionOfIE;
	var getAdditionCSS;
	var getCenterMargin;
	var createAddition

	this.setSize;
	this.setMinSize;
	this.addAddition;
	this.addPromotion;
	this.addPromotionHTML;

	this.write;

	this.logOn;
	this.logOut;
	this.redirectService;
	this.setDefaultPage;
	this.setDefaultSearch;

	// initalize
	init = function()
	{
		//getArguments();
		nIsIE = getVersionOfIE();

		rGnbTypeData = Array(
			{width: "100%", minwidth : "960px",height : "25px", top : "5px"},
			{width: "100%", minwidth : "965px",height : "60px", top : "5px"},
			{width: "100%", minwidth : "960px",height : "35px", top : "8px"},
			{width: "100%", minwidth : "980px",height : "40px", top : "10px"},
			{width: "855px", minwidth : "855px",height : "25px", top : "5px"}
		);
		
		rGnbMenu = Array(
			{text : "Nate.com", link : "http://www.nate.com/", cname : "main", target : ""},
			{text : "Mail", link : "http://mail.nate.com/", cname : "nosplit", target : ""},
			{text : "Tong", link : "http://tong.nate.com/", cname : "", target : "nate_pop"},
			{text : "TalkTalk", link : "http://bbs.nate.com/", cname : "", target : ""},
			{text : "News", link : "http://news.nate.com/", cname : "", target : ""},
			{text : "Shopping", link : "http://mall.nate.com/", cname : "", target : ""},
			{text : "Mobile", link : "http://mobile.nate.com/", cname : "", target : ""},
			{text : "T WORLD", link : "http://www.tworld.co.kr/outsite.html?http://www.tworld.co.kr?xtr_cate=LK&xtr_ref=nate.com&xtr_kw=-doccom main top 61*16 T WORLD Title", cname : "", target : "nate_pop"}
		);

		rGnbSelect = Array(
			{text : "More Services", link : ""},
			{text : "------------------", link : ""},
			{text : "Search", link : "http://nate.search.empas.com/"},
			{text : "Club", link : "http://club.nate.com/"},
			{text : "Cizle", link : "http://cizle.nate.com/"},
			{text : "Comics", link : "http://comics.nate.com/"},
			{text : "Finance", link : "http://finance.nate.com/"},
			{text : "Fortune", link : "http://fortune.nate.com/"},
			{text : "Game", link : "http://game.nate.com/"},
			{text : "Ú¸'s", link : "http://mis.nate.com/"},
			{text : "Life", link : "http://life.nate.com/"},
			{text : "Music", link : "http://music.nate.com/"},
			{text : "NateOn", link : "http://nateonweb.nate.com/"},
			{text : "My Nate", link : "http://my.nate.com/"},
			{text : "------------------", link : ""},
			{text : "Site Map", link : "http://www.nate.com/sitemap/"}
		);
	}
	
	// write CSS
	writeStyle = function()
	{
		var style = "";
		style += "<style type=\"text/css\">";
		style += "#nate_gnb *{line-height:14px;}";
		style += "#nate_gnb img{margin:0;padding:0;border-width:0;}";
		style += "#nate_gnb{position:relative;height:" + rGnbTypeData[nGnbType].height + ";width:" + ( ( sUserWidth != null ) ? sUserWidth : rGnbTypeData[nGnbType].width ) + ";min-width:" + ( ( nUserMinWidth != null ) ? nUserMinWidth : rGnbTypeData[nGnbType].minwidth ) + ";margin:0 auto;font-size:12px;font-family:µ¸¿ò;}";
		style += "#nate_gnb a{text-decoration:none;}";
		style += "#nate_gnb .minwidth{width:" + ( ( nUserMinWidth != null ) ? nUserMinWidth : rGnbTypeData[nGnbType].minwidth ) + ";height:" + rGnbTypeData[nGnbType].top + ";overflow:hidden;}";
		style += "#nate_gnb .logo{position:absolute;left:0;top:0;width:380px;overflow:hidden;}";
		style += "#nate_gnb .menu{position:absolute;right:0;width:660px;}";

		switch( nGnbType )
		{
			case 0:
				break;
			case 1:
				style += "#nate_gnb a.bi{float:left;display:inline;width:95px;height:30px;overflow:hidden;margin:" + getCenterMargin( 30 ) + "px 5px 0 0;}";
				style += getAdditionCSS();
				if( oPromotion )
				{
					style += "#nate_gnb .promotion{position:absolute;right:10px;bottom:2px;width:280px;height:" + ( 32 - parseInt( ( 32 - oPromotion.height ) / 2) ) + "px;overflow:hidden;text-align:right;}";
					style += "#nate_gnb .promotion a{width:" + oPromotion.width + "px;height:" + oPromotion.height + "px;overflow:hidden;}";
				}
				else if( sPromotionHTML )
				{
					style += "#nate_gnb .promotion{position:absolute;right:20px;bottom:2px;width:280px;height:32px;overflow:hidden;text-align:right;}";
				}
				break;
			case 2:
				style += "#nate_gnb a.bi{float:left;display:inline;width:83px;height:26px;overflow:hidden;margin:" + getCenterMargin( 26 ) + "px 5px 0 0;}";
				style += getAdditionCSS();
				break;
			case 3:
				style += "#nate_gnb a.bi{float:left;display:inline;width:83px;height:26px;overflow:hidden;margin:" + getCenterMargin( 26 ) + "px 5px 0 0;}";
				style += getAdditionCSS();
				break;
			case 4:
				if( nIsIE )
				{
					style += "#nate_gnb a.bw_setting{float:left;display:inline;margin:9px 0 0 10px;padding-left:10px;overflow:hidden;background:url('http://nsf.nate.com/m10/gnb_ie.gif') no-repeat 0 4px;color:#444;cursor:pointer;}";
				}
				break;
		}

		style += "#nate_gnb ul{float:right;display:inline;margin:0;padding:0;list-style:none;}";
		style += "#nate_gnb li{list-style:none;float:left;display:inline;margin-top:4px;padding:0 8px 0 7px;background:url('http://nsf.nate.com/m10/gnb_vbar.gif') no-repeat 0 2px;}";
		style += "#nate_gnb li a{color:#444;}";
		style += "#nate_gnb li.main{margin-right:7px;color:#6f54f2;background:url('http://nsf.nate.com/m10/gnb_msty.gif') no-repeat 100% 4px;}";
		style += "#nate_gnb li.main a{color:#6f54f2;}";
		style += "#nate_gnb li.nosplit{background:none;}";
		style += "#nate_gnb li.nomargin{margin-top:0;padding:0;}";
		style += "#nate_gnb li .border{float:left;display:inline;width:114px;height:19px;margin:0;padding:0;background:#c4c4c4;overflow:hidden;text-align:left;}";
		style += "#nate_gnb li .line{float:left;display:inline;width:112px;height:17px;margin:1px;padding:0;background:#fff;overflow:hidden;border:none;}";
		style += "#nate_gnb li .padding{float:left;display:inline;position:relative;width:110px;height:15px;left:1px;top:1px;margin:0;padding:0;background:#fff;overflow:hidden;}";
		style += "#nate_gnb select{float:left;display:inline;position:absolute;width:114px;height:19px;left:-2px;top:-2px;font-size:12px;color:#444;border:2px solid #fff;font-family:µ¸¿ò;}";
		style += "#nate_gnb .login, #nate_gnb .logout{height:19px;overflow:hidden;padding-left:7px !important;}";
		style += "#nate_gnb .login span{display:block;width:41px;height:16px;margin:2px 0;background:url('http://nateonweb.nate.com/en/images/menu/btn_gnb_sign_in.gif') no-repeat;cursor:pointer;}";
		style += "#nate_gnb .logout span{display:block;width:47px;height:16px;margin:2px 0;background:url('http://nateonweb.nate.com/en/images/menu/btn_gnb_sign_out.gif') no-repeat;cursor:pointer;}";
		style += "#nate_gnb a:hover, #nate_gnb a:active{text-decoration:underline;color:#6f54f2;}";
		style += "</style>";
		document.write( style );
	}

	// write gnb html element
	writeGNB = function()
	{
		var NAME=getCookie("n_");
		var promotion = "";
		var biImg = "";
		switch( nGnbType )
		{
			case 1:
				biImg = "http://nsf.nate.com/m10/gnb_bi_default.gif";
				break;
			case 2:
				biImg = "http://nsf.nate.com/m10/gnb_bi_mini.gif";
				break;
			case 3:
				biImg = "http://nsf.nate.com/m10/gnb_bi_srcres.gif";
				break;
		}

		g = "";
		g += "<div id=\"nate_gnb\">";
		g += "<div class=\"minwidth\"></div>";
		g += "<div class=\"logo\">";
		switch( nGnbType )
		{
			case 0:
				break;
			case 1:
				if( oPromotion )
				{
					promotion += "<div class=\"promotion\">";
					promotion += createAddition( "", oPromotion.imgurl, oPromotion.link, oPromotion.title );
					promotion += "</div>";
				}
				else if( sPromotionHTML )
				{
					promotion += "<div class=\"promotion\">";
					promotion += sPromotionHTML;
					promotion += "</div>";
				}
			case 2:
			case 3:
				g += createAddition( "bi", biImg, "http://www.nate.com/", "NATE.COM" );
				for( var i = 0; i < rAddition.length; i ++ )
				{
					g += createAddition( "addition" + ( i + 1 ), rAddition[i].imgurl, rAddition[i].link, rAddition[i].title, rAddition[i].target );
				}
				break;
			case 4:
				if( nIsIE )
				{
					g += "<a onclick=\"" + objName + ".setDefaultPage();\" class=\"bw_setting\">³×ÀÌÆ®´åÄÄÀ» ½ÃÀÛÆäÀÌÁö·Î</a>";
				}
				break;
		}
		g += "</div>";
		g += "<div class=\"menu\">";
		g += "<ul>";
		for( var i = 0; i < rGnbMenu.length; i ++ )
		{
			g += "<li";
			if( rGnbMenu[i].cname != "" )
			{
				g +=  " class=\"" + rGnbMenu[i].cname + "\"";
			}
			g += "><a href=\"" + rGnbMenu[i].link + "\"";
			if( rGnbMenu[i].target != "" )
			{
				g += "target=\"" + rGnbMenu[i].target + "\"";
			}
			g += ">" + rGnbMenu[i].text + "</a></li>";
		}
		g += "<li class=\"nosplit nomargin\">";
		g += "<div class=\"border\">";
		g += "<div class=\"line\">";
		g += "<div class=\"padding\">";
		g += "<select id=\"select_svc\" onchange=\"" + objName + ".redirectService(this);\">";
		for( var i = 0; i < rGnbSelect.length; i ++ )
		{
			g += "<option value=\"" + rGnbSelect[i].link + "\">" + rGnbSelect[i].text + "</option>";
		}
		g += "</select>";
		g += "</div>";
		g += "</div>";
		g += "</div>";
		g += "</li>";
		if( NAME == null || NAME == "" )
		{
			g += "<li class=\"login nosplit nomargin\"><a onclick=\"" + objName + ".logOn();\"><span></span>·Î±×ÀÎ</a></li>";
		}
		else
		{
			g += "<li class=\"logout nosplit nomargin\"><a onclick=\"" + objName + ".logOut();\"><span></span>·Î±×¾Æ¿ô</a></li>";
		}
		g += "</ul>";
		g += "</div>";
		g += promotion;
		g += "</div>";
		document.write( g );
	}

	// get cookie
	getCookie = function(name){
	  var cname = name + "=";
	  var dc = document.cookie;
	  if (dc.length > 0) { 
			 begin = dc.indexOf(cname);
		 if (begin != -1) {
			   begin += cname.length;end = dc.indexOf(";", begin);
			 if (end == -1) 
				end = dc.length;
				return unescape(dc.substring(begin, end));
			 }
		  }
		return null;
	}

	// check browser
	getVersionOfIE = function()
	{
		var ua = navigator.userAgent;
		return ( ua.indexOf( "MSIE" ) != -1 ) ? ua.split( "MSIE" )[1].split( ";" )[0] : false;
	}

	// make addition CSS
	getAdditionCSS = function()
	{
		var style = "";
		for( var i = 0; i < rAddition.length; i ++ )
		{
			style += "#nate_gnb a.addition" + ( i + 1 ) + "{float:left;display:inline;width:" + rAddition[i].width + "px;height:" + rAddition[i].height + "px;overflow:hidden;margin:" + getCenterMargin( rAddition[i].height ) + "px 0 0 10px;text-align:left;}";
		}
		return style;
	}

	getCenterMargin = function( height )
	{
		return ( parseInt( parseInt( rGnbTypeData[nGnbType].height ) - height ) / 2 );
	}

	// make addition HTML tag
	createAddition = function( cName, imgurl, link, title, target )
	{
		var rtn = "";
		rtn += "<a href=\"" + link + "\"";
		if( !!target )
		{
			rtn += " target=\"" + target + "\"";
		}
		rtn += " class=\"" + cName + "\"><img src=\"" + imgurl + "\" alt=\"" + title + "\" /></a>";
		return rtn;
	}


	// set GNB size
	this.setSize = function( width )
	{
		sUserWidth = width;
	}

	// set GNB minimum size
	this.setMinSize = function( width )
	{
		if( parseInt( width ) >= 830 )
			nUserMinWidth = ( typeof( width ) == "number" ) ? width + "px" : width;
	}

	// add addition
	this.addAddition = function( width, height, title, link, imgurl, target )
	{
		rAddition.push( {width : width, height : height, title : title, link : link, imgurl : imgurl, target : target} );
	}

	// add promotion
	this.addPromotion = function( width, height, title, link, imgurl, target )
	{
		oPromotion =  {width : width, height : height, title : title, link : link, imgurl : imgurl, target : target};
	}

	// add promotion for HTML String
	this.addPromotionHTML = function()
	{
		if( undefined !== window.GNB_PROMOTION_HTML && GNB_PROMOTION_HTML != null && typeof( GNB_PROMOTION_HTML ) == "string" )
		{
			sPromotionHTML = GNB_PROMOTION_HTML;
		}
	}

	// write gnb code
	this.write = function( type )
	{
		nGnbType = type;
		writeStyle();
		writeGNB();
	}

	// rediredt logon
	this.logOn = function(){
		var r_url = parent.document.location.href;
		var len = r_url.lastIndexOf("#");
		if ( len > 0 ) r_url = r_url.substring(0,len);
		if (r_url.indexOf("/logout.jsp") > 0 )parent.document.location="http://xo.nate.com/login.jsp?redirect=http://www.nate.com";
		else if ((r_url.indexOf("/notfound.jsp") < 0 ) && ( r_url.indexOf("/login.jsp") < 0 ) && (r_url.indexOf("/cplogin.jsp") < 0 ))
				parent.document.location="http://xo.nate.com/login.jsp?redirect=" + r_url ;
	}

	// rediredt logout
	this.logOut = function() {parent.document.location.replace('http://xo.nate.com/logout.jsp'); }

	// rediredt Nate Service
	this.redirectService = function( obj ) {
		if (obj.value != '') {
			location.target = '_top';
			top.location.href = obj.value;
		}
	}
	
	// set default page on IE
	this.setDefaultPage = function()
	{
		if( getVersionOfIE() )
		{
			document.body.style.behavior = 'url(#default#homepage)';
			document.body.setHomePage('http://www.nate.com/');
		}
	}

	// set default search on IE7
	this.setDefaultSearch = function()
	{
		window.open('http://www.nate.com/ie7/popup.html','','width=335,height=362');
	}

	init();
}
// create NateGNB object
window[objName] = new Class_NateGNB();
