/*

HTMLHttpRequest v1.0 beta
(c) 2001-2005 Angus Turnbull, TwinHelix Designs http://www.twinhelix.com

Licensed for redistribution and/or modification under the terms
of the GNU Lesser General Public License (version 2.1 or later)
as published by the Free Software Foundation.
See http://www.gnu.org/copyleft/lesser.txt for more details.

*/

function HTMLHttpRequest(myName,blankFile){this.myName=myName;this.blankFile=blankFile?blankFile:'about:blank';this.buf=null;this.doc=null;this.loading=false;this.onload=null;document._ifr_buf_count|=0;var ifrID='iframebuffer'+document._ifr_buf_count++;if(document.createElement&&document.documentElement&&(window.opera||navigator.userAgent.indexOf('MSIE 5.0')==-1)){var ifr=document.createElement('iframe');ifr.setAttribute('id',ifrID);ifr.setAttribute('src',this.blankFile);ifr.style.visibility='hidden';ifr.style.position='absolute';ifr.style.width=ifr.style.height=ifr.borderWidth='0px';this.buf=document.getElementsByTagName('body')[0].appendChild(ifr)}else if(document.body&&document.body.insertAdjacentHTML){document.body.insertAdjacentHTML('beforeEnd','<iframe name="'+ifrID+'" id="'+ifrID+'" src="'+this.blankFile+'" style="display:none"></iframe>')}if(window.frames&&window.frames[ifrID])this.buf=window.frames[ifrID];return this};HTMLHttpRequest.prototype.load=function(uri,callback){with(this){if(!uri||!buf||loading)return false;if(callback)this.onload=callback;doc=null;var ifrDoc=buf.contentDocument||buf.document;if(buf&&!ifrDoc)return setTimeout(myName+'.load("'+uri+'",null)',100);if(document.readyState){if(!window.opera&&ifrDoc.location&&ifrDoc.location.href!=location.href)ifrDoc.location.replace(uri);else buf.src=uri;setTimeout(myName+'.checkLoad(false)',(window.opera?250:100));loading=true;return true}else if(ifrDoc.write){ifrDoc.write('<html><body onload="if(!self.loaded)parent.'+myName+'.checkLoad(true);self.loaded=true"><iframe id="nestBuf" name="nestBuf" src="'+uri+'"></iframe></body></html>');ifrDoc.close();loading=true;return true}return false}};HTMLHttpRequest.prototype.checkLoad=function(nested){with(this){doc=buf.contentDocument?(nested?buf.contentDocument.getElementById('nestBuf').contentDocument:buf.contentDocument):(nested?buf.nestBuf.document:buf.document);if(nested||(buf.src!=blankFile&&(doc.readyState=='complete'||!document.getElementById&&doc.readyState=='interactive'))){loading=false;this.onload(doc)}else setTimeout(myName+'.checkLoad('+nested+')',50)}};

//<![CDATA[

function RemoteFileLoader(myName, blankFile)
{
 this.myName = myName;
 this.blankFile = blankFile;
 this.requesters = [];
 this.onload = null;
};

// The function you call; pass a URI and the ID of the target element into which it'll load.
RemoteFileLoader.prototype.loadInto = function(uri, destId) { with (this)
{
 // Try and find a free requester, otherwise create a new one.
 var req = 'createnew';
 for (var r = 0; r < requesters.length; r++)
 {
  if (!requesters[r].loading)
  {
   req = r;
   break;
  }
 }
 if (req == 'createnew')
 {
  req = requesters.length;
  requesters[req] = new HTMLHttpRequest(myName + '.requesters[' + req + ']', blankFile);
 }

 // Set its callback to the copyContent method and load the selected file.
 requesters[req].callback = new Function('doc', 'with (' + myName + ') { '+
  'copyContent(doc, "' + destId + '"); if (onload) onload() }');
 return requesters[req].load(uri);
}};

RemoteFileLoader.prototype.copyContent = function(src, destId)
{
 var srcBody = src.getElementsByTagName ? src.getElementsByTagName('body')[0] :
  (src.body ? src.body : null);
 var dest = document.getElementById ? document.getElementById(destId) :
  (document.all ? document.all[destId] : null);
 if (!srcBody || !dest) return;
 // innerHTML is still a little more reliable than importNode across browsers.
 if (srcBody.innerHTML) dest.innerHTML = srcBody.innerHTML;
 else if (document.importNode)
 {
  while (dest.firstChild) dest.removeChild(dest.firstChild);
  for (var i = 0; i < srcBody.childNodes.length; i++)
   dest.appendChild(document.importNode(srcBody.childNodes.item(i), true));
 }
};

// Create an example object for our document click handler.

var docClickLoader = new RemoteFileLoader('docClickLoader', 'about:blank');



// DOCUMENT CLICK HANDLING.

function docClickHandler(evt)
{
 evt = evt||window.event;
 var src = evt.target||evt.srcElement;
 if (src.nodeType && src.nodeType != 1) src = src.parentNode;
 while (src)
 {
  // Check for source links with IDs of the form "loadinto-IdOfTarget"
  if ((src.nodeName||src.tagName||'').toLowerCase() == 'a' && src.id &&
   src.id.match(/^loadinto-([^\-]+)$/))
  {
   // Call our docClickLoader object with the URI and destination. Easy!
   var ok = docClickLoader.loadInto(src.href || src.getAttribute('href'), RegExp.$1);
   if (!ok) return true;
   // No need to loop further once we've found our target element; cancel the default event.
   if (evt.stopPropagation) evt.stopPropagation();
   if (evt.cancelable && evt.preventDefault) evt.preventDefault();
   evt.returnValue = false;
   return false;
  }
  src = src.parentNode;
 }
};

// Attach the event handler to the document. Safari can't reliably cancel addEventListeners.
if (document.addEventListener && navigator.userAgent.indexOf('Safari') == -1)
{
 document.addEventListener('click', docClickHandler, false);
}
else
{
 document.hhrDocOC = document.onclick;
 document.onclick = function(evt)
 {
  if (document.hhrDocOC) document.hhrDocOC(evt);
  return docClickHandler(evt);
 };
}

//]]>
