var g_clientCheck;

//	--------------------------------------------------------------------

function ClientCheck()
{	
	this.ok = false;
	
	ClientCheck.AGENT	= [["msie ", ";"], ["applewebkit/", " "], ["konqueror/", ";"], ["opera/", "("], ["omniweb/", ";"], ["icab/", "("], ["rv:", ")"], ["mozilla/", " "]];
	ClientCheck.BROWSER	= ["msie", "safari", "konqueror", "opera", "omniweb", "icab", "gecko", "mozilla"];
	ClientCheck.SYSTEM	= ["win", "mac", "linux"];
	ClientCheck.OBJREF	= ["id", "all", "layer"];

	this.userAgent		= navigator.userAgent.toLowerCase();
	
	this.browser		= this.getBrowserType();
	this.system			= this.getOperatingSystem();
	this.version		= this.getBrowserVersion();
	this.object			= this.getObjectReference();
	
	this.flash			= this.getFlash();

	this.winvista		= this.userAgent.indexOf("windows nt 6.0") != -1;
	this.win2000		= this.userAgent.indexOf("windows nt 5.0") != -1;

	this.ie				= this.browser == ClientCheck.BROWSER[0];
	this.nn				= this.browser == ClientCheck.BROWSER[6] || this.browser == ClientCheck.BROWSER[7];
	this.ieWin			= this.ie && this.system == ClientCheck.SYSTEM[0];
	this.activex		= this.ieWin ? this.getActiveX("Shell.Explorer") : false;
	this.dom 			= this.getDom();
	this.ieEdit			= this.ieWin && this.activex && this.dom && parseFloat(this.version) >= 5.5;
	this.winxpsp2		= this.ieWin && this.userAgent.indexOf("sv1") != -1;
	this.ieWinvista		= this.ieWin && this.winvista;
	this.ieWin2000		= this.ieWin && this.win2000;
	
	this.mozillaID		= 1;
					
	this.ok 			= this.cm4allFilter();
	
	// IE Types
	this.fversion		= parseFloat(this.version);
	this.mversion		= parseInt(this.fversion);

	this.isIE4			= this.ie && (this.mversion == 4);
	this.isIE4up		= this.ie && (this.mversion >= 4);
	this.isIE5			= this.ie && (this.fversion >= 5 && this.fversion < 5.5);
	this.isIE5up		= this.ie && (this.mversion >= 5);
	this.isIE55			= this.ie && (this.fversion == 5.5);
	this.isIE55up		= this.ie && (this.fversion >= 5.5);
	this.isIE6			= this.ie && (this.mversion == 6);
	this.isIE6up		= this.ie && (this.mversion >= 6);
	this.isIE7			= this.ie && (this.mversion == 7);
	this.isIE7up		= this.ie && (this.mversion >= 7);
	this.isIE8			= this.ie && (this.mversion == 8);
	this.isIE8up		= this.ie && (this.mversion >= 8);
	
	// Netscapes
	this.isNS4up		= this.nn && (this.mversion >= 4)	&& !this.dom;
	// ^^^ attention NOT Netscape 6 up

	this.isMoz0			= this.nn && (this.fversion == 0.9) && this.dom;
	this.isMoz0up		= this.nn && (this.fversion >= 0.9) && this.dom;
	this.isMoz1			= this.nn && (this.mversion == 1) 	&& this.dom;
	this.isMoz1up		= this.nn && (this.mversion >= 1) 	&& this.dom;
	this.isMoz14up		= this.nn && (this.fversion >= 1.4)	&& this.dom;
	this.isMoz15up		= this.nn && (this.fversion >= 1.5)	&& this.dom;
	this.isMoz16up		= this.nn && (this.fversion >= 1.6)	&& this.dom;
	
	this.isBeta			= this.version.indexOf("a") != -1 || this.version.indexOf("b") != -1;

	return this;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getBrowserType = ClientCheck_getBrowserType;

function ClientCheck_getBrowserType()
{
	for (var i = 0; i < ClientCheck.BROWSER.length; i++)
	{
		var lookup = ClientCheck.BROWSER[i];
		
		if (this.userAgent.indexOf(lookup) != -1)
			return lookup;
	}

	return null;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getOperatingSystem = ClientCheck_getOperatingSystem;

function ClientCheck_getOperatingSystem()
{
	for (var i = 0; i < ClientCheck.SYSTEM.length; i++)
	{
		var lookup = ClientCheck.SYSTEM[i];

		if (this.userAgent.indexOf(lookup) != -1)
			return lookup;
	}

	return null;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getBrowserVersion = ClientCheck_getBrowserVersion;

function ClientCheck_getBrowserVersion()
{
	var version = "";
	
	for (var i = 0; i < ClientCheck.AGENT.length; i++)
	{
		var lookup = ClientCheck.AGENT[i][0];
		var endtag = ClientCheck.AGENT[i][1];
		
		var offset = this.userAgent.indexOf(lookup);
		
		if (offset != -1)
		{
			var start	= offset + lookup.length;
			var end		= this.userAgent.indexOf(endtag, offset);
			
			version		= this.userAgent.substring(start, end);		
			break;
		}
	}

	return version;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getObjectReference = ClientCheck_getObjectReference;

function ClientCheck_getObjectReference()
{
	if (document.getElementById)
		return ClientCheck.OBJREF[0];

	if (document.all)
		return ClientCheck.OBJREF[1];

	if (document.layers)
		return ClientCheck.OBJREF[2];

	return null;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getDom = ClientCheck_getDom;

function ClientCheck_getDom()
{
	var dom = false;
	
	if (this.ieWin && this.activex)
		dom = this.getActiveX("Msxml2.DomDocument.3.0");
	else
		dom = (document.implementation && document.implementation.createDocument) ? true : false;

	return dom;	
}

//	--------------------------------------------------------------------
		
		
ClientCheck.prototype.cm4allFilter = ClientCheck_cm4allFilter;

function ClientCheck_cm4allFilter()
{
	if (this.browser && this.system && this.version && this.object)
		if (this.object != ClientCheck.OBJREF[2] && this.dom)
			return true;

	return false;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getFlash = ClientCheck_getFlash;

function ClientCheck_getFlash()
{
	if (navigator.plugins && navigator.plugins.length > 0)
	{
		if (navigator.plugins["Shockwave Flash"])
			return true;
		else
			return false;
	}
	else
	{
		return this.getActiveX("ShockwaveFlash.ShockwaveFlash.1");
	}
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getActiveX = ClientCheck_getActiveX;

function ClientCheck_getActiveX(module)
{
	var x = new Function
	(
		"module",
		
		'try'
		+ '{'
		+ 'var x = new ActiveXObject(module);'
		+ 'return true;'
		+ '}'
		+ 'catch(e)'
		+ '{}'
		+ 'return false;'
	);
	
	return x(module);
}

//	--------------------------------------------------------------------

ClientCheck.prototype.info = ClientCheck_info;

function ClientCheck_info()
{
	alert(	  "browser: "		+ this.browser		+ "\n"
			+ "system: "		+ this.system		+ "\n"
			+ "version: "		+ this.version		+ "\n"
			+ "float version: " + this.fversion		+ "\n"
			+ "major version: " + this.mversion		+ "\n"
			+ "objectRef: "	 	+ this.object		+ "\n"
			+ "flash: "		 	+ this.flash		+ "\n"
			+ "activeX: "		+ this.activex		+ "\n"
			+ "dom: "			+ this.dom			+ "\n"
			+ "\n"
			+ "isIE4: "			+ this.isIE4		+ "\n"
			+ "isIE4up: "		+ this.isIE4up	    + "\n"
			+ "isIE5: "			+ this.isIE5		+ "\n"
			+ "isIE5up: "		+ this.isIE5up	    + "\n"
			+ "isIE55: "		+ this.isIE55		+ "\n"
			+ "isIE55up: "		+ this.isIE55up	    + "\n"
			+ "isIE6: "			+ this.isIE6		+ "\n"
			+ "isIE6up: "		+ this.isIE6up	    + "\n"
			+ "isNS4up: "		+ this.isNS4up	    + "\n"
			+ "isMoz0: "		+ this.isMoz0		+ "\n"
			+ "isMoz0up: "		+ this.isMoz0up	    + "\n"
			+ "isMoz1: "		+ this.isMoz1		+ "\n"
			+ "isMoz1up: "		+ this.isMoz1up	    + "\n"
		 );
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getObject = ClientCheck_getObject;

function ClientCheck_getObject(id, object)
{	
	var doc = null;
	
	if (object.document)
		doc = object.document;
	else
		doc = object;
	
	return (doc.getElementById) ? doc.getElementById(id) : doc.all(id);
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getEvent = ClientCheck_getEvent;

function ClientCheck_getEvent(evt, win)
{
	return (evt) ? evt : win.event;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.killEvent = ClientCheck_killEvent;

function ClientCheck_killEvent(evt, win, ret)
{	
	var	e = this.getEvent(evt, win);

	if (e != null)
	{
		this.stopEvent(e);
		
		if (!ret)
			this.returnEventFalse(e);
	}
	
	return e;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getKey = ClientCheck_getKey;

function ClientCheck_getKey(evt)
{
	return (evt.keyCode) ? evt.keyCode : evt.which;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getButton = ClientCheck_getButton;

function ClientCheck_getButton(evt)
{
	return (evt.button) ? evt.button : evt.which;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getTarget = ClientCheck_getTarget;

function ClientCheck_getTarget(evt)
{
	var object;
	
	 if (evt.target)
		object = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
	else
		object = evt.srcElement;

	return object;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getPageX = ClientCheck_getPageX;

function ClientCheck_getPageX(evt, win)
{
	return (evt.pageX != null) ? evt.pageX : evt.clientX + win.document.body.scrollLeft;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getPageY = ClientCheck_getPageY;

function ClientCheck_getPageY(evt, win)
{
	return (evt.pageY != null) ? evt.pageY : evt.clientY + win.document.body.scrollTop;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getOffsetX = ClientCheck_getOffsetX;

function ClientCheck_getOffsetX(evt, win)
{
	return (evt.offsetX != null) ? evt.offsetX : evt.pageX - evt.target.offsetLeft - win.document.body.offsetLeft;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getOffsetY = ClientCheck_getOffsetY;

function ClientCheck_getOffsetY(evt, win)
{
	return (evt.offsetY != null) ? evt.offsetY : evt.pageY - evt.target.offsetTop - win.document.body.offsetTop;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getObjectX = ClientCheck_getObjectX;

function ClientCheck_getObjectX(evt)
{
	return (evt.layerX != null) ? evt.layerX : evt.offsetX;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getObjectY = ClientCheck_getObjectY;

function ClientCheck_getObjectY(evt)
{
	return (evt.layerY != null) ? evt.layerY : evt.offsetY;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getClass = ClientCheck_getClass;

function ClientCheck_getClass()
{
	return (this.ie) ? "className" : "class";
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getStyleValue = ClientCheck_getStyleValue;

function ClientCheck_getStyleValue(object, property)
{
	var value = null;

	if (document.defaultView)
		value = document.defaultView.getComputedStyle(object, null).getPropertyValue(property);
	else if (object.currentStyle)
		value = eval("object.currentStyle." + property);
	
	return value;
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getRect = ClientCheck_getRect;

function ClientCheck_getRect(object, property)
{
	var value = null;

	if (document.defaultView)
	{
		value = document.defaultView.getComputedStyle(object, null).getPropertyValue(property);
		//	needs a fix
	}
	else if (object.currentStyle)
	{
		switch(property)
		{
			case "left":
				value = 	object.getBoundingClientRect().left;
				break;

			case "top":
				value = 	object.getBoundingClientRect().top;
				break;
	
			case "right":
				value = 	object.getBoundingClientRect().right;
				break;
				
			case "bottom":
				value = 	object.getBoundingClientRect().bottom;
				break;
				
			case "width":
				value = 	object.getBoundingClientRect().right;
				value -=	object.getBoundingClientRect().left;
				break;
				
			case "height":
				value = 	object.getBoundingClientRect().bottom;
				value -=	object.getBoundingClientRect().top;
				break;
		}
	}
			
	return parseInt(value);
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getRule = ClientCheck_getRule;

function ClientCheck_getRule(css, item, property)
{
	var style = null;
	
	if (this.ie)
		style = document.styleSheets[css].rules[item].style;
	else
		style = document.styleSheets[css].cssRules[item].style;
		
	return eval('style.' + property);
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getHandCursor = ClientCheck_getHandCursor;

function ClientCheck_getHandCursor()
{
	return (this.ie) ? "hand" : "pointer";
}

//	--------------------------------------------------------------------

ClientCheck.prototype.stopEvent = ClientCheck_stopEvent;

function ClientCheck_stopEvent(e)
{
	if (this.ie)
		e.cancelBubble = true;
	else
		e.stopPropagation();
}

//	--------------------------------------------------------------------

ClientCheck.prototype.returnEventFalse = ClientCheck_returnEventFalse;

function ClientCheck_returnEventFalse(e)
{
	if (this.ie)
		e.returnValue = false;
	else
		e.preventDefault();
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getXmlHttp = ClientCheck_getXmlHttp;

function ClientCheck_getXmlHttp()
{
	return (this.activex) ? new ActiveXObject("Msxml2.XMLHTTP") : new XMLHttpRequest();
}

//	--------------------------------------------------------------------

ClientCheck.prototype.getUniqueID = ClientCheck_getUniqueID;

function ClientCheck_getUniqueID(document)
{
	return (document.uniqueID) ? document.uniqueID : "mz__id" + this.mozillaID++;
}

//	--------------------------------------------------------------------

//	g_clientCheck = new ClientCheck();	//	use only in non cm4all environment!

var g_clientCheckTopWindow = getTopWindow();

if (!g_clientCheckTopWindow.g_clientCheck) {
	g_clientCheck = g_clientCheckTopWindow.g_clientCheck = new ClientCheck();
} else {
	g_clientCheck = g_clientCheckTopWindow.g_clientCheck;
}
