var items = new Object();

function toggleComments(id) {
	with(items[id]) {
		if (commentsToggled == false) {
			if (commentsReceived == false) {
				commentsLoadingDiv.style.display = "inline";
				ajaxQueue.push(id);
				ajaxProcessQueue();
			} else {
				commentsDiv.style.display = "block";
				if (typeof commentsWrite !== 'undefined') commentsWrite.style.display = "block";
			}
			commentsSwitchIcon.src = baseUrl+"/static/images/close.png";
		} else {
			commentsDiv.style.display = "none";
			if (typeof commentsWrite !== 'undefined') commentsWrite.style.display = "none";
			commentsSwitchIcon.src = baseUrl+"/static/images/open.png";
		}
		commentsToggled = !commentsToggled;
	}
}

var ajaxQueue = new Array();
var ajaxFree  = -1;

function ajaxProcessQueue() {
	if ((ajaxQueue.length > 0) && (ajaxFree == -1)) {
		var http_request = new XMLHttpRequest();
		var url = baseUrl+"/static/scripts/getComments.php";
		var params = "id="+ajaxQueue[ajaxQueue.length-1];
		
		http_request.open("POST", url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", params.length);
		http_request.setRequestHeader("Connection", "close");

		http_request.onreadystatechange = function () {
			if ( http_request.readyState == 4 ) {
				if ( http_request.status == 200 ) {
					ajaxReceiveData(http_request.responseText);
				} else {
					alert( "AJAX Problem: "+http_request.status );
				}
				http_request = null;
			}
		};

		http_request.send(params);	
		
		ajaxFree = ajaxQueue[ajaxQueue.length-1];
		ajaxQueue.pop();
	}
}

function ajaxReceiveData(data) {
	//Parse
	items[ajaxFree].commentsData = data.parseJSON();
	
	with(items[ajaxFree]) {
		if (commentsData.length > 0) {
			commentsList = new Array();
			for (i = 0; i < commentsData.length; i++) {
				commentsList.push({
					cDate : new TElement("div","entryCommentDate",ajaxFormatTimestamp(commentsData[i]["timestamp"])),
					cName : new TElement("div","entryCommentName","> "+commentsData[i]["user"]),
					cText : document.createElement("div")
				});
				
				commentsDiv.appendChild(commentsList[i].cDate.getElement());
				commentsDiv.appendChild(commentsList[i].cName.getElement());
				
				commentsList[i].cText.className = "entryCommentText";
				commentsList[i].cText.innerHTML = commentsData[i]["text"];
				commentsDiv.appendChild(commentsList[i].cText);
			}
		} else {
			commentsNoComments = new TElement("div","entryCommentText","There are no comments yet.");
			commentsDiv.appendChild(commentsNoComments.getElement());
		}

		commentsReceived = true;
		commentsLoadingDiv.style.display = "none";
		commentsDiv.style.display = "block";
		if (typeof commentsWrite !== 'undefined') commentsWrite.style.display = "block";
	}
	
	ajaxFree = -1;
	ajaxProcessQueue();
}

function ajaxFormatTimestamp(stamp) {
	out = parseInt(stamp.substr(8,2))+"."+parseInt(stamp.substr(5,2))+"."
		 + stamp.substr(0,4)+" at "+stamp.substr(11,2)+":"
		 + stamp.substr(14,2);
	return out;
}

function TElement(name,css,content) {
	this.ele = document.createElement(name);
	this.eleText = document.createTextNode(content);
	this.ele.appendChild(this.eleText);
	this.ele.className = css;
}
TElement.prototype.getElement = function() {
	return this.ele;
}
