/**
 * Contains functionality extensions and helper functions that makes life
 * easier for the JavaScript developer.
 *
 * <p>Dependancies:</p>
 * <ul>
 * </ul>
 */

function doesPackageExists (packageName) {
	var packages = packageName.split('.');
	var currentScope = window;
	for (var i=0; i<packages.length; i++) {
		if (currentScope[packages[i]] == undefined) return false;
		currentScope = currentScope[packages[i]];
	}
	return true;
}
function ensurePackageAvailability (packageName) {
	var packages = packageName.split('.');
	var currentScope = window;
	for (var i=0; i<packages.length; i++) {
		if (currentScope[packages[i]] == undefined) {
			currentScope[packages[i]] = new Object();
		}
		currentScope = currentScope[packages[i]];
	}
}
ensurePackageAvailability('com.rig.Utils');

function myURL() {
	var scope = this.document;
	while (scope.childNodes.length > 0) {
		scope = scope.childNodes[scope.childNodes.length-1];
	}
	if (scope.src != undefined && scope.src != null) {
		return scope.src;
	}
}
function parseURL(url) {
	var e = /(?:([^:/?#]+):)?(?:\/\/([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/;
	var sMatch = url.match(e);
	var URLInfo = {
		url:sMatch[0]
		,protocol:sMatch[1]
		,host:sMatch[2]
		,path:sMatch[3]
		,querystring:sMatch[4]
	}
	return URLInfo;
}


/* ***************************************************************************
 * STRING EXTENSIONS
 * Prototype methods for String object
 *************************************************************************** */

// Removes leading whitespaces
String.prototype.ltrim = function() {
	var re = /\s*((\S+\s*)*)/;
	return this.replace(re, "$1");
}
// Removes ending whitespaces
String.prototype.rtrim = function() {
	var re = /((\s*\S+)*)\s*/;
	return this.replace(re, "$1");
}
// Removes leading and ending whitespaces
String.prototype.trim = function() {
	return this.ltrim().rtrim();
}
String.prototype.format = function() {
  var params = String.prototype.format.arguments;
  var toReturn = this;

  for (var i = 0; i < params.length; i++) {
    var regex = new RegExp("\\{" + i + "\\}", "g");
    toReturn = toReturn.replace(regex, params[i]);
  }

  return toReturn;
}

/* ***************************************************************************
 * OBJECT HELPERS
 * Prototype methods for Object
 *************************************************************************** */

/**
 * Tests to see if the object contains a given property or function.
 * <p>Comparison of values is done by '==' operator.</p>
 * @param obj the object to test
 * @param value the value to test for
 * @return <code>true</code> if the object contains the property otherwise 
 *         <code>false</code>
 */
com.rig.Utils.containsKey = function(obj, key) {
	return obj[a] != undefined;
}

/**
 * Tests to see if the object contains a given value.
 * <p>Comparison of values is done by '==' operator.</p>
 * @param value the value to test for
 * @return <code>true</code> if the object contains the value otherwise 
 *         <code>false</code>
 */
com.rig.Utils.containsValue = function(obj,value) {
	// IE allows for quick find of values through indexOf method
	if (obj.indexOf != undefined) {
		return obj.indexOf(value) != -1;
	}
	for (var a in obj) {
  		if (obj[a] == value) return true;
  }
  return false;
}

/**
 * Tests to see if the value array contains any value.
 */
com.rig.Utils.isEmpty = function(values) {
    for (var i=0; i < values.length; i++) {
        if (values[i].trim() != '') return false;
    }
    return true;
}

/* ***************************************************************************
 * ARRAY EXTENSIONS
 * Prototype methods for Array object
 *************************************************************************** */

// Helper function for appending an array of values or a single value to the 
// end of an existing array.
com.rig.Utils.appendToArray = function(theArray, values) {
	if (values instanceof Array) {
		for (var i=0; i < values.length; i++) {
			theArray[theArray.length] = values[i];
		}
	} else {
		theArray[theArray.length] = values;
	}
}
com.rig.Utils.removeFromArray = function(theArray, value) {
	for (var i=0; i < theArray.length; i++) {
		if (theArray[i] == value) {
			theArray.splice(i,1);
			i--;
		}
	}
}

/* ***************************************************************************
 * DOM EXTENSIONS
 * Prototype methods for Array object
 *************************************************************************** */

com.rig.Utils.Position = function(x, y) {
	this.top = parseInt(y,10);
	if (isNaN(this.top)) this.top = 0;
	this.left = parseInt(x,10);
	if (isNaN(this.left)) this.left = 0;
}

com.rig.Utils.Size = function(x, y) {
	this.height = parseInt(y,10);
	if (isNaN(this.top)) this.top = 0;
	this.width = parseInt(x,10);
	if (isNaN(this.left)) this.left = 0;
}
