/*
This work is licensed under Creative Commons GNU GPL License
http://creativecommons.org/licenses/GPL/2.0/
and based on code by Russel Lindsay www.weetbixthecat.com
Copyright Kostik Naumov www.piterpen.net
(c) 20070108
*/

Array.prototype._sortPrep = function ( field ) {
	if (!this.maximum) {
		this.maximum = this[0][field];
		var i = this.length;
		while(i--) {
			if (this.maximum < this[i][field]) {this.maximum = this[i][field];}
		}
	}
	if (this.maximum) {
		var fill = new Array();
		var length = this.maximum.toString().length;
		var i = length; var zs = "";
		while (i--) {
			fill.push(zs); zs += "0";
		}
		for (var i=0; i < this.length; i++ )
			if (this[i][field]!=undefined) {
				this[i][field] = (this[i][field].toString().length < length ? fill[length - this[i][field].toString().length] : "") + this[i][field];
			}
	}
}

function alphaSort(a,b) {
	var A = String.prototype.toLowerCase.apply(a);
	var B = String.prototype.toLowerCase.apply(b);
	if (A.substring(0,2)=='a ') A=A.substring(2,A.length);
	if (B.substring(0,2)=='a ') B=B.substring(2,A.length);
	if (A.substring(0,2)=='( ') A=A.substring(2,A.length);
	if (B.substring(0,2)=='( ') B=B.substring(2,A.length);
	if (A.substring(0,2)=='" ') A=A.substring(2,A.length);
	if (B.substring(0,2)=='" ') B=B.substring(2,A.length);
	if (A.substring(0,2)=="' ") A=A.substring(2,A.length);
	if (B.substring(0,2)=="' ") B=B.substring(2,A.length);
	if (A.substring(0,3)=='an ') A=A.substring(3,A.length);
	if (B.substring(0,3)=='an ') B=B.substring(3,A.length);
	if (A.substring(0,4)=='the ') A=A.substring(4,A.length);
	if (B.substring(0,5)=='the ') B=B.substring(4,A.length);
	if (A < B){
	   return -1;
	}else if (A > B){
	  return  1;
	}else{
	  return 0;
	}
}

Array.prototype._sortIntAsc = function(field) {
	this._sortPrep(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype._sortIntDesc = function(field) {
	this._sortPrep(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	this.reverse();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype.sortAsc = function(field) {
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort(alphaSort);
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype.sortDesc = function(field) {
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort(alphaSort);
	this.reverse();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}


Array.prototype._parseInt = function(field) {
	for (var i=0; i < this.length; i++ ) {
		this[i][field] = this[i][field]*1;
	}
}

Array.prototype.sortIntAsc = function(field) {
	this._sortIntAsc(field);
	this._parseInt(field);
}

Array.prototype.sortIntDesc = function(field) {
	this._sortIntDesc(field);
	this._parseInt(field);
}
