var titleBarControl=
{
	images: "/images/titleBar/",	// Images Directory
	editDivClass: "editDiv",			// CSS Class for Edit Frame (for dragging)
	helpUp: new Image(),					// Help Button Up Image
	helpDown: new Image(),				// Help Button Down Image
	helpOver: new Image(),				// Help Button MouseOver Image
	currentHelp: "",							// Current Help Screen
	helpWindow: null,							// Pointer to Help window
	pageName: "",									// Name of currently-served page

	// Initialize the variables
	init: function()
	{
		this.helpUp.src=this.images+"helpUp.gif";
		this.helpDown.src=this.images+"helpDown.gif";
		this.helpOver.src=this.images+"helpOver.gif";
		
		// Find name of currently-served Page
		var s=window.location+"?";
		s=s.substring(0,s.indexOf("?")).replace("\\","/");
		if (s.indexOf("/")>=0)
			s=s.substring(s.lastIndexOf("/")+1);
		else if (s.indexOf(":")>=0)
			s=s.substring(s.indexOf(":")+1);
		this.pageName=s;
	},

	help: function()
	{
		var p=top.titleBarControl;
		p.currentHelp=this.pageName;
		var ok=false;
		try
		{
			ok=p.helpWindow.iExist;
		}
		catch(e){}
		if (ok)
			p.showHelp();
		else
		{
			var w=Math.min(screen.width-100, 800);
			var h=Math.min(screen.height-100, 500);
			p.helpWindow=openPopup("help.asp", w, h, "helpwindow", p);
		}
	},

	helpLoaded: function()
	{
		top.titleBarControl.showHelp()
	},

	showHelp: function()
	{
		this.helpWindow.showPage(this.currentHelp);
	}
};
titleBarControl.init();

// Outputs a Title Bar
function titleBar(text,suppressHelp,drag)
{
	var HTML="<table cellspacing=0 cellpadding=0 border=0"
	 +" class='titleBar' width='100%'><tr height=1><td nowrap width='100%'"
	 +(drag? " class='dragTitle'" : "")+">"
	 +"&nbsp;&nbsp;"+text+"</td>"
	 +(suppressHelp? "" :
		 "<td nowrap valign='bottom' width='1%'>"
	  +"<img src='"+titleBarControl.helpUp.src+"' style='cursor:pointer;'"
	  +" onmousedown='javascript:this.src=titleBarControl.helpDown.src;'"
	  +" onmouseover='javascript:this.src=titleBarControl.helpOver.src;'"
	  +" onmouseup='javascript:this.src=titleBarControl.helpUp.src;'"
	  +" onmouseout='javascript:this.src=titleBarControl.helpUp.src;'"
	  +" onclick='javascript:titleBarControl.help();' alt='Help'>&nbsp;</td>")
	 +"</tr></table>";
	document.write(HTML);
	if (drag)
		addLoadEvent(function(){titleBarDrag.setup();});
}

// Requires editFrame to work properly
var titleBarDrag=
{
	org: [0,0], // Origin of Drag within Window
	obj: null,	// Object being dragged
	
	// Set up a Drag (called in top-level page, from Child)
	setDragFromChild: function(div, e)
	{
		this.obj=div;
		this.org[0]=e.clientX;
		this.org[1]=e.clientY;
		document.onmousemove=titleBarDrag.parentDragging;
		document.onmouseup=titleBarDrag.parentEndDrag;
		document.onselectstart=function(){return false;};
	},

	// Event when the mouse is moved over the parent window
	parentDragging: function(e)
	{
		e=e? e : event;
		titleBarDrag.setPosition(e.clientX-titleBarDrag.org[0], e.clientY-titleBarDrag.org[1]);
	},

	// Stop dragging (called when mouse is released in Parent)
	parentEndDrag: function()
	{
		titleBarDrag.stopDrag();
		titleBarDrag.obj.titleBarDrag.stopDrag();
	},

	// Drag event (called in top-level page, from Child)
	dragFromChild: function(e)
	{
		var x=e.clientX-this.org[0];
		var y=e.clientY-this.org[1];
		this.setPosition(parseInt(this.obj.offsetLeft)+x,parseInt(this.obj.offsetTop)+y);
	},
	
	// Sets the position of the window, making sure it fits within the browser
	setPosition: function(x,y)
	{
		x=Math.max(0, Math.min(x, efControl.innerWidth()-this.obj.offsetWidth));
		y=Math.max(0, Math.min(y, efControl.innerHeight()-this.obj.offsetHeight));
		this.obj.style.left=x;
		this.obj.style.top=y;
	},

	// Find DIV owning window (called in Window)
	parentDiv: function()
	{
	  var p=self.frameElement;
	  var done=false;
	  var found=false;
	  while (!done)
	  {
	    try 
			{ 
				p=p.parentNode;
	      if (p.className==titleBarControl.editDivClass)
	      {
	        done=true;
	        found=true;
	      }
	    }
	    catch(e)
			{
				done=true;
			}
	  }
	  if (found)
			return p;
		else
			return null;
	},
	
	// Start dragging (called in Window)
	setDrag: function(e)
	{
		e=e? e : event;
		try
		{
			var dragObject=e.srcElement? e.srcElement : e.target;
			var cn=dragObject.className;
			if (cn=="dragTitle")
			{
				var pd=titleBarDrag.parentDiv();
				if (pd)
				{
					parent.titleBarDrag.setDragFromChild(pd,e);
					document.onmousemove=titleBarDrag.dragging;
					document.onmouseup=titleBarDrag.endDrag;
					document.onselectstart=function(){return false;};
				}
			}
		}
		catch(e){}
	},
	
	// Finish dragging (called in Window)
	endDrag: function()
	{
		titleBarDrag.stopDrag();
		parent.titleBarDrag.stopDrag();
	},

	// Switch off dragging
	stopDrag: function()
	{
		document.onmousemove=null;
		document.onmouseup=null;
		document.onselectstart=null;
	},
	
	// Mouse is moved inside the window being dragged
	dragging: function(e)
	{
		e=e? e: event;
		parent.titleBarDrag.dragFromChild(e);
	},

	// Enable dragging at outset (called in Window)
	setup: function()
	{
		document.onmousedown=titleBarDrag.setDrag;
		document.onmouseup=titleBarDrag.endDrag;
	}
}

