/** Class for utility functions. */
var Util = {

	/**
	 * Detect the operating system where the browser is running.
	 *
	 * @return string, or null if OS has not been found.
	 */
	detectOs: function() {
		var osName;
		if (navigator.appVersion.indexOf('Win') != -1) {
			osName = 'Windows';
		} else if (navigator.appVersion.indexOf('X11') != -1) {
			osName = 'UNIX';
		} else if (navigator.appVersion.indexOf('Linux') != -1) {
			osName = 'Linux';
		} else if (navigator.appVersion.indexOf('Mac') != -1) {
			osName = 'MacOS';
		}
		return osName;
	},

	/**
	 * Return whether larger base64 encoded inline images could be applied or not.
	 *
	 * @return boolean
	 */
	isInlineImageApplicable: function() {
		var versionStart = navigator.appVersion;
		var ieVersionStartPos = navigator.appVersion.indexOf('MSIE ') + 5;
		if (ieVersionStartPos > 4) {
			versionStart = versionStart.substr(ieVersionStartPos);
		}
		var version = parseInt(versionStart.substr(0, versionStart.indexOf('.')));
		return ((navigator.appName == 'Netscape' && version >= 5) || (navigator.appName == 'Microsoft Internet Explorer' && version >= 8));
	},

	/**
	 * Get the size of the browser window.
	 *
	 * @return object with properties of x (window width) and y (window height).
	 */
	getWindowSize: function() {
		var winWidth = 0;
		var winHeight = 0;
		if (window.innerHeight != null) {
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
		} else if (document.documentElement.clientWidth != null) {
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		}
		return {x: winWidth, y: winHeight};
	},

	/**
	 * Get the current mouse cursor position.
	 *
	 * @param e   Event object.
	 * @return object with properties of x and y.
	 */
	getMouseXY: function(e) {
		if (navigator.appName == "Microsoft Internet Explorer") {
			posX = event.clientX + document.body.scrollLeft;
			posY = event.clientY + document.body.scrollTop;
		} else {
			posX = e.pageX;
			posY = e.pageY;
		}

		if (posX < 0) {
			posX = 0;
		}
		if (posY < 0) {
			posY = 0;
		}
		return {x: posX, y: posY};
	},

	/**
	 * Retrieve the type of the variable.
	 *
	 * @param variable   The variable.
	 * @return string. Possible values are: int, float, string, boolean, object, array (if is empty or has mixed types), 
	 *				   int[], float[], string[], boolean[], object[], int[][], float[][], etc.
	 */
	getVarType: function(variable) {
		switch (typeof(variable)) {
			case 'string':
				return 'string';
			case 'number':
				if (variable.toString().match(/\./)) {
					return 'float';
				}
				return 'int';
			case 'boolean':
				return 'boolean';
			case 'object':
				if (variable instanceof Array) {
					var arrayType = 'array';
					for (var i = 0; i < variable.length; i++) {
						if (i == 0) {
							arrayType = Util.getVarType(variable[i]);
							if (arrayType == null) {
								return 'array';
							}
						} else if (Util.getVarType(variable[i]) != arrayType) {
							return 'array';
						}
						
					}
					if (arrayType != 'array') {
						arrayType += '[]';
					}
					return arrayType;
				}
				return 'object';
			default:
				return null;	
		}
	},

	/**
	 * Strip the search part (after the question mark, if it exists) from an url.
	 *
	 * @param url   The url string.
	 * @return string
	 */
	stripSearchPartFromUrl: function(url) {
		var queMarkPos = url.lastIndexOf('?');
        if (queMarkPos > -1) {
        	url = url.substr(0, queMarkPos);
		}
		return url;
	},

	/**
	 * Return whether an element can be found in an array or not.
	 *
	 * @param needle     Element.
	 * @param haystack   Array.
	 * @return boolean
	 */
	inArray: function(needle, haystack) {
		for (var i = 0; i < haystack.length; i++) {
			if (haystack[i] == needle) {
				return true;
			}
		}
		return false;
	}
};
