// JavaScript document
var cmntres =
{
	cnt: null,
	jsonhttp: null,
	res: null,

	construct: function()
	{
		cmntres.cnt = $('cmnt_res');
		cmntres.jsonhttp = getHTTPObject();
		cmntres.init();
	},

	init: function()
	{
		cmntres.res = null;
		cmntres.send();
	},

	send: function()
	{
		with (cmntres)
		{
			jsonhttp.open("GET", getRemDet("cmnt"), true);
			jsonhttp.onreadystatechange = cmntres.onStateChange;
			jsonhttp.send(null);
		}
	},

	onStateChange: function()
	{
		switch(cmntres.jsonhttp.readyState)
		{
			case 0:
				// UNINITIALIZED
			case 1:
				// LOADING
			case 2:
				// LOADED
			case 3:
				// INTERACTIVE
				break;
			case 4:
				// COMPLETED
				cmntres.onComplete(cmntres.jsonhttp.responseText);
				break;

			default:
				// ERROR
				break;
		}
	},

	onComplete: function(msg)
	{
		if ("false" != msg)
		{
			cmntres.res = msg.parseJSON();
		}
		cmntres.remove();
		cmntres.create();
	},

	create: function()
	{
		if (null == cmntres.res)
		{
			hTag.aC(cmntres.cnt, cmntres.htmlNull());
		}
		else
		{
			var row;
			for (var i=0;i<cmntres.res.length;i++)
			{
				row = (i%2)?2:1;
				hTag.aC(cmntres.cnt, cmntres.html(row, cmntres.res[i].nick,cmntres.res[i].date,cmntres.res[i].time,cmntres.res[i].desc));
			}
		}
	},

	remove: function()
	{
		var fC;
		while(cmntres.cnt.childNodes.length)
		{
			fC = cmntres.cnt.firstChild;
			cmntres.cnt.removeChild(fC);
		}
	},

	html: function(row, t4, t5, t6, t7)
	{
		// tags
		var c1 = hTag.cE('div');
		var c2 = hTag.cE('div');
		var c3 = hTag.cE('div');
		var c4 = hTag.cE('div');
		var c5 = hTag.cE('div');
		var c6 = hTag.cE('strong');
		var c7 = hTag.cE('div');
		// css
		hTag.cN(c1, 'tbl_item_'+row);
		hTag.cN(c2, 'clearfix');
		hTag.cN(c3, 'userIcon');
		hTag.cN(c4, 'userName');
		hTag.cN(c5, 'date');
		hTag.cN(c7, 'substance');
		// txtNodes

		var tN4 = hTag.tN(t4);
		var tN5_1 = hTag.tN("("+t5+" - ");
		var tN5_2 = hTag.tN(")");
		var tN6 = hTag.tN(t6);
		var tN7 = hTag.tN(t7.wordWrap(45,"\n", true));
		// add txtnodes
		//hTag.aC(c3, tN3);
		c3.innerHtml = "&nbsp;&nbsp;";
		hTag.aC(c4, tN4);
		hTag.aC(c5, tN5_1);
		hTag.aC(c5, c6);
		hTag.aC(c5, tN5_2);
		hTag.aC(c6, tN6);
		hTag.aC(c7, tN7);
		// build
		hTag.aC(c2, c3);
		hTag.aC(c2, c4);
		hTag.aC(c2, c5);
		hTag.aC(c1, c2);
		hTag.aC(c1, c7);

		return c1;
	},

	htmlNull: function()
	{
		var c1 = hTag.cE('div');
		var c2 = hTag.cE('div');

		hTag.cN(c1, 'tbl_item_1');
		hTag.cN(c2, 'substance');
		c2.style.textAlign = "center";
		c2.style.fontWeight = "bold";

		var tN1 = hTag.tN("No comments !");

		hTag.aC(c2, tN1);
		hTag.aC(c1, c2);

		return c1;
	}
};
addEvent(window, 'load', cmntres.construct, false);

var hTag =
{
	// create Tag
	cE: function(tagname)
	{
		return document.createElement(tagname);
	},

	// set classname
	cN: function(tag, cls)
	{
		if (isIE)
		{
			tag.setAttribute('className', cls);
		}
		else
		{
			tag.setAttribute('class', cls);
		}
	},

	// add child to
	aC: function(tag, child)
	{
		tag.appendChild(child);
	},

	// create textnode
	tN: function(txt)
	{
		return document.createTextNode(txt);
	}
}
