/**
 * Quickhelp
 * Andrey Potekhin, June 2007
 * Copyright (c) 2007 Taitl Design, LLC.
 *
 * Quickhelp implements small pop-up items that
 * describe purpose of online form fields and other controls
 * to the user.
 * 
 * Usage:
 * This script creates an empty QuickHelp object, wich needs to be initialized
 * with text items and written to page content. 
 * - Call quickhelp.onload() method from your page body onload() handler to 
 * 	initialize quickhelp items' positions on the page
 * - Call quichhelp.setItems() method from the end of your page body, 
 *	 let quickhelp add its layers to page content.
 */

var ns4 = document.layers ? 1 : 0;
var ie4 = document.all ? 1 : 0;
var ns6 = document.getElementById && !document.all ? 1 : 0;

function QuickHelp() 
{
	this.ns4=document.layers ? 1 : 0;
	this.ie4=document.all ? 1 : 0;
	this.ns6=document.getElementById&&!document.all ? 1 : 0;
	this.items = null;
	this.itemCount=0;
	
	this.setItems = QuickHelpSetItems;
	this.onload = QuickHelpOnload;
	this.setItemPositions = QuickHelpSetItemPositions;
	this.hide = QuickHelpHide;
	this.show = QuickHelpShow;
}

var quickhelp = new QuickHelp();

// Adds quick help layers to page content
function QuickHelpSetItems(items)
{
	this.items = items;
	this.itemCount=items.length;
	
	for (i = 1; i< this.itemCount; i++)
	{
		if (ie4||ns6) 
		{
			document.write('<div onclick="h(); return false;" align="center" id="object'+i+'" style="position: absolute; left:0px; top:0px; width:300px; height:250px; clip:rect(0px 300px 250px 0px); z-index:0; visibility:hidden;cursor:hand;"><TABLE BORDER="1" width="300" CELLPADDING="10" CELLSPACING="0" BORDERCOLOR="#AFAC6A" bgcolor="white">');
		}
		else if (ns4)
		{ 
			document.write('<LAYER top:0 visibility=hide ID="object'+i+'" name="object'+i+'" left=0 BGCOLOR=black onfocus="h(); return false;"><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="1"><TR><TD><TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" width="300">');
		}
		if (ie4||ns6||ns4) document.write('<TR><TD BGCOLOR="#FFFFFF" BORDERCOLORDARK="#AFAC6A" BORDERCOLORLIGHT="#E0DD88" width=300 align=center><font id=dkSmall6black>'+this.items[i]+'</FONT></TD></TR>');
	
		if (ie4||ns6) 
		{
			document.write('</TABLE></div>');
		}
		else if (ns4)
		{
			document.write('</TABLE></TD></TR></TABLE></LAYER>');
		}
	}
}

// Initializes quick help items' positions
// origin = 'topleft' means that (itemTop,itemLeft) is an offset from window top-left corner
// origin = 'pagecenter' means that (itemTop,itemLeft) is an offset from window center
// origin = 'default' (default) means that itemTop is offcet from window center, and itemLeft is an offset from window left
function QuickHelpOnload(itemWidth, itemLeft, itemTop, origin)
{
	this.winW = (ns4||ns6)? window.innerWidth : document.body.offsetWidth-20;
	this.winH = (ns4||ns6)? window.innerHeight : document.body.offsetHeight-4;
	this.menuwidth = itemWidth;
	
	if (!origin)
	{
		origin = 'default';
	}
	
	if (origin == 'default')
	{
		this.offsetleft = itemLeft;
		this.offsettop=this.winH / 2 + itemTop;
	}
	else if (origin = 'topleft')
	{
		this.offsetleft = itemLeft;
		this.offsettop=itemTop;
	}
	else if (origin = 'pagecenter')
	{
		this.offsetleft = this.winW / 2 + itemLeft;
		this.offsettop=this.winH / 2 + itemTop;
	}

	this.setItemPositions();
}

function QuickHelpSetItemPositions() 
{
	for (i = 1; i < this.itemCount; i++)
	{
		if (ie4) 
		{
			eval("object" + i + ".style.pixelTop=document.body.scrollTop+this.offsettop");
			eval("object" + i + ".style.pixelLeft=document.body.scrollLeft+this.offsetleft");
		}
		else if (ns6) 
		{
			document.getElementById("object" + i).style.top=window.pageYOffset+this.offsettop;
			document.getElementById("object" + i).style.left=window.pageXOffset+this.offsetleft;
		}
		else if (ns4) 
		{
			eval(document.object1.top=eval(window.pageYOffset+this.offsettop));
			eval(document.object1.left=eval(window.pageXOffset+this.offsetleft));
		}		
	}
}

function QuickHelpHide()
{
	for (i = 1; i < this.itemCount; i++)
	{
		if (ie4) 
		{
			eval("object" + i + ".style.visibility='hidden'");
		}
		else if (ns6) 
		{
			document.getElementById("object" + i).style.visibility = 'hidden';
		}
		else if (ns4) 
		{
			document.layers['object' + i].visibility = 'hide';
		}		
	}
}

function QuickHelpShow(i)
{
	if (ie4) {document.write('<a href="#" onmouseover=" if (object'+i+'.style.visibility == \'visible\') { } else { h(); object'+i+'.style.visibility = \'visible\';quickhelp.setItemPositions() }; return false;" onclick=" if (object'+i+'.style.visibility == \'visible\') {  } else { h(); object'+i+'.style.visibility = \'visible\'; }; return false;">')}

	else if (ns4) {document.write('<a href="#" onmouseover=" if (document.layers[\'object'+i+'\'].visibility == \'show\') { } else { h(); document.layers[\'object'+i+'\'].visibility = \'show\';quickhelp.setItemPositions() }; return false;" onclick=" if (document.layers[\'object'+i+'\'].visibility == \'show\') {  } else { h(); document.layers[\'object'+i+'\'].visibility = \'show\' }; return false;">')}

	else if (ns6){document.write('<a href="#" onmouseover=" if (document.getElementById(\'object'+i+'\').style.visibility == \'visible\') {  } else { h(); document.getElementById(\'object'+i+'\').style.visibility = \'visible\';quickhelp.setItemPositions() }; return false;" onclick=" if (document.getElementById(\'object'+i+'\').style.visibility == \'visible\') {  } else { h(); document.getElementById(\'object'+i+'\').style.visibility = \'visible\'}; return false;">')}
}

// Hides currently displayed quickhelp item(s).
function h()
{
	quickhelp.hide();
}

// Displays a quickhelp item.
// Deprecated - use quickhelp.show().
function href(i)
{
	quickhelp.show(i);
}
