// Image Tick Box for iPostcodes
// replace a checkbox with a image version without losing the functionality
// License: http://www.gnu.org/licenses/lgpl.txt
// Homepage: http://blog.leenix.co.uk/2009/10/jquery-plugin-imagetickbox-change-any.html
//
// Version 1.01 (iPostcodes)
// All checkboxes MUST HAVE unique id attribute

jQuery.fn.imageTickBox = function(options) {

    //Default text to change submit button too
    var settings = jQuery.extend({
        tickedImage: null,
        unTickedImage: null,
        imageClass: null,
        exclusiveCheckboxContainer: null
    }, options);

    //preload images
    jQuery("<img>").attr("src", settings.tickedImage);
    jQuery("<img>").attr("src", settings.unTickedImage);

    jQuery(this).each(function() {

        var imageTickBoxUnquieId = jQuery(this).attr("id");
        var imageURL;
        if (jQuery(this).attr("checked")) { imageURL = settings.tickedImage; } else { imageURL = settings.unTickedImage; }

        jQuery(this).hide().before("<img id='tickboxImage" + imageTickBoxUnquieId + "' src='" + imageURL + "' class='" + settings.imageClass + "'>");

        jQuery("#tickboxImage" + imageTickBoxUnquieId).click(function() {

            // disabled checkboxes must be processed corectly
            var disabled = jQuery(this).next().attr("disabled");
            if (disabled) {
                return false;
            }

            //saves current state
            var checked = jQuery(this).next().attr("checked");

            if (settings.exclusiveCheckboxContainer != null) {
                if (!checked) {
                    //enforce radio button behaviour
                    if (settings.exclusiveCheckboxContainer != null) {
                        $(settings.exclusiveCheckboxContainer + " :checkbox").filter(function() {
                            return this.id != $(this).next().id;
                        }).attr("checked", false).removeClass("checked").prev().attr("src", settings.unTickedImage);
                    }
                    // is not ticked, so tick
                    jQuery(this).attr("src", settings.tickedImage);
                    jQuery(this).next().addClass("checked").click();
                }
            } else {
                if (checked) {
                    // is ticked, so untick
                    jQuery(this).attr("src", settings.unTickedImage);
                    jQuery(this).next().removeClass("checked").click();
                } else {
                    // is not ticked, so tick
                    jQuery(this).attr("src", settings.tickedImage);
                    jQuery(this).next().addClass("checked").click();

                }
            }

            return false;
        });
    });
};

function radioButtonClick(btnId) {
    $('#tickboxImage' + btnId).click();
    $('#' + btnId).attr('checked', true); //Just in case
}
function getRadioButtonImpl(btnId) {
    return $('#tickboxImage' + btnId);
}

function initCheckboxes() {
    $("input:checkbox.checkBox").imageTickBox({
        tickedImage: "/Content/Images/Icons/Checkbox Checked.png",
        unTickedImage: "/Content/Images/Icons/Checkbox Unchecked.png",
        imageClass: "tickbox"
    });
}

function initCheckboxesForClass(className) {
    $("input:checkbox." + className).imageTickBox({
        tickedImage: "/Content/Images/Icons/Checkbox Checked.png",
        unTickedImage: "/Content/Images/Icons/Checkbox Unchecked.png",
        imageClass: "tickbox"
    });
}

function initRadioButtons(groupName) {
    var groupId = "#" + groupName;
    $(groupId + " input:checkbox.radioBox").imageTickBox({
        tickedImage: "/Content/Images/Icons/Checkbox Checked.png",
        unTickedImage: "/Content/Images/Icons/Checkbox Unchecked.png",
        imageClass: "tickbox",
        exclusiveCheckboxContainer: groupId
    });
}

function clearCheckboxes(groupName) {   
    var btnId;
    $('input:checkbox[name="' + groupName + '"]').each(function (index) {
        btnId = $(this).attr('id');        
        if ($('#' + btnId).hasClass("checked")) {            
            $('#tickboxImage' + btnId).click();
        }
    });
}
