function Geo() {

	this.input_prefix = 'consumer_';
	this.states_loaded = false;
	this.preselected_country_id = null;
	this.preselected_state_id = null;
	this.has_states = 0;
	this.has_postal_codes = false;
	
	var Me = this;
	
	// this simply gets the "requires zipcode" flag
	// and sets it to each options with $.data()
	// we also update the states if we have a country selected
	this.load_country_data = function() {
		jQuery.getJSON(
			'http://www.nativeyewear.com/geo/lookup/countries.json',
			function(data){
				if( data.countries.length > 0 ) {

					jQuery.each(data.countries, function(i, country){
						jQuery('#dropdown_' + Me.input_prefix + 'country_id option[value="' + country.id + '"]')
								.data('has_postal_codes', parseInt(country.has_postal_codes) );
					});

					jQuery('#dropdown_' + Me.input_prefix + 'country_id').removeAttr('disabled');

					var  country_id = jQuery('#dropdown_' + Me.input_prefix + 'country_id').val();

					if( typeof(country_id) != 'undefined' && country_id != '' && !isNaN(country_id) ) {
						
						if ( jQuery('#dropdown_' + Me.input_prefix + 'country_id option[value="' + country_id + '"]').data('has_postal_codes') == 1 ) {
							Me.has_postal_codes = true;
						}

						if ( !Me.states_loaded ) {
							Me.load_states( country_id );
						}
						else {
							Me.has_states = true;
							Me.show_locality_container();
						}
					}
				}
			}
		);
	}

	// load in the states via ajax and display appropriate elements
	this.load_states = function( country_id ) {

		var url = '/geo/lookup/country/' + country_id.toString() + '/states.json';
		
		jQuery.getJSON(
			url,
			function(data){


				if( data.states.length > 0 ) {

					Me.has_states = true;
					
					jQuery('#dropdown_' + Me.input_prefix + 'state_id').children('option').each(function(i){
						if( i > 0 ) {
							jQuery(this).remove();
						}
					});

					jQuery.each(data.states, function(i, state){
						jQuery('<option/>').val(state.id).html(state.name).appendTo('#dropdown_' + Me.input_prefix + 'state_id');
					});

					Me.show_locality_container();
					
				}
				else {
					jQuery('#dropdown_' + Me.input_prefix +'state_id').val('');
					jQuery('#'+ Me.input_prefix +'zip_code').val('');
					jQuery('#'+ Me.input_prefix +'city_name_textbox').val('');
					jQuery('div.locality_container.conditional').hide();
				}

				if( Me.preselected_state_id && typeof(Me.preselected_state_id) != 'undefined' ) {
					jQuery('#dropdown_' + Me.input_prefix + 'state_id').val(Me.preselected_state_id);
				}
			}
		);
	}

	this.reset_location_data = function() {
		
		jQuery('#dropdown_' + Me.input_prefix +'state_id').val('');
		jQuery('#'+ Me.input_prefix +'zip_code').val('');
		jQuery('#'+ Me.input_prefix +'city_name_textbox').val('');


		// jQuery('#dropdown_' + Me.input_prefix + 'country_id')
		// 				.parents('.address_form_container').eq(0)
		// 				.find('div.locality_container').not('.country, .po_box')
		// 				.hide();
		// 
	}

	this.apply_country_dropdown_event = function() {

		jQuery('#dropdown_' + Me.input_prefix + 'country_id').change(function(){
			var $this = jQuery(this);
			Me.country_dropdown_activate_handler($this);
		});
	}

	this.country_dropdown_activate_handler = function( $this ) {

		if( $this ) {
			var selected = $this.val();
		}
		else if( this.preselected_country_id ){
			var selected = Me.preselected_country_id;
		}
		else {
			// do nothing for now
		}

		if ( typeof($this) != 'undefined' && $this.children('option[value=' + selected + ']').eq(0).data('has_postal_codes') ) {
			Me.has_postal_codes = true;
		}
		else {
			Me.has_postal_codes = false;
		}

		jQuery('div.' + Me.input_prefix + ' div.locality_container:not(.conditional)').show().val('');

		if( typeof(selected) != 'undefined' && (selected > 0) ) {
			jQuery('#dropdown_' + Me.input_prefix + 'country_id').val(selected);
			Me.load_states(selected);
		}
		else {
			Me.reset_location_data();
		}
	}

	this.show_locality_container = function() { 

		if ( Me.has_states ) {
			var $state_dd = jQuery('#dropdown_' + Me.input_prefix + 'state_id');
			$state_dd.parent().show();
			
		}
		
		jQuery('div.' + Me.input_prefix + ' div.locality_container').show();
		
		if( Me.has_postal_codes ) {
			jQuery('#'+ Me.input_prefix +'zip_code').show();
		}
		else{
	
			jQuery('#'+ Me.input_prefix +'zip_code').val('').hide();
		}
	}
}

