/* Extensions to native JS objects
--------------------------------------------------------------------------*/
Object.extend(String.prototype, {
	/**
	 * Extends the .substring() method to allow negative numbers to
	 * reference indices from the end of the string rather than the beginning.
	 */
	substring: function(start, end) {
		if (start < 0) start = this.length + start;
		if (end < 0) end = this.length + end;
		if (end !== 0 && !end) end = this.length;
		var newString = '';
		for (var ssi=start; ssi<end; ssi++) {
			newString += this.charAt(ssi);
		}
		return newString;
	},

	/**
	 * Converts special characters to their HTML entities
	 */
	 htmlEntities: function() {
		var chars = {
			'&': 'amp',	'<': 'lt', '>': 'gt', '\"': 'quot'
		};

		var newString = this;
		for (var chacacter in chars) {
			var regExp = new RegExp(chacacter, 'g');
			newString = newString.replace(regExp, '&'+chars[chacacter]+';');
		}
		return newString;
	},

	/**
	 * Get Url Argument
	 * Turns a URL into an argument for links which function
	 * purely as event launchers.
	 */
	getUrlArgument: function() {
		// Parses a URL like "javascript:void(42);" and returns "42"
		if (val = this.match(/[Jj]avascript:void\(\'?(.*?)\'?\);?/i)) {
			if (val[1]) return val[1];
		}
		return this;
	},

	/**
	 * Trims a string
	 */
	trim: function() {
		return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
	},

	/**
	 * Is Numeric
	 * checks if a string is a number
	 */
	isNumeric: function () {
		return !this.match(/\D/);
	}
});

Object.extend(Form.Element, {
	setValue: function(inputElement, newValue) {
		inputElement = $(inputElement);
		switch(inputElement.type) {
			case 'text':
			case 'password':
				inputElement.value = newValue;
			break;
			case 'select':
			case 'select-one':
				for (var x=0; x<inputElement.options.length; x++) {
					if (inputElement.options[x].value == newValue) {
						inputElement.selectedIndex = x;
						return true;
					}
				}
				return false;
			break;
			case 'checkbox':
				inputElement.checked = bool(newValue);
			break;
			default:
				alert('setValue() can\'t yet handle input type: '+inputElement.type);
				return false;
			break;
		}
		return true;
	}
});

Object.extend(Number.prototype, {
	/**
	 * Pads a number with zeroes until it is the desired length (digits)
	 * (Caution: Returns a string, not a float, out of necessity)
	 * Example: (7).zeroPad(3) == '007'
	 */
	zeroPad: function(digits) {
		var str = this.toString();
		while (str.length < digits) {
			str = '0'+str;
		}
		return str;
	}
});

Object.extend(Array.prototype, {
	/**
	 * Deletes any occurrences of needle from Array
	 * Not recommended for use on multidimensional arrays.
	 */
	deleteVals: function(needle) {
		newValue = new Array();
		if (this.length > 0) {
			for (var n=0; n<this.length; n++) {
				if (this[n] != needle) newValue.push(this[n]);
			}
		}
		return newValue;
	},

	/**
	 * Weeds out duplicate values in an array.
	 * Not recommended for use on multidimensional arrays.
	 */
	unique: function() {
		newArray = new Array();
		for (key in this) {
			if (typeof(this[key]) != 'function') {
				if (newArray.inArray(this[key]) == -1) newArray.push(this[key]);
			}
		}
		return newArray;
	},

	/**
	 * In Array
	 * Finds a value in an array
	 */
	inArray: function(needle) {
		if (this.length > 0) {
			for (var n=0; n<this.length; n++) {
				if (this[n] == needle) return n;
			}
		}
		return -1;
	}
});



/* Patches to Prototype
--------------------------------------------------------------------------*/
Object.extend(Position, {
	distanceBetween: function(coords0, coords1) {
		// Determines the straight distance between two points.
		// coords1 = [x: 1, y: 1]
		// or
		// coords1 = [1,1]
		if (coords0 instanceof Array) {
			var x0=coords0[0];
			var y0=coords0[1];
		} else {
			var x0=coords0.x;
			var y0=coords0.y;
		}
		if (coords1 instanceof Array) {
			var x1=coords1[0];
			var y1=coords1[1];
		} else {
			var x1=coords1.x;
			var y1=coords1.y;
		}
		return Math.sqrt((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1))
	}
});

Object.extend(Object, {
	extendProperties: function(originalObject, extendingObject) {
		// Tries to intelligently extend an object's properties based
		// on direction given in the property names of the extending
		// object.
		for (var originalKeyName in extendingObject) {
			var modifier = originalKeyName.substring(0,1);
			var keyName = originalKeyName.substring(1);
			if (extendingObject[originalKeyName] instanceof Array) {
				// - Prune: Remove from it
				// + Merge: Add to it
				// ! Overwrite: Replace it entirely
				switch(modifier) {
					case '-':
						for (var x=0; x<extendingObject[originalKeyName].length; x++) {
							originalObject[keyName] = originalObject[keyName].deleteVals(extendingObject[originalKeyName][x]);
						}
					break;
					case '+':
						originalObject[keyName] = originalObject[keyName].concat(extendingObject[originalKeyName]);
					break;
					default:
						keyName = originalKeyName; // No valid modifier, so we need to restore the wrongly clipped keyName
					case '!':
						originalObject[keyName] = extendingObject[originalKeyName];
					break;
				}
			} else if (extendingObject[originalKeyName] instanceof Object) {
				// - Prune: Remove from it
				// + Merge: Add to it or overwrite preexisting conflicting values.
				// ! Overwrite: Replace it entirely
				switch(modifier) {
					case '-':
						for (var key in extendingObject[originalKeyName]) {
							if (typeof(Object.prototype[key]) == 'undefined') {
								delete(originalObject[keyName][key]);
							}
						}
					break;
					case '+':
						Object.extend(originalObject[keyName], extendingObject[originalKeyName]);
					break;
					default:
						keyName = originalKeyName; // No valid modifier, so we need to restore the wrongly clipped keyName
					case '!':
						originalObject[keyName] = extendingObject[originalKeyName];
					break;
				}
			} else {
				originalObject[originalKeyName] = extendingObject[originalKeyName];
			}
		}
	}
});

Object.extend(Element, {
	/**
	 * Tons faster and simpler than Prototype's
	 */
	hasClassName: function(element, className) {
		return Element.manipulateClass(element, className, 'find');
	},

	addClassName: function(element, className) {
		return Element.manipulateClass(element, className, 'add');
	},

	removeClassName: function(element, className) {
		return Element.manipulateClass(element, className, 'remove');
	},

	clearClassNames: function(element) {
		element.className = '';
	},

	manipulateClass: function(element, className, action) {
		//var classes = element.getAttribute('className');
		var classes = element.className;
		var cNames = [];
		if (classes != null) {
			cNames = classes.split(' ');
			if (action == 'remove') {
				cNames = cNames.deleteVals(className)
			} else {
				for (var x=0; x<cNames.length; x++) {
					if (cNames[x] == className) return (action == 'find');
				}
			}
		}
		if (action == 'add') {
			if (typeof(className) == 'array') {
				cNames = cNames.concat(className);
			} else {
				cNames.push(className);
			}
		}
		if (action == 'add' || action == 'remove') {
			//element.setAttribute('className', cNames.join(' ').trim());
			element.className = cNames.join(' ').trim();
		}
		if (action == 'find') return false;
	},

	/**
	 * Element.visible
	 * By Kramer
	 *
	 * I was disappointed that Prototype's Element.visible really only
	 * reports on the status of element.style.display. What I really
	 * wanted to know was "can the user see this element, or is it
	 * contained inside a hidden element?" This extension fixes that with
	 * the new "recursive" option.
	 *
	 * Recursive option added to seek up the tree to make sure
	 * all parent nodes are visible, too. This should effectively
	 * return a true/false indicating whether the given element is
	 * indeed VISIBLE TO THE USER.
	 */
	visible: function(element, recursive) {
		if (!recursive) return $(element).style.display != 'none';
		var search = element;
		while (search = search.parentNode) {
			if ($(search).style.display == 'none') return false;
		}
		return true;
	}
});

Event._observe = Event.observe;
Object.extend(Event, {
	/**
	 * Returns the keycode related to an event, browser-independent
	 */
	keyCode: function(event) {
		if (typeof(event.which) == 'undefined') return event.keyCode;
		return Math.max(event.which, event.keyCode);
	},

	/**
	 * Event.observe now returns an ID which can be sent to Event.stopObserving to terminate that event handler.
	 */
	registry: [],

	id: 0,

	observe: function(element, name, observer, useCapture) {
		Event.id++;
		var regEntry = {
			id: Event.id,
			element: element,
			name: name,
			observer: observer,
			useCapture: useCapture
		};
		Event.registry.push(regEntry);
		Event._observe(element, name, observer, useCapture);
		return regEntry;
	},

	unObserve: function(regEntry) {
		for (var x=0; x<Event.registry.length; x++) {
			var a = Event.registry[x];
			if (a.id == regEntry.id) {
				Event.registry.splice(x, 1);
				Event.stopObserving(a.element, a.name, a.observer, a.useCapture);
				return true;
			}
		}
		return false;
	},

	fire: function(el, name) {
		for(var x=0; x<Event.registry.length; x++){
			if(Event.registry[x].element == el && Event.registry[x].name == name){
				Event.registry[x].observer();
			}
		}
	}
});


/* Plain old functions
--------------------------------------------------------------------------*/
document.getElementsBySelector = function(selectors, withinNodes) {
	if (!(selectors instanceof Array)) selectors = [selectors];
	if (typeof(withinNodes) == 'undefined') {
		var withinNodes = [document.getElementsByTagName('body')[0]];
	} else if (!(withinNodes instanceof Array)) {
		var withinNodes = [withinNodes];
	}
	var resultNodes = [];
	for (var cSel=0; cSel<selectors.length; cSel++) {
		for (var cNodes=0; cNodes<withinNodes.length; cNodes++) {
			var resultNodes = resultNodes.concat(Element.getElementsBySelector(withinNodes[cNodes], selectors[cSel]));
		}
	}
	return resultNodes.unique();
};

function getViewportCenter() {
	return {
		x: getViewportSize().width/2,
		y: getViewportSize().height/2
	};
}
function getViewportSize() {
	return {
		width: self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth),
		height: self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight)
	};
}

/**
 * Changes pretty much any variable into its boolean equivalent
 * Strings like yes, no, true, false, y, n
 * Numbers like 0, 1, 2...
 * @return bool
 */
function bool(arg) {
	// Returns true or false
	// Does everything concievable to make arg into a boolean value
	if (typeof(arg) == 'undefined') return false;
	switch(arg.toLowerCase()) {
		case 'yes':
		case 'true':
		case 'y':
			return true;
		break;
		case 'false':
		case 'no':
		case 'n':
			return false;
		break;
		default:
			if (arg === true) return true;
			if (arg === false) return false;
			if (parseInt(arg) > 0) return true;
			if (parseInt(arg) == 0) return false;
		break;
	}
	return null; // Inconclusive
}

/**
 * Converts a UNIX timestamp into a JS Date object
 */
function unixToDate(unixtime) {
	var time = new Date();
	time.setTime(unixtime*1000);
	return time;
}

/**
 * For-each within an object. Recursive.
 **/
function foreach(obj, func) {
	for (var key in obj) {
		if (obj[key] instanceof Object) {
			foreach(obj[key], func);
		}
		func(obj[key]);
	}
}

/**
 * Executes the given command within a different thread
 **/
function newThread(toExec) {
	setTimeout(toExec, 1);
}
