/*
** css_brower_selector
**
** This script examines a string (usually the USER_AGENT string) and determines the browser (along
** with version) and the operating system.  Based on that we add a bunch of class selectors to the
** HTML element.
**
** This is based on the script beow, but has been rewritten and expanded.
**
**   CSS Browser Selector v0.3.1
**   Rafael Lima (http://rafael.adm.br)
**   http://rafael.adm.br/css_browser_selector
**   License: http://creativecommons.org/licenses/by/2.5/
**   Contributors: http://rafael.adm.br/css_browser_selector#contributors
**
** USAGE
**
**   1. Copy and paste the line above, inside <head> and </head> tag
**
**      <script src="css_browser_selector.js" type="text/javascript"></script>
**
**   2. Set CSS attributes with the code of each browser/os you want to hack
**
** Examples:
** 
**     * html.gecko div#header { margin: 1em; }
**     * .opera #header { margin: 1.2em; }
**     * .ie .mylink { font-weight: bold; }
**     * .mac.ie .mylink { font-weight: bold; }
**     * .[os].[browser] .mylink { font-weight: bold; } -> without space between .[os] and .[browser]
** 
** Available OS Codes [os]:
** 
**     * win - Microsoft Windows
**     * linux - Linux (x11 and linux)
**     * mac - Mac OS
**     * freebsd - FreeBSD new
**     * ipod - iPod Touch new
**     * iphone - iPhone new
**     * webtv - WebTV new
**     * mobile - J2ME Devices (ex: Opera mini) new
** 
** Available Browser Codes [browser]:
** 
**     * ie - Internet Explorer (All versions)
**     * ie_8 - Internet Explorer 8.x
**     * ie_7 - Internet Explorer 7.x
**     * ie_6 - Internet Explorer 6.x
**     * ie_5 - Internet Explorer 5.x
**     * gecko - Mozilla, Firefox (all versions), Camino
**     * firefox_2 - Firefox 2
**     * firefox_3 - Firefox 3
**     * opera - Opera (All versions)
**     * opera_8 - Opera 8.x
**     * opera_9 - Opera 9.x
**     * opera_10 - Opera 10.x
**     * konqueror - Konqueror
**     * webkit or safari - Safari, NetNewsWire, OmniWeb, Shiira, Google Chrome
**     * safari_3 - Safari 3.x
**     * safari_4 - Safari 4.x
**     * chrome - Google Chrome
** 
** Extra Codes:
** 
**     * js - Will be available when js is enable, so you can show/hide some stuffs
*/

/*
** function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' '+s+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent); 
*/


function replacement(browser, version, minVersion, maxVersion) {
	var classNames = browser;
	
	if (version != undefined) {
		for (var i = minVersion; i <= maxVersion; i++) {
			classNames += ' ' + browser + (version <  i ? '_lt_' : '_ge_')  + i;
			classNames += ' ' + browser + (version <= i ? '_le_' : '_gt_')  + i;
			classNames += ' ' + browser + (version == i ? '_'    : '_not_') + i;
		}
	}
	
	return classNames;
}

function css_browser_selector(userAgent) {
	var userAgent = userAgent.toLowerCase();
	
	var classNames = 'not_camino not_chrome not_ie not_firefox not_freebsd not_gecko not_iphone not_ipod js not_konqueror not_linux not_mac_os not_mobile not_opera not_safari not_shiira not_sunrise not_webkit not_webtv not_windows';
	
	var is = function(t) { return userAgent.indexOf(t) > -1; };
	
	if (is('applewebkit/')) {
		
		// Chrome	
		if (/chrome\/(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_chrome/, replacement('chrome', RegExp.$1, 0, 4));
						
		else if (is('chrome/'))
			classNames = classNames.replace(/not_chrome/, 'chrome', 0, 0, 4);
		
		// Safari		
		else if (is('safari/') && /version\/(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_safari/, replacement('safari', RegExp.$1, 1, 4))
			
		// Early version of Safari and other browsers that use AppleWebKit			
		else if (/applewebkit\/(\d\d\d)[ .]/.test(userAgent)) {
			if (RegExp.$1 < 400)
				classNames = classNames.replace(/not_safari/, replacement('safari', 1, 1, 4));
			else if (RegExp.$1 < 500)
				classNames = classNames.replace(/not_safari/, replacement('safari', 2, 1, 4));
			else if (RegExp.$1 < 528)
				classNames = classNames.replace(/not_safari/, replacement('safari', 3, 1, 4));
			else
				classNames = classNames.replace(/not_safari/, replacement('safari', 4, 1, 4));
		}
			
		else
			; // AppleWebKit always has a version; this condition should never be encountered
			
		classNames = classNames.replace(/not_webkit/, 'webkit');
	}
	
	else if (is('konqueror/'))
		classNames = classNames.replace(/not_konqueror/, 'konqueror');
	
	else if (is('gecko')) {
	
		if (/camino\/(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_camino/, replacement('camino', RegExp.$1, 1, 2));
	
		else if (/firefox\/(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_firefox/, replacement('firefox', RegExp.$1, 2, 3));
		
		classNames = classNames.replace(/not_gecko/, 'gecko');
	}
	
	else if (is('opera')) {
		
		if (/version\/(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_opera/, replacement('opera', RegExp.$1, 1, 10));
		
		else if (/opera(\s|\/)(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_opera/, replacement('opera', RegExp.$2, 1, 10));
			
	}
	
	else if (is('msie')) {

		if (is('webtv'))
			classNames = classNames.replace(/not_webtv/, 'webtv');
	
		else if (/msie\s(\d+)/.test(userAgent))
			classNames = classNames.replace(/not_ie/, replacement('ie', (RegExp.$1 == '7' && typeof XDomainRequest != "undefined") ? 8 : RegExp.$1, 6, 8));
			
	}
		
	if (is('iphone'))
		classNames = classNames.replace(/not_iphone/, 'iphone');
		
	else if (is('ipod'))
		classNames = classNames.replace(/not_ipod/, 'ipod');
				
	else if (is('j2me'))
		classNames = classNames.replace(/not_mobile/, 'mobile');
	
	else if (is('freebsd'))
		classNames = classNames.replace(/not_freebsd/, 'freebsd');		
	
	else if (is('linux') || is('x11'))
		classNames = classNames.replace(/not_linux/, 'linux');
	
	else if (is('darwin') || is('mac'))
		classNames = classNames.replace(/not_mac_os/, 'mac_os' + (/mac os x/.test(userAgent) ? ' mac_os_x' : ''));
	
	else if (is('win'))
		classNames = classNames.replace(/not_windows/, 'windows');
		
	document.getElementsByTagName('HTML')[0].className += ' ' + classNames; /* */
	
	return classNames;
}
