/*******************************************************
* jslib.js - Library of core langauge helper functions *
********************************************************
* Author: Phil Ewigton (phil.ewington@i-model.co.uk)   *
* Created: 26/08/2009                                  *
* History: N/A                                         *
*******************************************************/

/**
 * function to perform prototypal inheritance
 * @param o {object} object to inherit from
 * @return {object} newly created object with 
   inheritance from passed object
 */
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

/**
 * function to copy an object byVal
 * @param o {object} the object to copy
 * @return {object} the copied object byVal
 */
if (typeof Object.copy !== 'function') {
	Object.copy = function (o) {
		n = new Object();
		for (var e in o) {
			n[e] = o[e];
		}
		return n;
	};
}

/**
 * trims whitespace from strings
 * @return {string} trimmed string
 */
String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, "");
};

/**
 * function to repeat a string x times
 * @param c {integer} number of times to repeat
 * @return {string}
 */
String.prototype.repeat = function (c) {
	s = '';
	for (var i=0; i<c; i++) s += this;
	return s;
};

/**
 * function to padd a string with another string
 * @param ps {string} string to pad with
 * @param l {integer} total length of new string
 * @param d {string} one of 'left' || 'right'
 * @return {string}
 */
String.prototype.pad = function (ps, l, d) {
	if (d == null || d == 'left') {
		// pad to the left
		return ps.repeat(l - this.length) + this;
	}
	else if (d == 'right') {
		// pad to the right
		return this + ps.repeat(l - this.length);
	}
	// no padding added, return unchanged string
	return this;
};

/**
 * Determines if key value exists in an array
 * @param obj {any} object to find
 * @param fromIndex {integer} start point (defaults to 0)
 * @return {integer} array index
 */
if (!Array.prototype.find) {
  Array.prototype.find = function (obj, fromIndex) {
	if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj) {
			return i;
		}
    }
    return -1;
  };
}


/**
 * function to determine an array
 * @param a {array} reference to array to check
 * @return {boolean}
 */
function isArray(a) {
	if (typeof a === 'object') {
		if (typeof a.length === 'number' &&
			!(a.propertyIsEnumerable('length'))) {
				return true;
		}
	}
	return false;
}

/**
 * method to randomize an array
 * @return {array} randomized array
 */
Array.prototype.randomize = function () {
	n = Math.round(this.length / 2);
	for (i=0; i<n; i++) {
		r = Math.round(Math.random(i) * n);
		e = this.splice(r,1);
		this.push(e);
		this.reverse();
	}
	return this;
}
