(function ($) {
    $.fn.imgCheckBox = function (options) {
        var defaults = {
            checkedImage: null,
            unCheckedImage: null,
            imageClass: null,
            labelClass: null,
            labelMouseOverClass: null
        };
        var options = $.extend(defaults, options);
        $("<img/>", { src: options.checkedImage });
        $("<img/>", { src: options.unCheckedImage });

        if ($.browser.msie && parseInt(jQuery.browser.version) < 8) { return; }

        $(this).each(function () {
            var chkBox = $(this);
            var imageURL = chkBox.is(':checked') ? options.checkedImage : options.unCheckedImage;
            var img = $("<img/>").attr("src", imageURL).attr("class", options.imageClass);
            var lbl;

            var idValue = chkBox.attr("id");
            if (idValue != null) {
                img.attr("id", idValue);
                lbl = $("label[for=" + chkBox.attr("id") + "]");
                chkBox.removeAttr("id");
            }

            var container = $("<div/>").css("display", "inline-block");
            if (lbl) {
                $(lbl).attr("class", options.labelClass);
                if (options.labelMouseOverClass != null) {
                    $(lbl).mouseover(function () {
                        $(this).addClass(options.labelMouseOverClass);
                    });
                    $(lbl).mouseout(function () {
                        $(this).removeClass(options.labelMouseOverClass);
                    });
                }
            }

            img.click(function () {
                if (chkBox.attr("checked")) {
                    img.attr("src", options.unCheckedImage);
                    img.attr("checked", "");
                }
                else {
                    img.attr("src", options.checkedImage);
                    img.attr("checked", "checked");
                }

                if (chkBox.click) {
                    chkBox.click();
                }

                return false;
            });

            $(lbl).click(function () {
                img.click();
                return false;
            });

            chkBox.after(container);
            $(img).appendTo(container);

            $(this).hide();
        });
    };

    $.fn.setValue = function (options) {
        var defaults = { isChecked: false, force: false };
        var options = $.extend(defaults, options);
        $(this).each(function () {
            var isChecked = options.isChecked;
            var force = options.force;

            if ((!force) && ($(this).is(':checked') == isChecked)) {
                return false;
            }

            if (force && ($(this).is(':checked') == isChecked)) {
                $(this).click();
            }

            $(this).click();
            $(this).attr("checked", isChecked);
        });
    };
})(jQuery);
