var DropDownAddressManager = function(country, county, town){
	this.country = $(country);
	this.county = $(county);
	this.town = $(town);
	this.countyId = 0;
	
	this.serviceUrl = '/json/?controller=genericData';
	
	this.bindControls();
	
}

DropDownAddressManager.prototype.getCounties = function(){
	// Disable the counties and towns field
	this.disableField(this.county);
	this.disableField(this.town);
	
	// Remove and then add loading graphic next to county
	this.removeProgressGraphic(this.county);
	this.showProgressGraphic(this.county);
	
	// Clear the counties
	this.clearSelect(this.county);
	
	var object = this;
	
	// Populate the counties
	$.getJSON(this.serviceUrl + '&method=getCounties', 'country_id=' + $(this.country).val(), function(data){
		
		counties = data.data;
		locale = data.locale;
		
		if (counties.length > 0) {
		
			for (var i = (counties.length - 1); i >= 0; i--) {
				$(object.county).append('<option value="' + counties[i].id + '">' + counties[i].county + '</option>');
			}
			
			// Remove county loading graphic
			object.removeProgressGraphic(object.county);
			
			// Enable the counties field
			object.enableField(object.county);
			
			// Set the first option to selected
			object.selectFirst(object.county);
			
		}
		
		$('span.currency').text(locale.currency);
		
		
	});
	
}

DropDownAddressManager.prototype.showProgressGraphic = function(field){
	$(field).after('<img src="/images/ajax/arrows.gif" style="display: none;" />');
	$(field).next('img').fadeIn('fast');
}

DropDownAddressManager.prototype.removeProgressGraphic = function(field){
	var field = field;
	$(field).next('img').fadeOut('fast', function(){
		$(field).next('img').remove();
	});
}

DropDownAddressManager.prototype.disableField = function(field){
	$(field).attr('disabled', 'disabled');
}

DropDownAddressManager.prototype.enableField = function(field){
	$(field).removeAttr('disabled');
}

DropDownAddressManager.prototype.selectFirst = function(field){
	$('option[value="0"', field).attr('selected', 'selected');
}

DropDownAddressManager.prototype.clearSelect = function(field){
	$('option[value!="0"]', field).remove();
}

DropDownAddressManager.prototype.getTowns = function(){
	
	// Disable the towns field
	this.disableField(this.town);

	var object = this;

	if($(this.town).is('select')){
	
		// Remove and then add loading graphic next to county
		this.removeProgressGraphic(this.town);
		this.showProgressGraphic(this.town);
	
		// Clear all towns
		this.clearSelect(this.town);
		this.countyId = $(this.county).val();
		// Populate the towns
		$.getJSON(this.serviceUrl + '&method=getTowns', 'county_id=' + $(this.county).val(), function(data){
			
			towns = data.data;
			
			if (towns.length > 0) {
			
				for (var i = (towns.length - 1); i > 0; i--) {
					$(object.town).append('<option value="' + towns[i].id + '">' + towns[i].town + '</option>');
				}
				
				// Remove the progress graphic
				object.removeProgressGraphic(object.town);
				
				// Enable the town field
				object.enableField(object.town);
				
				// Set the first option to selected
				object.selectFirst(object.town);
				
			}
			
			
		});
	
	} else {
		// Remove the progress graphic
		this.removeProgressGraphic(this.town);
		// Enable the town field
		$(this.town).val('type your town here');
		$('.town_id').val(0);
		this.enableField(this.town);
	}
	
}

DropDownAddressManager.prototype.changeAds = function(table, id){
	advertTL.change(table, id);
	advertTR.change(table, id);
	advertBL.change(table, id);
	advertBR.change(table, id);
}

DropDownAddressManager.prototype.bindControls = function(){

	var object = this;
	var countyId = 0;
	$(this.country).change(function(){
		object.getCounties();
		object.changeAds('country', $(object.country).val());
	});
	
	if($(object.town).is(':text') && !$(object.town).val()){
		$(object.town).val('type your town here');
		$(object.town).focus(function(){
			if($(this).val() == 'type your town here'){
				$(this).val('');
			}
		});
	}
	
	
	
	$(this.county).change(function(){
		object.getTowns();
		object.changeAds('county', $(object.county).val());
		
		if($(object.town).is(':text')){

			// Bind the autocomplete
			$(object.town).autocomplete({
				ajax: '/json/?controller=directory&method=getTown&countyId=' + $(object.county).val(),
				match: function(typed) { return this.name.match(new RegExp(typed, 'i'))},
				insertText: function(location) { return location.name },
				template: function(location) { return '<li>' + location.name + '</li>' },
				timeout: 0
			}).bind("activate.autocomplete", function(e,data) {
				$('.town_id').val(data.id);
				if($('#fuelpoint_search').length > 0){
					directoryManager.search($('#fuelpoint_search form, #tabular form').serialize());
				}
			});
		
		}
	});
	
	$(this.town).change(function(){
		object.changeAds('town', $(object.town).val());
	});
	
	
	
	
	
	
}
