// Global variables
var globalIsIE = document.all?1:0;
var globalIsNetscape = document.layers?1:0;
var globalIsGecko = document.getElementById && !document.all;
// Constants
var DRAWER_NAME = "tipsDrawerSlider";
var IFRAME_NAME = "tipsDrawer_drawerIFrame";
var GLOBAL_IFRAME_NAME = "global_drawerIFrame";
var TAB_NAME = "tipsDrawer_clickTab";
// This sets how many pixels of the tab should show when collapsed
var tipsDrawer_minDrawer = 0;
// How many pixels should it move every step?
var tipsDrawer_move = 50;
if (globalIsIE) tipsDrawer_move = 12;
// Specify the scroll speed (in ms)
var tipsDrawer_scrollSpeed = 1;
// Timeout ID for drawer
var tipsDrawer_timeoutID = 1;
// Width of the drawer frame (in px)
var tipsDrawer_fixedWidth = 350;
// Container for loading message HTML
var tipsDrawer_loadingMsg = "";
// Container for drawer's URL 
var tipsDrawer_url = "";
// Initialise all drawers
function initDrawers(showHidden)
{
	if (document.getElementById(IFRAME_NAME) != null)
	{
		tipsDrawer_initDrawer(showHidden);
	}
	
	if (document.getElementById(GLOBAL_IFRAME_NAME) != null)
	{
		global_initDrawer(showHidden);
	}
}
// Initialise the tips Drawer
function tipsDrawer_initDrawer(showHidden)
{
	tipsDrawer_Drawer = new tipsDrawer_makeDrawer(DRAWER_NAME);
	tipsDrawer_Drawer.setWidth(tipsDrawer_minDrawer);
	tipsDrawer_Drawer.css.overflow = "hidden";
	
	window.onresize = tipsDrawer_resize;
	tipsDrawer_load(false);
	
	return;
}
// Construct drawer object
function tipsDrawer_makeDrawer(obj)
{
	this.drawerOrigObject=document.getElementById(obj);
	this.drawerFrameObject=document.getElementById(IFRAME_NAME);
	
	this.setWidth = tipsDrawer_internalSetWidth;
	this.getWidth = tipsDrawer_internalGetWidth;
	this.left = tipsDrawer_internalGetLeft;
	this.top = tipsDrawer_internalGetTop;
	this.pageWidth = tipsDrawer_internalGetPageWidth;
	this.pageHeight = tipsDrawer_internalGetPageHeight;
	
	tipsDrawer_internalResize(this);
	
	// Get CSS for div tag
	if (globalIsNetscape)
		this.css = eval('document.' + obj);
	else if (globalIsGecko)
		this.css = document.getElementById(obj).style;
	else if (globalIsIE)
		this.css = eval(obj + '.style');
		
	// Initialise the closed state
	this.state = 1;
	this.go = 0;
	
	if (globalIsNetscape)
		this.width=this.css.document.width;
	else if (globalIsGecko)
		this.width=document.getElementById(obj).offsetWidth;
	else if (globalIsIE)
		this.width=eval(obj+'.offsetWidth');
	
	this.obj = obj + "Object";
	eval(this.obj + "=this");
}
// Private function to get page height value
function tipsDrawer_internalGetPageHeight()
{
	if (globalIsNetscape)
		pageheight=document.body.css.document.height;
	else if (globalIsGecko||globalIsIE)
		pageheight=document.body.clientHeight;
	
	return pageheight
}
// Private function to get page width value
function tipsDrawer_internalGetPageWidth()
{
	return document.body.scrollWidth - 2;
}
// Private function to set width value
function tipsDrawer_internalSetWidth(value)
{
	this.css.width = value + "px";
	if (navigator.userAgent.indexOf ("Opera") != -1)
	{
		var operaIframe=document.getElementById(IFRAME_NAME);
		operaIframe.style.width = (value-tipsDrawer_minDrawer) + "px" ;
	}
}
// Private function to get width value
function tipsDrawer_internalGetWidth()
{
	if (globalIsNetscape)
		return eval(this.css.document.width);
	else if (globalIsGecko||globalIsIE)
		return eval(this.drawerOrigObject.offsetWidth);
}
// Private function to get left value
function tipsDrawer_internalGetLeft()
{
	if (globalIsNetscape||globalIsGecko)
		leftfunc=parseInt(this.css.left);
	else if (globalIsIE)
		leftfunc=eval(this.css.pixelLeft);
	
	return leftfunc;
}
// Private function to get top value
function tipsDrawer_internalGetTop()
{
   if (globalIsNetscape||globalIsGecko)
      topfunc=parseInt(this.css.top);
   else if (globalIsIE)
      topfunc=eval(this.css.pixelTop);
   return topfunc;
}
// Resize and position the drawer. It goes just below the
// drawer open / close button and is sized to window height
function tipsDrawer_internalResize(obj)
{
	var tipsDrawer_clickTab = document.getElementById(TAB_NAME);
	var topOffset = tipsDrawer_getTopPosition(tipsDrawer_clickTab) + tipsDrawer_clickTab.offsetHeight + 1;
	obj.drawerFrameObject.style.height = (obj.pageHeight() - topOffset) - 18;
	obj.drawerFrameObject.style.width = tipsDrawer_fixedWidth;
	obj.drawerOrigObject.style.top = topOffset;
	obj.drawerOrigObject.style.width = parseInt(obj.drawerFrameObject.style.width);
	
	if (isLTR)
	{
		obj.drawerOrigObject.style.left = tipsDrawer_getLeftPosition(tipsDrawer_clickTab) + tipsDrawer_clickTab.offsetWidth;
	}
	else
	{
		obj.drawerOrigObject.style.left = tipsDrawer_getLeftPosition(tipsDrawer_clickTab);
	}
	
	// set z-order to a high number so the drawer stays on top
	obj.drawerOrigObject.style.zIndex = "101";
	obj.drawerOrigObject.style.position = "absolute";
}
// Only tipsDrawer_moveOutDrawer() should call this
function tipsDrawer_internalMoveOut()
{
	if(tipsDrawer_Drawer.left() - tipsDrawer_move > tipsDrawer_Drawer.pageWidth() - tipsDrawer_fixedWidth)
	{
		tipsDrawer_Drawer.go=1;
		var newwidth= tipsDrawer_Drawer.getWidth() + tipsDrawer_move
		tipsDrawer_Drawer.setWidth(newwidth);
		tipsDrawer_Drawer.css.left = tipsDrawer_Drawer.left() - tipsDrawer_move;
		tipsDrawer_timeoutID = setTimeout("tipsDrawer_internalMoveOut()",tipsDrawer_scrollSpeed);
	}
	else
	{
		tipsDrawer_Drawer.css.left = tipsDrawer_Drawer.pageWidth() - tipsDrawer_fixedWidth;
		tipsDrawer_Drawer.setWidth(tipsDrawer_Drawer.width);
		tipsDrawer_Drawer.go = 0;
		tipsDrawer_Drawer.state = 0;
	}
}
// Only tipsDrawer_moveOutDrawer() should call this
function tipsDrawer_internalMoveOutRTL()
{
	if (tipsDrawer_Drawer.getWidth()+tipsDrawer_move <= tipsDrawer_Drawer.width) {
		tipsDrawer_Drawer.go = 1;
		var newwidth = tipsDrawer_Drawer.getWidth() + tipsDrawer_move
		tipsDrawer_Drawer.setWidth(newwidth);
		tipsDrawer_Drawer.css.left = tipsDrawer_Drawer.left()
		tipsDrawer_timeoutID = setTimeout("tipsDrawer_internalMoveOutRTL()",tipsDrawer_scrollSpeed);
	}
	else
	{
		tipsDrawer_Drawer.css.left = tipsDrawer_Drawer.left();
		tipsDrawer_Drawer.setWidth(tipsDrawer_Drawer.width);
		tipsDrawer_Drawer.go = 0;
		tipsDrawer_Drawer.state = 0;
	}
}
// Only tipsDrawer_moveInDrawer() should call this
function tipsDrawer_internalMoveIn()
{
	if((tipsDrawer_Drawer.left() + tipsDrawer_move) < (tipsDrawer_Drawer.pageWidth() - tipsDrawer_minDrawer))
	{
		tipsDrawer_Drawer.go = 1;
		var newwidth = tipsDrawer_Drawer.getWidth() - tipsDrawer_move
		tipsDrawer_Drawer.setWidth(newwidth);
		tipsDrawer_Drawer.css.left = tipsDrawer_Drawer.left() + tipsDrawer_move;
		tipsDrawer_timeoutID = setTimeout("tipsDrawer_internalMoveIn()",tipsDrawer_scrollSpeed);
	}
	else
	{
		tipsDrawer_Drawer.setWidth(tipsDrawer_minDrawer);
		tipsDrawer_Drawer.css.left = tipsDrawer_Drawer.pageWidth() - tipsDrawer_minDrawer;
		tipsDrawer_Drawer.go = 0;
		tipsDrawer_Drawer.state = 1;
		tipsDrawer_load(false);
	}
}
// Only tipsDrawer_moveInDrawer() should call it
function tipsDrawer_internalMoveInRTL()
{
	if((tipsDrawer_Drawer.getWidth()-tipsDrawer_move) >  tipsDrawer_minDrawer)
	{
		tipsDrawer_Drawer.go=1;
		var newwidth= tipsDrawer_Drawer.getWidth()-tipsDrawer_move
		tipsDrawer_Drawer.setWidth(newwidth);
		tipsDrawer_Drawer.css.left=tipsDrawer_Drawer.left();
   }
   else
   {
      tipsDrawer_Drawer.setWidth(tipsDrawer_minDrawer);
      tipsDrawer_Drawer.css.left=tipsDrawer_Drawer.left();
      tipsDrawer_Drawer.go=0;
      tipsDrawer_Drawer.state=1;
      tipsDrawer_load(false);
   }
}
// Called to open the drawer
function tipsDrawer_moveOutDrawer()
{
	if(this.tipsDrawer_Drawer == null) tipsDrawer_initDrawer();
	
	if(tipsDrawer_Drawer.state)
	{
		clearTimeout(tipsDrawer_timeoutID);
		tipsDrawer_load(true);
		
		if (isLTR)
			tipsDrawer_internalMoveOut();
		else
			tipsDrawer_internalMoveOutRTL();
		
		// change the toggle button image & alt text
		var tipsDrawer_clickTab = document.getElementById(TAB_NAME);
		tipsDrawer_clickTab.setAttribute("alt", tipsDrawer_minimize);
		tipsDrawer_clickTab.setAttribute("title", tipsDrawer_minimize);
		tipsDrawer_clickTab.setAttribute("src", openedImage);
	}
}
// Called to close the drawer
function tipsDrawer_moveInDrawer()
{
	if(this.tipsDrawer_Drawer == null) tipsDrawer_initDrawer();
	if(!tipsDrawer_Drawer.state)
	{
		clearTimeout(tipsDrawer_timeoutID);
		
		if (isLTR)
			tipsDrawer_internalMoveIn();
		else
			tipsDrawer_internalMoveInRTL();
			
		// change the toggle button image & alt text
		var tipsDrawer_clickTab = document.getElementById(TAB_NAME);
		tipsDrawer_clickTab.setAttribute("alt", tipsDrawer_maximize);
		tipsDrawer_clickTab.setAttribute("title", tipsDrawer_maximize);
		tipsDrawer_clickTab.setAttribute("src", closedImage);
	}
}
// Called to open / close the drawer. This is the method that 
// external functions should call
function tipsDrawer_toggleDrawer()
{
	if(this.tipsDrawer_Drawer == null) tipsDrawer_initDrawer();
	
	if(!tipsDrawer_Drawer.state)
	{
		tipsDrawer_moveInDrawer();
	}
	else
	{
		tipsDrawer_moveOutDrawer();
	}
}
// Resize and reposition the drawer.
function tipsDrawer_resize()
{
	if(this.tipsDrawer_Drawer != null)
	{
		var tipsDrawer_clickTab = document.getElementById(TAB_NAME);
		if (isLTR)
			tipsDrawer_Drawer.drawerOrigObject.style.left = tipsDrawer_getLeftPosition(tipsDrawer_clickTab) + tipsDrawer_clickTab.offsetWidth - tipsDrawer_fixedWidth;
		else
			tipsDrawer_Drawer.drawerOrigObject.style.left = tipsDrawer_getLeftPosition(tipsDrawer_clickTab);
		
		var newHeight = tipsDrawer_Drawer.pageHeight() - tipsDrawer_Drawer.top() - 18;
		
		if(newHeight > 0)
			tipsDrawer_Drawer.drawerFrameObject.style.height = tipsDrawer_Drawer.pageHeight() - tipsDrawer_Drawer.top() - 18;
	}
}
// Get top screen position of the tag
function tipsDrawer_getTopPosition(tag)
{
	var posX = 0;
	var elem = tag;
	if (elem != null)
	{
		if(elem.offsetParent)
		{
			for(;elem.offsetParent; elem = elem.offsetParent)
			{
				posX += elem.offsetTop;
			}
		}
		else
		{
			posX = elem.offsetTop;
		}
	}
	
	return posX;
}
// Get left screen position of the tag
function tipsDrawer_getLeftPosition(tag)
{
	var posY = 0;
	var elem = tag;
	if(elem != null)
	{
		if(elem.offsetParent)
		{
			for(; elem.offsetParent; elem=elem.offsetParent)
			{
				posY += elem.offsetLeft;
			}
		}
		else
		{
			posY = elem.offsetLeft;
		}
	}
	
	return posY;
}
// Toggle page source so style tips load only when drawer is open
function tipsDrawer_load(isBlank)
{
	var fob = tipsDrawer_Drawer.drawerFrameObject;
	
	if (isBlank)
	{
		tipsDrawer_Drawer.css.display="block";
		if (fob.src.indexOf(tipsDrawer_url) < 0)
		{
			fob.src = tipsDrawer_url;
		}
	}
	else
	{
		tipsDrawer_Drawer.css.display="none";
		if ((fob.src.indexOf(dummyUrl) < 0) && (fob.src.indexOf(tipsDrawer_url) < 0))
		{
			fob.src = dummyUrl;
			fob.contentWindow.document.open();
			fob.contentWindow.document.write(tipsDrawer_loadingMsg);
			fob.contentWindow.document.close();
		}
	}
}
// Set the loading page inner html
function tipsDrawer_setLoadingMessage(stylesheet, message)
{
	tipsDrawer_loadingMsg = "<html><head>"
	tipsDrawer_loadingMsg += stylesheet;
	tipsDrawer_loadingMsg += "</head><body "
	tipsDrawer_loadingMsg += isLTR ? "dir=\"ltr\"" : "dir=\"rtl\"";
	tipsDrawer_loadingMsg += "><p align='center'>";
	tipsDrawer_loadingMsg += message;
	tipsDrawer_loadingMsg += "</p></body></html>";
}
// Set additional mouse down handler to the existing one, if exists.
function tipsDrawer_handleMouseDown(e)
{
	var doHide = false;
	
	// do not hide when scrollbar or drawer button is clicked
	if (globalIsIE)
	{
		var evt = window.event;
		if (evt.srcElement.id != TAB_NAME)
		{
			if (isLTR)
			{
				doHide = (evt.clientX < document.body.clientWidth) && (evt.clientY < document.body.clientHeight);
			}
			else
			{
				doHide = (evt.clientX > 18) && (evt.clientY < document.body.clientHeight);
			}
		}
	}
	else
	{
		// Gecko always has its vertical scrollbar on the right
		if (e.target.id != TAB_NAME)
		{
			doHide = (e.clientX < document.body.clientWidth) && (e.clientY < document.body.clientHeight);
		}
	}
	
	if (doHide)
	{
		tipsDrawer_moveInDrawer();
	}
	
	if (tipsDrawer_originalMouseDown != null)
	{
		tipsDrawer_originalMouseDown(e);
	}
    return true;
}

