
var editor = Class.create();

editor.prototype = {
	prefix: null,
	paramname: null,
	value: null,
	cred: null,
	assoc: null,
	assoctype: null,

	initialize: function (paramname, value, cred, assoc_id, assoc_type)
	{
	},

	onException: function (req, e)
	{
		trace(e);
	},
	

	additem: function (listparent, insert_before)
	{
try{
		var t = 0;
		var self = this;
		
		var onComplete = function (req)
		{
			try{
				var xml = req.responseXML.documentElement;

				var res = xml2js(xml);

					var errorcode = res.errorcode;
					if (errorcode == null || errorcode > 0) {
						trace("additem() errorcode: " . errorcode);
						this.on_error(this.prefix, errorcode);
						return;
					}

					for (var i = 0; i < res.item.length; i++) {
						var item = res.item[i];
						var newtag = this.setup_new_node(item);

						window.new_tag = newtag;
						var t = $("add_new_" + this.prefix + "s");
						$(listparent).insertBefore(newtag, t);
					}
					this.reset_fields();
			}catch(e) {
				trace(e);
			}
		};

		
		if( ! this.is_acceptable() ) {
			trace("not acceptable");
			return;
		}
		var pars = this.construct_query_string(
			"_add",
			this.addquery_paramnames
		);
	
		var request_url = "/api/" + this.prefix;
		var aj = new Ajax.Request( request_url, {
									method: 'post',
									parameters: pars,
									onComplete: onComplete.bind(this),
									onException: this.onException.bind(this)
									} );
} catch(e) {
	trace(e);
}
	},

	deleteitem: function (anchorID) 
	{
		var self = this;
		var onComplete = function (req)
		{
try{
			var res = req.responseXML.documentElement;
			var nodelist = res.getElementsByTagName('errorcode');
			var errorcode = nodelist[0].firstChild.nodeValue;
			if (errorcode == null || errorcode > 0) {
				trace("deleteitem() errorcode: " . errorcode);
				return;
			}
			var li = $(self.prefix + "LI_" + itemid); 
			li.parentNode.removeChild(li);
} catch(e) {
	trace(e);
}
		};

		var re = /(\d+)$/;
		if( re.exec(anchorID) == false )
			return;

		var itemid = (RegExp.$1);


		var url = "/api/" + this.prefix;
		var pars = "_delete=cmd&" + this.paramname + "=" + this.value + "&uniqid=" + itemid
						+ "&assoc=" + this.assoc
						+ "&assoctype=" + this.assoctype
						+ "&cred=" + cred 
						+ "&.rand=" + Math.floor(Math.random() * 65536);
		var aj = new Ajax.Request( url, {
									method: 'post',
									parameters: pars,
									onComplete: onComplete.bind(this),
									onException: this.onException.bind(this)
									} );
	},

	construct_query_string: function (actiocode, query_params)
	{
		var self = this;
		var actioncode = "_add";

		var hash = {
						assoc:		self.assoc,
						assoctype:	self.assoctype,
						cred:		cred,
						rand:		Math.floor(Math.random() * 65536)
					};
		
		hash[ this.paramname ] = (this.value);

		for (var i=0; i < query_params.length; i++) {
			var k = query_params[i];
			var id = (this.prefix + k);
			hash[k] = $(id).value;
		}

		var pars =  actioncode + "=cmd" + "&" + $H(hash).collect( function (a, b) {
									return a.key + "=" + encodeURIComponent(a.value)
								}) . join("&");
		return pars;
	}
};

var tagEditor = Class.create();
tagEditor.prototype = Object.extend(
	new editor(), 
{
/**
 * @param prefix     html element id prefix.
 * @param paramname  specifies item type inplicitly. gid/pplid/eid/locid/aid.
 * @param cred
 * @param assoc_id   id who tags this.
 * @param assoc_type id type. people/group.
 */
	initialize: function (paramname, uniqid, cred, assoc_id, assoc_type)
	{
		this.prefix = "tag";
		this.paramname = paramname;
		this.cred = cred;
		this.value = uniqid;
		this.addquery_paramnames = ["text"];
		this.addresponse_paramnames = ["tagid", "text" ];
		this.assoc = assoc_id;
		this.assoctype = assoc_type;
	},

	setup_new_node: function (item)
	{
		var self = this;
		var itemid = item.tagid;

		var newtag = createElement(
			[ "li", {
				id: self.prefix + "LI_" + itemid,
				childNode: [
					[ "span", {
						className: "nobr",
						childNode: [
							[ "a", {
								href:		"javascript:void(0);",
								id:			self.prefix + "A_" + itemid,
								onclick:	function () {
									self.deleteitem.apply(self, [ self.prefix + "A_" + itemid] );
								},
								className:	((1) ? "delete" : ""),
								childNode: [
									[ "img", {
										src: "http://img.mogue.jp/img/bg/window/delete.gif",
										className: "icon",
										alt: item.text + " 削除"
										
									} ]
								]
							} ],
							[ "a", {
								href:		"/tag/" + item.tagid + "/",
								title:		item.text,
								childNode: [
									[ "text", item.text]
								]
							} ]
						]
					} ]
				]
			} ]
		);
		return newtag;
	},

	on_error: function (prefix, errcode)
	{
		if ( navigator.userAgent.indexOf("Safari") == -1 ) {
			Effect.Shake( "tagtext" );
		}
		Element.show( prefix + "_error_status" );
	},

	reset_fields: function (cloned, returned)
	{
		var id = self.prefix + "text" ;
		$(this.prefix + "text").value = "";
		$(this.prefix + "text").focus();
	},

	is_acceptable: function ()
	{
		return ($(this.prefix + "text").value != '');
	}


}
);

var linkEditor = Class.create();
linkEditor.prototype = Object.extend(
	new editor(), 
{
	initialize: function (paramname, value, cred, assoc_id, assoc_type)
	{
		this.prefix = "link";
		this.paramname = paramname;
		this.value =value;
		this.cred = cred;
		//this.addquery_paramnames = ["text", "url", "type"]
		this.addquery_paramnames = ["text", "url"]
		this.addresponse_paramnames = ["tagid", "text", this.prefix + "url"];
		this.assoc = assoc_id;
		this.assoctype = assoc_type;
	},

	setup_new_node: function (item)
	{
		var self = this;
		var itemid = item.uniqid;

		var newnode = createElement(
			[ "li", {
				id: self.prefix + "LI_" + itemid,
				childNode: [
					[ "span", {
						className: "nobr",
						childNode: [
							[ "a", {
								href:		"javascript:void(0);",
								id:			self.prefix + "A_" + itemid,
								onclick:	function () {
									self.deleteitem.apply(self, [ self.prefix + "A_" + itemid] );
								},
								className:	((1) ? "delete" : ""),
								childNode: [
									[ "img", {
										src: "http://img.mogue.jp/img/bg/window/delete.gif",
										className: "icon",
										alt: item.text + " 削除"
										
									} ]
								]
							} ],
							[ "a", {
								href:		item.linkurl,
								title:		item.text,
								childNode: [
									[ "text", item.text]
								]
							} ]
						]
					} ]
				]
			} ]
		);
		return newnode;
	},

	on_error: function (prefix, errcode)
	{
		if ( navigator.userAgent.indexOf("Safari") == -1 ) {
			Effect.Shake( "linkurl" );
		}
		Element.show( prefix + "_error_status" );
	},

	reset_fields: function (cloned, returned)
	{
		var id = this.prefix + "text" ;
		$(id).value = "";
		//$(id).focus();
		$(this.prefix + "text").value = "";
		$(this.prefix + "url").value = "";
		$(this.prefix + "url").focus();
	},

	is_acceptable: function ()
	{
		return ($(this.prefix + "url").value != '');
	}
}

);


