
// Setup our global name space if needed
if (typeof ATLAS  === 'undefined') {
	ATLAS = {};
}

// Util namespace
ATLAS.Util = {};

/**
 * Function: getViewPortHeight
 * 
 * Returns:
 * Height of the browser view port in pixels. The view port
 * is the visible region of the document not including scrollbars, toolbars
 * etc. This method should work across most browsers.
 */
ATLAS.Util.getViewPortHeight = function() {
	var viewPortHeight;
	if (window.innerHeight) {	// All browsers but IE
		viewPortHeight = window.innerHeight;  
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// IE 6 when there is a DOCTYPE
		viewPortHeight = document.documentElement.clientHeight;
	} else if (document.body.clientWidth) {
		// IE4, IE5, and IE6 without DOCTYPE
		viewPortHeight = document.body.clientHeight;
	}
	return viewPortHeight;
};

/**
 * Perform a shallow clone of an object.
 * From http://www.gnucitizen.org/projects/coreapi/
 */
ATLAS.Util.cloneProperties = function (o) {
	function Clone(o) {
		for (var i in o) {
			if (o.hasOwnProperty(i)) {
				this[i] = o[i];
			}
		}
	}
	return new Clone(o);
};

/**
 * Returns the current time. Use this with ATLAS.Util.elapsed.
 */
ATLAS.Util.tic = function () {
	var now = new Date();
	return now.getTime();
};
	
/**
 * Returns the time difference in ms.
 * <pre>
 * var start = ATLAS.Util.tic();
 * ... (some calcs)
 * var stop = ATLAS.Util.tic();
 * ...
 * alert("Took: "+ATLAS.Util.elapsed(start, stop);
 * </pre>
 * If stop is not specified then the current time is used making it 
 * equivalent to:
 * <pre>
 * ATLAS.Util.elapsed(start, ATLAS.Util.tic())
 * </pre>
 */
ATLAS.Util.elapsed = function (start, stop) {
	if (typeof stop === 'undefined') {
		var now = new Date();
		stop = now.getTime();
	}
	return (stop - start) + ' ms';
};
	
/**
 * Adds the newElement to the array if the newElement is not already present in the array.
 * This uses a linear search to check if the newElement already exists in the array.
 * @returns true if newElement was added, false if the array already contained the newElement
 */	
ATLAS.Util.pushIfUnique = function (array, newElement) {
	var found = false;
	for (var i = 0; i < array.length; i++) {
		if (array[i] === newElement) {
			found = true;
			break;
		}
	}
	if (!found) {
			array.push(newElement);
	}
	return !found;
}


/**
 * Returns the value of a integer variable from the current URL query string, 
 * limiting its value to the range 'min' to 'max', and returning the default
 * should it not exist.
 * @param name           name of the url parameter 
 * @param paramMap		 map of the url parameters, created using OpenLayers.Util.getParameters()
 * @param defaultValue   value to return if no parameter exists in the url, or
 *                       the value is not an integer.
 */
ATLAS.Util.getIntParam = function (name, paramMap, defaultValue, min, max) {
	var urlParameters = OpenLayers.Util.getParameters();
	var nStr = urlParameters[name];
	var n;
	if (typeof(nStr) !== undefined) {
		n = parseInt(nStr, 10);
		// if the input is not a number set the default
		if (isNaN(n)) {			
			n = defaultValue;
		}
		// Limit the range.
		if (n < min) {
			n = min;
		}
		if (n > max) {
			n = max;
		}
	} else {
		n = defaultValue;		// default
	}
	return n;
};



// X-Browser isArray(), including Safari
// from http://www.bram.us/2008/02/01/javascript-isarray-check-if-an-elementobject-is-an-array/
ATLAS.Util.isArray = function (obj) {
  return obj.constructor == Array;
};


/**
 * This function returns the ID of the layer from its layerName. The layerName is structured
 * as follows: 'ra:my_layer_name-d1v2m3', where 'd1m2v3' corresponds to the layerId. If no
 * Id text is detected then the full layer name is returned.
 */
ATLAS.Util.getLayerId = function(layerName) {
	// Match 'd', a number, 'v', a number, 'm', a number at the end of the string. 
	var layerId = layerName.match(/d\d+v\d+m\d+$/);
	if (layerId !== null) {
		layerId = layerId[0];	// Match returns an array so get the result
	} else {
		layerId = layerName;
	}
	// Make sure the layerId does not contain the layer info separator
	// character as this will much up parsing the bookmark.
	// Here we do a replace with split and join to avoid regular
	// expression complications if LAYER_INFO_SEP is changed to 
	// a regular expression meta character.
	var layerId = layerId.split(ATLAS.Multimap.LAYER_INFO_SEP).join('-');
	return layerId;
};
