/**
 * Checkbox
 * 
 * Manages a checkbox
 * 
 * @require     MooTools
 * 
 * @author      Steffen Maechtel <s.maechtel@netzbewegung.com>
 * @version     1.00 alpha
 */

var Nb_Form_Checkbox = new Class({
    initialize: function(element, options) {
        options = options || {};
        
        this.original = {};
        this.original.checkbox = element;
        
        this.checked = this.getChecked();
        
        // hide orginal radios
        this.hideOriginal();
        
        // create custom radios
        this.custom = this.getCustom();
        
        // add events
        this.addEvents();
    },
    getCustom: function()
    {
        /*
         *  Example:
         *  <div class="usercontrol-checkbox"></div>
         */
        
        var custom = {};

        
        custom.checkbox = new Element('div', {'class': this.original.checkbox.get('class')});
            
        if (this.checked)
        {
            custom.checkbox.addClass('checkbox-checked');
        }
        
        this.original.checkbox.grab(custom.checkbox, 'after');
        
        return custom;
    },
    addEvents: function()
    {
        this.custom.checkbox.addEvent('click', this.onClick.create({
            bind: this,
            event: true
        }));
    },
    onClick: function(event)
    {
        event.stop();
        
        if (this.checked == true)
        {
            this.checked = false;   
            this.original.checkbox.checked = false;
            this.custom.checkbox.removeClass('checkbox-checked');
        }
        else
        {
            this.checked = true;
            this.original.checkbox.checked = true;
            this.custom.checkbox.addClass('checkbox-checked');
        }
    },
    getChecked: function()
    {
        if (this.original.checkbox.checked)
        {
            return true;
        }
        else
        {
            return false;
        }

    },
    hideOriginal: function()
    {
        this.original.checkbox.setStyle('display', 'none');
    }
});