/*
 * NiceForm - jQuery plugin 0.0.1
 *
 * Copyright (c) 2009 Marcin Malessa
 *
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 *
 */

;(function($) {

  $.fn.extend({
    nfield: function(x,options){
      options = jQuery.extend({
	x: 11,
	y: 10
	},options);
      return this.each(function() {
        var div=document.createElement('div');
        if($(this).css('float')=='none') $(div).css({'float':'left'});
        else  $(div).css({'float':$(this).css('float')});
        if($(this).css('display')) $(div).css({'display':$(this).css('display')});
        if($(this).css('visibility')) $(div).css({'visibility':$(this).css('visibility')});
        if($(this).css('mainwidth')) $(div).css({'width':$(this).css('mainwidth')});
        $(this).before(div);
        $(this).appendTo(div);
        $(document.createElement('br')).appendTo(div);
        if($(this).attr('title'))
          $(document.createElement('label')).text($(this).attr('title')).attr({'class':'nf-label'}).appendTo(div);

        if($(this).attr('readonly')) $(this).attr('class','nf-field-readonly');
        else $(this).attr('class','nf-field');
      });
    },


    nbutton2: function(options){
      return this.each(function() {
        new $.nbuttons2(this,options);
      });
    }
  });


  $.nbuttons2 = function(obj,options){
    options = jQuery.extend({
	state:0,
        onclick:(options && typeof(options.onclick)=='function')?options.onclick:function(){return true;}
	},options);
    var state=options.state;

    $(obj).click(function(){
      toggle_state(); 
      options.onclick();
    });

    function set_state(){
      if(state==1) $(obj).attr({'class':'nf-btt-on'});
      else $(obj).attr({'class':'nf-btt-off'});
    }

    function toggle_state(){
      state=state==1?0:1;
      set_state();
    }
  }
})(jQuery);
