/*
 *  mapleAjax - XMLHttpRequest based menus
 *  Copyright (C) 2005 Adrian Montero <maplechori@gmail.com>
 *
 *  This program 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.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */



function mapleAjax(doc) 
{
	this.xhttpObj = null;
	this.mDoc = doc;
	this.innerDoc = null;
}


mapleAjax.prototype.getRpc  = function() { return this.xhttpObj; }
mapleAjax.prototype.getDoc  = function() { return this.mDoc;     }
mapleAjax.prototype.getResp = function() { return this.innerDoc; }

mapleAjax.prototype.callback = function(arg)
{

	var closure = function()
	{	
		if (arg.xhttpObj.readyState == 4)
		{
			var outerNodes;
			var innerNodes;
			var where = arg.where;

			var idtag = arg.idtag;

			innerDoc = arg.xhttpObj.responseXML;

			var mDoc = arg.mDoc;
			outerNodes = innerDoc.childNodes;

			if (outerNodes.length > 1)
			{
				innerNodes = outerNodes.item(1).childNodes;
			}
			else
			{
				innerNodes = outerNodes.item(0).childNodes;	
			}
			
			var ajaxLink = mDoc.getElementById(idtag);

			for (var i=0; i < innerNodes.length; i++)
			{
				var atts = innerNodes.item(i).attributes;

				if (atts)
				{
					var name = atts.getNamedItem("name").nodeValue;
					var url = atts.getNamedItem("url").nodeValue;
				
					var anchor = mDoc.createElement("a");						
					var br = mDoc.createElement("br");
			
					var textNode = mDoc.createTextNode(name);
	
					anchor.setAttribute("href", url);
					anchor.appendChild(textNode);

					ajaxLink.appendChild(anchor);
					ajaxLink.appendChild(br);

				}

			}
			
			arg = null;
			return;
		}
	}
	  

	return closure;
}

mapleAjax.prototype.getTags = function(which)
{
	var numTag = 3;
	var pullarray = new Array(3);

	pullarray[0] = "ajaxLinks";
	pullarray[1] = "projLinks";
	pullarray[2] = "persLinks";

	
	switch(which)
	{
		case "links":
			numTag = 0;
			pull = pullarray[numTag];
			break;

		case "projects":
			numTag = 1;
			pull = pullarray[numTag];
			break;

		default:
			numTag = 0;
			pull = pullarray[numTag];
			break;
	}


	try 
	{
		var ourTags = this.getDoc().firstChild.getElementsByTagName('INPUT');
	}
	catch(ex)
	{
		alert('error in getTags()' + ex + ' ');
		return;
	}

	try
	{
		var cNode = ourTags.item(numTag);
	}
	catch(ex)
	{
		alert('failed getting that cNode');
		return;
	}

	
	if (cNode.getAttribute("value") == '>')
	{
		cNode.setAttribute("value", "<"); 
		
		pData = this.getDoc().getElementById(pull);
	
		if (pData)
		{
			while ( pData.firstChild )
				pData.removeChild(pData.firstChild);
		}
		return;

	}
	else if (cNode.getAttribute("value") == '<')
	{

		cNode.setAttribute("value", ">"); 
		this.xhttpObj = null;

		var pData = this.mDoc.getElementById(pull);					

		if (pData.firstChild)	
			return true;
		
		this.setBrowser();
		this.send(which, pull);


	}
	else
		return;

}

mapleAjax.prototype.send = function(where, idtag) 
{		
	this.where = where;
	this.idtag = idtag;
	this.getRpc().onreadystatechange = this.callback(this);
	
	this.getRpc().open("GET","http://mapleman.linuxreal.org/cgi/ajaxWorker.xml?call=" + where);
	this.getRpc().send(null);
}


mapleAjax.prototype.setBrowser = function()
{

	if (this.getDoc().all) 
	{ 
		xhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	else 
	{ 
		xhttp = new XMLHttpRequest(); 
	}
			
	this.xhttpObj = xhttp;
}


function _m_ajaxLoad(document, which, start)
{
	var mAjax = null;
	mAjax = new mapleAjax(document)
	mAjax.setBrowser();	

	if (start)
	{
		mAjax.send("links", "ajaxLinks");

		var mAjax2 = null;
		mAjax2 = new mapleAjax(document);
		mAjax2.setBrowser();
	    mAjax2.send("projects", "projLinks");
	}	
	else
	{
		mAjax.getTags(which)
	}


}



