/******************************************************************************/
/* Some Plugins to extend JQuery for LMDB Administration App                  */
/******************************************************************************/

$(function(){
  
  /**
   * Switcher on/off a appliquer sur les checkboxes
   * @param {Object} settings
   */
  $.fn.switcher = function(settings) {
    
    var config = {
      on : '/img/online.png',
      off : '/img/offline.png'
    } ;
    
    if(settings) $.extend(config,settings) ;
    
    this.each(function(){
      
      if(this.type=="checkbox" && $(this).is(":visible"))
      {
        var checkbox = this ;
        $(checkbox).hide() ;
        
        var img = $("<img src='"+(checkbox.checked?config.on:config.off)+"' />") ;
        img.bind("click",function(){
          checkbox.checked = !checkbox.checked ;
          this.src=(checkbox.checked?config.on:config.off) ;  
        })
        
        $(this).after(img) ;  
      }
    })
    
    return this ;
  }
  /**
   * Fin Switcher
   */
  
  /**
   * Checked on/off a appliquer sur les checkboxes
   * @param {Object} settings
   */
  $.fn.checked = function(checked) {
    
    if(arguments.length==0) checked = true ;
    
    this.each(function(){
      
      if(this.type=="checkbox")
      {
        this.checked = checked ;
      }
    })
    
    return this ;
  }
  /**
   * Fin Checked
   */
  
  /**
   * Toggle Checked on/off a appliquer sur les checkboxes
   * @param {Object} settings
   */
  $.fn.toggleChecked = function() {
    
    this.each(function(){
      
      if(this.type=="checkbox")
      {
        this.checked = !this.checked ;
      }
    })
    
    return this ;
  }
  /**
   * Fin Toggle
   */
  
  /**
   * Disabled on/off a appliquer sur les inputs
   * @param {Object} settings
   */
  $.fn.disabled = function(disabled) {
    
    if(arguments.length==0) disabled = true ;
    
    this.each(function(){
      var tagName = this.tagName.toLowerCase() ; 
      if(tagName=="input"||tagName=="select"||tagName=="textarea")
      {
        this.disabled = (disabled) ? "disabled" : "" ;
      }
    })
    
    return this ;
  }
  /**
   * Fin Disabled
   */
})

