//Ben Beeson
/*----OBJECT---*/
Object.defineProperty(Object.prototype, "extendWith", { 
	value: function(obj){
		for(var key in obj){
			if(this[key]==null) this[key] = obj[key]
		}
	},
	enumerable : false
});
Object.defineProperty(Object.prototype, "combineWith", { 
	value: function(obj){
		var result = {}
		for(var key in this) result[key] = this[key]
		for(var key in obj) if(result[key]==null) result[key] = obj[key]
		return result
	},
	enumerable : false
});
/*----ARRAY----*/
Array.prototype.each = function(f) { //iterates through an array
	for (var i = 0; i < this.length; i++) {
		var r = f(i,this[i],this.length-i-1)
		if(r!=null) return r
	}
	return null
}
Array.combineN = function(array,split){ //combines 2d array of group of elements eg [[1,2],[a,b]] --> [1a,1b,2a,2b]
	if(!array || array.length == 0) return array
	while(array.length > 1){
	    var a = array.splice(0,1)[0]
	    var b = array.splice(0,1)[0]
	    var tmp = []
	    for(var i = 0; i < a.length; i++){
		for(var j = 0; j < b.length; j++){
		    tmp.push(a[i]+(split!=null?split:"")+b[j])
		}
	    }
	    array.unshift(tmp)
	}
	return array[0]
}
if(Array.slice == null)
Array.slice = function(arr, start, end){
	var result = []
	end = Math.min(end,arr.length)
	for(var i = start; i < end; i++) result.push(arr[i])
	return result
}
/*-----MATH----*/
Math.mod = function (a,n){return (a%n+n)%n}

/*----STRING---*/
String.prototype.reverse = function() {
	return this.split('').reverse().join('')
}
String.prototype.searchAll = function(r,a,b) {
	var res = []
	var tmp = this
	var c = 0
	var len,str
	while (true){
		if((s = tmp.search(r)) == -1) break;
		tmp.replace(r, function (b) {
			len = b.length
			str = b
			return b
		})
		res.push({st:c+s,len:len,str:str,a:a,b:b})
		c += s+len
		tmp = tmp.substring(s+len, tmp.length)
	}
	return res
}
String.prototype.repeat = function(c) {
	var res = ""
	for (var i = 0; i < c; i++) {res+=this}
	return res
}
String.prototype.trim = function() {
	return this.replace(/^[ ]*/,"").replace(/[ ]*$/,"")
}
String.prototype.maxSize = function(n,left) {
	if(this.length < n) return this
	else if(left) return this.substring(0,n)
	else return this.substr(-n)
}
String.prototype.pad = function (len,p,l){
    if(l) return p.repeat(len-this.length)+this.substring(0,len)
    else return this.slice(-len)+p.repeat(len-this.length)
}
String.prototype.hashCode = function() {
	var hash = 0;
	if (this.length == 0) return hash;
	for (i = 0; i < this.length; i++) {
		char = this.charCodeAt(i);
		hash = ((hash << 5) - hash) + char;
		hash = hash & hash; // Convert to 32bit integer
	}
	return hash;
}
String.prototype.splitN = function(size) { //
	if(size<=0) return []
	var arr = new Array(Math.ceil(this.length/size))
	var count = 0;
	while(count < this.length){
		arr[count/size] = this.substr(count,size)
		count+=size
	}
	return arr
}
function getTime(){
	if(typeof window != 'undefined' && 'performance' in window){
		if(performance.now) return performance.now()
		else if(performance.webkitNow) return performance.webkitNow()
	}
	return new Date().getTime()
}
