/* Expand Comments version 0.3
 *  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) {
    //alert('Unfortunatelly you browser doesn\'t support this feature.');
    return false;
  }

  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)');
        }
      }
    }
  }

  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;

	/* The spot to append comments to */
	permLink = xmlDoc.getElementsByTagName( "link" )[0].firstChild.data;
	/* Find the post's div */
	divs = document.getElementsByTagName( "div" );
	theDiv = -1;
	for( p = 0; p < divs.length; p++ ) {
		//if( divs[p].getAttribute( "class" ) == "post" ) {
		if( divs[p].className == "post" ) {
			links = divs[p].getElementsByTagName( "a" );
			for( k = 0; k < links.length; k++ ) {
				if( links[k].href == permLink ) {
					theDiv = p;
					p = divs.length;
					k = links.length;
				}
			}
		}
	}

	if( theDiv >= 0 ) {
		postID = divs[theDiv].id;

		appendDiv = document.getElementById( postID.replace( "post-", "expComDiv-" ) );

		/* Loop through the comments from feed in reverse, and append */
		entries = xmlDoc.getElementsByTagName( "item" );
		for( i = entries.length - 1; i >= 0; i-- ) {
			comment = document.createElement( "div" );
			if( i % 2 == 0 ) {
				comment.className = "";
			}
			else {
				comment.className = "alt";
			}

			/* Create the title */
			title = document.createElement("cite");
			title.innerHTML = entries[i].getElementsByTagName("title")[0].firstChild.data.replace( "by: ", "" );
			comment.appendChild( title );

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

			/* Now the Link */
			small = document.createElement( "small" );
			small.className = "commentmetadata";

			link = document.createElement( "a" );
			link.href = entries[i].getElementsByTagName("link")[0].firstChild.data;
			linkText = entries[i].getElementsByTagName( "pubDate" )[0].firstChild.data;
			link.innerHTML = linkText.substring( 0, linkText.length - 6 );
			
			small.appendChild( link );
			comment.appendChild( small );

			/* Now the Comment Body */
			text = document.createElement( "div" );
			entryNode = entries[i].getElementsByTagName( "encoded" );
			if( entryNode.length <= 0 ) {
				entryNode = entries[i].getElementsByTagName("content:encoded");
			}

			text.innerHTML = entryNode[0].firstChild.data;
			comment.appendChild( text );

			/* Append the comment block */
			appendDiv.appendChild( comment );
		}
	}
	appendLink = document.getElementById(appendDiv.id.replace("expComDiv-","expComLink-"));
	appendLink.innerHTML = "Hide Comment Preview";
}
