var E2AJAX = {

	ajaxNode : 1873513,
	ajaxUrl : "/index.pl",

	varChange: function(name, value, callback) {
		USERVARS[name] = value;
		varString = Hash.toQueryString(USERVARS);
		data = {node_id:  E2AJAX.ajaxNode,
			mode: "allvars",
			varString: varString,
			name: name
		 };
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl,{method: 'get', parameters: data, onComplete: callback});
	},

	privateMessage: function(recipient, msgText) {
		E2AJAX.sendMessage("/msg "+recipient+" "+msgText);
	},

getLastMessage: function() {

},

	sendMessage: function(msgText,checkmsg,callback) {
		if ((msgText.length ==0)&&(checkmsg == '')) {
			E2AJAX.update("chatterbox_chatter", "showchatter", {}, 0);
			$('talk_button').update('<input type="button" value="talk" onclick="updateTalk()"/>'); 
		}
		else {
			data = {
				node_id: E2AJAX.ajaxNode,
				mode: "message",
				deletelist: checkmsg,
				msgtext: msgText.replace( /[^ -~]/gm , function(x) { return "&#" + x.charCodeAt(0) + ";" ; } )
			};
			myAjax = new Ajax.Request(E2AJAX.ajaxUrl,{method: 'get', parameters: data, requestHeaders: Thu, 1 Jan 1970 00:00:00 GMT, onComplete: callback});
		}


	},

	update: function(id, htmlcode, args, replaceID, callback) {
 args = Object.toJSON(args);

		if (replaceID == null) {replaceID = 1;}
		data = {node_id:  E2AJAX.ajaxNode,
			mode: "update",
			htmlcode: htmlcode,
			 args: args
			};

		myAjax = new Ajax.Request(E2AJAX.ajaxUrl,{method: 'get', parameters: data, requestHeaders: Thu, 1 Jan 1970 00:00:00 GMT, onComplete: function(response) {
		if (callback != null) {
			callback();
		}
		if (replaceID) {
			$(id).replace(response.responseText);
 		}
		else {
			$(id).update(response.responseText);
		}

		 }
		});
	},

	voteNode: function(node_vote, weight) {
		data = {node_id:  E2AJAX.ajaxNode,
			mode: "vote"
	
			};
		data["vote__" + node_vote] = weight;
		$("voteinfo_"+node_vote).select(".vote_buttons").first().update("Voting ...");
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl,
			{
				method: 'get', 
				parameters: data, 
				onSuccess: function() {  E2AJAX.update("voteinfo_"+node_vote, "voteit", node_vote+",2", 0);} } );
	},

	bookmarkNode: function(node_bookmark) {
		data = {
			op : "bookmark",
			node_id : node_bookmark
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl, {method: 'get', parameters: data});
	},

	coolNode: function (node_cool) {
		data = {
			op : "cool",
			cool_id : node_cool
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl, {method: 'get', parameters: data});
	},

	updateNodelet: function(nodeletName, args) {
		nodeletID = nodeletName.toLowerCase().replace(/\W/g,'');
		if (args == null) {args = {};}
		args.nodelet = nodeletName;
		E2AJAX.update(nodeletID,'updateNodelet',args);
	},

	createTitle: function(title) {
		data = {type : "e2node", 
			op : "new",
			node : title};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl, {parameters: data, onSuccess: function() {E2AJAX.titleCreated = 1;}});
	},

	createWriteup: function(parent_e2node, doctext, writeuptype) {
		data = {op: "new",
			type: "writeup",
			writeup_doctext: doctext,
			writeup_parent_e2node: parent_e2node,
			writeuptype: writeuptype
		};
		new Ajax.Request(E2AJAX.ajaxUrl, {parameters: data, method: "post"});
	},

	postWriteup: function(title, doctext, writeuptype) {
		E2AJAX.createTitle(title);
		Deferrer.defer(function() {E2AJAX.getNodeInfo(title,"e2node","node_id");}, "E2AJAX.titleCreated");
		Deferrer.defer(function() {E2AJAX.createWriteup(E2AJAX.parentNode, doctext, writeuptype);}, "E2AJAX.parentNode");
	},

	getNodeInfo: function(nodeTitle, nodeType, field) {
		data = {node_id:  E2AJAX.ajaxNode,
			mode: "getNodeInfo",
			type: nodeType,
			field: field,
			title: nodeTitle
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl,{method: "get", parameters: data, onComplete: function(response) { E2AJAX.parentNode = response.responseText;} });
},

	markNotificationSeen: function(notified_id) {
		//$("notified_"+notified_id).fade();
		Effect.BlindUp("notified_"+notified_id);
		data = {
			node_id:  E2AJAX.ajaxNode,
			notified_id: notified_id,
			mode: "markNotificationSeen"
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl,{method: "get", parameters: data});
},

	checkNotifications: function() {
		E2AJAX.checkList("checkNotifications", E2AJAX.notificationOn, E2AJAX.notificationOff, ".notified", "notified_");
	},

	checkCools: function(){
		E2AJAX.checkList("checkCools", E2AJAX.coolOn, E2AJAX.coolOff, ".cooled", "cooled_");
	},

	checkMessages: function(){

		E2AJAX.checkList("checkMessages", E2AJAX.messageOn, E2AJAX.messageOff, ".messaged", "messaged_");
	},

	checkFeedItems: function(){

		E2AJAX.checkList("checkFeedItems", E2AJAX.feedItemOn, E2AJAX.feedItemOff, ".feeditem", "feeditem_");
	},

	itemOff: function(id) {
		Effect.BlindUp($(id));
},

	itemOn: function(id, IDGroup, holder, content, position) {
		if (position == "top") {
			$(holder).insert({ top: content});
		}
		else {
			$(holder).insert({ bottom: content});
		}
		Effect.BlindDown($(IDGroup+id));
		new Effect.Highlight.defer($(IDGroup+id));
	},

	messageOff: function(messaged_id) {
		E2AJAX.itemOff(messaged_id);
	},

	messageOn: function(id, content, position) {

		content = "<div class='messaged' id='messaged_"+id+"'>"+content+"</div>";
		E2AJAX.itemOn(id,'messaged_','chatterbox_chatter',content,'bottom');
	},

	feedItemOff: function(id) {
		E2AJAX.itemOff(id);
	},

	feedItemOn: function(id, content, position) {
		content = "<div class='feeditem' id='feeditem_"+id+"' style='padding: .5em; margin-bottom: .2em'>"+content+"</div>";
		E2AJAX.itemOn(id,'feeditem_','feed_list',content,'top');
	},

	notificationOff: function(notified_id) {
		E2AJAX.itemOff(notified_id);
	},

	notificationOn: function(notified_id, content, position) {
		content = 

		 "<li class='notified' id='notified_"+notified_id+"'><a class='closebox' title='hide notification' href='javascript:E2AJAX.markNotificationSeen("+notified_id+")'>&#91;x]</a> "+content+"</li>";
		E2AJAX.itemOn(notified_id,'notified_','notifications_list',content,'top');
	},

	checkList: function(mode, callbackOn, callbackOff, currentListClass, currentListIDGroup) {
		data = {node_id:  E2AJAX.ajaxNode,
			mode: mode
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl,
		{
			method: "get",
			parameters: data,
			onComplete: function(response) {
				responseJSON = response.responseText.evalJSON();
				i = 1;
				while (responseJSON[i]) {

					if ($(currentListIDGroup+responseJSON[i].id) === null) {
						callbackOn.defer(responseJSON[i].id, responseJSON[i].value, "top");
					}
					i++;
				}
				$$(currentListClass).each(function(item) {
					if (typeof(responseJSON[item.id.replace(currentListIDGroup,'')]) === 'undefined') {
						callbackOff.defer(item.id);
					}
				});
			}
		});

	},

	addFeedItem: function(doctext) {
		data = {
			type : "feeditem",
			node_id : E2AJAX.ajaxNode,
			title: "feeditem",
			op : "new",
			doctext : doctext
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl, {
			parameters: data, 
			onSuccess: 
				function() {
					E2AJAX.checkFeedItems(); $('new_feed_item').value = '';
				}
		});
	},

	deleteFeedItem: function(item_id) {
		data = {
			mode : "deleteFeedItem",
			node_id : E2AJAX.ajaxNode,
			feeditem_nodeid : item_id
		};
		myAjax = new Ajax.Request(E2AJAX.ajaxUrl, {
			parameters: data, 
			onSuccess: 
				function() {
					E2AJAX.checkFeedItems();
				}
		});
		
	}

	

};
