/*
	Stylus: JavaScript DOM or XPath ALTERNATIVE (almost CSSPath)
	Send your feedback to marc@palaueb.com (thanks!)
	
	Version: 0.6 Functional
	03/03/2007 (2007/03/03) Marc Palau - http://www.nbsp.es
	
	TODO:
		check: http://www.w3.org/TR/css3-selectors/#selectors
		
	Implementation:
		ELEMENT[NUMBER] and CLASS[NUMBER] and #IDElement Always returns ONE value
		
		ELEMENT and .CLASS allways return one Array
		
	BookMarkling the Script
		javascript:xtrctr=function(element){alert("FILL WITHOUT \\n nor \\r\n\nThis script return a single element or an array of elements.\n\nReturned value: "+element+"\n\nYou can do what you want with this elements!!");};scScript=document.createElement('script');scScript.src='http://www.javascript.es/apis/extractor.js';scScript.type='text/javascript';void(document.getElementsByTagName('head')[0].appendChild(scScript));
		
	
*/
var Stylus={
getElementsByClass:function(css,obj){
	var s = [];
	var els = obj.getElementsByTagName("*");
	for(var i=0,t=els.length;i<t;i++){
		var clase = els[i].className;
		if(clase == css){s[s.length]=els[i];}
	}
	return s;
},getObj:function(obj,que){
	var tFill;var element;
	var qq=que.indexOf("[");
	var u=que.charAt(0);
	if(qq!=-1){
		var num=que.substring(qq+1,que.length-1);que=que.substring(0,qq);
		tFill=true;
	}else{
		tFill=false;
	}
	
	if(u=="#"){//ELEMENT ID
		que=que.substring(1,que.length);
		element = obj.getElementById(que);
	}else if(u=="."){//CLASS
		que=que.substring(1,que.length);
		element=Stylus.c2a(Stylus.getElementsByClass(que,obj));
	}else{//TAGNAME
		//IMPLEMENTAR CLASS EN TAG (li.pepe);
		element=Stylus.c2a(obj.getElementsByTagName(que));
	}
	if(tFill){
		if(Stylus.isco(element)){var elemx=Stylus.c2a(element.getElementsByTagName(que));
		}else{var elemx = element;}
		element = elemx[num];
	}
	return element;
},extract:function(ex,obj){
	if(!obj){obj=document;}
	var inici;var fi;
	var es=ex.indexOf(" ");
	var pare=(es!="-1");
	inici=ex.substring(0,es);fi=ex.substring(es+1,ex.length);
	if(inici=="")inici=fi;
	if(pare){
		robj=Stylus.getObj(obj,inici);return Stylus.extract(fi,robj);
	}else{
		var robj=Stylus.getObj(obj,inici);
		if(Stylus.isco(robj)){robj=Stylus.c2a(robj);}
		return robj;
	}
},isco:function(q){
	if(!q)return false;
	return !q.tagName&&!q.push;
},c2a:function(colection){
	var sortida=[];
	for(var i=0,tcol=colection.length;i<tcol;i++){sortida[sortida.length]=colection[i];}
	return sortida;
}}