$(function() {
	
	$('#location_select select,#location_select input').attr('disabled','disabled');

	$('#continent').focus(function(){
		var continent = $(this).val(),
			action = 'edit';
		if (continent != 'NULL') {
			$.ajax({
				url:"/ajax/plant/location",
				type: "POST",
				data: ({continent_id: continent, action: action}),
				dataType: "html",
				beforeSend: function(){
					$('.formloader').remove();
					$('#continent').after('<span class="formloader"></span>');
				},
				success: function(html){
					$('.formloader').remove();
					$('#country').remove;
					$('#country_select').html(html);
				}
			});
		}
	});
	
	$('#continent').live('change', function(){
		var continent = $(this).val(),
			action = 'edit';
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({continent_id: continent, action: action}),
			dataType: "html",
			beforeSend: function(){
				$('#continent').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#country').remove;
				$('#country_select').html(html);
			}
		});
	});
	
	$('#country').live('change', function(){
		var country = $(this).val(),
			action = $('#action').val();
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({country_id: country, country: country, action: action}),
			dataType: "html",
			beforeSend: function(){
				$('.formloader').remove();
				$('#country').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#province').remove();
				$('#province_select').html(html);
			}
		});
	});
	
	$('#entry_country_id').live('change', function(){
		var country = $(this).val(),
			action = '';
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({country_id: country, country: country, action: action, prefix: 'entry_', suffix: '_id'}),
			dataType: "html",
			beforeSend: function(){
				$('.formloader').remove();
				$('#entry_country_id').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#entry_province_select').html(html);
			}
		});
	});

	$('#province').live('change', function(){
		var province = $(this).val(),
			country = $('#country').val(),
			action = $('#action').val();
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({province_id: province, country: country, action: action}),
			dataType: "html",
			beforeSend: function(){
				$('#province').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#region').remove();
				$('#region_select').html(html);
			}
		});
	});
	
	$('#entry_province_id').live('change', function(){
		var province = $(this).val(),
			country = $('#entry_country_id').val(),
			action = '';
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({province_id: province, country: country, action: action, prefix: 'entry_', suffix: '_id'}),
			dataType: "html",
			beforeSend: function(){
				$('#entry_province_id').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#entry_region_select').html(html);
			}
		});
	});

	$('#region').live('change', function(){
		var region = $(this).val(),
			country = $('#country').val(),
			action = $('#action').val();
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({region_id: region, country: country, action: action}),
			dataType: "html",
			beforeSend: function(){
				$('#region').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#district').remove();
				$('#district_select').html(html);
			}
		});
	});

	$('#entry_region_id').live('change', function(){
		var region = $(this).val(),
			country = $('#entry_country_id').val(),
			action = '';
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({region_id: region, country: country, action: action, prefix: 'entry_', suffix: '_id'}),
			dataType: "html",
			beforeSend: function(){
				$('#entry_region_id').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('.formloader').remove();
				$('#entry_district_select').html(html);
			}
		});
	});
	
	$("#location_select a[rel=add_province]").live('click',function(){
		$('#add_province').remove();
		$('#province').after('<input type="text" name="new_province" id="new_province" /><input type="button" id="province_button" value="Speichern" />');
	});
	
	$("#location_select a[rel=add_region]").live('click',function(){
		$('#add_region').remove();
		$('#region').after('<input type="text" name="new_region" id="new_region" /><input type="button" id="region_button" value="Speichern" />');
	});
	
	$("#location_select a[rel=add_district]").live('click',function(){
		$('#add_district').remove();
		$('#district').after('<input type="text" name="new_district" id="new_district" /><input type="button" id="district_button" value="Speichern" />');
	});
	
	$('#province_button').live('click', function(){
		var country = $('#country').val(),
			new_province = $('#new_province').val();
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({country: country, new_province: new_province}),
			dataType: "html",
			beforeSend: function(){
				$('#province_button').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('#province_button').remove();
				$('#new_province').remove();
				$('.formloader').remove();
				$('#province').after(html);
				$('#country').change();
			}
		});
	});
	
	$('#region_button').live('click', function(){
		var province = $('#province').val(),
			new_region = $('#new_region').val();
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({province: province, new_region: new_region}),
			dataType: "html",
			beforeSend: function(){
				$('#region_button').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('#region_button').remove();
				$('#new_region').remove();
				$('.formloader').remove();
				$('#region').after(html);
				$('#province').change();
			}
		});
	});
	
	$('#district_button').live('click', function(){
		var region = $('#region').val(),
			new_district = $('#new_district').val();
		$.ajax({
			url:"/ajax/plant/location",
			type: "POST",
			data: ({region: region, new_district: new_district}),
			dataType: "html",
			beforeSend: function(){
				$('#district_button').after('<span class="formloader"></span>');
			},
			success: function(html){
				$('#district_button').remove();
				$('#new_district').remove();
				$('.formloader').remove();
				$('#district').after(html);
				$('#region').change();
			}
		});
	});
});
