/**
* Trim whitespace from left of string
*
* @return string String with whitespace removed
**/
String.prototype.ltrim = function() {
	return this.replace(/^\s+/g,'');
}

/**
* Trim whitespace from right of string
*
* @return string String with whitespace removed
**/
String.prototype.rtrim = function() {
	return this.replace(/\s+$/g,'');
}

/**
* Trim whitespace from both left and right of string
*
* @return string String with whitespace removed
**/
String.prototype.trim = function() {
	return this.ltrim().rtrim();
}

/**
* Get version number of IE browser
*
* @return float Version number of browser if IE, or 0 if not IE
**/
function ieVersion() {
	if (navigator.appName != 'Microsoft Internet Explorer')
		return 0;
	var pos = navigator.appVersion.indexOf('MSIE');
	if (pos == -1)
		return 0;
	var version = parseFloat(navigator.appVersion.substr(pos + 4));
	return version;
}

/**
* Simulate hover effects on non-link elements in IE
*
* Call from {@code document.onload} event using something like {@code ieHover('th, td', 'clickable', 'clickableHover')}
* IE 7.0 has proper support of :hover effect so we only apply this function if browser version is lower than that
*
* @param string tagNames Type of element (or multiple types separated by commas)
* @param string className Type of class to which to apply hover effect
* @param string hoverClass (Optional) class to add on hover. If not provided then defaults to className + 'Hover';
**/
function ieHover(tagNames, className, hoverClass) {
	var ieVer = ieVersion();
	if (ieVer && ieVer < 7.0) {
		className = className.trim();
		if (hoverClass)
			hoverClass = hoverClass.trim();
		if (!hoverClass)
			hoverClass = className + 'Hover';
		hoverClass = ' ' + hoverClass;

		var tags = tagNames.split(',');
		for (var j = 0; j < tags.length; j++) {
			var tag = tags[j].trim();
			var els = document.getElementsByTagName(tag);
			for (var i = 0; i < els.length; i++) {
				// spaces are added around classes so we don't accidentally match substrings of class-names
				var elementClass = ' ' + els[i].className + ' ';
				if (elementClass.indexOf(' ' + className + ' ') != -1) {
					els[i].onmouseover = function() {
						this.className += hoverClass;
					}
					els[i].onmouseout = function() {
						this.className = this.className.replace(hoverClass, '');
					}
				}
			}
		}
	}
}
