jQuery.threeStates = {
  config : {image:'/js/3_check.jpg',width : 20,	height: 20	},
  getValue : function(obj)
  {
    if (isNaN($('input',obj).val()))
      return 0;
    else
      return parseInt($('input',obj).val());
  },

  setValue : function(obj,newVal)
  {
    $('input',obj).val(newVal);
    $('input',obj).parent().css('background-position','0 -'+(newVal * jQuery.threeStates.config.height)+'px');
  }
  
};

/*jQuery.fn.threeStates = function ()
{
  return this.each(function() {
                         $(this).wrap("<span></span>");
                         $(this).after("<input type='hidden' name='"+$(this).attr('name')+"' value='"+$(this).val()+"' />");
                         
                         owner = $(this).parent();
                         owner.click(function() {
                           jQuery.threeStates.setValue(this,(jQuery.threeStates.getValue(this)+1) % 3);
                           });
                         owner.css({'width':config.width,'height':config.height,'float':'left','cursor':'pointer','background-image':'url('+config.image+')','background-repeat':'no-repeat'});
                         owner.addClass('threeStates');
                         $(this).remove();
                         
                         jQuery.threeStates.setValue(this,jQuery.threeStates.getValue(this));                     
                       });
          
};*/
        
(function($){
        $.fn.threeStates = function(){
          var config = jQuery.threeStates.config;

          getValue = function(obj)
          {
            if (isNaN($('input',obj).val()))
              return 0;
            else
              return parseInt($('input',obj).val());
          };

          setValue = function(obj,newVal)
          {
            $('input',obj).val(newVal);
            $('input',obj).parent().css('background-position','0 -'+(newVal * config.height)+'px');
          };
          
          this.each(function() {
                                 $(this).wrap("<span></span>");
                                 $(this).after("&nbsp;<input type='hidden' name='"+$(this).attr('name')+"' value='"+$(this).val()+"' />");
                                 
                                 owner = $(this).parent();
                                 owner.click(function() {
                                  switch(getValue(this))
                                  {
                                    case 0:setValue(this,1);break;
                                    case 1:setValue(this,0);break;
                                    case 2:setValue(this,1);break;
                                  }
                                   //setValue(this,(getValue(this)+1) % 3);
                                   });
                                 owner.css({'width':config.width,'height':config.height,'cursor':'pointer','background-image':'url('+config.image+')','background-repeat':'no-repeat','line-height':'20px','margin-top': '1px'});
                                 owner.addClass('threeStates');
                                 $(this).remove();
                                 
                                 setValue(this,getValue(this));
                                 
                               });
          
        };
})(jQuery)

