(function(){  

  var VOZ = {
            
        elems:[],
            
        
//Verify when the DOM is ready.
//Callback is the anonymous function to execute when the dom is ready
domReady:function(callback){
  var done = false; //Create a variable called done with false as value;
  //Checking every 10 milliseconds if the document.body and document.getElementById are ready to work with them
  //If the they are ready to work then we change done to true;
  var checkLoaded = setInterval(function(){ if(document.body && document.getElementById){done = true;}},10);
  //Checking every 10 milliseconds if done == true
  //If it is true then execute the callback
  var checkInter = setInterval(function(){if(done){clearInterval(checkLoaded);clearInterval(checkInter);callback();}},10);
},
                
//Get Elements By Id -------------------- SI
getById:function(){
  var elems = [];
  for(var i = 0;i<arguments.length;i++){
      if(typeof arguments[i] === 'string'){
           elems.push(document.getElementById(arguments[i]));
      }
  }
  this.elems = elems;
  return this;
},

//Get Elements By Name
getByName:function(name){
  var elems = [];
  for(var i = 0;i<arguments.length;i++){
      if(typeof arguments[i] === 'string'){
         var e = document.getElementsByName(arguments[i]);
         for(var j=0;j<e.length;j++){
             elems.push(e[j]);
          }
     }
  }
  this.elems = elems;
  return this;
},

//Get Elements By TagName
getByTag:function(){
  var elems = [];
  for(var i = 0;i<arguments.length;i++){
      if(typeof arguments[i] === 'string'){
         var e = document.getElementsByTagName(arguments[i]);
         for(var j=0;j<e.length;j++){
             elems.push(e[j]);
         }
      }
  }
  this.elems = elems;
  return this;
},

//Get Elements By ClassName
getByClass:function(name,type,parent){
  var elems = [];
  var pattern = new RegExp("(^| )" + name + "( |$)");
  var e = (parent || document).getElementsByTagName(type || '*')
  for(var i=0;i<e.length;i++){
      if(pattern.test(e[i].className)){
         elems.push(e[i]);
      }
  }
  this.elems = elems;
  return this;
},

//Check Or Uncheck the elements
checked:function(bol){
  for(var i=0;i<this.elems.length;i++){
      if(this.elems[i].nodeName.toLowerCase()==='input' && (this.elems[i].getAttribute('type') == 'checkbox' || this.elems[i].getAttribute('type') == 'radio')){
         this.elems[i].checked = bol;
      }
  }
  return this;
},

//Return the value of the elements or an array with all the values found
//This function does not chain
getValue:function(){
  var elemstemp = [];//Create a temporary array to save the elements found
  for(var i=0;i<this.elems.length;i++){//Walk through all the elements cheking for their value
     if(this.elems[i].getAttribute('type') == 'checkbox' || this.elems[i].getAttribute('type') == 'radio'){
            //If the form element is checkbox or an radio we verify if it is checked
           //If it is true then we save the value
            if(this.elems[i].checked === true){
            elemstemp.push(this.elems[i].value);
            }
     } else{
            elemstemp.push(this.elems[i].value);
     }
            
   }
     //elemstemp.push(if(this.elems[i].getAttribute)this.elems[i].value); //Adding the value into the temporary Array
  if(elemstemp.length === 1){ //If the temporary array just have one element then we return like a single value
     return elemstemp[0];
  }
  return elemstemp; //Here we return the temporary array with all the values found
},

//Set value for the elements found
//Val is the value for the elements found
setValue:function(val){
  for(var i=0;i<this.elems.length;i++){ //Walk through all the elements adding a value
      //If the form element is checkbox or an radio we verify if val == elem.value
      //If it is true then checked it
       if(this.elems[i].getAttribute('type') == 'checkbox' || this.elems[i].getAttribute('type') == 'radio'){
            if(this.elems[i].value === val){
              this.elems[i].checked = true;             
            }
  }
  else{
       this.elems[i].value = val; //Set the val to value attribute of the element
  }
  }
  return this;////Return this in order to chain
},

//Add a className to our elements
addClass:function(name){
  for(var i = 0;i<this.elems.length;i++){
      this.elems[i].className += ' ' + name;
  }
  return this;
},

on:function(action, callback){
  if(this.elems[0].addEventListener){
     for(var i = 0;i<this.elems.length;i++){
      this.elems[i].addEventListener(action,callback,false);    
     }
  }
  else if(this.elems[0].attachEvent){
     for(var i = 0;i<this.elems.length;i++){
         this.elems[i].attachEvent('on'+action,callback);
     }
  }
  return this;
},


appendText:function(text){
  text = document.createTextNode(text);
  for(var i = 0;i<this.elems.length;i++){
      this.elems[i].appendChild(text);
  }
  return this;
},

//Updated Method setStyle
//Now it takes an object as parameter
//e.g. var mystyle ={color:'red',background:'black'};
//     .setStyle(mystyle) OR we can passit directly AS .setStyle({color:'red',background:'black'});
//We should know that the name of the styles are as used in javascript
//e.g. background-color is backgroundColor, margin-left is marginLeft, etc.
setStyle:function(objStyle){
  for(var i = 0;i<this.elems.length;i++){//Going across all the elements and add them all the styles 
      for(var k in objStyle){ //Walk the ObjStyle Object using te property name as style name and the value as the style value
          //E. g. {top:20px} is elem.top = 20px;
          this.elems[i].style[k] = objStyle[k];    
      }    
  }
  return this; //Return this to chaing
},

//SetOpacity
setOpacity: function(level){
  for(var i = 0;i<this.elems.length;i++){
     if(this.elems[0].filters){
        this.elems[i].style.filter='alpha(opacity='+level+')';
     }
     else{
         this.elems[i].style.opacity=level/100;
     }
  }
  return this;//Return this to chaing
},


//Fade Out Method
//Using the Helpers Object
//Time is in milliseconds  e.g. 8 secons = 8000
fadeOut:function(time){
  for(var i=0;i<this.elems.length;i++){//Going across all the elements and execute FadeIn In the Helpers Object
          Helpers.fadeOut(this.elems[i],time)
  }
  return this;//Return this to chaing
},

//Fade In Method
//Using the Helpers Object
//Time is in milliseconds  e.g. 8 secons = 8000
fadeIn:function(time){
  for(var i=0;i<this.elems.length;i++){ 
          Helpers.fadeIn(this.elems[i],time)
  }
  return this;//Return this to chaing
},


//Show/Hide an element
toggleHide:function(){
  this.elem.style['display'] = (this.elem.style['display']==='none' || '') ?'block':'none';
  return this;
},

//Odd the found values based by their index
odd:function(){
  var myodd = [];
  for(var i=1;i<this.elems.length;i+=2){
      myodd.push(this.elems[i]);
  }
  this.elems = myodd;
  return this;
},

//Even the found values based in their index
even:function(){
  var myeven = [];
  for(var i=0;i<this.elems.length;i+=2){
      myeven.push(this.elems[i]);
  }
  this.elems = myeven;
  return this;  
  }

}


var Helpers={
//Method to set the opacity of the element
 //It is almost the same than setOpacity
//elem is the element affected by the opacity
//level de amount of opacity, the percent: the values are between 0 and 100
setOpa:function(elem,level){
                if(level>=0 && level<=100){//The opacity should be between 0 and 100
                    elem.style.opacity = (level/100); //Set The opacity for Firefox, Safari, Opera, Chrome,etc.
                    elem.style.filter = 'alpha(opacity='+level+')'; //Set opacity for IE :(
                }
            },
//Method to create the Fade Out effect
//Using the method setOpa
//elem is the element affected by the effect
//time is the duration of the effect
fadeOut:function(elem,time){
  var level = 100; //Set the level at 100
  var interval = setInterval(function(){ //Create an interval using the setOpa method
       Helpers.setOpa(elem,--level); //Using seOpa Method decrementing level by one
       if(level==0){ //If level has the value of 0 then stop the interval
          clearInterval(interval);
       }
  },time/100);
},

//Method to create the Fade In effect
//Using the method setOpa
//elem is the element affected by the effect
//time is the duration of the effect
fadeIn:function(elem,time){
  var level = 0;//Set the level at 0
  var interval = setInterval(function(){//Create an interval using the setOpa method
       Helpers.setOpa(elem,++level);  //Using seOpa Method decrementing level by one
       if(level==100){//If level has the value of 100 then stop the interval
          clearInterval(interval);
       }
   },time/100);
}

}

window.$$ = VOZ;
})();
