ONEHAT.widget.Contact.locationFinder = function() {
	var obj = {
		init: function() {
			var oThis = this;
			
			// Get elements to work with
			this.page1 = Ext.get('contactPage1');
			this.page2 = Ext.get('contactPage2');
			this.form = Ext.get('contactForm');
			this.stateInput = Ext.get('contactStateAbbr');
			this.countyInput = Ext.get('contactCounty');
			this.goButton = Ext.get('contactGo');
			this.resetButton = Ext.get('contactResetButton');
			this.branchAddressContainer = Ext.get('branchAddressContainer');
			this.loadingContainer = Ext.get('loadingContainer');
			this.contactStatus = Ext.get('contactStatus');
			this.contactLink = Ext.get('contactLink');
			this.googleMapContainer = Ext.get('googleMapContainer');
			this.mhMap = Ext.get('mhMap');
			
			// set initial state
			this.truckID = null;
			this.locationID = null;
			
			// combo boxes
			this.stateCombo = new Ext.form.ComboBox({
				typeAhead: true,
				triggerAction: 'all',
				transform: 'contactStateAbbr',
				width: 218,
				forceSelection: true,
				listeners: {
					select: function(combo, record, index) { // user has selected a state
						oThis.countyCombo.setValue('');
						var lastOptions = oThis.countyStore.lastOptions;
						if (lastOptions !== undefined) { // first time being called, there is no 'lastOptions' var
							lastOptions = { params: {} };
						}
						Ext.apply(lastOptions.params, {
							stateAbbr: record.data.value
						});
						oThis.countyStore.reload(lastOptions);
						oThis.countyCombo.enable();
					}
				}
			});
			this.countyStore = new Ext.data.JsonStore({
				baseParams: {
					ajaxAction: 'getCounties'
				},
				url: 'ajax/resale/addressBook/mh',
				root: 'counties',
				successProperty: 'status',
				totalProperty: 'total',
				fields: [
					{ name: 'county', type: 'string' },
					{ name: 'FIPS', type: 'int' },
					{ name: 'locationID', type: 'int' }
				]
			});
			this.countyCombo = new Ext.form.ComboBox({
				allowBlank: true,
				disabled: true,
				allQuery: 'getAll',
				applyTo: this.countyInput,
				displayField: 'county',
				emptyText: 'Select County...',
				forceSelection: false,
				hiddenName: 'FIPS',
				id: 'FIPS',
				minChars: 1,
				msgTarget: this.contactStatus,
				name: 'county',
				pageSize: 0,
				queryParam: 'q',
				selectOnFocus: true,
				shadow: 'drop',
				store: this.countyStore,
				typeAhead: true,
				triggerAction: 'all',
				valueField: 'FIPS',
				width: 218,
				listeners: {
					
					beforequery: function(queryEvent) {
						queryEvent.combo.store.baseParams.stateAbbr = oThis.stateCombo.getValue();
					},
					
					select: function(combo, record, index) { // user has selected a customer
						oThis.goButton.removeClass('disabled');
					},
					
					invalid: function(combo, msg) {
						oThis.goButton.addClass('disabled');
					},
					
					blur: function(combo) {
						var newValue = combo.el.dom.value;
						if (newValue === 'Select County...' || newValue === '') { // User has gone back to no county selection
							combo.setValue('');
						}
					},
					
					render: function(combo) {
						// clear "loading..."
						oThis.contactStatus.dom.innerHTML = '';
						oThis.loadingContainer.removeClass('offscreen');
					}
					
				}
			});
			
			// Google Map
			google.load('maps', '2');


			
			// Assign event handlers
			this.goButton.on('click', function(e) {
				oThis.handleGoButton(e);
			});
			this.resetButton.on('click', function(e) {
				oThis.handleResetButton(e);
			});
			this.contactLink.on('click', function(e) {
				oThis.handleContactLink(e);
			});


			// fix multiple-classname problems
			if (Ext.isIE6) {
				Ext.select('.button.small', false, 'customerEditorCommandButtons').each(function(el) {
					if (el.hasClass('default')) {
						el.addClass('button_small_default');
						el.removeClass(['button', 'small', 'default']);
					} else {
						el.addClass('button_small');
						el.removeClass(['button', 'small']);
					}
				});
			}
		},
		
		handleGoButton: function(e) {
			var oThis = this;
			
			try {
				// check for validity of form elements
				if (this.stateCombo.hiddenField.value === '') { throw new Error('Please select a state.'); }
				if (this.countyCombo.hiddenField.value === '') { throw new Error('Please select a county.'); }
				
			} catch (err) {
				Ext.Msg.alert('Error', err.message);
				e.stopEvent();
				return false;
			}
			
			// send request to get location data
			this.ajax = Ext.Ajax.request({
				url: 'ajax/resale/addressBook/mh',
				method: 'POST',
				success: handleSuccess,
				failure: function(a){
					Ext.Msg.alert('Error', a.msg);
				},
				scope: oThis,
				params: {
					ajaxAction: 'getLocation'
				},
				form: this.form
			});
			
			function handleSuccess(a, b) {
				var data = eval(arguments[0].responseText);
				if (data.status === 'OK') {
					// populate data on page 2
					this.branchAddressContainer.dom.innerHTML = data.html;
					this.locationID = data.locationID;
					this.outOfFootprint = data.outOfFootprint;

					Ext.get('greeting').dom.innerHTML = data.outOfFootprint ? 'Please contact Ed Miller at:<br />MH Equipment Company' : 'Your local MH Equipment branch office is:<br />MH Equipment Company';
					
					// switch pages
					this.page1.addClass('hide');
					this.page2.removeClass('hide');
					
					// show Google Map
					this.showMap(data);
					
				} else {
					Ext.Msg.alert('Error', data.status);
				}
			}
		},
		
		showMap: function(data) {
			if (google) {
				this.mhMap.addClass('hide');
				this.googleMapContainer.removeClass('hide');
	
				if (!this.googleMap) {
					this.googleMap = new google.maps.Map2(this.googleMapContainer.dom);
					this.googleMap.addControl(new google.maps.SmallMapControl());
					//this.googleMap.addControl(new GMapTypeControl());
					
				}
				
				var latLng = new google.maps.LatLng(data.lat, data.lng);
				this.googleMap.setCenter(latLng, 13);
	
				
				// create marker & set params
				if (this.googleMap.marker) {
					this.googleMap.removeOverlay(this.googleMap.marker);
				}
				this.googleMap.marker = new GMarker(latLng);
				this.googleMap.addOverlay(this.googleMap.marker);
			}
		},
		
		hideMap: function() {
			if (google) {
				this.mhMap.removeClass('hide');
				this.googleMapContainer.addClass('hide');
			}
		},
		
		handleResetButton: function(e) {
			// switch pages
			this.page1.removeClass('hide');
			this.page2.addClass('hide');
			this.hideMap();
		},
		
		handleContactLink: function(e) {
			this.contactLink.dom.href += '?locationID=' + this.locationID + '&outOfFootprint=' + (this.outOfFootprint ? 1 : 0);
		}
	};
	obj.init();
	return obj;
}();
