(function ($) 
	{
    $.fn.selectChain = function (options) 
	{
        var defaults = {
            key: "id",
            value: "label"
        };
        
        var settings = $.extend({}, defaults, options);
        
        if (!(settings.target instanceof $)) settings.target = $(settings.target);
        
        return this.each(function () 
			{				
            var $$ = $(this);
            
            $$.change(function () 
			{
                var data = null;
                if (typeof settings.data == 'string') 
				{
                    data = settings.data + '&' + this.name + '=' + $$.val();
                } 
				else if (typeof settings.data == 'object') 
				{
                    data = settings.data;
                    data[this.name] = $$.val();
					var other_pos = document.getElementsByTagName('select');
					for(var i=0; i<other_pos.length; i++)
					{
						if (other_pos[i].selectedIndex) {
							var selected_text = other_pos[i].options[other_pos[i].selectedIndex].value;
							data[other_pos[i].name] = selected_text;
						}
						else{
							data[other_pos[i].name] = '';
						}
					}
                }
                settings.target.attr("disabled", "disabled");
                settings.target.empty();
				o = document.createElement("OPTION");
                o.value = '';
                o.text = 'Loading';
                settings.target.get(0).options[0] = o;
                
                $.ajax(
				{
                    url: settings.url,
                    data: data,
                    type: (settings.type || 'get'),
                    dataType: 'json',
                    success: function (j) 
					{
						
                       var options = [], i = 0, o = null;
                       settings.target.show();
					   settings.target.attr("disabled", "");
                        for (i = 0; i < j.length; i++) 
						{
                            // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
                            o = document.createElement("OPTION");
                            o.value = typeof j[i] == 'object' ? j[i][settings.key] : j[i];
                            o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i];
                            settings.target.get(0).options[i] = o;
                        }

						// hand control back to browser for a moment
						setTimeout(function () 
						{
			    			settings.target
                                .find('option:first')
                                .attr('selected', 'selected')
                                .parent('select')
                                .trigger('change');
						}, 0);
			
                    },
                    error: function (xhr, desc, er) 
					{
                        // add whatever debug you want here.
			            //alert("No bike sub-types found in this brand. Push 'Go' at the top of the left sidebar menu to view all products assigned to this brand.");
						 settings.target.attr("disabled", "disabled");
                    }
                });
            });
        });
    };
})(jQuery);
