/*
 * jQuery cd_checkbox plugin
 *
 * Revision: $Id$
 * Version: 0.1
 * 
 */
 
 (function($) {
    $.fn.cd_checkbox = function(o) {
        return this.each(function() {
            new $cd_checkbox(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
    };

    /**
     * The cd_checkbox object.
     *
     * @constructor
     * @name $cd_checkbox
     * @param Object e The element to create the cd_checkbox for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/cd_checkbox
     */
    $.cd_checkbox = function(e, o) {
        this.options    = $.extend({}, defaults, o || {});
        
        this.checkbox   = $(e);
        
        var self = this;

        self.setup();
    };

    // Create shortcut for internal use
    var $cd_checkbox = $.cd_checkbox;

    $cd_checkbox.fn = $cd_checkbox.prototype = {
        cd_checkbox: '0.1'
    };

    $cd_checkbox.fn.extend = $cd_checkbox.extend = $.extend;

    $cd_checkbox.fn.extend({
        /**
         * Setups the cd_checkbox.
         *
         * @name setup
         * @type undefined
         * @cat Plugins/cd_checkbox
         */
        setup: function() {
            var self = this;
            
            // Hide the standard checkbox
            this.checkbox.hide();
            
            // Create a replacement cd_checkbox anchor
            var el = document.createElement('a');
            var ele = $(el);
            ele.addClass("cd_checkbox");
            ele.attr('href', '#');
            if( self.checkbox.is(":checked") == true ) {
                ele.addClass("checked");
            } else {
                ele.removeClass("checked");
            }
            ele.click(function() {
                self.checkbox.attr("checked", ((self.checkbox.is(":checked")) ? false : true));
                if( self.checkbox.is(":checked") == true ) {
                    $(this).addClass("checked");
                } else {
                    $(this).removeClass("checked");
                }
                return false;
            });
            
            // set the default state
            if( this.checkbox.checked == true ) {
                ele.addClass('checked');
            }
            
            // set the label action
            var label = $('label[for='+this.checkbox.attr('id')+']');
            if( label.length > 0 ) {
                label.click( function() {
                    ele.toggleClass('checked');
                } );
            }
            
            this.checkbox.parent().prepend(ele);
        }
    });

    $cd_checkbox.extend({
        /**
         * Gets/Sets the global default configuration properties.
         *
         * @name defaults
         * @descr Gets/Sets the global default configuration properties.
         * @type Hash
         * @param Hash d A set of key/value pairs to set as configuration properties.
         * @cat Plugins/cd_checkbox
         */
        defaults: function(d) {
            return $.extend(defaults, d || {});
        }
    });

})(jQuery);