 /*
 /// jQuery RadioGroup plugin
 /// jQuery is required !!!
 ///
 /// Copyright (c) 2009 Tom Gröger, TomSoft
 /// http://www.tomsoft.de/
 ///
 /// Version 1.40 - 20.12.2009
 /// Licensed under MIT License
 /// http://en.wikipedia.org/wiki/MIT_License
 ///
  */

  ////////////////////// RadioGroup //////////////////////////
  

  
    $.fn.RadioGroup = function() {
        /// <summary>
        /// JQuery plugin that creates a TomSoft.Form.RadioGroup object and 
        /// attaches this to the Wrapper of the RadioGroup-Container.
        /// <returns type="jQuery" />

        if (this.length != 1) {
            throw new Error("bad selector for RadioGroup " + this.selector,
            								 "window.document");
            return this;
        }

        var items = [];
        _init(this);

        // 
        // JQuery object extensions
        //
        this.val = function(value) {
            /// <summary>
            /// Returns the value of the checked radiobutton within the Group 
            /// or checks the radiobutton with the given value. if the value 
            /// does not exist then the radiogroup remains unchecked
            /// </summary>

            if (arguments.length) {
                value = value.toString();   // setter
                for (var i = 0; i < items.length; ++i) {
                    var j = items[i];
                    j.checked(false);
                    if (value == j.val()) {
                        j.checked(true);
                        break;
                    };
                };
                return this;
            }
            // getter
            var j = this.selected();
            return ((j) ? j.val() : -1);
        };

        this.enable = function(bEnable) {
            /// <summary>
            /// enable/disable RadioGroup and associated Labels
            /// </summary>    
            bEnable = (typeof (bEnable) == 'boolean') ? bEnable : true;
            for (var i = 0; i < items.length; ++i) {
                items[i].enable(bEnable);
            }
            return this;
        };

        this.disable = function() {
            /// <summary>
            /// if called without arguments the method disables all RadioButton
            /// and associated Labels. If numeric arguments are passed then 
            /// all RadioButtons of the group are enabled except those with 
            /// matching the index position.
            /// </summary>
            /// <example>
            ///     $Controls.radioGroupHerkunft.disable(1,4,5); 
            /// </example>    
            if (arguments.length) {
                for (var i = 0; i < items.length; ++i) {
                    // check if pos i is in the argument list to be disabled
                    var bEnable = ($.inArray(i, arguments) == -1);
                    items[i].enable(bEnable);
                };
                return this;
            };
            return this.enable(false);
        };

        this.selected = function() {
            /// <summary>
            /// Returns the checked radiobutton within the Group 
            /// </summary>
            for (var i = 0; i < items.length; ++i) {
                if (items[i].checked()) {
                    return items[i];
                };
            };
            return null;
        };

        this.get = function(index) {
            /// <summary>
            /// Returns modified RadioButton Object by position or ID(s):
            ///
            /// radio.get( 0 )          returns first jRadioLabel
            /// radio.get( 0,2,3 )      returns an Array of jRadioLabels in Pos 0,2 and 3
            ///                         usage: $.each( radio.get(0,..), 
            ///                                   function(){this.disable()})
            /// radio.get("checked")    return selected jRadioLabel
            /// radio.get("all")        return list array of all RadioButtons
            /// </summary>

            var list = [];

            for (var cx = 0; cx < arguments.length; ++cx) {
                var arg = arguments[cx];
                if (typeof (arg) == 'string') {
                    if (arg == "checked")   return this.selected();
                    if (arg == "all")       return items;

                    for (var i = 0; i < items.length; ++i) {
                        if (items[i].radio.id == arg)
                            list.push(items[i]);
                    };
                }
                else if (arg >= 0 && arg < items.length) {
                    list.push(items[arg]);
                };
            };
            if (list.length)
                return (list.length == 1) ? list[0] : list;

            return;
        };

        return this;

        //
        // Evaluates a callback over each item and returns the
        // item if the callback-result is true
        //
        function _init(jObj) {

            var jLabel = $("label", jObj);
            var jRadio = $(":radio", jObj);
            for (var i = 0; i < jRadio.size(); ++i) {
                items.push(_jRadioLabel(jRadio.get(i), jLabel.get(i)));
            };
        };

        //
        // Creates a modifies jQuery Object-Wrapper for the given Radio/Label pair
        // The jQuery radio Object has some added methods and properties:
        //     .radio:          returns the dom-RadioButton Object
        //     .label           returns the jQuery-Label Object
        //     .checked:        gets/sets the checkded state of the RadioButton
        //
        function _jRadioLabel(radio, label) {

            var jRadio = $(radio);

            jRadio.radio = radio;
            jRadio.label = $(label);

            if (jRadio.label.size() == 1)
                label.htmlFor = radio.id;

            jRadio.checked = function(bChecked) {
                /// <summary>
                /// gets or sets the checkded state of the RadioButton
                /// </summary>    
                ///	<returns type="boolean">checked state</returns>
                if (arguments.length) {
                    bChecked = (typeof (bChecked) == 'boolean') ? bChecked : true;
                    radio.checked = bChecked; // setter
                    return jRadio;
                };
                return radio.checked;  // getter
            };

            jRadio.enable = function(bEnable) {
                /// <summary>
                /// enable/disable RadioButton and associated Label
                /// </summary>    
                bEnable = (typeof (bEnable) == 'boolean') ? bEnable : true;
                radio.disabled = !bEnable;
                jRadio.label.css("color", bEnable ? "" : "#ccc")
                if (!bEnable) radio.checked = false;
                return jRadio;
            };

            jRadio.disable = function() {
                /// <summary>
                /// diables  RadioButton and associated Label
                /// </summary>    
                return jRadio.enable(false);
            };

            return jRadio;
        };
    };

