/* Expand Comments version 0.8
 *  Copyright 2006 Andrew Rader
 *
 * This file is part of Expand Comments
 *
 * Expand Comments is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Expand Comments is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Expand Comments; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

// makeAjaxRequest is taken from www.sitepoint.com/article/take-command-ajax
// which is taken from other various tuts. Sharing is caring.
// For documentation's sake, callback is a function that takes
// a single argument. This argument holds what is returned from
// the server, as XML of retXML is true, or as Text if not

function makeAjaxRequest(url, callback, retXML)
{
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (http_request) {

		http_request.onreadystatechange = function() {
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					if (retXML) {
						eval(callback + '(http_request.responseXML)');
					}
					else {
						eval(callback + '(http_request.responseText)');
					}
				}
			}
		}
	}

	/* This RANDOM query is so the page isn't cached */
	url += "&RANDOM=" + (Math.random() * Date.parse(new Date()))
	http_request.open('GET', url, true);
	http_request.send(null);
}

/*
 * handleAsyncRed: Handle the return call of the call to 'makeAjaxRequest'
 */
function handleAsyncRet( retVal ) {
	/* The XML Doc */
	xmlDoc = retVal.documentElement;

	/* Use the global theID to find the div target */
	if( theID >= 0 ) {
		appendDiv = document.getElementById( "expComDiv-" + theID );

		/* Loop through the entries from the XML */
		entries = xmlDoc.getElementsByTagName( "comment" );
		for( i = 0; i < entries.length; i++ ) {
			/* Create the container for this single comment */
			comment = document.createElement( "div" );
			if( i % 2 == 0 ) {
				comment.className = expcom_comtext_even_class;
			}
			else {
				comment.className = expcom_comtext_odd_class;
			}

			/* Create the Author & Link */
			authLinkNode = entries[i].getElementsByTagName("comment_author_url");
			if( authLinkNode.length > 0 && authLinkNode[0].firstChild ) {
				authLink = authLinkNode[0].firstChild.data;
				authCite = document.createElement( "cite" );
				authCite.innerHTML = authLink;
				authCite.className = expcom_authclass;

				comment.appendChild( authCite );
			}

			says = document.createTextNode( " says: " );
			comment.appendChild( says );

			approvedNode = entries[i].getElementsByTagName("comment_approved");
			if( approvedNode.length > 0 && approvedNode[0].firstChild ) {
				approved = approvedNode[0].firstChild.data;
				/* Is this approved? */
				if( approved == "0" ) {
					appr = document.createElement( "em" );
					appr.innerHTML = "Your comment is awaiting moderation.";
					comment.appendChild( appr );
				}
			}

			br = document.createElement( "br" );
			comment.appendChild(br);

			/* Now the Date */
			small = document.createElement( "small" );
			small.className = expcom_dateclass;

			linkNode = entries[i].getElementsByTagName("comment_link");
			if( linkNode.length > 0 && linkNode[0].firstChild ) {
				link = document.createElement( "a" );
				link.href = linkNode[0].firstChild.data;
				dateNode = entries[i].getElementsByTagName("comment_date");
				if( dateNode.length > 0 && dateNode[0].firstChild ) {
					theDate = dateNode[0].firstChild.data;
				}
				else {
					dateNode = entries[i].getElementsByTagName("comment_date_gmt");
					if( dateNode.length > 0 && dateNode[0].firstChild ) {
						theDate = dateNode[0].firstChild.data;
					}
					else {
						theDate = "";
					}
				}
				timeNode = entries[i].getElementsByTagName( "comment_time" );
				if( timeNode.length > 0 && timeNode[0].firstChild ) {
					theTime = " at "+timeNode[0].firstChild.data;
				}
				else {
					theTime = "";
				}

				link.innerHTML = theDate + theTime;

				link.title = "";
				small.appendChild( link );
			}
	
			/* Can we edit this comment? */
			canEdit = entries[i].getElementsByTagName("comment_edit");
			if( canEdit.length > 0 && canEdit[0].firstChild ) {
				space = document.createTextNode( " " );
				small.appendChild( space );

				edit = document.createElement("a");
				edit.href = canEdit[0].firstChild.data;
				edit.innerHTML = "edit";
				small.appendChild( edit );
			}

			comment.appendChild( small );

			/* Now the Comment Body */
			entryNode = entries[i].getElementsByTagName( "comment_content" );
			if( entryNode.length > 0 && entryNode[0].firstChild ) {
				text = document.createElement( "div" );
				text.className = expcom_comcontentclass;

				text.innerHTML = entryNode[0].firstChild.data;
				comment.appendChild( text );
			}
			
			/* Append the comment block */
			appendDiv.appendChild( comment );
		}

		/* Now make a collapse div/link */
		collDiv = document.createElement( "div" );
		collDiv.className = expcom_collclass;
		coll = document.createElement( "a" );
		coll.className = "bottom-collapse-link";
		coll.href = "#";
		if( coll.addEventListener ) {
			coll.addEventListener('click',collapse,false);
		} else {
			coll.attachEvent('onclick', collapse);
		}
		coll.id = "expCollLink-" + theID;
		coll.innerHTML = expcom_col;

		collDiv.appendChild( coll );

		appendDiv.appendChild( collDiv );

		theID = -1;
	}

	appendLink = document.getElementById(appendDiv.id.replace("expComDiv-","expComLink-"));
	appendLink.innerHTML = expcom_col;
}
