﻿/*
 * jQuery Bowers & Wilkins checkbox switch plug-in 0.1
 *
 */

(function($) {
    $(document).ready(function() {
        $.fn.makeSwitch = function() {

            //for each checkbox in array of elements
            this.each(function() {
                var element = this;
                if (element.checked == true) { //if checkbox is checked then set the control to On else set it to Off
                    $(this).replaceWith('<span id="' + element.id + 'Switch" class="switchControl" title="Click to switch on/off">On<\/span><input type="hidden" id="' + element.id + '" name="' + element.id + '" value="On" />');
                } else {
                    $(this).replaceWith('<span id="' + element.id + 'Switch" class="switchControl off" title="Click to switch on/off">Off<\/span><input type="hidden" id="' + element.id + '" name="' + element.id + '" />');
                }
            });

            //Click event for control to change value of hidden field and the controls appearance
            $('span.switchControl').click(function(e) {
                var self = $(this);

                self.toggleClass('off');
                (self.text() === 'On') ? self.text('Off').next().attr('value', '') : self.text('On').next().attr('value', 'On');
               
               // if (document.getElementById("grillSwitch") != null) {
                   // document.getElementById("frmgrill").submit();
                  //}

                return false;
            });

        };
    });
})(jQuery);

