	(function( $ ){

		$.fn.DynDD = function( options ) {

			var o = {
				"receiver" : 'oiplug/ajax_receiver.php',
				"BuyPrices" : new Array(100000,200000,300000,400000,500000,600000),
				"RentPrices" : new Array(300,500,700,900,1100,1300),
				"firstOption" : '<option value="NULL">keine Angabe</option>',
                "preSelect" : 1
				
			};
			
			return this.each(function() {			
				if ( options ) {
					$.extend( o, options );
				}
					if (  $(this).nextAll("select").filter(":first").attr("id") == undefined)
						return false;						
					
					var postData = { };
					var StrAppend = o.firstOption;
					
						if ( $(this).val() == "KAUF" ) {				
							for ( x in o.BuyPrices)
								StrAppend += '<option value="'+o.BuyPrices[x]+'">'+numFormat(o.BuyPrices[x])+' €</option>';

						$("#PriceFrom").empty().append(StrAppend);
						$("#PriceTo").empty().append(StrAppend);


						} else if ($(this).val() == "MIETE_PACHT") {
							for ( x in o.RentPrices)
								StrAppend += '<option value="'+o.RentPrices[x]+'">'+numFormat(o.RentPrices[x])+' €</option>';

						$("#PriceFrom").empty().append(StrAppend);
						$("#PriceTo").empty().append(StrAppend);

						}
				
					postData["target"] = $(this).nextAll("select").filter(":first").attr("id");
					postData[$(this).attr("id")] = $(this).val();											
					var victim = "#"+postData["target"];							
					var p = $(this).prevAll("select");
                                                                    
                    $(this).nextAll("select").empty().append(o.firstOption);
                                                                                                        
					p.each(function() {							
						var name = $(this).attr("id");
						postData[name] = $(this).val();
					});
                                                    
    					$.ajax({
    					   	url: (o.receiver),
    						data: postData,
    						type:     "POST",
    						timeout:  5000,
    						dataType: "html",						
     						
                            beforeSend: function(){
                                $(victim).hide().after('<div class="ajaxLoad"><img src="template/ajaxload.gif" /> Lade...</div>');
                            },
                            
    						success: function(data){    						      
				                $(".ajaxLoad").remove();
                                if (o.preSelect == 1)
                                    $(victim).append(data).show();                                    
                                else                                                                                                    		
                                    $(victim).append(data).val(jQuery('option:first', victim).val()).show();
    						}
        					
    					});				
	// 			
			});           
		};                
	})( jQuery );		                              
// Neccessary Func
	function numFormat(number) {
	number = '' + number;
		if (number.length > 3) {
			var mod = number.length % 3;
			var output = (mod > 0 ? (number.substring(0,mod)) : '');
			
			for (i=0 ; i < Math.floor(number.length / 3); i++) {
				if ((mod == 0) && (i == 0))
					output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
				else
					output+= '.' + number.substring(mod + 3 * i, mod + 3 * i + 3);
			}
		return (output);
	}
		else return number;
}

