
//
// dependency: yahoo yui library, ./php/me2login.php
//

Me2Login = {
	busy: false,

	login: function(id, pw) {
		if( Me2Login.busy ) {
			return;
		}

		if( !id || !pw ) {
			alert("아이디와 패스워드로 빈 문자열을 받지 않습니다.");
			return;
		}

		var url = "./php/me2login.php";
		var dat = "callback=Me2Login.handleJson&id=" + id + "&pw=" + pw;
		YAHOO.util.Connect.asyncRequest("POST", url, Me2Login.callback, dat);
		Me2Login.busy = true;
	},

	logout: function() {
		Me2Login.id = null;
	},

	handleSuccess: function(o) {
		if( o.responseText.match("Me2Login.handleJson") ) {
			eval(o.responseText);
		}
		else {
			alert(o.responseText);
		}
		Me2Login.busy = false;
	},

	handleFailure: function(o) {
		Me2Login.busy = false;
	},

	handleJson: function(json) {
		if( !json ) {
			return;
		}

		Me2Login.afterRead(json);
	},

	afterRead: function(json) {
	}
}

Me2Login.callback = {
	success: Me2Login.handleSuccess,
	failure: Me2Login.handleFailure,
	scope: Me2Login
};

//
// dependency: yahoo yui library, ./php/me2post.php
//

Me2Post = {
	busy: false,

	write: function(id, pw, post_body, post_tags) {
		if( Me2Post.busy ) {
			return;
		}

		if( !id || !pw ) {
			alert("아이디와 패스워드로 빈 문자열을 받지 않습니다.");
			return;
		}

		var url = "./me2post.php";
		var dat = "callback=Me2Post.handleJson&akey=7308b5ebd98dd87888dc7e6c0f0daa4d&id=" + id + "&pw=" + pw + "&post_body=" + URL.encode(post_body) + "&post_tags=" + URL.encode(post_tags);
		YAHOO.util.Connect.asyncRequest("POST", url, Me2Post.callback, dat);
		Me2Post.busy = true;
	},

	handleSuccess: function(o) {
		if( o.responseText.match("Me2Post.handleJson") ) {
			eval(o.responseText);
		}
		else {
			alert(o.responseText);
		}
		Me2Post.busy = false;
	},

	handleFailure: function(o) {
		Me2Post.busy = false;
	},

	handleJson: function(json) {
		if( !json ) {
			return;
		}

		Me2Post.afterRead(json);
	},

	afterRead: function(json) {
	}
};

Me2Post.callback = {
	success: Me2Post.handleSuccess,
	failure: Me2Post.handleFailure,
	scope: Me2Post
};

//
// dependency: yahoo yui library, ./php/me2comment.php
//

Me2Comment = {
	busy: false,

	write: function(id, pw, comment, permalink) {
		if( Me2Comment.busy ) {
			return;
		}

		if( !id || !pw ) {
			alert("아이디와 패스워드로 빈 문자열을 받지 않습니다.");
			return;
		}

		var url = "./php/me2comment.php";
		var dat = "callback=Me2Comment.handleJson&id=" + id + "&pw=" + pw + "&comment=" + URL.encode(comment) + "&permalink=" + URL.encode(permalink);
		YAHOO.util.Connect.asyncRequest("POST", url, Me2Comment.callback, dat);
		Me2Comment.busy = true;
	},

	handleSuccess: function(o) {
		if( o.responseText.match("Me2Comment.handleJson") ) {
			eval(o.responseText);
		}
		else {
			alert(o.responseText);
		}
		Me2Post.busy = false;
	},

	handleFailure: function(o) {
		Me2Comment.busy = false;
	},

	handleJson: function(json) {
		if( !json ) {
			return;
		}

		Me2Comment.afterRead(json);
	},

	afterRead: function(json) {
	}
};

Me2Comment.callback = {
	success: Me2Comment.handleSuccess,
	failure: Me2Comment.handleFailure,
	scope: Me2Comment
};

RecentGames = {
	busy: false,

	read: function(id) {
		if( RecentGames.busy ) {
			return;
		}

		if( !id ) {
			return;
		}

		var url = "./php/getRecentGames.php";
		var dat = "callback=RecentGames.handleJson&id=" + id;
		YAHOO.util.Connect.asyncRequest("POST", url, RecentGames.callback, dat);
		RecentGames.busy = true;
	},

	handleSuccess: function(o) {
		if( o.responseText.match("RecentGames.handleJson") ) {
			eval(o.responseText);
		}
		else {
			alert(o.responseText);
		}
		RecentGames.busy = false;
	},

	handleFailure: function(o) {
		RecentGames.busy = false;
	},

	handleJson: function(json) {
		if( !json ) {
			return;
		}

		RecentGames.afterRead(json);
	},

	afterRead: function(json) {
	}
};

RecentGames.callback = {
	success: RecentGames.handleSuccess,
	failure: RecentGames.handleFailure,
	scope: RecentGames
};


URL = {
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

    _utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		
		return utftext;
	},

	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		
		return string;
	}
};
