
/** 
	Extend de String, ajout de nouvelles methodes
*/
String.implement({
	// return the object pointed by the string ("object.object2.array[2]") or null
	eval : function(){
		return  eval(eval("'"+this+"'"));
	},
	
	// modification of the MooTools substitute method
	// now you could use a object syntaxt pointer ("object.object2.array[2]") in your template
	substitute : function(object, regexp){
		return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
			if (match.charAt(0) == '\\') return match.slice(1);
			if(/\./.test(match)){
				var path = match.replace(/[{}]/g,"").split(".");
				var name = path.getLast();
				var oSrc = object;
//				console.time("str")
				for(var i=0,l=path.length-1;i<l;i++){
					try{
						oSrc = oSrc[path[i]];
					}catch(e){}
				}
//				console.timeEnd("str")
				
				return (oSrc[name] != undefined) ? oSrc[name] : '';
			}
			
			return (object[name] != undefined) ? object[name] : '';
		})
	}
});


/** 
	Extend de Element, ajout de nouvelles methodes
*/
Element.implement({
	// renvoie la css reelement appliquee a l'element (useless ?)
	getCSS: function(sCssRule, btoInt) {
		var strValue = "";
		if(document.defaultView && document.defaultView.getComputedStyle) {
			try{
				strValue = document.defaultView.getComputedStyle(this, null).getPropertyValue(sCssRule);
			}
			catch(e) { strValue = ""; }
		}
		else if(this.currentStyle) {
			try{
				sCssRule = sCssRule.replace(/\-(\w)/g, function (strMatch, p1){
					return p1.toUpperCase();
				});
				strValue = this.currentStyle[sCssRule];
			} catch(e) {
				strValue = "";
			}
		}
		return btoInt ? parseInt(strValue) : strValue;
	},
	
	// permet de facilement recuperer un parametre passe dans l'attribut class sous la form param:'value'
	// si le param est omis, renvoie un hash de tous les param/value present
	// les quotes sont supprimees
	// <div class="msg:'du texte' pos:'first'" id="test"></div>
	// $("test").getCssParam('msg') --> "du texte" 
	// $("test").getCssParam() --> {"msg":"du texte", "pos":"first"} 
	getCssParam: function (param){
		var o = {}, sp;
		this.className.split(" ").each(function (el, i){
			if(/:/.test(el)) {
				sp = el.split(":");
				o[sp[0]] = sp[1].replace(/^'|'$/g, "");
			}
		})
		return param ? o[param] : o;
	},
	
	// pour un element de formulaire renvoie le label imbrique ou associe via l'attribut for, false si pas de label 
	getLabel : function (){ 
		if(!this.form) return false;
		// si element imbrique dans un <label>
		if(this.getParent('label')) return this.getParent('label');
		// 
		var labs = this.form.getElementsByTagName('label');
		for(var i = 0 ; i < labs.length; i++){
			var theFor = labs[i].getAttribute('for') || labs[i].getAttribute('htmlFor');
			if(theFor == this.id) {
				return labs[i];
			}
		}
		return false;
	},
    getLabelContent : function (){
        var lab = $(this).getLabel();
//        console.dir(lab);
        if(!lab){
            return false;
        }
        var oImg = $(lab).getElement("img");
        if(oImg){
            return oImg.get("alt");
        }
        else {
            return lab.innerHTML;
        }
    },

    test: function (fTest){
        return fTest(this);

    },

	// est-ce que l'element est fils de container?
	isChildrenOf: function(container) {
		if(!container) return;
		for(var n = this; n && n != container; n = n.parentNode);
		return n != null ? true : false;
	},
	
	// renvoie les "Hstyles"
	getHStyle : function(elm) {
		var hStyle = 
			this.getStyle("padding-left").toInt() +
			this.getStyle("padding-right").toInt() +
			this.getStyle("border-left-width").toInt() +
			this.getStyle("border-right-width").toInt();
		return F6.isQuirks ? 0 : hStyle;
	},
	
	// renvoie les "Vstyles"
	getVStyle : function() {
		var vStyle = 
			this.getStyle("padding-bottom").toInt() +
			this.getStyle("padding-top").toInt() +
			this.getStyle("border-top-width").toInt() +
			this.getStyle("border-bottom-width").toInt();
		return F6.isQuirks ? 0 : vStyle;
	},
	
	aria: function (){
		switch(arguments.length){
			case 1: // obj
				for(var r in arguments[0]){
					if(!/role|tabindex/.test(r)) attr = "aria-"+r;
					else attr = r;
					this.setAttribute(attr, arguments[0][r]);
				}
				break;
			case 2: // key, value
				if(!/role|tabindex/.test(arguments[0])) attr = "aria-"+arguments[0];
				else attr = arguments[0];
				this.setAttribute(attr, arguments[1]);
				break;
		}
		return this;
	}
});

/** 
	Extend de Array, ajout de nouvelles methodes
*/
Array.implement({
	// remplace un item d'un tableau cible par son index, par value, renvoie l'ancienne valeur
	swap: function (index, value){
		var $tmp = this[index];
		this[index] = value;
		return $tmp;
	}
});

Selectors.Pseudo.focusable = function(){
    var ok = /a|button|input|textarea|select/.test(this.tagName.toLowerCase());
    if(this.type && this.type == "hidden")
    {
        return false;
    }
    if($(this).getProperty("tabindex") == 0)
    {
        return true;
    }
    return ok;
};



