/*
 * Wildfire Spark
 * Copyright(c) 2003-2010, Wildfire Technology Limited
 * licensing@wildfire-it.com
 *
 * http://www.wildfire-it.com
 */

var SparkCommentsFactory = function() {
	this.store = {};
};

SparkCommentsFactory.prototype = {
	getInstance:function(id, type) {
		return this.store[id] || this.createInstance(id, type);
	},

	createInstance:function(id, type) {
		var inst = new SparkComments(id, type);
		this.store[id] = inst;
		return inst;
	}
};
var sparkCommentsFactory = new SparkCommentsFactory();

var SparkComments = function(id, type) {
	var t = this;
	t.id = id;
	t.type = type;
};

SparkComments.prototype = {

	baseUri:'/_/ServletEngine/Comment',

	form:null,

	type:null,

	createForm:function(trigger) {
		var t = this;

		if (t.form)
			return;

		t.form = t.renderForm();

		t.trigger = trigger;

		var ct = t.getContainer(trigger);

		ct.appendChild(t.form);

		//t.form.scrollIntoView();
	},

	renderForm:function(replyToComment) {
		var t = this;

		var email, name;

		var form = document.createElement('form');
		form.setAttribute('method', 'post');
		form.setAttribute('action', t.baseUri);

		t.addField(form, 'comments-key', {type:'hidden', value:t.id});
		t.addField(form, 'redir', {type:'hidden', value:document.location});

		if (replyToComment)
			t.addField(form, 'reply-to', {type:'hidden', value:replyToComment});

		if (!_sparkusr_) {
			name = t.addField(form, 'name', {label:'Name'});
			email = t.addField(form, 'email', {label:'Email'});
		}

		var placeholder = replyToComment ? 'Reply' : 'Comment';

		var textarea = t.addTextarea(form, 'comment', {label:'Comment', placeholder:placeholder, hideLabel:true});

		var submitBtn = t.addField(form, null, {type:'submit', value:placeholder});
		submitBtn.className = 'sendComment';

		var cancelBtn = t.addField(form, null, {type:'button', value:'Cancel'});
		cancelBtn.className = 'cancelComment';
		cancelBtn.onclick = function() {
			form.parentNode.removeChild(form);
		};

		form.onsubmit = function() {
			try {
				var uri = t.baseUri + '?comments-key=' + t.id + '&comment=' + escape(textarea.value);

				if (name)
					uri += '&name=' + name.value;

				if (email) {
					if (new RegExp('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$').test(email.value))
						uri += '&email=' + email.value;
					else {
						alert("Invalid Email");
						return false;
					}

				}
				if (replyToComment)
					uri += '&reply-to=' + replyToComment;

				if (t.type)
					uri += '&type=' + t.type;

				uri += '&referer=' + location.pathname;

				if (location.search)
					uri += escape(location.search);

				MicroAjax(uri, function(response) {
					t.updateList(response);
					form.reset();
				});

				return false;
			} catch(e) {
				put(e);
				return false;
			}
		};
		return form;
	},

	addField:function(form, k, cfg) {
		var field = document.createElement('input');
		field.setAttribute('type', cfg.type || 'text');

		if (k)
			field.setAttribute('name', k);

		field.setAttribute("placeholder", cfg.placeholder || '');

		field.setAttribute('value', cfg.value || '');
		this.addElement(form, field, cfg);

		return field;
	},

	addTextarea:function(form, k, cfg) {
		cfg = cfg || {};
		var textarea = document.createElement('textarea');
		if (k)
			textarea.setAttribute('name', k);

		textarea.setAttribute("placeholder", cfg.placeholder || '');
		textarea.innerHTML = cfg.value || '';
		this.addElement(form, textarea, cfg);

		return textarea;
	},

	addElement:function(form, field, cfg) {
		var el;

		if (cfg.label) {
			var label = document.createElement('label');

			if (!cfg.hideLabel)
				label.innerHTML = cfg.label;

			label.appendChild(field);
			el = label;
		} else
			el = field;

		form.appendChild(el);
	},

	reply:function(trigger, comment) {
		var t = this;

		if (t.replyForm && t.replyForm.parentNode)
			t.replyForm.parentNode.removeChild(t.replyForm);

		t.replyForm = t.renderForm(comment);
		t.trigger = trigger;

		trigger.parentNode.parentNode.parentNode.appendChild(t.replyForm);
	},

	remove:function(trigger, comment) {
		var t = this;

		if (!confirm("Are you sure you want to delete this comment?"))
			return;

		var uri = t.baseUri + '?comments-key=' + t.id + '&delete=' + comment;

		if (t.type)
			uri += '&type=' + t.type;

		t.trigger = trigger;

		MicroAjax(uri, function(response) {
			t.updateList(response);
		});
	},

	removeReply:function(trigger, comment) {
		var t = this;

		if (!confirm("Are you sure you want to delete this reply?"))
			return;

		var uri = t.baseUri + '?comments-key=' + t.id + '&delete-reply=' + comment;

		if (t.type)
			uri += '&type=' + t.type;

		t.trigger = trigger;

		MicroAjax(uri, function(response) {
			t.updateList(response);
		});
	},

	report:function(trigger, comment) {
		var t = this;

		var reason = prompt("Please enter a reason for Reporting this comment.", "");

		if (reason) {
			var uri = t.baseUri + '?comments-key=' + t.id + '&report=' + comment + "&reason=" + reason + '&referer=' + location.pathname;

			if (location.search)
				uri += escape(location.search);

			if (t.type)
				uri += '&type=' + t.type;

			t.trigger = trigger;

			MicroAjax(uri, function(response) {
				//t.updateList(response);
			});
		}
	},

	showAll:function(trigger) {
		var t = this;

		t.trigger = trigger;

		var uri = t.baseUri + '?comments-key=' + t.id;

		if (t.type)
			uri += '&type=' + t.type;

		uri += '&_debug=true';

		MicroAjax(uri, function(response) {
			t.updateList(response);

			trigger.innerHTML = "Show top 5";
			trigger.onmousedown = function() {
				t.showTop(this);
			};
		});
	},

	showTop:function(trigger) {
		var t = this;
		var uri = t.baseUri + '?comments-key=' + t.id + '&offset=0&rows=5';

		t.trigger = trigger;

		MicroAjax(uri, function(response) {
			t.updateList(response);
			trigger.innerHTML = "Show All Comments";
			trigger.onmousedown = function() {
				t.showAll(this);
			};
		});
	},

	like:function(trigger) {
		var t = this;
		t.trigger = trigger;
		var uri = '/_/ServletEngine/Opinion?comments-key=' + t.id + '&like=true&referer=' + location.pathname;

		if (location.search)
			uri += escape(location.search);

		if (t.type)
			uri += '&type=' + t.type;

		MicroAjax(uri, function(response) {
			t.updateOpinion(response);
		});
	},

	dislike:function(trigger) {
		var t = this;
		t.trigger = trigger;
		var uri = '/_/ServletEngine/Opinion?comments-key=' + t.id + '&dislike=true&referer=' + location.pathname;

		if (t.type)
			uri += '&type=' + t.type;

		MicroAjax(uri, function(response) {
			t.updateOpinion(response);
		});
	},

	updateOpinion:function(response) {
		var t = this;
		var ct = t.getContainer(t.trigger);
		var opinion = $('div.spark-opinion', ct)[0];//.getElementByClassName('spark-opinion', 'div');

		var tmp = document.createElement('div');
		tmp.innerHTML = response;

		opinion.parentNode.insertBefore(firstChild(tmp), opinion);
		opinion.parentNode.removeChild(opinion);
	},

	getContainer:function(el) {
		while (!$(el).hasClass('spark-comments')) {
			el = el.parentNode;
		}
		return el;
	},

	updateList:function(response) {
		var t = this;

		var ct = t.getContainer(t.trigger);
		var ul = ct.getElementsByTagName('ul')[0];
		var cursor = ul.nextSibling;
		var parentCt = ul.parentNode;
		parentCt.removeChild(ul);

		var tmp = document.createElement('div');
		tmp.innerHTML = response;

		parentCt.insertBefore(tmp.firstChild, cursor);
		if (t.form) {
			t.form.parentNode.removeChild(t.form);
			t.form = null;
		}
	}
};
