﻿var elmHeight = "50"; // should be specified based on image size

// Extend JQuery Functionality For Custom Radio Button Functionality
jQuery.fn.extend({
    pimpMyForms: function() {
        // Initialize with initial load time control state
        $.each($(this), function() {
            var elm = $(this).children().get(0);
            elmType = $(elm).attr("type");
            $(this).data('type', elmType);
            $(this).data('checked', $(elm).attr("checked"));
            $(this).pmfClear();
        });
        $(this).mousedown(function() { $(this).pmfEffect(); });
        $(this).mouseup(function() { $(this).pmfHandle(); });
    },
    pmfClear: function() {
        if ($(this).data("checked") == true) {
            $(this).css("backgroundPosition", "0 -" + (elmHeight * 2) + "px");
        }
        else {
            $(this).css("backgroundPosition", "0 0");
        }
    },
    pmfEffect: function() {
        if ($(this).data("checked") == true)
            $(this).css({ backgroundPosition: "0 -" + (elmHeight * 3) + "px" });
        else
            $(this).css({ backgroundPosition: "0 -" + (elmHeight) + "px" });
    },
    pmfHandle: function() {
        var elm = $(this).children().get(0);
        if ($(this).data("checked") == true && $(this).data('type') != 'radio')
            $(elm).pmfUncheck(this);
        else
            $(elm).pmfCheck(this);

        if ($(this).data('type') == 'radio') {
            $.each($("input[name='" + $(elm).attr("name") + "']"), function() {
                if (elm != this)
                    $(this).pmfUncheck(-1);
            });
            doAffordabilityMagic();
        }

        if ($(this).data('type') == 'checkbox') {
            doAffordabilityMagic();
        }
    },
    pmfCheck: function(div) {
        $(this).attr("checked", true);
        $(div).data('checked', true).css({ backgroundPosition: "0 -" + (elmHeight * 2) + "px" });
    },
    pmfUncheck: function(div) {
        $(this).attr("checked", false);
        if (div != -1) {
            $(div).data('checked', false).css({ backgroundPosition: "0 0" });
        } else {
            $(this).parent().data("checked", false).css({ backgroundPosition: "0 0" });
        }
    }

});
