(function(jQuery) {

    /**
     *
     * Select Box Master and SLave Values
     *
     * @param Object options An object literal containing key/value pairs to provide optional attributes.
     *
     * inputMasterId - String - Id of the Master Input Field
     * inputSlaveId - String - Id of the Slave Input Field
     * selectValues - JSON Object with Master Slave select box definitions
     *
     *
     */


    jQuery.fn.selectDependency = function(options) {


        var defaults = {
            inputMasterId: 0,
            inputSlaveId: 0,
            selectValues: 0
        }

        /* Set options */
        var opts = jQuery.extend(defaults, options);

        /* Get Master and Slave dropdowns */
        $inputMaster = jQuery(this).find('#' + opts.inputMasterId);
        $inputSlave = jQuery(this).find('#' + opts.inputSlaveId);


        //return if inputs or JSON are not set or selection is empty
        // return if empty selection
        if (this.length == 0) return;
        // return if inputs are empty
        if ($inputMaster.length == 0 || $inputSlave.length == 0) return;
        opts.selectValues = eval(opts.selectValues)
        // return if JSON is undefined
        if (opts.selectValues == undefined) return;

        $selectMaster = $inputMaster.parent().find('select')
        $selectSlave = $inputSlave.parent().find('select')

        // Add all Master options to Select Box
        var selectedOptionM = 0;
        $selectMaster[0].options.length = 0;
        jQuery.each(opts.selectValues.masterItems, function(key, value) {
            addMaster(key, value);
        });


        // Add a single Master Value
        function addMaster(key, value) {
            /* add options to Master Select */
            var options = $selectMaster.attr('options');
            options[options.length] = new Option(key, value.mastervalue);
            if (value.selected) {
                selectedOptionM = options.length - 1;
            }

        }

        $selectMaster.change(function() {

            // set Values of Slave Box
            $selectSlave[0].options.length = 0;
            //$inputSlave.attr('value', inputSlaveIV);

            // Add all Options to Select Box
            var selectedText = jQuery(this).find('option:selected').attr('text');

            selectedOptionS = 0;
            jQuery.each(opts.selectValues.masterItems[selectedText].slaveItems, function(key, value) {
                addSlave(key, value);
            });

            // select option
            $selectSlave.find('option').eq(selectedOptionS).attr('selected', 'selected');
            $selectSlave.trigger('change');

        })

        //  Add a single Slave Value
        function addSlave(key, value) {
            // add options to Slave Select
            var options = $selectSlave.attr('options');
            options[options.length] = new Option(key, value.slavevalue);
            if (value.selected) {
                selectedOptionS = options.length - 1;
            }
        }

        // set initial Value
        $selectMaster.find('option').eq(selectedOptionM).attr('selected', 'selected');
        $selectMaster.trigger('change');

    }
})(jQuery);

