

(function($) {

    $.fn.remoteChained = function(parent_selector, url, options) { 
        
        return this.each(function() {
            
            /* Save this to self because this changes when scope changes. */            
            var self   = this;
            var backup = $(self).clone();
                        
            /* Handles maximum two parents now. */
            $(parent_selector).each(function() {
                $(this).bind("change", function() {
					var id = $(this).attr("id");
					if(id=='il'){
						$("#ilce").attr("disabled", true);
						$("#semt").attr("disabled", true);	
						$("#mahalle").attr("disabled", true);	
					}else if(id=='ilce'){						
						$("#semt").attr("disabled", true);	
						$("#mahalle").attr("disabled", true);	
					}
					else if(id=='semt'){						
						$("#mahalle").attr("disabled", true);	
					}
                    /* Build data array from parents values. */
                    var data = {};
                    $(parent_selector).each(function() {
                        var id = $(this).attr("id");
                        var value = $(":selected", this).val();
                        data[id] = value;
                    });
                    
                    $.getJSON(url, data, function(json) {
						
						/*   enable the select    */
						
						if(id=='il'){
							$("#ilce").attr("disabled", false);
							$("#semt").attr("disabled", false);	
							$("#mahalle").attr("disabled", false);	
						}else if(id=='ilce'){						
							$("#semt").attr("disabled", false);	
							$("#mahalle").attr("disabled", false);	
						}
						else if(id=='semt'){						
							$("#mahalle").attr("disabled", false);	
						}

                        /* Clear the select. */
                        $("option", self).remove();

                        /* Add new options from json. */
                        for (var key in json) {
                            if (!json.hasOwnProperty(key)) {
                                continue;
                            }
                            /* This sets the default selected. */
                            if ("selected" == key) {
                                continue;
                            }
                            var option = $("<option />").val(key).append(json[key]);
                            $(self).append(option);    
                        }
                        
                        /* Loop option again to set selected. IE needed this... */ 
                        $(self).children().each(function() {
                            if ($(this).val() == json["selected"]) {
                                $(this).attr("selected", "selected");
                            }
                        });

                        /* If we have only the default value disable select. 
                        if (1 == $("option", self).size() && $(self).val() === "") {
                            $(self).attr("disabled", "disabled");
                        } else {
                            $(self).removeAttr("disabled");
                        }*/
                        
                        /* Force updating the children. */
                        $(self).trigger("change");
                        
                    });
                });

                /* Force updating the children. */
                $(this).trigger("change");             

            });
        });
    };
    
    /* Alias for those who like to use more English like syntax. */
    $.fn.remoteChainedTo = $.fn.remoteChained;
    
})(jQuery);

/////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////\/\\\/
// JavaScript Document

