// JavaScript Document
function $(selector){
  if(window==this)
    return new $(selector);
  
  //Creating the DOM element array
  this.obj=new Array();
  
  //Selector is already a DOM element
  if(!selector.substr){
    this.obj[0]=selector;
    return this;
  }
  
  this.selector_type=selector.substr(0,1);
  //Id
  if(this.selector_type=="#"){
    this.obj.push(document.getElementById(selector.substr(1)));
  }
  //Class
  else if(this.selector_type=="."){
    var sel=selector.substr(1);
    if(!document.getElementsByClassName){
      var all=document.getElementsByTagName("*");
      var max=all.length;
      for(var i=0;i<max;i++){
        var cl=all[i].className;
        if(cl.indexOf(" ")==-1){
          if(cl==sel){
            this.obj.push(all[i]);
          }
        }
        else{
          cl=cl.split(" ");
          var cl_max=cl.length;
          for(var j=0;j<cl_max;j++){
            if(cl[j]==sel){
              this.obj.push(all[i]);
            }
          }
        }
      }
    }
    else
      this.obj=document.getElementsByClassName(sel);
  }
  //Html
  else this.obj=document.getElementsByTagName(selector);
  return this;
}

$.prototype.append=function(html){
  var d=document.createElement("div");
  d.innerHTML=html;
  var max=d.childNodes.length;
  for(var i=0;i<max;i++){
    for(var j=this.obj.length;j>0;){
      j--;
      this.obj[j].appendChild(d.cloneNode(true).childNodes[i]);
    }
  }
  return this;
}
$.prototype.html=function(html){
  if(html!=undefined){
    for(var j=this.obj.length;j>0;){
        j--;
        this.obj[j].innerHTML=html;
    }
    return this;
  }
  else return this.obj[0].innerHTML;
}
$.prototype.prepend=function(html){
  var d=document.createElement("div");
  d.innerHTML=html;
  for(var j=this.obj.length;j>0;){
            j--; 
    if(!this.obj[j].firstChild){
      var max=d.childNodes.length;
      for(var i=0;i<max;i++){
        this.obj[j].appendChild(d.childNodes[i]);
      }
    }
    else{
      for(var i=d.childNodes.length-1;i>=0;i--){

          this.obj[j].insertBefore(d.childNodes[i],this.obj[j].firstChild);
      }
    }
  }
  return this;
}
$.ajax=function(page,param,callback){
if(!callback && typeof param =="function")
  callback=param;
  var xhr,p="",m;
  var that=this;
  if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
      // Évite un bug du navigateur Safari :
      if (xhr.overrideMimeType) {
        xhr.overrideMimeType("text/xml");
      }
 
    } else {
 
      if (window.ActiveXObject) {
   
        try { // essaie de charger l'objet pour IE
          xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try { // essaie de charger l'objet pour une autre version IE
           xhr = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
            window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest.");
          } // try-catch
        } // try-catch
      }
 
    } // if-else
		
		
		m="GET";
		if (typeof param == "object"){
			m="POST";
			for(i in param)
				p += (p?"&":"")+ i+"="+encodeURIComponent(param[i]);
		}
		else
			p=param;
		xhr.open(m,page,true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 && xhr.status == 200 && callback)
				callback.call(that,xhr);  			    
			}
		xhr.send(p);
}


$.prototype.load=function(page,param,callback){
	var that=this;
  if(!callback)
    callback=false;
  $.ajax(page,param,function(xhr){
    var max = that.obj.length;
    var target;
    for(var j=0;j<max;j++){
      target = that.obj[j];
      target.innerHTML=xhr.responseText;
          }
      var scripts=target.getElementsByTagName("script")
      for(var i=0;i<scripts.length;++i){
        if(scripts[i].src){
          var s=document.createElement("script");
          s.src=scripts[i].src;
          document.body.appendChild(s);
            continue;
        }
        var s=scripts[i];
        if (s.text){
            eval(s.text);
        }
      }
      if(callback!=false){
        callback.call(that,xhr);
      }
});
  return this;
}
$.prototype.show=function(){
  for(var j=this.obj.length;j>0;){
      j--;
      this.obj[j].style.display="";
  }
  return this;
}
$.prototype.hide=function(){
  for(var j=this.obj.length;j>0;){
      j--;
      this.obj[j].style.display="none";
  }
  return this;
}

$.prototype.toggle=function(){
  for(var j=this.obj.length;j>0;){
      j--;
      if(this.obj[j].style.display=="none"){
        this.obj[j].style.display="";
      }
      else{
        this.obj[j].style.display="none";
      }
  }
  return this;
}

$.prototype.css=function(prop,value){
  if(!value){
    var res=new Array();
    for(var j=this.obj.length;j>0;){
      j--;
      res[j]=this.obj[j].style[$.camelize(prop)];
    }
    if(j==0) return res[j];
    return res;
  }
  for(var j=this.obj.length;j>0;){
      j--;
      this.obj[j].style[$.camelize(prop)]=value;
  }
  this.prop=prop;
  return this;
}
$.prototype.val=function(value){
  if(!value){
    var res=new Array();
    for(var j=this.obj.length;j>0;){
      j--;
      res[j]=this.obj[j].value;
    }
    if(j==0) return res[j];
    return res;
  }
  for(var j=this.obj.length;j>0;){
      j--;
      this.obj[j].value=value;
  }
  return this;
}
$.prototype.attr=function(type,value){
  if(!value){
    var res=new Array();
    for(var j=this.obj.length;j>0;){
      j--;
      res[j]=this.obj[j]. getAttribute(type,"true");
    }
    if(j==0) return res[j];
    return res;
  }
  for(var j=this.obj.length;j>0;){
      j--;
      this.obj[j].setAttribute(type,value,"true");
  }
  return this;
}
$.prototype.css_anim=function(prop){
  for(var j=this.obj.length;j>0;){
      j--;
      var actual_pos=this.obj[j].style[$.camelize(prop)];
      document.title=actual_pos;
      if(actual_pos==this.finalvalue)
        clearInterval(this.efx);
        
      this.obj[j].style[$.camelize(prop)]=(parseInt(actual_pos.replace("px",""))+this.step)+"px";
  }
  return this;
}
$.prototype.animate=function(prop,value1,value2,time){
  this.css(prop,value1);
  var tick=20;   
  var nb_tick=time/tick;
  this.step=(value2-value1)/nb_tick;
  this.finalvalue=value2+"px";
  var that=this;
  this.efx=setInterval(function(){that.css_anim.call(that,prop)},tick);
  return this;
}
$.prototype.each=function(func){
  that=this;
  for(var j=this.obj.length;j>0;){
      j--;
      func.call(this.obj[j]);
  }
  return this;
}
$.camelize=function(str){
  str=str.split("-");
  var le=str.length;
  var out=str[0];
  for (var i=1;i<le;i++){
      out+=str[i].substr(0,1).toUpperCase()+str[i].substr(1)
  }
  return out;
}
$.prototype.bind=function(event,func){
  for(var j=this.obj.length;j>0;){
      j--;
      $.registerEventListener(this.obj[j],event,func);
  }
  return this;
}
$.prototype.remove=function(){
  for(var j=this.obj.length;j>0;){
      j--;
      if (this.obj[j].parentNode)
				this.obj[j].parentNode.removeChild( this.obj[j] );
  }
  return this;
}
$.prototype.dump=function(){
  var out;
  this.each(function(){
    out+=this.tagName;
  });
  alert(out);
}
$.prototype.focus=function(func){
  if(func!=undefined){
    //Bind focus event on object
    for(var j=this.obj.length;j>0;){
        j--;
        $.registerEventListener(this.obj[j],"focus",func);
    }
  }
  else{
    //Get focus on first element
    this.obj[0].focus();
  }
  return this;
}
$.prototype.blur=function(func){
  if(func!=undefined){
    //Bind focus event on object
    for(var j=this.obj.length;j>0;){
        j--;
        $.registerEventListener(this.obj[j],"blur",func);
    }
  }
  else{
    //Get focus on first element
    this.obj[0].blur();
  }
  return this;
}
$.prototype.click=function(func){
  if(func!=undefined){
    //Bind click event on object
    for(var j=this.obj.length;j>0;){
        j--;
        $.registerEventListener(this.obj[j],"click",func);
    }
  }
  else{
    //Get click on first element
    this.obj[0].click();
  }
  return this;
}
$.prototype.keyup=function(func){
  if(func!=undefined){
    //Bind click event on object
    for(var j=this.obj.length;j>0;){
        j--;
        $.registerEventListener(this.obj[j],"keyup",func);
    }
  }
  else{
    //Get keyup on first element
    this.obj[0].keyup();
  }
  return this;
}
// cross browser function for registering event handlers
$.registerEventListener=function (elem, event, func) {
    if (elem.addEventListener) {
        elem.addEventListener(event, func, false);
        return true;
    } else if (elem.attachEvent) {
        var result = elem.attachEvent("on"+event, func);
        return result;
    }
    // maybe we could implement something with an array
    return false;
}

function verifMail(mail){
  	var place = mail.indexOf("@",1);
  	var point = mail.indexOf(".",place+1);
  	if ((place > -1)&&(mail.length >2)&&(point > 1)){
  		return(true);
  	}
  	else{
  		return false;
  	}
}
