/*
 * @title		포탈 JS프레임 초기화
 * @author		백종일
 * @date		2009. 06. 10
 * @description	JS공통파일(모든 페이지에 포함시켜야함)
 */

/* ▼ 헤더 */
_item.root = Object.extend($('g_BODY'),_item.gui);

// 즐겨찾기
if($('g_FAVORITE'))
{
	$('g_FAVORITE').addEvent("click",function() {
		var strTitle= "▶ 아이템매니아 ◀";

		var domain = document.domain;
	
		var favorite= new _http.data(window.location.href);

		if("sidebar" in window)
		{
			window.sidebar.addPanel(strTitle,favorite.getDomain(),"");
		}
		else if("external" in window)
		{
			window.external.AddFavorite(favorite.getDomain(),strTitle);
		}
		else
		{
			alert('지원하지 않는 브라우져입니다.');
		}
		favorite = null;
	});
}

// 시작페이지로
$('g_STARTPAGE').addEvent("click",(function() {
	if("external" in window)
	{
		try
		{
			var favorite= new _http.data(window.location.href);
			this.parentNode.style.behavior = 'url(#default#homepage)';
			this.parentNode.setHomePage(favorite.getDomain());
			favorite = null;
			return;
		}
		catch(Err)
		{}
	}
	alert('지원하지 않는 브라우져입니다.');
}).bind($("g_STARTPAGE")));

if($("g_SUB_SETTLE_INFO")){ Object.extend($("g_SUB_SETTLE_INFO"),_item.layer); }
	

function _header()
{
	// SELECTBOX변환
	_item.selectbox_convertAll();


}
/* ▲ 헤더 */

function fnLayerSize()
{
	var bUserBrowserSize	= (document.documentElement.clientWidth > 0) ? document.documentElement.clientWidth : document.body.clientWidth;
	var bStartContentsSize	= (bUserBrowserSize > 879) ? Math.floor((bUserBrowserSize-880) / 2) : 0;
	var bSETTLE_INFO		= document.getElementById('g_SUB_SETTLE_INFO');
	var bMY_INFO_EDIT		= document.getElementById('g_MY_INFO_EDIT');

	bSETTLE_INFO.style.left		= (bStartContentsSize+643)+'px';
	bMY_INFO_EDIT.style.left	= (bStartContentsSize+565)+'px';
}



/* ▼ e090701 오픈 이벤트 */ 

function e_gift_box(){
					var frm = $("e090701Frm");
					var paramsValue = _http.encodeURI("gkey="+frm.gkey.value);
					new _xhr("/portal/center/event/event_ing/e090701_open_event/gift_box_.php",{type:'POST',params:paramsValue},null,{complete:receiveGift});	 	
			}

 function receiveGift(request) {
	var returnData = request.responseText.split(";");	
  	if(returnData[0] == "true"){ 
		if (confirm("선물상자가 지급되었습니다. \n\n이벤트 페이지에서 확인하시겠습니까?"))
		{
			location.href=returnData[2];
		}
 	}else{
 		alert(returnData[1]); 
	}
	$("div_e090701").style.display="none"; 
 }
/* ▲ e090701 오픈 이벤트 */


/* ▼ e090929 추석 이벤트 */ 

function e_gift_ricecake(){

	var check1	=	$('definemode').value;
	if(check1 == 1 ){
		var loginurl	=	$('loginurl').value;
		if(loginurl == 'trade.itemmania.com')
		{
			alert('로그인 후 이용하세요');
			location.href='http://trade.itemmania.com/user/login_form.html?returnUrl=http://trade.itemmania.com/';

		}
		else
		{
			alert('로그인 후 이용하세요');
			var url = "http://www.itemmania.com/portal/user/login_form.html?returnUrl=http://www.itemmania.com/portal/center/event/view.html?linkurl=e090929_chuseok/index.html&all=t&all=t";
			_window.open('login',url,500,500);
		}
		return;
	}

	var frm = $("e090929Frm_1");
	var paramsValues = _http.encodeURI("gkey="+frm.gkey.value);
	new _xhr("/portal/center/event/event_ing/e090929_chuseok/gift_item.php",{type:'POST',params:paramsValues},null,{complete:receive_Gift});	 	
}

 function receive_Gift(request) {
	var returnData = request.responseText.split(";");	
	//alert(returnData);
  	if(returnData[0] == "true"){ 
		if (confirm("오예~! 송편1개 획득!! \n\n이벤트 페이지에서 확인하시겠습니까?"))
		{
			location.href=returnData[2];
		}
 	}else{
 		alert(returnData[1]); 
	}
	$("div_e090929_1").style.display="none"; 
 }

 function e_gift_moon(){

	var check1	=	$('definemode').value;
	if(check1 == 1 ){
		var loginurl	=	$('loginurl').value;
		if(loginurl == 'trade.itemmania.com')
		{
			alert('로그인 후 이용하세요');
			location.href='http://trade.itemmania.com/user/login_form.html?returnUrl=http://trade.itemmania.com/';

		}
		else
		{
			alert('로그인 후 이용하세요');
			var url = "http://www.itemmania.com/portal/user/login_form.html?returnUrl=http://www.itemmania.com/portal/center/event/view.html?linkurl=e090929_chuseok/index.html&all=t&all=t";
			_window.open('login',url,500,500);
		}
		return;
	}

	var frm = $("e090929Frm_2");
	var paramsValues = _http.encodeURI("gkey="+frm.gkey.value);
	new _xhr("/portal/center/event/event_ing/e090929_chuseok/gift_item_moon.php",{type:'POST',params:paramsValues},null,{complete:receive__Gift});	 	
}

 function receive__Gift(request) {
	var returnData = request.responseText.split(";");	
	//alert(returnData);
  	if(returnData[0] == "true"){ 
		if (confirm("아싸! 보름달 1개 획득!! \n\n이벤트 페이지에서 확인하시겠습니까?"))
		{
			location.href=returnData[2];
		}
 	}else{
 		alert(returnData[1]); 
	}
	$("div_e090929_2").style.display="none"; 
 }
/* ▲ e090929 추석 이 이벤트 */


if($('imi_first'))
{
	var bUserBrowserSize	= (document.documentElement.clientWidth > 0) ? document.documentElement.clientWidth : document.body.clientWidth;
	var bUserBrowserSize2	= (document.documentElement.clientHeight > 0) ? document.documentElement.clientHeight : document.body.clientHeight;
	var bStartContentsSize	= (bUserBrowserSize > 467) ? Math.floor((bUserBrowserSize-467) / 2) : 0;
	var bStartContentsSize2	= (bUserBrowserSize2 > 146) ? Math.floor((bUserBrowserSize2-146) / 2)-73 : 0;
	$('imi_first').style.left = bStartContentsSize+'px';
	$('imi_first').style.top = bStartContentsSize2+'px';
}

var _cookie_update = {
	req: null,
	rsCookie: null,
	start: function ()
	{
		var nowDay = new Date();
		var nYear = nowDay.getYear();
		var nMonth = nowDay.getMonth()+1;
		var nDate = nowDay.getDate();
		var nHour = nowDay.getHours();
		var nMin = nowDay.getMinutes();
		var nSec = nowDay.getSeconds();
		if (nMonth < 10)
		{
			nMonth = "0"+nMonth;
		}
		if (nDate < 10)
		{
			nDate = "0"+nDate;
		}
		if (nHour < 10)
		{
			nHour = "0"+nHour;
		}
		if (nMin < 10)
		{
			nMin = "0"+nMin;
		}
		if (nSec < 10)
		{
			nSec = "0"+nSec;
		}

		this.dateStr = nYear + "-" + nMonth + "-" + nDate + " " + nHour + ":" + nMin + ":" + nSec;
		this.update(this);
	},
	setCookie: function (xml)
	{
		alert(this.dateStr+"__aaa");
		alert(xml);
		var value = this.dateStr + "_" + xml.getAttribute("total").toString() + "_" + xml.getAttribute("msg").toString() + "_" + xml.getAttribute("coupon").toString() + "_" + xml.getAttribute("ipoint").toString();
		//var value = this.dateStr + ":" + xml.getAttribute("total").toString() + ":" + xml.getAttribute("use").toString() + "; expires=; path=/;";
		alert(value+"___");
		document.cookie = "myPoints=" + escape(value) + "; path=/;";;
		return;
	},
	getCookie:	function ()
	{
		alert("getCookie");
		alert(document.cookie);
		var cookie = document.cookie;
		var first = cookie.indexOf("myPoints=");
		if (first >= 0)
		{
			var str = cookie.substring(first,cookie.length);
			var last = str.indexOf(";");

			if (last < 0)
			{
				last = str.length;
			}
			str = str.substring(0, last).split("=");
			this.rsCookie = str[1].split("_");
			alert(this.rsCookie[1]);
		}
		else
		{
			alert("get null");
			return null;
		}
	},
	update: function()
	{
		if (this.req)
		{
			//this.req.setConfig("/_include/_remocon_mileage.php",
			this.req.setConfig("/_include/_point_update.php",
								{type:"POST", params:""}, null,
								{self:this, overall:this.OnComplete, error:this.OnComplete});
		}
		else
		{
			//this.req = new _xhr("/_include/_remocon_mileage.php",
			this.req = new _xhr("/_include/_point_update.php",
								{type:"POST", params:""}, null,
								{self:this, overall:this.OnComplete, error:this.OnComplete});
		}
	},
	OnComplete: function(request)
	{
		if(request.readyState!=4 || request.status!=200)
		{
			return;
		}



		//var xml = _xml.getElement(request.responseXML,"mileage",0);
		var xml = _xml.getElement(request.responseXML,"myPoints",0);
		//var nodesss = request.responseXML;
		//element	= element.replace('xsl:','');
		//alert(nodesss);
		//var xml = nodesss.getElementsByTagName("myPoints")[0];
		//alert(xml.getAttribute("total"));
		//var xml = request.responseText;
		if (!xml)
		{
			alert("xml null");
		} else {
			alert(xml);
			this.setCookie(xml);
			this.getCookie();

			alert(this.rsCookie[1]);
		}
	}
};

