/* Cross-Browser Split v0.1; MIT-style license
By Steven Levithan <http://stevenlevithan.com>
An ECMA-compliant, uniform cross-browser split method */

var goodSplit = false;
var testElements="a-a".split(/(-)/);
if (testElements.length == 3 && testElements[1] == "-") goodSplit = true;

if (!goodSplit) {
String.prototype.split = function(separator, limit) {
	var flags = "";
	
	/* Behavior for separator: If it's...
	- Undefined: Return an array containing one element consisting of the entire string
	- A regexp or string: Use it
	- Anything else: Convert it to a string, then use it */
	if (separator === undefined) {
		return [this.toString()]; // toString is used because the typeof this is object
	} else if (separator === null || separator.constructor !== RegExp) {
		// Convert to a regex with escaped metacharacters
		separator = new RegExp(String(separator).replace(/[.*+?^${}()|[\]\/\\]/g, "\\$&"), "g");
	} else {
		flags = separator.toString().replace(/^[\S\s]+\//, "");
		if (!separator.global) {
			separator = new RegExp(separator.source, "g" + flags);
		}
	}
	
	// Used for the IE non-participating capturing group fix
	var separator2 = new RegExp("^" + separator.source + "$", flags);
	
	/* Behavior for limit: If it's...
	- Undefined: No limit
	- Zero: Return an empty array
	- A positive number: Use limit after dropping any decimal value (if it's then zero, return an empty array)
	- A negative number: No limit, same as if limit is undefined
	- A type/value which can be converted to a number: Convert, then use the above rules
	- A type/value which cannot be converted to a number: Return an empty array */
	if (limit === undefined || +limit < 0) {
		limit = false;
	} else {
		limit = Math.floor(+limit);
		if (!limit) return []; // NaN and 0 (the values which will trigger the condition here) are both falsy
	}
	
	var match,
		output = [],
		lastLastIndex = 0,
		i = 0;
	
	while ((limit ? i++ <= limit : true) && (match = separator.exec(this))) {
		// Fix IE's infinite-loop-resistant but incorrect RegExp.lastIndex
		if ((match[0].length === 0) && (separator.lastIndex > match.index)) {
			separator.lastIndex--;
		}
		
		if (separator.lastIndex > lastLastIndex) {
			/* Fix IE to return undefined for non-participating capturing groups (NPCGs). Although IE
			incorrectly uses empty strings for NPCGs with the exec method, it uses undefined for NPCGs
			with the replace method. Conversely, Firefox incorrectly uses empty strings for NPCGs with
			the replace and split methods, but uses undefined with the exec method. Crazy! */
			if (match.length > 1) {
				match[0].replace(separator2, function(){
					for (var j = 1; j < arguments.length - 2; j++){
						if (arguments[j] === undefined) match[j] = undefined;
					}
				});
			}
			
			output = output.concat(this.substring(lastLastIndex, match.index), (match.index === this.length ? [] : match.slice(1)));
			lastLastIndex = separator.lastIndex;
		}
		
		if (match[0].length === 0) {
			separator.lastIndex++;
		}
	}
	
	return (lastLastIndex === this.length)
		? (separator.test("") ? output : output.concat(""))
		: (limit ? output : output.concat(this.substring(lastLastIndex)));
};
}
