var HelpWindows=new Array();
var helpCache=new Array();

function fs_jsHelpWindow(id,helppath,hasClose)
{
document.writeln("<div id=\""+id+"\"></div>");

this.helpWindow=document.getElementById(id);

this.helpWindow.className="fsHelpWindow";
this.helpWindow.style.visibility='hidden';
this.helpPath=helppath;

if (hasClose==true)
  {
  var closeelem=document.createElement("p");
  closeelem.helpWindow=this.helpWindow;
  closeelem.style.cursor="pointer";
  closeelem.style.textAlign="right";
  closeelem.onclick=function(){this.helpWindow.style.visibility='hidden';}
  closeelem.appendChild(document.createTextNode("Close"));
  
  this.helpWindow.appendChild(closeelem);
	}

this.helpWindow.contentDiv=document.createElement("div");
this.helpWindow.appendChild(this.helpWindow.contentDiv);

if (hasClose==true)
  {
  var closeelem=document.createElement("p");
  closeelem.helpWindow=this.helpWindow;
  closeelem.style.cursor="pointer";
  closeelem.style.textAlign="right";
  closeelem.onclick=function(){this.helpWindow.style.visibility='hidden';}
  closeelem.appendChild(document.createTextNode("Close"));
  
  this.helpWindow.appendChild(closeelem);
	}

this.httpRequest=null;
if (this.XMLHttpRequest!=null)
  this.httpRequest=new XMLHttpRequest();
else if (window.ActiveXObject)
  this.httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
else
  alert("Your browser does not support XMLHTTPRequest");
		
this.displayHelp=function(content)
  {
	this.helpWindow.contentDiv.innerHTML=content;
	this.helpWindow.style.visibility='visible';
	}
	
this.showHelp=function(helpFile,relativeToElem,width,height)
  {	
	this.awaitingData=true;
	
	if (width!=null) this.helpWindow.style.width=width;
	if (height!=null) this.helpWindow.style.height=height;
		
	if (relativeToElem)
	  {
		pos=fs_jsCalcGoodPos(relativeToElem,this.helpWindow);
  	this.helpWindow.style.left=pos[0];
  	this.helpWindow.style.top=pos[1];
		}
		
	if (helpCache[helpFile]!=null)
	  {
		this.currentTopic=helpFile;
		this.displayHelp(helpCache[helpFile]);
		}
	else
	  {
		this.currentTopic=helpFile;
  	this.httpRequest.abort();
    this.httpRequest.onreadystatechange=fs_jsHelpCallback;
    this.httpRequest.open("GET",this.helpPath+helpFile,true);
    this.httpRequest.send(null);
		}
	}
	
this.hideHelp=function()			{this.helpWindow.style.visibility="hidden";}
	
HelpWindows[HelpWindows.length]=this;
return this;
}

fs_jsHelpCallback=function()
	{
	for (i=0; i<HelpWindows.length; i++)
	  {
		var win=HelpWindows[i];
		
		if (win.awaitingData==true)
		  {
			if (win.httpRequest.readyState!=4)
    	  continue;

			helpCache[win.currentTopic]=win.httpRequest.responseText;
			win.displayHelp(win.httpRequest.responseText);
			win.awaitingData=false;
			}
		}
	}

function fs_jsCalcGoodPos(elem,elem2)
  {
	var pos=fs_jsGetElemPos(elem);
	
	if ((pos[0]+elem.offsetWidth+elem2.offsetWidth)>document.body.scrollWidth)
	  pos[0]-=elem2.offsetWidth;
	else
	  pos[0]+=elem.offsetWidth;
			
	if ((pos[1]+elem.offsetHeight+elem2.offsetHeight)>document.body.scrollHeight)
	  pos[1]-=elem2.offsetHeight;
	else
	  pos[1]+=elem.offsetHeight;
		
	return pos;
	}
	
function fs_jsGetElemPos(elem)
  {
	var l=elem.offsetLeft;
	var t=elem.offsetTop;
	
	if (elem.offsetParent) 
	  {
		while (elem = elem.offsetParent) 
		  {
			l+=elem.offsetLeft
			t+=elem.offsetTop
		  }
	  }
		
	return [l,t];
	}
	  
/*void fs_jsCreateHelpWindow(id,width,height)
	{
  document.writeln("<div id=\""+id+"\">");
  var elem=document.getElementById(id);

  elem.className="fsHelpWindow";
  elem.style.visibility='hidden';
  elem.style.width=width;
  elem.style.height=height;
  }

void fs_jsShowHelp(id,helpfile)
	{
  var elem=document.getElementById(id);

	sendHttpRequest2("{$linkhttp}/help/"+helpfile+".php",fs_jsHelpCallback,elem);
  }

function fs_jsHelpCallback()
{
if (xmlhttp.readyState!=4)
  return;

xmlhttp_callback_data.src="<?php echo theme_path('images/');?>pcard/grayaddme.gif";
xmlhttp_callback_data.onclick=null;
xmlhttp_callback_data.style.cursor='default';
}*/

