// 
// BROWSER - OS COMPATIBILITY SCRIPT (The prototype for CSS.HACK plugin)
//
/* NOTICE: This is a jQuery-dependent script, but all you need to do is link this file.

		Detects the current Web Browser and Operating System then adds a class to the BODY tag. 
		This allows css to be flexible in different browsers and OS. A reliable replacement for
		"CSS hacking". Below are the list of compatibility classes.   
   
	[OPERATING SYSTEM]			[CLASS]
		Windows					.Win
		MAC						.Mac
		 
	[SUPPORTED BROWSER]			[CLASS]			[VERSION CLASS]
		Firefox					.Firefox		.FF3; .FF4
		Safari					.Safari			.Safari3
		Chrome					.Chrome			.Chrome1
		Internet Explorer		.IE				.IE6; .IE7
		Opera					.Opera
		Other Mozilla			.Mozilla
 
	[USAGE]
	* The compatibility class(es) must be placed first in a css rule. 
		EX: .IE7 .foo { .. }
			.Win.FF3 .foo { .. }
			.Mac.IE7 .foo, .Mac.IE8 .foo { .. }	
*/

jQuery(document).ready( function() {
	
	var isDebug = false;
	
	var uagent = navigator.userAgent.toLowerCase();
	var b = jQuery.browser;
	var browser;
	var version;	
	
	b.chrome = /chrome/.test( uagent.toLowerCase() );	
   
	// Is it Microsoft Internet Exlorer?
	if ( b.msie ) {
		
		browser = "IE";
		version = browser + ParseVersion( jQuery.browser.version.substring( 0, 2 ) );
		
		CSSHACK_Console( "Browser: Internet Explorer" );
	}    
	// Is it Google Chrome?
	if ( b.chrome ) {
		
		CSSHACK_Console( "Browser: Google Chrome" );
		
		browser = "Chrome";
		version = browser + ParseVersion( uagent.substring( uagent.indexOf( 'chrome/' ) + 7 ).substring( 0, 2 ) );
        
		// If it's Chrome but jQuery thinks it's Safari, then let's tell it it's not.
		b.safari = false;
	}    
	// Is it Safari?
	if ( b.safari ) {
	
		CSSHACK_Console( "Browser: Safari" );
        
		browser = "Safari";
		version = browser + ParseVersion( uagent.substring( uagent.indexOf( 'version/' ) + 8 ).substring( 0, 2 ) );		
	}    
	// Is it Mozilla?
	if ( b.mozilla ) {
		
		//Is it Firefox?
		if ( uagent.indexOf( 'firefox' ) != -1 ) {
			
			CSSHACK_Console( "Browser: Firefox" );	
			
			browser = "Firefox";
			version = "FF" + ParseVersion( uagent.substring( uagent.indexOf( 'firefox/' ) + 8 ).substring( 0, 2 ) ); 					
		}
		// If not then it must be another Mozilla
		else {
			browser = "Mozilla";
			version = "";
			CSSHACK_Console( "Browser: Other Mozilla?" );	
		}
	}
    
	// Is this a version of Opera?
	if ( b.opera ) {
		browser = "Opera";
		CSSHACK_Console( "Browser: Opera" );	
	}
	
	// Add the browser name
	Classify( browser );        
	// Add the version number
	Classify( version );
	
	// OS Dectection
	if ( uagent.indexOf('win') != -1 ) {
		Classify( 'Win' );
		CSSHACK_Console( "OS: Microsoft Windows" );
	}
	if ( uagent.indexOf('mac') != -1 ) {
		Classify( 'Mac' );
		CSSHACK_Console( "OS: MAC" );
	}

	function Classify( css_class ) {
		jQuery( "body" ).addClass( css_class );
	}
	
	function ParseVersion( ver ) {
		var val = parseInt( ver ).toString();
		CSSHACK_Console( "Browser Version: " + (( val === "NaN" ) ? "Something went wrong, version value is not a number." : val ) );	
		
		return ( ( val === "NaN" ) ? "" : val );		
	}
	
	function CSSHACK_Console( msg ) {
		if ( isDebug )
			console.log( "CSS.HACK Prototype [" + msg + "]" );		
	}

});
