/*
 * Wrench JavaScript 1.5 Environment v1.0.0
 * http://wrench.monkeyscript.org/
 *
 * Copyright © 2009 Daniel Friesen
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
Object.invert=function invert(b){var c={};for(var a in b){if(b.hasOwnProperty(a)){c[b[a]]=a}}return c};Object.merge=function merge(){var d=Array.slice(arguments),h,c=false;if(typeof d[0]==="boolean"){c=d.shift()}h=d.shift();if(h===null||typeof h!=="object"){throw new TypeError()}for(var g=0,b=d.length;g<b;g++){var f=d[g];for(var e in f){if(f.hasOwnProperty(e)){if(c&&isObject(f[e])){if(!isObject(h[e])){h[e]={}}h[e]=merge(true,h[e],f[e])}else{h[e]=f[e]}}}}return h};(function(b){Object.count=function a(c){if(b){return c.__count__}return Object.keys(c).length}})(({}).__count__===0);if(!Object.keys){Object.keys=function keys(c){var b=[];for(var a in c){if(c.hasOwnProperty(a)){b.push(a)}}return b}}if(!Object.values){Object.values=function values(c){var b=[];for(var a in c){if(c.hasOwnProperty(a)){b.push(c[a])}}return b}}Number.prototype.pad=Number.prototype.padLeft=function pad(a,c,b){return this.toString(b||10).padLeft(a||0,c||"0")};Math.rand=function rand(b,a){return Math.floor(Math.random()*(a-b+1))+b};if(!Math.sum){Math.sum=function sum(){if(!arguments.length){throw new TypeError()}var a=Array.slice(arguments);var b=a.shift();while(a.length){b+=a.shift()}return b}}Math.avg=function avg(){return Math.sum.apply(Math,arguments)/arguments.length};(function fn(a){if(a in String){return fn}String[a]=function(b){return String.prototype[a].apply(b,Array.slice(arguments,1))};return fn})("split")("explode")("repeat")("expand");Array.slice=function slice(a){return Array.prototype.slice.apply(a,Array.prototype.slice.call(arguments,1))};(function fn(a){if(a in Array){return fn}Array[a]=function(b){return Array.prototype[a].apply(b,Array.slice(arguments,1))};return fn})("diff")("intersect")("unique")("shuffle")("item")("has")("indexOf")("lastIndexOf")("every")("some")("filter")("forEach")("map")("reduce")("reduceRight")("join");String.prototype.repeat=function repeat(a,b){return Array.fill(typeof a==="number"?a:1,this).join(b||"")};String.prototype.expand=function expand(a){return this.repeat(Math.ceil(a/this.length)).substr(0,a)};String.prototype.toFirstUpperCase=function toFirstUpperCase(){return this.charAt(0).toUpperCase()+this.substr(1)};String.prototype.toFirstLowerCase=function toFirstLowerCase(){return this.charAt(0).toLowerCase()+this.substr(1)};String.prototype.toTitleCase=function toTitleCase(){return this.replace(/\w+/g,function(a){return a.toFirstUpperCase()})};String.prototype.startsWith=function startsWith(a){return this.substr(0,a.length)===a};String.prototype.endsWith=function endsWith(a){return this.substr(-a.length)===a};String.prototype.contains=function contains(a){return this.indexOf(a)>-1};String.prototype.numberOf=function numberOf(a,d){d=d||0;var b,e=0;while((b=this.indexOf(a,d))>=0){e++;d=b+a.length}return e};String.prototype.reverse=function reverse(){return this.split("").reverse().join("")};if(!String.prototype.trimLeft){String.prototype.trimLeft=function trimLeft(){return this.replace(/^\s\s*/,"")}}if(!String.prototype.trimRight){String.prototype.trimRight=function trimRight(){return this.replace(/\s*\s*$/,"")}}if(!String.prototype.trim){String.prototype.trim=function trim(){return this.trimLeft().trimRight()}}String.prototype.strip=function strip(c,b){if(!c){throw new TypeError("Stripping requires a list of characters to strip")}b=b||3;var c=Object.invert(typeof c==="string"?c.split(""):c);var d=0,a=this.length;if(b&1){while(this.charAt(d) in c){d++}}if(b&2){while(this.charAt(a-1) in c&&a>d){a--}}return this.substring(d,a)};String.prototype.stripLeft=function stripLeft(a){return this.strip(a,1)};String.prototype.stripRight=function stripRight(a){return this.strip(a,2)};String.prototype.pad=function pad(a,c){c=c||" ";var b=Math.max(0,a-this.length);return c.expand(Math.floor(b/2))+this+c.expand(Math.ceil(b/2))};String.prototype.padLeft=function padLeft(a,b){b=b||" ";return b.expand(Math.max(0,a-this.length))+this};String.prototype.padRight=function padRight(a,b){b=b||" ";return this+b.expand(Math.max(0,a-this.length))};String.prototype.partition=function partition(c){if(c instanceof RegExp){var a=this.match(a);var d=a?a.index:-1;c=a[0]}else{var d=this.indexOf(c)}var b=c.length;return d>-1?[this.substr(0,d),c,this.substr(d+b)]:[this.toString(),"",""]};String.prototype.partitionRight=function partitionRight(a){var b=this.lastIndexOf(a);return b>-1?[this.substr(0,b),a,this.substr(b+a.length)]:["","",this.toString()]};String.prototype.explode=function explode(b,a){if(!a){return this.split(b)}var d=this.split(b);var c=d.splice(0,a);if(d.length){var e=c.pop();c.push([e].concat(d).join(b))}return c};String.prototype.scan=function scan(b,e){var a,c=[];e=e||0;if(b.global){var d=this.substr(e);while(a=b.exec(d)){c.push(a.length>1?a.slice(1):a[0])}}else{while(a=this.substr(e).match(b)){e+=a.index+a[0].length;c.push(a.length>1?a.slice(1):a[0])}}return c};String.prototype.toCamelCase=function toCamelCase(){return this.replace(/[-_][a-z]/g,function(a){return a.charAt(1).toUpperCase()})};String.prototype.toUnderscore=function toUnderscore(){return this.replace(/[A-Z]/g,function(a){return"_"+a.toLowerCase()})};String.prototype.toDash=function toDash(){return this.replace(/[A-Z]/g,function(a){return"-"+a.toLowerCase()})};if(!Array.prototype.indexOf){Array.prototype.indexOf=function(b){var a=this.length>>>0;var c=Number(arguments[1])||0;c=(c<0)?Math.ceil(c):Math.floor(c);if(c<0){c+=a}for(;c<a;c++){if(c in this&&this[c]===b){return c}}return -1}}if(!Array.prototype.lastIndexOf){Array.prototype.lastIndexOf=function(b){var a=this.length;var c=Number(arguments[1]);if(isNaN(c)){c=a-1}else{c=(c<0)?Math.ceil(c):Math.floor(c);if(c<0){c+=a}else{if(c>=a){c=a-1}}}for(;c>-1;c--){if(c in this&&this[c]===b){return c}}return -1}}if(!Array.prototype.every){Array.prototype.every=function(b){var a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}var d=arguments[1];for(var c=0;c<a;c++){if(c in this&&!b.call(d,this[c],c,this)){return false}}return true}}if(!Array.prototype.filter){Array.prototype.filter=function(b){var a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}var e=new Array();var d=arguments[1];for(var c=0;c<a;c++){if(c in this){var f=this[c];if(b.call(d,f,c,this)){e.push(f)}}}return e}}if(!Array.prototype.forEach){Array.prototype.forEach=function(b){var a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}var d=arguments[1];for(var c=0;c<a;c++){if(c in this){b.call(d,this[c],c,this)}}}}if(!Array.prototype.map){Array.prototype.map=function(b){var a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}var e=new Array(a);var d=arguments[1];for(var c=0;c<a;c++){if(c in this){e[c]=b.call(d,this[c],c,this)}}return e}}if(!Array.prototype.some){Array.prototype.some=function(b){var d=0,a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}var c=arguments[1];for(;d<a;d++){if(d in this&&b.call(c,this[d],d,this)){return true}}return false}}if(!Array.prototype.reduce){Array.prototype.reduce=function(b){var a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}if(a==0&&arguments.length==1){throw new TypeError()}var c=0;if(arguments.length>=2){var d=arguments[1]}else{do{if(c in this){d=this[c++];break}if(++c>=a){throw new TypeError()}}while(true)}for(;c<a;c++){if(c in this){d=b.call(null,d,this[c],c,this)}}return d}}if(!Array.prototype.reduceRight){Array.prototype.reduceRight=function(b){var a=this.length>>>0;if(typeof b!="function"){throw new TypeError()}if(a==0&&arguments.length==1){throw new TypeError()}var c=a-1;if(arguments.length>=2){var d=arguments[1]}else{do{if(c in this){d=this[c--];break}if(--c<0){throw new TypeError()}}while(true)}for(;c>=0;c--){if(c in this){d=b.call(null,d,this[c],c,this)}}return d}}Array.fill=function fill(c,d){var a=new Array(Number(c||0));for(var b=a.length-1;b>=0;--b){a[b]=d}return a};Array.prototype.repeat=function repeat(a){return Array.prototype.concat.apply([],Array.fill(a,this))};Array.prototype.has=function has(a){return this.indexOf(a)>-1};Array.prototype.unique=function unique(){return this.filter(function(c,b,a){return a.indexOf(c)>=b})};Array.prototype.shuffle=function shuffle(){return this.sort(function(){return Math.random()>0.5?1:-1})};Array.prototype.item=function item(a){return this[a]};Array.prototype.remove=function remove(c,a){if(a===0){return}if(a===-1){a=Infinity}a=a||1;while(a){var b=this.indexOf(c);if(b<0){break}this.splice(b,1);a--}};Array.prototype.append=function append(a){return Array.prototype.push.apply(this,a)};Array.prototype.clear=function clear(){return this.splice(0,this.length)};Array.prototype.clean=function clean(a){return this.filter(function(b){if(b===undefined){return false}if(a!==false&&b===null){return false}if(a===true&&b===""){return false}return true})};Array.prototype.rand=function(){return this[Math.rand(0,this.length-1)]};Array.prototype.reduceNative=function reduceNative(a){return this.reduce(function(d,c){return a(d,c)})};Array.prototype.flat=function flat(){var a=[];function b(c){if(c instanceof Array){c.forEach(b)}else{a.push(c)}}this.forEach(b);return a};Array.prototype.diff=function diff(a){return this.filter(function(b){return !a.has(b)})};Array.prototype.intersect=function intersect(a){return this.filter(function(b){return a.has(b)})};if(!Object.getPrototypeOf){if(typeof"test".__proto__==="object"){Object.getPrototypeOf=function(a){return a.__proto__}}else{Object.getPrototypeOf=function(a){return a.constructor.prototype}}}(function(e){this.isString=function b(g){return typeof g==="string"||e.call(g)==="[object String]"};this.isNumber=function d(g){return typeof g==="number"||e.call(g)==="[object Number]"};this.isArray=function c(g){return e.call(g)==="[object Array]"};this.isObject=function a(g){return e.call(g)==="[object Object]"};this.isFunction=function f(g){return e.call(g)==="[object Function]"}}).call(this,Object.prototype.toString);
