/**********************************************************
Author:
Adam Barry
Klestrup partners
www.klestrup-partners.dk

Date: May 17 2010

© 2010 Adam Barry, all rights reserved
-----------------------------------------------------------

Name:
prototypes script

-----------------------------------------------------------
Description:
Various prototypes used for extending JavaScript objects

-----------------------------------------------------------
Usage:
Simply place a link to the this script in the head-section
of the XHTML page. The prototypes will then automatically
be available to other JavaScripts when the page has loaded.

<script type="text/javascript" src="prototypes.js"></script>

-----------------------------------------------------------
Dependencies:
None

**********************************************************/


/**********************************************************
String prototypes
**********************************************************/

/*: Prototype string reverse
-----------------------------------------------------------
Example:
var message = "This is a message";
var reversedMessage = message.reverse()

(Result is "egassem a si sihT")
----------------------------------------------------------*/
String.prototype.reverse = function () {
	var s = "";
	var i = this.length;

	while (i > 0) {
		s += this.substring(i - 1, i);
		i--;
	}
	return s;
}


/*: Trim
-----------------------------------------------------------
Example:
var message = "This is a message";
trim(message,'age');

(Restult is "This is a mess")
----------------------------------------------------------*/
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


/**********************************************************
Array prototypes
**********************************************************/

/*: Prototype array contains
-----------------------------------------------------------

----------------------------------------------------------*/
Array.prototype.contains = function (obj) {
	var i = this.length;
	while (i--) {
		if (this[i] === obj) {
			return true;
			break;
		}
	}
	return false;
}

/*: Prototype array position
-----------------------------------------------------------

----------------------------------------------------------*/
Array.prototype.position = function (obj) {
	var i = this.length;
	while (i--) {
		if (this[i] === obj) {
			return i;
			break;
		}
	}
	return false;
}

/*: Prototype array contains array
-----------------------------------------------------------

----------------------------------------------------------*/
Array.prototype.containsArray = function (arrayObject) {
	var matches = new Array();
	for (var i = 0; i < arrayObject.length; i++) {
		me = arrayObject[i];
		for (var x = 0; x < this.length; x++) {
			if (this[x] == me) {
				matches.push(true);
			}
		}
	}
	if (matches.length == arrayObject.length) {
		return true;
	}
	else {
		return false;
	}
}
