
/**
* Common utilities for Javascript required by more advanced 
*
* @prerequisite jQuery.js
*
* @author Websell Masters Ltd
* @copyright Websell Masters Ltd
* @date 17/08/09
*/

/**
* jQuery enhancement
* //search through text, used for getting suggestions
*/
$.expr[':'].suggestion = function(a,i,m){
	return $(a).text().toUpperCase().indexOf(m[3].toUpperCase())==0;
};


/**
* Shrink an array (left side and optionally right side)
* @param start index
* @param end index
* @return trimmed array
*/
Array.prototype.shrink = function(start, end) {
	if (this.length==0) {
		return [];
	}
	
	if (end === undefined) {
		end = this.length;
	}
	
	end = Math.min(end, this.length);
	if (start>end || end>this.length) {
		return [];
	} else {
		var result = new Array(); 
		
		for(var j=0,i=start;i<end;i++,j++) {
			result[j] = this[i];
		}
		
		return result;
	}
}

/**
* Search for a term (ignoring case and only considering A-Z)
* Compensates for html tags and skips contents
* @param term to search for
* @return index of found term
*/
Array.prototype.find = function(term) {
	if (term === undefined || term=="") {
		return -1;
	}

	for (var i=0;i<this.length;i++) {
		if (this[i].toUpperCase().startsWith(term.toUpperCase())) {
			return i;
		}
	}
	
	return -1;
}

/**
* Search for a term (ignoring case and only considering A-Z)
* Compensates for html tags and skips contents
* @param term to search for
* @return index of found term
*/
String.prototype.startsWith = function(str) {
	return (this.match("^" + str)==str);
}
