var meCommentAjax = {
	
	eidCtrl : 'meCommentCtrl',
	
	path : null,
	ratings : {},
	reports : {},
	reportcount : 0,
	
	
	/**
	 * Perform serverside comment command
	 * 
	 * @param string command Command to perform
	 * @param object args Object of key => value arguments to pass
	 * @param function callback Callback function
	 * @param this context Context for the callback function
	 */
	commentCmd : function(command, args, callback, context)
	{
		if(callback == undefined)
			return false;
		
		var url = '/meProxy/meCommentAjax.php';
		var ajax = new meAjax();
		
		if(context == undefined)
			context = null;
		
		ajax.setOnCompleteCallback(null, function(obj)
		{
			var tmp = obj.getResponseText();
			if(tmp.length > 0)
				eval('var ret = ' + tmp + ';');
			else
				ret = null;
			
			callback.call(context, ret);
		});
		
		var get = {'cmd' : command};
		return ajax.requestPost(url, get, args, true);
	},
	
	
	/**
	 * Add ratings
	 * 
	 * @param object Object in ID => Rating format
	 */
	addRatings : function(ratings)
	{
		for(var i in ratings)
			this.ratings[i] = ratings[i];
	},
	
	
	/**
	 * Add reported comments
	 * 
	 * @param array Array of reported comment ID's
	 */
	addReported : function(reports)
	{
		for(var i in reports)
			this.reports[this.reportcount++] = reports[i];
	},
	
	
	/**
	 * Rate comment
	 * 
	 * @param int id Comment ID
	 * @param int rating Rating 1-5
	 * @param string eid Element id where to write back new rating
	 */
	rateComment : function(id, rating, eid, countid)
	{
		this.commentCmd('rate', {'id' : id, 'rating' : rating, 'path' : this.path, 'ratings' : this.ratings}, function(obj)
		{
			this.setRateCtrl(id, rating);
			
			var tmp = {};
			tmp[id] = rating;
			this.addRatings(tmp);
			
			if(obj.ratinghtml != undefined)
				document.getElementById(eid).innerHTML = obj.ratinghtml;
			
			if(obj.count != undefined)
				document.getElementById(countid).innerHTML = obj.count;
		}, this);
	},
	
	
	/**
	 * Remove rating controller's mouse events and set stars to given rating
	 * 
	 * @param int id Comment ID
	 * @param int rating Rating 1-5
	 */
	setRateCtrl : function(id, rating)
	{
		var elem = document.getElementById(this.eidCtrl + id);
		var i = 0;
		
		row = elem.rows[0];
		for(; i<5; i++)
		{
			cell = row.cells[i];
			//select stars up to rating value
			if(i < rating)
				cell.className = 'sel';
			else
				cell.className = 'unsel';
			
			//set cursor to default
			cell.style.cursor = 'default';
			
			//remove event handlers
			cell.onclick = null;
			cell.onmouseover = null;
			cell.onmouseout = null;
		}
	},
	
	
	/**
	 * OnOver handler for rate controller
	 * 
	 * @param string ctrlid Controller table id
	 * @param int index Rate index, 1-5
	 */
	onRateOver : function(id, index)
	{
		var elem = document.getElementById(this.eidCtrl + id);
		row = elem.rows[0];
		for(var i=0; i<index; i++)
			row.cells[i].className = 'sel';
	},
	
	
	/**
	 * OnOut handler for rate controller
	 * 
	 * @param string ctrlid Controller table id
	 */
	onRateOut : function(id)
	{
		var elem = document.getElementById(this.eidCtrl + id);
		row = elem.rows[0];
		for(var i=0; i<5; i++)
			row.cells[i].className = 'unsel';
	},
	
	
	/**
	 * Report inappropriate comment
	 * 
	 * @param int id Comment ID
	 */
	reportInappropriate : function(id, eid)
	{
		this.commentCmd('reportInappr', {'id' : id, 'path' : this.path, 'reported' : this.reports}, function(obj)
		{
			var tmp = {'0' : id};
			this.addReported(tmp);
			
			var elem = document.getElementById(eid);
			if(obj.html != undefined)
			{
				elem.innerHTML = obj.html;
				elem.onclick = null;
				elem.className = "inactive";
			}
		}, this);
	},
	
	
	/**
	 * Remove comment
	 * 
	 * @param int id Comment ID
	 * @param string eid Entry element ID
	 */
	removeComment : function(id, eid)
	{
		this.commentCmd('remove', {'id' : id}, function(obj)
		{
			//hide comment entry
			document.getElementById(eid).style.display = "none";
		});
	},
	
	
	/**
	 * Reject comment
	 * 
	 * @param int id Comment ID
	 * @param string eid Entry element ID
	 */
	rejectComment : function(id, eid)
	{
		this.commentCmd('reject', {'id' : id}, function(obj)
		{
			//hide comment entry
			document.getElementById(eid).style.display = "none";
		});
	},
	
	
	/**
	 * Admit comment
	 * 
	 * @param int id Comment ID
	 * @param string eid ID of the element that can be hidden afterwards (containing links reject | admit)
	 */
	admitComment : function(id, eid)
	{
		this.commentCmd('admit', {'id' : id}, function(obj)
		{
			//hide actions
			document.getElementById(eid).style.display = "none";
		});
	}
};