$.fn.dissableSubmit = function(){
  this.each(function(){
    $(this).bind( 'submit', function(){
      $("input[type=submit]", this)
          .addClass( 'disabled')
          .attr( 'disabled', 'disabled')
    })
  });
}

$.fn.enableSubmit = function(){
  this.each(function(){
    $("input[type=submit]", this)
        .removeClass( 'disabled')
        .attr( 'disabled', false)
  });
}

$.fn.ajaxContent = function(){
  this.each(function(){
    $(this).css( 'position', 'relative');
    $(this).append( '<div class="saving_ajax_content"></div>');
  });
}
$.fn.ajaxContentRemove = function(){
  this.each(function(){
    $(this).css( 'position', 'relative');
    $(this).find('.saving_ajax_content').remove();
  });
}

$.fn.appendAjaxLoader = function(){
  this.each(function(){
    var el = $('<span>');
    $(el)
      .attr( 'class', 'ajax-loader');
          
    $(this).after(el);
    
    $(el).parent().css({
      'position': 'relative',
      'float': 'left'
    });
  });
}

$.fn.removeAjaxLoader = function(){
  this.each(function(){
    $('.ajax-loader', $(this).parent()).remove();
  });
}

$.fn.toggleCheckbox = function( options){
  var opts = $.extend( options, {
      reverse: false
  });
  
  function make( el) {
    if( $(el).attr( 'checked') == true) {
        if( opts.reverse !== false) {
          $(opts.el).show();
        } else {
          $(opts.el).hide();
        }
    } else {
        if( opts.reverse !== false) {
          $(opts.el).hide();
        } else {
          $(opts.el).show();
        }
    }
  }
  this.each(function(){
      var el = this;
      $(function(){
          make( el);
      })
      $(el).click(function(){
          make( el)
      })
  });
}

$.fn.submitSend = function( options){
  var opts = $.extend( options, {
    className: 'disabled',
    label: 'Espere...'
  });
  
  this.each(function(){
    if( $(this).attr( 'tagName') == 'A') {
      $(this).click(function(){
          $(this).attr( 'disabled', true);
          $(this).fadeTo( 1, .4);
          $(this).addClass( opts.className);
          $(this).html( opts.label);
          $(this).click(function(){return false});
          $(this).mouseover(function(){return false});
      })
    } else if( $(this).attr( 'tagName') == 'INPUT')
        var form = this.form;
        var el = this;
        $(form).submit(function(){
            $(el).attr( 'disabled', true);
            $(el).fadeTo( 1, .4);
            $(el).addClass( opts.className);
            $(el).val( opts.label);
        })
  });
}
