(function ($) {
    jQuery.fn.extfillme = function (options) {
        var opts = $.extend({}, $.fn.extfillme.defaults, options);
        var $this = $(this);
        
        return this.each(function (i, e) {
            var $t = jQuery(this);
            
            $(opts.master_elem).bind('change', function (event) {
                if (jQuery.inArray($(this).val(), opts.zap_value) != -1) {
                    if ($t.is('input')) {
                        $t.val("");
                    }
                    else {
                        $t.empty();
                    }
                }
                switch (opts.fill_with) {
                case 'value':
                    //debug('VALUE'); 
                    //if we want To Not Fire When Specific value
                    if (jQuery.inArray($(this).val(), opts.value_to_skip) == -1) {
                        _self_fill($t, $(this).val());
                    }
                    break;
                case 'text':
                    //debug('TEXT');
                    if (jQuery.inArray($(this).find('option:selected').text(), opts.value_to_skip) == -1) {
                        _self_fill($t, $(this).find('option:selected').text());
                    }
                    break;
                case 'ajax':
                    //debug('AJAX');
                    if (jQuery.inArray($(this).val(), opts.value_to_skip) == -1) {
                        _ajax_action(opts, $(this), $t);
                    }
                    break;
                default:
                    //code to be executed if n is different from case 1 and 2
					alert('Choose One of - "value","text" or "ajax" ');
                }
            });
        })
    }
    //defaults
    jQuery.fn.extfillme.defaults = {
        master_elem: '.master',
        fill_with: 'value',        
        value_to_skip: [],
        zap_value: [],
        ajaxFile: 'default.php',
        progress_image: 'images/indicator.gif',
        progress_indicator: true
    };
    //private part


    function _self_fill(self, to_fill) {
        if (self.is('input')) {
            self.val(to_fill);
        }
        else {
            self.html(to_fill);
        }
    }

    function _self_clear(self) {
        if (self.is('input')) {
            self.val("");
        }
        else {
            self.empty();
        }
    }
    function _ajax_action(opts, master, self) {
        
        $.ajax({
            async: true,
            url: opts.ajaxFile,
            //dataType: "json",
            data: "Mvalue=" + master.val() + "&Mtext=" + master.find('option:selected').text(),
            beforeSend: function () {
                //debug('beforeSend AJAX');
                if (opts.progress_indicator == true) {
                    _bussy_box_show(self, opts.progress_image);
                }
            }, success: function (msg) {
                //alert('AJAX success : '+msg);
                self.html(msg);
            }, error: function (msg) {
                // debug(msg.responseText);
                debug('AJAX error' + msg.responseText);
                self.html(msg.responseText);
            }, complete: function (msg) {
                // Handle the complete event
                if (opts.progress_indicator == true) {
                    _bussy_box_hide(self, opts.progress_image);
                }
                //debug(msg);
            }
        });
    }
    function _bussy_box_show(self, progress_image) {
        self.hide();
        self.after('<img id="loading" style="text-align:center;vertical-align:top;background-color: transparent;" src="' + progress_image + '">');
    }
    function _bussy_box_hide(self, progress_image) {
        self.next("img#loading").remove();
        self.show();
    }

    function _load_missing_js($file_name) {
        $.getScript($file_name, function () {
            return true;
        });
    }
    function debug($obj) {
        if (window.console && window.console.log) {
            //			window.console.log('ajaxcheckbox debug: Start');
            window.console.log($obj);
            //			window.console.log('ajaxcheckbox debug: End');
        }
        else alert($obj);
    };
})(jQuery);
