(function($) {
    $.fn.dscustomcheck = function(opt) {
        var opts = $.extend({}, $.fn.dscustomcheck.defaults, opt);
        var r = $;

        this.each(function() {            
            if (this._jqueryDsCustomCheck != null) {
                r = this._jqueryDsCustomCheck;
                return false;
            }

            if (opts.className == null) {
                return true;
            }            
            var tag = this.tagName.toLowerCase();            
            if (tag == 'input' && (this.type == 'checkbox' || this.type == 'radio')) {                
                this._jqueryDsCustomCheck = new CustomCheck(this, opts);
            }
        });

        return r;
    }

    $.fn.dscustomcheck.defaults = {
        width: null,
        height: null,
        className: null
    };

    var CustomCheck = function(element, opt) {        
        this.update = function() {
            var pos;            
            if ($element.attr('checked') == true) {                
                pos = positions.checked;
            } else {                
                pos = positions.unchecked;
            }
            
            if (!$element.attr('disabled')) {
                if (status == 'hover') {
                    pos = pos.hover;
                } else if (status == 'pushed') {
                    pos = pos.pushed;
                } else {
                    pos = pos.enabled;
                }                
            } else {
                pos = pos.disabled;                
            }
            
            $span.css('background-position', '0 -' + pos + 'px');
        }

        
        var _this = this;
        var $element = $(element);

        var width = opt.width;
        var height = opt.height;
        var status = 'default';
        var type = element.type;

        $element.hide();

        var $span = $element.before('<span></span>').prev('span');

        $span.addClass(opt.className);
        $span.hover(function() {
            status = 'hover';
            _this.update();
        });

        $span.mousedown(function() {            
            status = 'pushed';
            _this.update();           
        });

        $span.mouseup(function() {
            $element.click();
            //var changed = true;
            if (type == 'checkbox') {
                //$element.attr('checked', !$element.attr('checked'));
                _this.update();

                $element.change();
            } else { //radio                
                /*if ($element.attr('checked')) {
                    changed = false;
                } else {*/
                    //$element.attr('checked', true);
                    $('input[name="'+$element.attr('name')+'"]').each(function() {
                        $(this).dscustomcheck().update();
                    });
               // }
            }

            
            /*if (changed) {
                $element.change();
            }*/
        });        

        if (width == null) {
            width = $span.width();
        }

        if (height == null) {
            height = $span.height();
        }

        var positions = {
            'unchecked': {
                'enabled': 0,
                'disabled': height,
                'hover': height*2,
                'pushed': height*3
            },
            'checked': {
                'enabled': height*4,
                'disabled': height*5,
                'hover': height*6,
                'pushed': height*7
            }
        };

        this.update();
    };
})(jQuery);