// The lines below exist for backward compatibility. Deprecated. Use the Browser object.
var isIE = (navigator.appName.search(/Microsoft Internet Explorer/) >= 0);
var isIE5 = (isIE && navigator.appVersion.search(/MSIE 5/) >= 0);
var isIE5Mac = (isIE5 && navigator.appVersion.search(/Macintosh/) >=0);
var isIE55 = (isIE && navigator.appVersion.search(/MSIE 5\.[5-9]/) >= 0) ;
var isIE6 = (isIE && navigator.appVersion.search(/MSIE 6/) >= 0);
isIE = (isIE55 || isIE5Mac || isIE6);
var isNN = (navigator.appName == "Netscape");
var isNN6 = (isNN && navigator.appVersion.charAt(0) >= "5");
isNN = isNN6;



function Browser() {
	this.browserString = navigator.userAgent;

	this.UNKNOWN = 'x';
	this.IE = 'i';
	this.NETSCAPE = 'n';
	this.MOZILLA = 'm';
	this.SAFARI = 's';
	this.FIREFOX = 'f';
	this.browserType = this.UNKNOWN;
	this.browserName = "Unknown";

	this.WINDOWS = 'w';
	this.MAC = 'm';
	this.UNIX = 'u';
	this.LINUX = 'l';

	this.osType = this.UNKNOWN;
	this.osName = "Unknown";

	this.version = 0.0;
	this.versionString = "0.0";

	// parse browser string
	this.parseString = parseBrowserString;
	this.parseString();

	this.isQualified = false;
	// temp allow safari without version check
	if (this.browserType == this.SAFARI || (this.browserType == this.IE && this.version >= 5.0) || (this.browserType == this.IE && this.osType == this.MAC && this.version >= 5.1) || (this.browserType == this.NETSCAPE && this.version >= 6.2) || (this.browserType == this.MOZILLA && this.version >= 1.0) || (this.browserType == this.FIREFOX && this.version >= 1.0)) {
		this.isQualified = true;
	}

	// for backward compatibility
	this.isIE = (this.browserType == this.IE);
	this.isIE5 = (this.isIE && this.version < 6.0 && this.version >= 5.0);
	this.isIE5Mac = (this.isIE && this.osType == this.MAC && this.version < 6.0);
	if (this.isIE && this.version < 5.5) undefined = null;
	this.isIE55 = (this.isIE5 && this.version >= 5.5);
	this.isIE6 = (this.isIE && this.version >= 6.0);
	this.isNN = (this.browserType == this.NETSCAPE);
	this.isNN6 = (this.isNN && this.version >= 6.0);

	this.isFlashFriendly = (this.browserType != this.SAFARI && this.browserType != this.MOZILLA && !this.isIE5);
	
	this.viewWidth = 0;
	this.viewHeight = 0;

	this.refreshViewSize = browserRefreshViewSize;

	this.getViewHeight = browserGetViewHeight;
	this.getViewWidth = browserGetViewWidth;

	this.hasDoneFlashDetection = false;
	this.flashInstalled = false;
	this.flashVersion = 0;

	this.detectFlash = browserDetectFlash;
	this.isFlashInstalled = browserIsFlashInstalled;
	this.getFlashVersion = browserGetFlashVersion;
	
	this.encodeURL = browserEncodeURL;
	this.getCookie = browserGetCookie;
	this.setCookie = browserSetCookie;

}

function parseBrowserString() {
	var start = 0;

	var bs = this.browserString;
	var ieIndex = bs.indexOf("MSIE");
	var geckoIndex = bs.indexOf("Gecko");
	var nnIndex = bs.indexOf("Netscape");
	var safariIndex = bs.indexOf("Safari");
	var firefoxIndex = bs.indexOf("Firefox");

	if (ieIndex >= 0){
		this.browserType = this.IE;
		this.browserName = "Internet Explore";
		if (bs.indexOf("Mac") >= 0) {
			this.osType = this.MAC;
			this.osName = "Mac";
		} else {
			this.osType = this.WINDOWS;
			this.osName = "Windows";
		}
		start = ieIndex;
	} else if (geckoIndex >= 0) {
		if (nnIndex >= 0) {
			this.browserType = this.NETSCAPE;
			this.browserName = "Netscape";
			start = nnIndex;
		} else if (firefoxIndex >= 0) {
			this.browserType = this.FIREFOX;
			this.browserName = "Firefox";
			start = firefoxIndex;
		} else if (safariIndex >= 0) {
			this.browserType= this.SAFARI;
			this.browserName = "Safari";
			start = safariIndex;
		} else {
			this.browserType = this.MOZILLA;
			this.browserName = "Mozilla";
			start = bs.indexOf("rv:");
		}
		if (bs.indexOf("Linux") >= 0) {
			this.osType = this.LINUX;
			this.osName = "Linux";
		} else if (bs.indexOf("Windows") >= 0) {
			this.osType = this.WINDOWS;
			this.osName = "Windows";
		} else if (bs.indexOf("Mac") >= 0) {
			this.osType = this.MAC;
			this.osName = "Mac";
		}
	}

	// try to find the version with regular expression.
	bs = bs.substring(start);
	var regExp = /\d+[.]\d*/g;
	var vers = regExp.exec(bs);
	if (vers != null) {
		this.version = parseFloat(vers[0]);
	}

	regExp = /\d+[.]\d*[.]?\d*/g;
	vers = regExp.exec(bs);
	if (vers != null) {
		this.versionString = vers[0];
	}

/*
	Matcher vm = Pattern.compile("\\d+[.]\\d*").matcher(browserString);
	if (vm.find(start)) {
		String vers = vm.group();
		version = Double.parseDouble(vers);
	}
	vm = Pattern.compile("\\d+[.]\\d*[.]?\\d*").matcher(browserString);
	if (vm.find(start)) {
		versionString = vm.group();
	}
*/

}

function browserGetViewHeight(refresh) {
	if (refresh) this.refreshViewSize();
	return this.viewHeight;
}

function browserGetViewWidth(refresh) {
	if (refresh) this.refreshViewSize();
	return this.viewWidth;
}

function browserRefreshViewSize() {
	if (this.browserType == this.IE) {
		if (document.compatMode && document.compatMode != 'BackCompat') {
			this.viewWidth = document.documentElement.clientWidth;
			this.viewHeight = document.documentElement.clientHeight;
		} else {
			this.viewWidth = document.body.clientWidth;
			this.viewHeight = document.body.clientHeight;
		}
	} else if (window.innerWidth != undefined) {
		this.viewWidth = window.innerWidth;
		this.viewHeight = window.innerHeight;
	}
}

function browserGetElementHeight(node) {
	var h;
	if (this.browserType == this.IE) {
		if (document.compatMode && document.compatMode != 'BackCompat') {
			//debug("IE: not backk compat");
			h = node.offsetHeight;
		} else {
			//debug("IE: BackCompat");
			h = node.clientHeight;
		}
	} else {
		//debug("netscape");
		h = node.offsetHeight;
	}
	//debug("height: " + h);
	//debug("clientHeight: " + node.clientHeight);
	//debug("offsetHeight: " + node.offsetHeight);
	return h;
}


function browserIsFlashInstalled() {
	if (!this.hasDoneFlashDetection) {
		this.detectFlash();
	}
	return this.flashInstalled;
}

function browserGetFlashVersion() {
	if (!this.hasDoneFlashDetection) {
		this.detectFlash();
	}
	return this.flashVersion;
}

function browserDetectFlash() {
	// check Netscape-compatible plug-ins first.
	if (navigator.plugins && navigator.plugins.length) {
		for (var x=0; x < navigator.plugins.length; x++) {
			if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
				var version = navigator.plugins[x].description.split('Shockwave Flash ')[1];
				this.flashVersion = parseInt(version);
				this.flashInstalled = true;
				break;
			}
		}
	}

	// then ActiveX-style plug-ins
	else if (window.ActiveXObject) {
		var max_versions = 20;
		var oFlash;
		for (var x = 2; x <= max_versions; x++) {
			try {
				oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
				if(oFlash) {
					this.flashInstalled = true;
					this.flashVersion = x;
				}
			} catch(e) {}
		}
	}
	this.hasDoneFlashDetection = true;
}

/* Encode the URL with the encoder String, usually obtained
* by the caller in the .jsp file. This encoding is equivalent
* to Servlet API's Response.encodeURL(). A global URL_ENCODER var
* can be defined in the file that includes this file, browser.js.
* Then the encoder string does not have to be passed. If
* URL_ENCODER is not defined and the an encoder string is not
* passed, the url will not be encoded.
*
* Note that Resin requires the encoder string to be before
* the query string. That is what is function does.
*/
function browserEncodeURL(url, encoderString) {
	if (encoderString == undefined) {
		if (URL_ENCODER != undefined && URL_ENCODER != null) {
			encoderString = URL_ENCODER;
		}
	}

	if (encoderString != undefined && url.indexOf(encoderString) < 0) {
		var qIndex = url.indexOf("?");
		if (qIndex >= 0) {
			url =  url.substring(0, qIndex) + encoderString + url.substring(qIndex);
		} else {
			url = url + encoderString;
		}
	}
	return url;
}

/** for backward compatibility */
function encodeURL(url, encoderString) {
	return browserEncodeURL(url, encoderString);
}

function browserGetCookie(cookieName) {
	var value = null;
	if (document.cookie.length > 0) {
		var index = document.cookie.indexOf(cookieName + "=");
		if (index >= 0) {
			var start = index + cookieName.length + 1;
			var end = document.cookie.indexOf(";", start);
			if (end == -1) end = document.cookie.length;
			value = document.cookie.substring(start, end);
		}
	}
	return value;
}

function browserSetCookie(cookieName, cookieValue, daysToLive) {
	document.cookie = cookieName + "=" + cookieValue;
	var expDate = null;
	if (daysToLive != undefined && daysToLive != null) {
		expDate = new Date();
		expDate.setDate(expDate.getDate() + daysToLive);
		document.cookie += ";expires=" + expDate;
	}
}

function ElementPosition(node) {
	this.top = browserGetOffsetTop(node, 0);
	this.left = browserGetOffsetLeft(node, 0);
	this.height = node.offsetHeight;
	this.width = node.offsetWidth;
}

/**
* Recursively add up the offsetTop of all offsetParents. It appears that offsetParent is the parentNode
* that this node's offset is relative to.
*/
function browserGetOffsetTop(node, top) {
	//debug("node: " + node.tagName + ", top: " + node.offsetTop);
	top += node.offsetTop;
	if (node.offsetParent.tagName == "BODY") return top;
	else return browserGetOffsetTop(node.offsetParent, top);
}

/**
* Recursively add up the offsetLeft of all offsetParents. The debug statement is defined in debug.js,
* which must be included in the html page for the debug to work.
*/
function browserGetOffsetLeft(node, left) {
	//debug("node: " + node.tagName + ", left: " + node.offsetLeft);
	left += node.offsetLeft;
	if (node.offsetParent.tagName == "BODY") return left;
	else return browserGetOffsetLeft(node.offsetParent, left);
}