/**************************\
  GSSI Environment script
\**************************/

if (! GSSI) { var GSSI = {}; };
if (! GSSI.Environment ) { GSSI.Environment = {}; };

GSSI.Environment._DOMHandlers = [];
GSSI.Environment._DOMFired = false;

GSSI.Environment.Agent = navigator.userAgent.toLowerCase();

GSSI.Environment.Browser = {  /* Browser sniffing based on Prototype's */
	IE:     !!(window.attachEvent && !window.opera),
	Opera:  !!window.opera,
	WebKit: GSSI.Environment.Agent.indexOf('applewebkit/') > -1,
	Gecko:  (GSSI.Environment.Agent.indexOf('gecko') > -1) &&
			(GSSI.Environment.Agent.indexOf('khtml') == -1),
	KHTML:	(GSSI.Environment.Agent.indexOf('khtml') > -1) ||
			(GSSI.Environment.Agent.indexOf('konqueror') > -1) ||
			(GSSI.Environment.Agent.indexOf('safari') > -1)
};

GSSI.Environment.Browser.IE5 = GSSI.Environment.Browser.IE && (GSSI.Environment.Agent.indexOf("msie 5")!=-1);
GSSI.Environment.Browser.IE6 = GSSI.Environment.Browser.IE && (GSSI.Environment.Agent.indexOf("msie 6")!=-1);
GSSI.Environment.Browser.IE7 = GSSI.Environment.Browser.IE && (GSSI.Environment.Agent.indexOf("msie 7")!=-1);
GSSI.Environment.Browser.IE8 = GSSI.Environment.Browser.IE && (GSSI.Environment.Agent.indexOf("msie 8")!=-1);
if ( GSSI.Environment.Browser.IE6 ) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

GSSI.Environment.Browser.IEVersion = (GSSI.Environment.Browser.IE8 ? 8
								   : (GSSI.Environment.Browser.IE7 ? 7 
								   : (GSSI.Environment.Browser.IE6 ? 6
								   : (GSSI.Environment.Browser.IE5 ? 5
								   : 0 ))));

GSSI.Environment.OS = {
	Windows: GSSI.Environment.Agent.indexOf("win") != -1,
	Win2K:   GSSI.Environment.Agent.indexOf("windows nt 5.0") != -1,
	WinXP:   GSSI.Environment.Agent.indexOf("windows nt 5.1") != -1,
	Vista:   GSSI.Environment.Agent.indexOf("windows nt 6.0") != -1,
	Mac:     GSSI.Environment.Agent.indexOf("mac") != -1
};



GSSI.Environment.addClass = function ( node, name ){
	if(node) node.className = ((node.className || '') == '' ? '' : node.className + ' ') + name;
}

GSSI.Environment.withDOM = function () {
	GSSI.Environment._setup();
	GSSI.Environment._DOMFired = true;
	var f;
	for( var i=GSSI.Environment._DOMHandlers.length-1; i>=0; i-- ) {
		f = GSSI.Environment._DOMHandlers[i];
		if ( f ) { f(); };
		 GSSI.Environment._DOMHandlers[i] = null;
	}
}

GSSI.Environment.onDOMReady = function( func, priority ) {
	if (GSSI.Environment._DOMFired) {
		if (func) { func(); }
		return;
	};
	
	if(priority) {
		GSSI.Environment._DOMHandlers.unshift( func );
	} else {
		GSSI.Environment._DOMHandlers.push( func );
	}
}

GSSI.Environment._setup = function () {
	try {
		if ( GSSI.Environment.Browser.IE ) {
			GSSI.Environment.addClass( document.body, "ie" );
			if ( GSSI.Environment.Browser.IE5 ) {
				GSSI.Environment.addClass( document.body, "ie5" );
			} else if ( GSSI.Environment.Browser.IE6 ) {
				GSSI.Environment.addClass( document.body, "ie6" );
			} else if ( GSSI.Environment.Browser.IE7 ) {
				GSSI.Environment.addClass( document.body, "ie7" );
			}  else if ( GSSI.Environment.Browser.IE8 ) {
				GSSI.Environment.addClass( document.body, "ie8" );
			}
		} else if ( GSSI.Environment.Browser.Gecko ) {
			GSSI.Environment.addClass( document.body, "gecko" );
		} else if ( GSSI.Environment.Browser.WebKit ) {
			GSSI.Environment.addClass( document.body, "webkit" );
		} else if ( GSSI.Environment.Browser.Opera ) {
			GSSI.Environment.addClass( document.body, "opera" );
		}
	
		if ( GSSI.Environment.OS.Windows ) {
			GSSI.Environment.addClass( document.documentElement, "win" );
			if ( GSSI.Environment.OS.WinXP ) {
				GSSI.Environment.addClass( document.documentElement, "winxp" );
			} else if ( GSSI.Environment.OS.Win2K ) {
				GSSI.Environment.addClass( document.documentElement, "win2k" );
			} else if ( GSSI.Environment.OS.Vista ) {
				GSSI.Environment.addClass( document.documentElement, "vista" );
			}
		} else if ( GSSI.Environment.OS.Mac ) {
			GSSI.Environment.addClass( document.documentElement, "mac" );
		}
	} catch (e) {}
};





do {
	/* Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
		if ( window.location.protocol != "https:" ) {
			document.write("<sc" + "ript id='__ie_gssi_observeDOMReady' defer='defer' src='javascript:void(0);'><\/sc" + "ript>");
			GSSI.Environment.DOMReadyBrowserDevice = document.getElementById("__ie_gssi_observeDOMReady");
			GSSI.Environment.DOMReadyBrowserDevice.onreadystatechange = function() {
				if (this.readyState == "complete") {
					GSSI.Environment.withDOM(); // call the onload handler
				}
			};
			break;
		}
	/*@end @*/
	
	/* Safari/Konqueror */
	/* WebKit must be tested for before DOM2 standard because WebKit supports document.addEventListener but NOT the 
	   DOMContentLoaded event. */
	if (/WebKit/i.test(navigator.userAgent)) { 
		GSSI.Environment.DOMReadyBrowserDevice = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				clearInterval(GSSI.Environment.DOMReadyBrowserDevice);
				GSSI.Environment.withDOM(); 
			}
		}, 10);
		break;
	}
	
	/* Mozilla/Opera9/DOM2 Compliant browsers */
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", GSSI.Environment.withDOM, false);
		break;
	}

	/* Anything else */
	if (window.addEventListener) {
		window.addEventListener("load", GSSI.Environment.withDOM, false);
	} else if(window.attachEvent) {
		window.attachEvent("onload", GSSI.Environment.withDOM);
	}

} while(0);
