var layerpopuplogin;

function openPopLogin(){
	layerpopuplogin = new Popup("layerpopuplogin", "/login.do", {overlay:true,width:460,height:490});
	
}

function closePopLogin(){
	layerpopuplogin.close();
	
}

function popLogingoLoginP(){
	if(get('memId').value ==''){
		alert('ID¸¦ ÀÔ·ÂÇÏ¼¼¿ä');
		get('memId').focus();
	}else if(get('passwd').value ==''){
		alert('ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇÏ¼¼¿ä');
		get('passwd').focus();
	}else{
		doPopLoginFormSubmit();
	}
}

function popLoginGoUrl(arg){
	if(arg == '1'){
		document.location.replace('/memberJoin.do');
	}else if(arg == '2'){
		document.location.replace('/chkAddUserView.do');
	}else if(arg == '3'){
		document.location.replace('/findIdPw.do');
	}
	//window.self.close();
}


function popLoginEnter() {
	if (event.keyCode == "13") {
		doPopLoginFormSubmit();
	}
}


//Æû Ã¼Å© =====================================
var popLoginForm = new Validate.Form("popLoginForm");
popLoginForm.set("memId",		"ID",			"ISNULL");
popLoginForm.set("passwd",	"ÆÐ½º¿öµå",			"ISNULL");

function doPopLoginFormSubmit(){
	if(popLoginForm.checkValue()) {
		var target = get("popLoginForm").target;
		get("popLoginForm").action = "https://"+document.domain+"/loginP.do";
		get("popLoginForm").target = get("ifrmforlogin").name;
		get("popLoginForm").submit();
		get("popLoginForm").target = target;
		
	}
}


function loginResultMsg(arg0,arg1){

	var result = arg0;
	var rstPwdFlag = arg1;

	if(rstPwdFlag == 'Y'){
		location.href =  "/modMyInfo.do";
	}else if(result == "true"){
		//alert("Á¤»óÀûÀ¸·Î µî·ÏµÇ¾ú½À´Ï´Ù.");
		//alert(get('popLoginFormReferer').value);
		var requrl = get('popLoginFormReferer').value;

		if(requrl.indexOf('login')>0 
				|| requrl.indexOf('mainXml')>0 
				|| requrl.indexOf('logout')>0 
				|| requrl.indexOf('menu_xml.xml')>0
				|| requrl.indexOf('addMemberView')>0
				|| requrl.indexOf('joincomplete')>0){
			location.href =  '/index.do';
		}else{
			location.href =  requrl;
		}
		
	} else {
		
		alert("¾ÆÀÌµð Á¤º¸°¡ ¾ø°Å³ª ÆÐ½º¿öµå°¡ Æ²¸³´Ï´Ù");
		
	}
}

var Popup = Class.create({

	// Å¬·¡½º »ý¼ºÀÚ ÇÔ¼ö
	initialize: function(element, url, options) {
		// ºñ¾îÀÖ´Â option °´Ã¼¸¦ »ý¼ºÇÑ´Ù.
		this.options = {
			top: 0, left: 0, width: 0, height: 0, 
			overlay : false,
			onLoad: Prototype.emptyFunction, 
			onCloseBefore: Prototype.emptyFunction,
			onClose: Prototype.emptyFunction
		};
		Object.extend(this.options, options || {});
		if($(element)) this.element = $(element);
		else this.element = new Element("div",{id:element});
		$$("body")[0].insert(this.element);
		this.closeHandler = this.close.bind(this);
		this.url = url;
		
		this.open();
	},

	// ÆË¾÷À» »ý¼ºÇÑ´Ù.
	open : function() {
		new Ajax.Request(this.url,{
			onSuccess : function(request) {
				if(this.options.overlay) {
					this.overlay = new Effect.Overlay();
					this.overlay.show();
				}
				this.element.update(request.responseText);
				var arrayPageSize = getPageSize();
				var arrayPageScroll = getPageScroll();
				var width = (this.options.width ? this.options.width : this.element.down("div").offsetWidth);
				var height = (this.options.height ? this.options.height : this.element.down("div").offsetHeight);
				var toTop = (arrayPageScroll[1] + ((document.body.clientHeight - height) / 2));
				//alert(document.body.clientHeight+","+document.body.offsetHeight+","+height);
				var top = (this.options.top ? this.options.top : toTop > 0? toTop : 0);
				var left = (this.options.left ? this.options.left : (((arrayPageSize[0] - width) / 2)));
				this.element.setStyle({position:"absolute", display:"block", width:width+"px", height:height+"px",
									top:(height > 0 ? top : 0)+"px", left:(width > 0 ? left : 0)+"px", zIndex:this.options.overlay ? this.overlay.options.zIndex+1 : 1000});
				this.startDrag();
				this.element.select(".popupClose").invoke("observe", "click", this.closeHandler).invoke("setStyle",{cursor:"pointer"});
				if(typeof this.options.onLoad == "function") this.options.onLoad();
			}.bind(this),

			onFailure : function(request) {
				trace(request.responseText);
			}
		});
	},

	// ÇöÀç ÆË¾÷¿¡ µå·¡±× ¼Ó¼ºÀ» ºÎ¿©ÇÑ´Ù.
	startDrag : function() {
		this.draggable = new Effect.Drag(this.element);
	},

	// ÇöÀç ÆË¾÷¿¡ µå·¡±× ¼Ó¼ºÀ» »èÁ¦ÇÑ´Ù.
	stopDrag : function() {
		this.draggable.stop();
	},
	
	// ÇöÀç ÆË¾÷À» »èÁ¦ÇÑ´Ù.
	close : function() {
		if(this.options.overlay) this.overlay.hide();
		this.element.select(".popupClose").invoke("stopObserving", "click", this.closeHandler);
		if(typeof this.options.onCloseBefore == "function") this.options.onCloseBefore();
		this.element.remove();
		if(typeof this.options.onClose == "function") this.options.onClose();
	}
});
