WritePost = {
	busy: false,

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

		post_body = post_body.replace(/&/gi, "$*$");
		post_body = post_body.replace(/\?/gi, "$%$");
		post_tags = post_tags.replace(/&/gi, "$*$");
		post_tags = post_tags.replace(/\?/gi, "$%$");

		var url = "php/writePost.php";
		var dat = "url=http://me2day.net/api/create_post/" + id + ".json?callback=WritePost.handleJson" + 
				  "&id=" + id +
				  "&pw=" + pw +
				  "&post_body=" + post_body +
				  "&post_tags=" + post_tags +
				  "&x=" + x +
				  "&y=" + y;
		YAHOO.util.Connect.asyncRequest("POST", url, WritePost.callback, dat);
		WritePost.busy = true;
	},

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

	handleFailure: function(o) {
		WritePost.busy = false;
	},
	
	handleJson: function(json) {
		if( !json ) {
			return;
		}

		WritePost.afterRead(json);
	},

	afterRead: function(json) {
	}
};

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

/////////////////////////////////////////////////////////////////////////////////////////

WriteComment = {
	busy: false,

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

		post_body = post_body.replace(/&/gi, "$*$");
		post_body = post_body.replace(/\?/gi, "$%$");

		var url = "php/writeComment.php";
		var dat = "url=http://me2day.net/api/create_comment/" + id + ".xml" + 
				  "&id=" + id +
				  "&pw=" + pw +
				  "&post_body=" + post_body +
				  "&post_id=" + escape(post_id);
		YAHOO.util.Connect.asyncRequest("POST", url, WriteComment.callback, dat);
		WriteComment.busy = true;
	},

	handleSuccess: function(o) {
		if( o.responseText.match("<code>0</code>") ) {
			WriteComment.onSuccess();
		}
		else {
			alert(o.responseText);
		}
		WriteComment.busy = false;
	},

	onSuccess: function() {
	},

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

	onFailure: function() {
	}
};

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

/////////////////////////////////////////////////////////////////////////////////////////

WriteDB = {
	busy: false,

	write: function(id, x, y, reg_time, reg_date, tags, refer) {
		if( WriteDB.busy ) {
			return;
		}

		refer = refer ? refer : "";

		var url = "php/writeDB.php?callback=WriteDB.handleJson" + 
				  "&id=" + id +
				  "&x=" + x +
				  "&y=" + y +
				  "&reg_time=" + reg_time + 
				  "&reg_date=" + reg_date + 
				  "&tags=" + tags +
				  "&refer=" + refer;
		YAHOO.util.Connect.asyncRequest("GET", url, WriteDB.callback, null);
		WriteDB.busy = true;
	},

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

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

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

		WriteDB.afterRead(json);
	},

	afterRead: function(json) {
	}
};

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

/////////////////////////////////////////////////////////////////////////////////////////

WriteShadow = {
	busy: false,

	write: function(id, post) {
		if( WriteShadow.busy ) {
			return;
		}

		post = post.replace(/&/g, "$*$");
		post = post.replace(/\?/g, "$%$");
		var url = "php/proxyShadow.php";
		var dat = "callback=WriteShadow.handleJson" + 
				  "&id=" + id +
				  "&post=" + post;
		YAHOO.util.Connect.asyncRequest("POST", url, WriteShadow.callback, dat);
		WriteShadow.busy = true;
	},

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

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

	handleJson: function(json) {
		if( json.success && json.result.id && json.result.me2permalink ) {
			var id = json.result.id;
			var pubDate = Me2Permalink.to_pubdate(json.result.me2permalink);
			WriteShadow.afterRead(id, pubDate);
		}
		else {
			WriteShadow.onFailure();
		}
	},

	onFailure: function() {
	}
};

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

/////////////////////////////////////////////////////////////////////////////////////////

WriteAnonymous = {
	busy: false,

	write: function(post_body, post_tags, x, y) {
		if( WriteAnonymous.busy ) {
			return;
		}
		
		post_body = post_body.replace(/&/gi, "$*$");
		post_body = post_body.replace(/\?/gi, "$%$");
		post_tags = post_tags.replace(/&/gi, "$*$");
		post_tags = post_tags.replace(/\?/gi, "$%$");

		var url = "php/writeAnonymous.php";
		var dat = "url=http://me2day.net/api/create_post/me2map.json?callback=WriteAnonymous.handleJson" + 
				  "&post_body=" + post_body +
				  "&post_tags=" + post_tags +
				  "&x=" + x +
				  "&y=" + y;
		YAHOO.util.Connect.asyncRequest("POST", url, WriteAnonymous.callback, dat);
		WriteAnonymous.busy = true;
	},

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

	handleFailure: function(o) {
		WriteAnonymous.busy = false;
	},
	
	handleJson: function(json) {
		if( !json ) {
			return;
		}

		WriteAnonymous.afterRead(json);
	},

	afterRead: function(json) {
	}
};

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