var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


String.prototype.trim = function() {
return this.replace( /^\s+|\s+$/, "" );
}
var focustips;
(function()
{
// object constructor
function onfocusTooltips()
{
// store reference to this
focustips = this;
// initial properties
this.tooltip = null;
this.parent = null;
this.target = null;
// bind a document focus handler
if (BrowserDetect.browser == 'Explorer') {
document.onactivate = function(e)
{
// if the target is a link, iframe, object or form element
if(/^(a|iframe|object|input|select|textarea)$/i.test(event.srcElement.nodeName))
{
// store the target
focustips.target = event.srcElement ;
// call tooltip creation timer
focustips.focusTimer();
}
if(/^(input|select|textarea)$/i.test(event.srcElement.nodeName))
{
focustips.addClassName(event.srcElement,'focus');
}
};
} else {
document.addEventListener('focus', function(e)
{
// if the target is a link, iframe, object or form element
if(/^(a|iframe|object|input|select|textarea)$/i.test(e.target.nodeName))
{
// store the target
focustips.target = e.target;
// call tooltip creation timer
focustips.focusTimer();
}
}, true);
}
// bind a document blur handler
if (BrowserDetect.browser == 'Explorer') {
document.ondeactivate = function()
{
// call tooltip remover
focustips.blurTip();
if(/^(input|select|textarea)$/i.test(event.srcElement.nodeName))
{
focustips.removeClassName(event.srcElement,'focus');
}
};
} else {
document.addEventListener('blur', function()
{
// call tooltip remover
focustips.blurTip();
}, true);
}
}
// delay timer
onfocusTooltips.prototype.focusTimer = function()
{
// second loop
if(this.timer != null)
{
// clear timer
clearInterval(this.timer);
this.timer = null;
// call tooltip creator
this.focusTip(focustips.target);
}
// first loop
else
{
// set interval
this.timer = setTimeout(function(){ focustips.focusTimer(); },400);
}
};
// create tooltip
onfocusTooltips.prototype.focusTip = function()
{
//don't continue if we don't have the necessary window props
// this could be converted to test for different names of properties
// if(typeof window.innerWidth == 'undefined') { return; }
// remove any existing tooltip
this.blurTip();
// don't continue if we don't have a target node, or the node has no title
// attribute
if(this.target == null || this.target.getAttribute('title') == null ||
this.target.getAttribute('title') == '') { return; }
//if tooltip is null
if(this.tooltip == null)
{
//get window dimensions
var windim = {
x : window.innerWidth,
y : window.innerHeight
};
// create toolTip
var attrs = { 'class' : 'tooltip' };
this.tooltip =
document.getElementsByTagName('body')[0].appendChild(this.createElement('div', attrs));
// get focussed object co-ordinates
if(this.parent == null)
{
this.parent = {
x : this.getPosition(this.target, 'x') - 3,
y : this.getPosition(this.target, 'y') + 2
};
}
// offset tooltip from target
this.parent.y += this.target.offsetHeight;
// apply tooltip position
this.tooltip.style.left = this.parent.x + 'px';
this.tooltip.style.top = this.parent.y + 'px';
// write in title attribute
this.tooltip.appendChild(document.createTextNode(this.target.title));
// restrict width
if(this.tooltip.offsetWidth > 300)
{
this.tooltip.style.width = '300px';
}
//get tooltip tip.extent
var extent = {
x : this.tooltip.offsetWidth,
y : this.tooltip.offsetHeight
};
// if tooltip exceeds window width
if((this.parent.x + extent.x) >= windim.x)
{
//shift tooltip left
this.parent.x -= extent.x;
this.tooltip.style.left = this.parent.x + 'px';
}
//get scroll height
var scroll = window.pageYOffset;
// if tooltip exceeds window height
if((this.parent.y + extent.y) >= (windim.y + scroll))
{
//shift tooltip up
this.parent.y -= (extent.y + this.target.offsetHeight + 4);
this.tooltip.style.top = this.parent.y + 'px';
}
}
};
// remove tooltip
onfocusTooltips.prototype.blurTip = function()
{
//if tooltip exists
if(this.tooltip != null)
{
//remove and nullify tooltip
document.getElementsByTagName('body')[0].removeChild(this.tooltip);
this.tooltip = null;
this.parent = null;
}
//cancel timer
clearInterval(this.timer);
this.timer = null;
};
// find object position
onfocusTooltips.prototype.getPosition = function(ele, dir)
{
var pos = dir=='x' ? ele.offsetLeft : ele.offsetTop;
var tmp = ele.offsetParent;
while(tmp != null)
{
pos += dir=='x' ? tmp.offsetLeft : tmp.offsetTop;
tmp = tmp.offsetParent;
}
return pos;
};
// create an element and attributes / text nodes
onfocusTooltips.prototype.createElement = function(tagname, attrs)
{
	//create the element
	var ele = document.createElement(tagname);
	// run through attributes argument if present
	if(typeof attrs != 'undefined')
	{
		for(var i in attrs)
		{
			switch(i)
			{
//create a text node
				case 'text' :
					ele.appendChild(document.createTextNode(attrs[i])); break;
// create a className attribute
				case 'class' : ele.className = attrs[i];
// create a generic attribute
				default : ele.setAttribute(i, attrs[i]);
			}
		}
	}
return ele;
};

onfocusTooltips.prototype.addClassName = function(elem, className){
	this.removeClassName (elem, className);
	elem.className = (elem.className + " " + className).trim();
};
onfocusTooltips.prototype.removeClassName = function(elem, className){
elem.className = elem.className.replace(className, "").trim();
};
// instantiate and run
var tooltips = new onfocusTooltips();
})();
