// UDMv4.1 // Onfocus Tooltips extension v1.0 //
/***************************************************************\
  ULTIMATE DROP DOWN MENU Version 4.1 by Brothercake
  http://www.udm4.com/
  
  Reference tooltips by Björn Rixman, http://twob.net
****************************************************************/

// Referencing tooltips: start with ':'
// set the container to class="tiptext" to hide it in the browser...

// usage, title=":elmId" 	':' + the ID of the container for the tooltip text. 

// inline tooltip texts, start with ...
// usage, title="...Tooltip text"	'...' + text. DO NOT use html in the title.

var rootElm = "page"; //where the script should start looking for title tags, defaults to document if you set it to ""...
var showDelay ="200"; // delay in ms.

var tt = new Object;
um.addReceiver(initialiseToolTips,'010');
// add new receiver function //"010" = "Ready", 
// defined event code means "pass this event with its object"

function initialiseToolTips() 
{ 
	//root-elm (=document), attr, attr_value, tag/nodeName, useWildcards
	tt.allTags = getChildElementsByAttr (rootElm,"title", "","", false); 
	for (var i=0;i<tt.allTags.length;i++)
	{
		if(tt.allTags[i].title) 
		{
			
			if (tt.allTags[i].title.indexOf(':') == 0) //if the title starts with :
			{
				textObj = document.getElementById(tt.allTags[i].title.substring(1));
				tt.allTags[i].title = '';
				if (textObj) 
				{
					tt.allTags[i].xtitle = '';
					tt.allTags[i].tipref = textObj;
					tt.allTags[i].style.cursor = 'help';
					tt.allTags[i].onfocus=tt.focusTimer;
					tt.allTags[i].onblur=tt.blurTip;
					tt.allTags[i].onmouseover=tt.focusTimer;
					tt.allTags[i].onmouseout=tt.blurTip;
				}
			} else if (tt.allTags[i].title.indexOf('...') == 0) {
				tt.allTags[i].xtitle = tt.allTags[i].title.substring(3);
				tt.allTags[i].title = '';
				tt.allTags[i].style.cursor = 'help';
				tt.allTags[i].onfocus=tt.focusTimer;
				tt.allTags[i].onblur=tt.blurTip;
				tt.allTags[i].onmouseover=tt.focusTimer;
				tt.allTags[i].onmouseout=tt.blurTip;
			} 
		}
	}
	
	tt.goTimer=null;
	tt.toolTip=null;
	tt.toolTipParent=null;
}
/* OLD style with smart tooltip to every title, and reference to every [tip]
function initialiseToolTips() 
{ 
	tt.allTags = getChildElementsByAttr (rootElm,"title", "","", false); 
	//root-elm(document), attr, attr_value, tag/nodeName, useWildcards
	for (var i=0;i<tt.allTags.length;i++)
	{
		if(tt.allTags[i].title) 
		{
			if (tt.allTags[i].title.indexOf('[tip]') != -1) //if the title starts with [tip]
			{
				textObj = document.getElementById(tt.allTags[i].title.substring(5));
				tt.allTags[i].title = '';
				if (textObj) 
				{
					tt.allTags[i].xtitle = '';
					tt.allTags[i].tipref = textObj;
					tt.allTags[i].style.cursor = 'help';
					tt.allTags[i].onfocus=tt.focusTimer;
					tt.allTags[i].onblur=tt.blurTip;
					tt.allTags[i].onmouseover=tt.focusTimer;
					tt.allTags[i].onmouseout=tt.blurTip;
				}
			} else {
				tt.allTags[i].xtitle = tt.allTags[i].title;
				tt.allTags[i].style.cursor = 'help';
				tt.allTags[i].onfocus=tt.focusTimer;
				tt.allTags[i].onblur=tt.blurTip;
				tt.allTags[i].onmouseover=tt.focusTimer;
				tt.allTags[i].onmouseout=tt.blurTip;
			}
		}
	}
	
	tt.goTimer=null;
	tt.toolTip=null;
	tt.toolTipParent=null;
}
 */
 
 
//delay timer
tt.focusTimer = function(e) 
{
	//second loop	
	if(tt.goTimer!=null) 
	{
		clearInterval(tt.goTimer);
		tt.goTimer=null;
		//pass object to create tooltip
		tt.focusTip();
	}
	//first loop
	else
	{
		//get focused object
		(e) ? tt.obj=e.target : tt.obj=event.srcElement;
		tt.goTimer=setInterval('tt.focusTimer()',showDelay);
	}
}


//create tooltip
tt.focusTip = function()
{
	tt.blurTip(); //first, remove any existing tooltip	

	if(tt.toolTip==null) {
		tt.obj.title = '';
		
		if (!tt.obj.tipref) { 
			tt.text = tt.obj.xtitle; //tt.obj.title;
		} else {
			tt.text = tt.obj.tipref.innerHTML;
		}
		
		//create toolTip
		tt.winSize = um.getWindowDimensions(); //get window dimensions
		tt.attribs = { 'class':'', 'innerHTML':tt.text }
		tt.toolTip=um.createElement('div',tt.attribs);
		tt.toolTip.className = 'tooltip';
		
		//get focused object co-ordinates
		if(tt.toolTipParent==null) {
			tt.toolTipParent={ x : um.getRealPosition(tt.obj,'x')+2, y : um.getRealPosition(tt.obj,'y')+2 };
		}

		// offset tooltip from object
		tt.toolTipParent.y+=tt.obj.offsetHeight;

		//apply tooltip position
		tt.toolTip.style.left=tt.toolTipParent.x+'px';
		tt.toolTip.style.top=tt.toolTipParent.y+'px';

		//add to document
		document.getElementsByTagName('body').item(0).appendChild(tt.toolTip);

		//restrict width
		if(tt.toolTip.offsetWidth>300) { tt.toolTip.style.width='300px'; }

		//get tooltip extent
		tt.extent={	x : tt.toolTip.offsetWidth,	y : tt.toolTip.offsetHeight	};

		//if tooltip exceeds window width
		if((tt.toolTipParent.x+tt.extent.x)>=tt.winSize.x) {
			//shift tooltip left
			tt.toolTipParent.x-=tt.extent.x;
			tt.toolTip.style.left=tt.toolTipParent.x+'px';
		}
		//get scroll height
		tt.scroll=um.getScrollAmount();
		//if tooltip exceeds window height
		if((tt.toolTipParent.y+tt.extent.y)>=(tt.winSize.y+tt.scroll)) {
			//shift tooltip up
			tt.toolTipParent.y-=(tt.extent.y+tt.obj.offsetHeight+4);
			tt.toolTip.style.top=tt.toolTipParent.y+'px';
		}
	}
}

//remove tooltip
tt.blurTip = function()
{
	//if tooltip exists
	if(typeof tt.toolTip!="undefined"&&tt.toolTip!=null) 
	{
		tt.obj.title = tt.obj.xtitle;
		document.getElementsByTagName('body').item(0).removeChild(tt.toolTip);
		tt.toolTip=null;
		tt.toolTipParent=null;
		tt.text = null;
	}
	clearInterval(tt.goTimer);
	tt.goTimer=null;
}


/***************************************************************\
	UDMv4.1 // Gecko Iframe-shim extension v1.0 //
	ULTIMATE DROP DOWN MENU Version 4.1 by Brothercake
	http://www.udm4.com/

	THIS DOESN'T WORK IN MAC BUILDS
	add receivers for menu opening and closing events for Gecko browsers 1.1 or later but exclude Safari, which spoofs as Gecko
	in safari, mac/ie5 and msn, the applet still shows through so there's no point
	in Opera 7 the applet shows through *and* the iframe has infinite z-order so it's above the menu
	in older gecko builds the applet is covered, but the iframe also has infinite z-order */
	
var ifs = new Object; //global object
ifs.ids = 0; //id counter

function  hideApplets() 
{
	//2006-10 Removed the check for Gecko, so that IE gets its share of the iframeShim aswell...
	//if(navigator.product == 'Gecko' && parseInt(navigator.productSub,10) >= 20020826 && navigator.vendor != 'Apple Computer, Inc.') { 
		if(document.getElementsByTagName('applet').length > 0) {
			//alert('we got java applets in the house!');
			um.addReceiver(createIframe,'060');
			um.addReceiver(removeIframe,'070');
		}
	//}
}

function createIframe(menu) 
{
	var shadowOffset = 2; //Set shadow offset [in pixels] for iframe to cover shadow layer
	menu.id = 'ifsMenu-' + ifs.ids; //assign an id to the menu 
	
	//create iframe
	ifs.attrs = { 'class':'iframeShim', 'id' : 'ifsShim-' + ifs.ids };
	ifs.frame = um.createElement('iframe', ifs.attrs);
	ifs.ids ++; //increase counter
	
	//append to body
	document.getElementsByTagName('body')[0].appendChild(ifs.frame);
	
	//set dimensions to menu offset dimensions
	ifs.frame.style.width = (menu.offsetWidth+shadowOffset) + 'px';
	ifs.frame.style.height = (menu.offsetHeight+shadowOffset) + 'px';
	
	//move cover to menu position
	ifs.frame.style.left = um.getRealPosition(menu,'x') + 'px';
	ifs.frame.style.top = um.getRealPosition(menu,'y') + 'px';
}


function removeIframe(menu) 
{
	//for all menus within parent of this menu
	ifs.menus = menu.parentNode.getElementsByTagName('ul');
	ifs.menusLen = ifs.menus.length;
	//for each menu
	for(i=0; i<ifs.menusLen; i++) {
		
		//if menu ID matches an iframe id
		if(/ifsMenu/.test(ifs.menus[i].id))	{
			
			//work out iframe id
			ifs.frameID = ifs.menus[i].id.replace('ifsMenu','ifsShim');
			
			//remove child with that id
			document.getElementsByTagName('body')[0].removeChild(document.getElementById(ifs.frameID));
			
			//reset menu id
			ifs.menus[i].id = '';
		}
	}
}
addOnLoad("hideApplets"); //add the check to the window.onload thingie... 