var Member = Class.create();
Object.extend(Object.extend(Member.prototype, Window.prototype), {
	initialize : function(option) {
		this._initialize(option);
		this.rootFolderName = "PROFILE",
		this.folderName = "";
		this.cannotClose();
		this.buildButtons();
		this.buildContents();
		this.attachListener();
		this.userInformation = {};
		this.request();				
	},
	setContent : function(content) {
		this.registerFormElement.innerHTML = content;
		this.resizeToContent();
	},
	buildButtons : function() {
		this.buttons = this.buildElement(this.contentHeader);
		this.loginButton = this.buildElement(this.buttons,
		{elementName : "img", src: "style/img/login.png"}, {cursor: "pointer"});
		this.registerButton = this.buildElement(this.buttons,
		{elementName : "img", src: "style/img/adduser.png"}, {cursor: "pointer"});
		this.infoButton = this.buildElement(this.buttons,
		{elementName : "img", src: "style/img/userinfo.png"}, {cursor: "pointer"});
	},
	attachListener : function() {
		Event.observe(this.registerButton, "click", this.buildRegisterForm.bindAsEventListener(this));
		Event.observe(this.loginButton, "click", this.buildLoginForm.bindAsEventListener(this));
		Event.observe(this.infoButton, "click", this.viewUserInfo.bindAsEventListener(this));
	},
	attachRegisterListener: function() {
		Event.observe("javawide_member_zipcode", "focus", this.buildZipCodeFindForm.bind(this));
		Event.observe("javawide_member_photo", "focus", this.buildPhotoUploadForm.bindAsEventListener(this));
		Event.observe("javawide_member_command", "click", this.newMember.bindAsEventListener(this));
	},
	attachLoginListener: function() {
		Event.observe("javawide_member_pw", "keydown", this.login.bindAsEventListener(this));
	},
	login : function(evt) {
		var keyCode = evt.keyCode || evt.which;
		if(Event.KEY_RETURN != keyCode) return;
		Event.stop(evt);
		var logicParam = $H({process : "Login"});
		new Ajax.Request("content/jsp/member/login.jsp", {
			parameters : Form.serialize(this.registerFormElement) + "&" + logicParam.toQueryString(),
			onSuccess : function(response) {
				var userInfo = response.responseXML.firstChild.firstChild.firstChild;
				if("0" == userInfo.nodeValue) {
					this.userInformation = {id: "GUEST", logged: false};
					return alert("로그인에 실패했습니다.");
				}
				while(userInfo) {
					this.userInformation[userInfo.tagName] = userInfo.firstChild.nodeValue;
					userInfo = userInfo.nextSibling;
				}
				Event.stopObserving(this.loginButton, "click", this.buildLoginForm.bindAsEventListener(this));
				this.userInformation.logged = true;
				this.loginButton.setAttribute("src", "style/img/logged.png");
				this.viewUserInfo();
			}.bindAsEventListener(this),
			onFailure : function(response) {
				this.setContent("요청한 link가 없거나 서버 오류가 발생했습니다.");
			}.bindAsEventListener(this)
		});
	},
	request : function() {
		new Ajax.Request("content/jsp/member/info.jsp", {
			onSuccess : function(response) {
				this.onRequest();
				if(!response.responseXML.firstChild) {
					alert("this is guest");
					this.userInformation = {id: "GUEST", logged: false};
					return;
				}
				var userInfo = response.responseXML.firstChild.firstChild.firstChild;
				while(userInfo) {
					this.userInformation[userInfo.tagName] = userInfo.firstChild.nodeValue;
					userInfo = userInfo.nextSibling;
				}
				this.userInformation.logged = true;
				this.loginButton.setAttribute("src", "style/img/logged.png");
				this.viewUserInfo();
			}.bindAsEventListener(this),
			onFailure : function(response) {
				this.userInformation = {id: "GUEST", logged: false};
			}.bindAsEventListener(this)
		});
	},
	viewUserInfo : function() {
		this.registerFormElement.innerHTML = "";
		for(var key in this.userInformation) {
			if("photo" == key) {
				var _width = this.content.getWidth() - this.registerFormElement.getWidth() - 20;
				this.subElement.innerHTML = "<img src='" + this.rootFolderName + "/"
				 + this.userInformation[key] + "' style='width:" + _width + "px;'>";
			} else {
				var div = this.buildElement(this.registerFormElement);
				div.innerHTML = key + " : " + this.userInformation[key];
			}
		}
	},
	newMember : function() {
		var logicParam = $H({process : "NewMember"});
		new Ajax.Request("content/jsp/controller/executeUpdate.jsp", {
			parameters : Form.serialize(this.registerFormElement) + "&" + logicParam.toQueryString(),
			onSuccess : function(response) {
				alert("가입 되었습니다.");
			}.bindAsEventListener(this),
			onFailure : function(response) {
				alert("입력되지 않은 부분이 있습니다. 모두 입력해 주세요.");
			}.bindAsEventListener(this)
		});
	},
	buildZipCodeFindForm : function() {
		// 20px는 스크롤바 크기
		var _width = this.content.getWidth() - this.registerFormElement.getWidth() - 20;
		this.subElement.setStyle({width : _width + "px"});
		new Ajax.Request("content/jsp/member/zipCodeFindForm.jsp", {
			onSuccess : function(response) {
				this.subElement.innerHTML = response.responseText;
				var zipCodeKey = $("javawide_zipCode_key");
				zipCodeKey.focus();
				Event.observe(this.subElement.getElementsByTagName("form")[0], "submit", function(evt) {
					Event.stop(evt);
				}.bindAsEventListener(this));
				Event.observe(zipCodeKey, "keyup", this.zipCodeFind.bindAsEventListener(this));
			}.bindAsEventListener(this),
			onFailure : function(response) {
				this.setContent("요청한 link가 없거나 서버 오류가 발생했습니다.");
			}.bindAsEventListener(this)
		});
	},
	zipCodeFind : function(evt) {
		var keyCode = evt.keyCode || evt.which;
		if(Event.KEY_RETURN != keyCode) return;
		Event.stop(evt);
		var logicParam = $H({process : "findZipCode"});
		new Ajax.Request("content/jsp/controller/execute.jsp", {
			parameters : Form.serialize(this.subElement) + "&" + logicParam.toQueryString(),
			onSuccess : function(response) {
				var _height = this.content.getHeight() - this.subElement.getHeight();
				var zipCodeContainer = $("zipCodeContainer");
				zipCodeContainer.innerHTML = "";
				if(_height > 0)
					zipCodeContainer.setStyle({overflow : "auto", height: _height + "px"});
				var zipCode = response.responseXML.firstChild.firstChild;
				while(zipCode) {
					var zid = zipCode.firstChild;
					var address = zid.nextSibling;
					var zipCodeElem = this.buildElement(zipCodeContainer);
					var zipCodeValue = zid.firstChild.nodeValue;
					var addressValue = address.firstChild.nodeValue;
					zipCodeElem.innerHTML = addressValue + "(" + zipCodeValue + ")";
					Event.observe(zipCodeElem, "click", this.setZipCode.bindAsEventListener(this, zipCodeValue));
					zipCode = zipCode.nextSibling;
				}
			}.bindAsEventListener(this)
		});
	},
	setZipCode : function(evt, zid) {
		$("javawide_member_zipcode").setAttribute("value", zid);
	},
	buildPhotoUploadForm : function() {
		var _width = this.content.getWidth() - this.registerFormElement.getWidth() - 20;
		this.subElement.setStyle({width : _width + "px"});
		// 업로더 생성
		this.uploader = system.menu.show({id : "52", parentId : this.id});
		if(this.uploader) {
			this.uploader.setVisible(true);
			this.uploader.onUploadCompleted = function() {};
			this.uploader.onReadyWrittenFileName = function(evt) {
				this._onReadyWrittenFileName(evt);
				$("javawide_member_photo").setAttribute("value", this.writtenFileName);
				this.parent.subElement.innerHTML = 
				"<img src='content/jsp/file/download.jsp?FOLDERNAME=" +
				encodeURIComponent(this.parent.rootFolderName) + "&FILENAME=" + "/resampled" +
				encodeURIComponent(this.writtenFileName) + "'>";
			}.bindAsEventListener(this.uploader);
		}
	},
	buildContents : function() {
		this.registerFormElement = this.buildElement(this.content, null,
		{cssFloat : "left", styleFloat: "left"});
		this.subElement = this.buildElement(this.content, null,
		{cssFloat : "right", styleFloat: "right"});
		this.resizeToContent();
	},
	buildRegisterForm : function() {
		this.subElement.innerHTML = "";
		new Ajax.Request("content/jsp/member/registerForm.jsp", {
			onSuccess : function(response) {
				this.registerFormElement.innerHTML = response.responseText;
				this.attachRegisterListener();
			}.bindAsEventListener(this),
			onFailure : function(response) {
				this.setContent("요청한 link가 없거나 서버 오류가 발생했습니다.");
			}.bindAsEventListener(this)
		});
	},
	buildLoginForm : function() {
		if(this.userInformation.logged) {
			this.userInformation = {id: "GUEST", logged: false};
			new Ajax.Request("content/jsp/member/logout.jsp", {
				onFailure : function(response) {
					alert("로그 아웃이 실패했습니다.");
				}
			});
			this.loginButton.setAttribute("src", "style/img/login.png");
		}
		this.subElement.innerHTML = "";
		new Ajax.Request("content/jsp/member/loginForm.jsp", {
			onSuccess : function(response) {
				this.registerFormElement.innerHTML = response.responseText;
				this.attachLoginListener();
			}.bindAsEventListener(this),
			onFailure : function(response) {
				this.setContent("요청한 link가 없거나 서버 오류가 발생했습니다.");
			}.bindAsEventListener(this)
		});
	}
});