/**
 *  jquery.selector.attr.js
 * @author Florian Girault
 * @version 1.0
 * 
 * Small jQuery plugin to enable free testing on element 
 * attributes. Evaluates the expression in elt:attr(string).
 * Note the attributes with @attribute as usual when using
 * it in the expression...
 * Example of use:
 * 		$('p:attr( @id>2 && @id<4 )').hide(); would hide every paragraphs with 2<id<4...
 * 
 * You can compare strings with the following syntax:
 * 		$('p:attr( "@id">"B" )') would select every paragraphs id>B in alphabetical order...
 * 
 */
if (typeof JQUERY_SELECTOR_ATTR_JS_ == 'undefined') {//don't like including twice...
	
	var JQUERY_SELECTOR_ATTR_JS_ = '';
	
	if (typeof String.prototype.reverse == 'undefined') {
		//A simple reverse function by Shachi Bista
		String.prototype.reverse = function(){
			splitext = this.split("");
			revertext = splitext.reverse();
			reversed = revertext.join("");
			return reversed;
		}
	}
	
	jQuery.EvaluateAttrCond = function(a, i, m){
		try{
			
			var paramstring = m[3];
			var reg = /@[a-zA-Z0-9]*/;
			//Correct paramstring: jQuery parser doesn't take the '"' 
			//if you dont let a space between the ()...
			if (paramstring.search(/"/) > paramstring.search(/[a-zA-Z0-9]/)) {
				paramstring = '"' + paramstring;
			}
			paramstring = paramstring.reverse();
			if (paramstring.search(/"/) > paramstring.search(/[a-zA-Z0-9]/)) {
				paramstring = '"' + paramstring;
			}
			paramstring=paramstring.reverse();
			
			//Start the real fun
			var regex_results = reg.exec(paramstring, "g");
			var i = 0;
			while (regex_results != null && i < 10) {
				var attr = regex_results[0].substring(1, regex_results[0].length);
				var attr_val = a.getAttribute(attr);
				var tempregex = new RegExp(regex_results[0]);
				paramstring = paramstring.replace(tempregex, attr_val);
				i++;
				regex_results = reg.exec(paramstring, "g");
			}
			var result = eval(paramstring);
			return result;
			
		}catch (e){
			throw new Error('[:attr] string format error');
		}
	}
	
	jQuery.extend(jQuery.expr[":"], {
		attr: "jQuery.EvaluateAttrCond(a,i,m)"
	});
	
}//if (typeof JQUERY_SELECTOR_ATTR_JS_ == 'undefined')
