/**
 * Comments for Stardoll
 * @author miken
 */
var Comm = {
	enabled: false,
	type: null,
	tid: null,
	oid: null,
	area: null,
	txta: null,
	eid: null,
	page: 0,
	wDiv: null,
	wPos: [],
	empty: false,

	init: function( enabled, type, tid, oid ) {
		if (enabled) {
			ajaxEngine.registerRequest('comments', '/'+language+'/ajax/comments.php');
			ajaxEngine.registerAjaxObject('commentsUpdater', Comm);
			Comm.enabled = true;
			Comm.type = type;
			Comm.tid = tid;
			Comm.oid = oid;
			Comm.area = document.getElementById('commentsarea');
			Comm.txta = document.getElementById('commentstextarea');
			var so = new SWFObject(staticUrl+'/flash/loading.swf','signupFlashLayer','100','100','7','#ffffff');
			so.addParam('wmode', 'transparent');
			so.addParam('menu', 'false');
			so.write('commentswait');
		}
	},
	add: function() {
		if (Comm.enabled && Comm.txta.value.length > 0) {
			Comm.wait();
			var txt = Comm.txta.value;
			ajaxEngine.sendRequest('comments', 'type='+Comm.type, 'action=add', 'tid='+Comm.tid, 'oid='+Comm.oid, 'page='+Comm.page, 'txt='+encodeURIComponent(txt));
			Comm.txta.value = '';
		}
	},
	get: function( page ) {
		if (Comm.enabled) {
			Comm.wait();
			Comm.page = page;
			ajaxEngine.sendRequest('comments', 'type='+Comm.type, 'tid='+Comm.tid, 'oid='+Comm.oid, 'page='+page);
		}
	},
	del: function( eid ) {
		if (Comm.enabled) {
			Comm.wait();
			Comm.eid = eid;
			ajaxEngine.sendRequest('comments', 'type='+Comm.type, 'action=delete', 'tid='+Comm.tid, 'oid='+Comm.oid, 'eid='+eid, 'page='+Comm.page);
		}
	},
	ajaxUpdate: function( resp ) {
		var actNode = null;
		var htmlNode = null;
		for (i=0; i<resp.childNodes.length; i++) {
			if ('action' == resp.childNodes[i].tagName) {
				actNode = resp.childNodes[i];
			} else {
				htmlNode = resp.childNodes[i];
			}
		}
		if (null != actNode) {
			var type = actNode.getAttribute('type');
			var result = actNode.getAttribute('result');
			if ('delete' == type) {
				if (1 == result) {
					document.getElementById('commEntry'+Comm.eid).style.display = 'none';
					Comm.eid = null;
				} else {
					popover.openFramed(sdJsLang.error, actNode.firstChild.nodeValue, Comm.area);
				}
			} else if ('add' == type) {
				if (1 <= result) {
					if (1 == result) {
						var newEntry = document.createElement('DIV');
						newEntry.innerHTML = actNode.firstChild.nodeValue;
						if (Comm.empty) {
							Comm.area.replaceChild(newEntry, Comm.area.firstChild);
							Comm.empty = false;
						} else {
							Comm.area.insertBefore(newEntry, Comm.area.firstChild);
						}
					} else {
						Comm.page = 0;
					}
				} else {
					var msg = '<span class="big">'+actNode.firstChild.nodeValue+'</span>';
					popover.openFramed(sdJsLang.error, msg, Comm.area);
				}
			}
		}
		if (null != htmlNode) {
			Comm.area.innerHTML = htmlNode.firstChild.nodeValue;
		}
		Comm.noWait();
	},
	wait: function() {
		if (Base.isIE) { Comm.area.style.zoom = 1; } /* hasLayout issue in IE */
		if (Comm.wDiv == null) {
			Comm.wDiv = document.getElementById('commentswait');
			Comm.wPos = Base.getPos(Comm.area);
		}
		Comm.wDiv.style.left = (Comm.wPos[0] + ((Comm.area.offsetWidth - 30)/2) - 40) + 'px';
		Comm.wDiv.style.top = (Comm.wPos[1] + (((Comm.area.offsetHeight - 150) > 0) ? (Comm.area.offsetHeight - 150) : 0)) + 'px';
		Comm.wDiv.style.display = 'block';
	},
	noWait: function() {
		Comm.wDiv.style.display = 'none';
	},
	rep: function( eid , cid ) {
		var sType = null;
		switch (Comm.type) {
			case 1:  sType = '&dollCommentId='; break;
			case 2:  sType = '&albumCommentId='; break;
			case 3:  sType = '&sceneryCommentId='; break;
			case 13: sType = '&diaryCommentId=' + cid + '&targetId=' + Comm.tid + '&authorId=' + Comm.oid; break;
			case 14: sType = '&cinemaCommentId=' + cid + '&targetId=' + Comm.tid; break;
			default: break;
		}
		if (null != sType) {
			reportUser(eid, sType);
		}
	}
}