/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 212 2009-09-03 05:33:44Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#basic-modal a.basic_login').click(function (e) {
		e.preventDefault();
		login.locatehref = e.target.href ;
		
		$.get("/kr/include/login.html", function(data){
  	$(data).modal({		
				onOpen: login.open,
				onShow: login.show,
				onClose: login.close
			});
		});
	});	
	$('#basic-modal a.basic_jogin').click(function (e) {
		e.preventDefault();
		$('#basic-join-content').modal();		
	});	
});

//개체 라이브러리를 선언함
var login = {
	locatehref : String,
	message: null, //메세지를 미리 선언함
	open: function (dialog) {
		dialog.overlay.fadeIn(100, function () {
			dialog.container.fadeIn(100, function () {
				dialog.data.fadeIn(100, function () {
				
				});
			});
		});			
		
	},
	show: function (dialog) {
		$('.submitbutton').click(function (e) {
			e.preventDefault(); //화면 상태 유지
			if (login.validate()) { //입력항목 체크
				var msg = $('.login-message');
				msg.fadeOut(function () {
					msg.removeClass('login-error').empty();
				});
				$('form').fadeOut(200);
				$('.login-content').animate({
					height: '80px'
				}, function () {
					$('.login-loading').fadeIn(200, function () {
						$.ajax({
							url: '/kr/include/login.html',
							data: $('form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							success: function (data) {
								$('.login-loading').fadeOut(200, function () {
								if( data  == "ok"  ){		
									$.modal.close();
									login.close();
									var sURL = unescape(window.location.pathname);
									
									function refresh()
									{
									    window.location.href = login.locatehref;									    									    
									    
									}
									javascript:refresh();
																
								}
								else{
									//alert(data);
									login.message = "회원정보가 없습니다";
									login.showError();
									$('form').fadeIn(200);
									//msg.html(data).fadeIn(200); //페이지 내용 보이기	
								}
							});
							},
							error: login.error
						});
					});
				});
									
			}
			else {
				if ($('.login-message:visible').length > 0) {
					var msg = $('.login-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						login.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('.login-message').animate({
						height: '30px'
					}, login.showError);
				}
				
			}
		})
	},
	close: function (dialog) {
		$.modal.close();
		},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		login.message  = "";
		
   	var email = $('#login-email').val();
		if (!email) {
			login.message += '이메일을 입력하세요 ';
		}
		else {
			if (!login.validateEmail(email)) {
				login.message += '이메일 형식이 올바르지 않습니다. ';
			}
		}
		if (!$('#login-password').val()) {
			login.message += '패스워드를 입력하세요 ';
		}
		
		if (login.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
				
	},
	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;	

		return true;
	},
	showError: function () {
		$('.login-message').html($('<div class="login-error"></div>').append(login.message)).fadeIn(200);
	}
	
};


