/**
 * dependes on simpleajax.js
 */



/**
 * Open a connection to the specified URL, which is
 * intended to provide an XML message.  No other data is
 * sent to the server.  This is the same as calling
 * xmlOpen("GET", url, null, responseHandler).
 *
 * @param string url    The URL to connect to.
 * @param function responseHandler The Javascript function handling server response.
 */
function xmlGet(url, responseHandler)
{
    xmlOpen("GET", url, null, responseHandler);
}

/**
 * Open a connection to the specified URL, which is
 * intended to respond with an XML message.
 * 
 * @param string method The connection method; either "GET" or "POST".
 * @param string url    The URL to connect to.
 * @param string toSend The data to send to the server; must be URL encoded.
 * @param function responseHandler The Javascript function handling server response.
 */
function xmlOpen(method, url, toSend, responseHandler)
{
    if (window.XMLHttpRequest)
    {
        // browser has native support for XMLHttpRequest object
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // try XMLHTTP ActiveX (Internet Explorer) version
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(req)
    {
        req.onreadystatechange = responseHandler;
        req.open(method, url, true);
        req.setRequestHeader("content-type","application/x-www-form-urlencoded");
        req.send(toSend);
    }
    else
    {
        alert('Your browser does not seem to support XMLHttpRequest.');
    }
}


var lastcpinfobubble = null;
var lastcpinfourl = null;
var lastajaxparams = null;
function cpInfoBubble(onelementid, ajaxurl, ajaxparams, positiontype) {
	// if ($('cpinfobubble')) $('cpinfobubble').remove();
	var infobbl = document.getElementById('cpinfobubble');
	if (infobbl) {
		try {
			infobbl.parentNode.removeChild(infobbl);
		} catch(e) {
			// Catch error
			// alert('error');
		}
	}
	if (lastcpinfobubble==onelementid) {
		lastcpinfobubble = null;
		if (ajaxparams==lastajaxparams) return false;
	}
	lastcpinfobubble = onelementid;
	lastajaxparams = ajaxparams;
   
  /**
   * Add View Layer
   */
	var newdiv2 = document.createElement('div');		// the box inside
  newdiv2.setAttribute('id','cpinfobubble');
 	newdiv2.setAttribute('class', 'cpinfobubble');
	newdiv2.style.position = 'relative';
  newdiv2.style.float = 'none';
  newdiv2.style.left = '0px';
  newdiv2.style.top = '0px';
	if (positiontype=='mouse') {
		alert('mouse pos not supported yet');
	} else {
		document.getElementById(onelementid).appendChild(newdiv2);
	}

	/**
   * Fill box with HTML from server
   */
  lastcpinfourl = ajaxurl;
  lastajaxparams = ajaxparams;
  
  //var myAjax = new Ajax.Updater( 'cpinfobubble', ajaxurl,  { method: 'get',  parameters: ajaxparams });
  xmlGet(ajaxurl+'?'+ajaxparams, infobubbleResponseHandler);
  
  return false;
}


/**
 * Handler for server's response to notes.xml request.
 * Notes are pulled from notes.xml and replace the
 * contents of the DIV with id 'notesSection'.
 */
function infobubbleResponseHandler()  {
		// Make sure the request is loaded (readyState = 4)
		if (req.readyState == 4) {
				document.getElementById('cpinfobubble').innerHTML = req.responseText;
				// Make sure the status is "OK"
				/*
				if (req.status == 200) {
						var swappableSection = document.getElementById('notesSection');
						var notes = req.responseXML.getElementsByTagName('note');
						var str = '';
						for(i=0; i < notes.length; i++) {
								var noteNode = notes.item(i);
								if(noteNode != null && noteNode.hasChildNodes())
								{
										str += noteNode.getAttribute('name') + ': ';
										str += noteNode.firstChild.nodeValue + '<br />';
								}
						}
						swappableSection.innerHTML = str;
				} else {
						alert("There was a problem retrieving the XML data:\n" + req.statusText);
				}
				*/
		}
}

