/* Set to true to turn on obnoxious debug alerts */
var storesDebugMode = false;

/* Define geocoder here to assist caching, and use a decent cache. */
var geocoder = null;

/*
 * Stores - Defines the "StoresClass" JS class used for supporting client-side
 * store location operations.
 */
var StoresClass = Class
		.create( {

			/*
			 * Display alert box if debugging.
			 */
			log : function(args) {
				if (storesDebugMode) {
					alert("LOG: " + args);
				}
			},

			/*
			 * Validate the input arguments
			 */
			validateInputs : function(storeLocationString) {
				if (storeLocationString == null
						|| storeLocationString.length == 0
						|| storeLocationString == this.defaultStoreLocationString) {
					alert(errorMissingField);
					return false;
				}
				return true;
			},

			/*
			 * Search the page to find the store location search user input
			 * field.
			 */
			getSearchString : function() {

				var searchField = document
						.getElementById("searchForm:locationText");				
				if (searchField) {
					return searchField.value;
				}
			},

			/*
			 * Use Google Maps to convert user-submitted search criteria to a
			 * lat/lon point.
			 */
			geolocate : function() {

				if (!GBrowserIsCompatible()) {
					alert("Browser is not compatible with google map api");
				}

				if (!geocoder) {
					geocoder = new GClientGeocoder();
					geocoder.setCache(new GFactualGeocodeCache());
				}

				if (!this.validateInputs(this.getSearchString())) {
					return false;
				}

				var storeLocationString = this.getSearchString();

				geocoder.setBaseCountryCode("us");
				geocoder
						.getLocations(
								storeLocationString + " US",
								function(response) {
									if (!response || !response.Placemark) {
										alert(errorMissingField);
									} else {
										var place = response.Placemark[0];
										if (!place) {
											alert(errorMissingField);
											return;
										}

										var accuracy = place.AddressDetails.Accuracy;
										var country = place.AddressDetails.Country.CountryNameCode;

										if ("US" != country) {
											alert(errorMissingField);
											return;
										}

										var point = new GLatLng(
												place.Point.coordinates[1],
												place.Point.coordinates[0]);

										/*
										 * Populate hidden fields, return true
										 * to allow form submission.
										 */
										document
												.getElementById("searchForm:searchPointLat").value = point
												.lat();
										document
												.getElementById("searchForm:searchPointLng").value = point
												.lng();

										oamSubmitForm(
												'searchForm',
												'searchForm:locatorLink',
												null,
												[ [ 'lat', point.lat() ],
														[ 'lng', point.lng() ], ]);
									}
								});

				/* Disallow form submission by returning false. */
				return false;
			},

			/*
			 * Use Google Maps to convert user-submitted search criteria to a
			 * lat/lon point.
			 */
			geolocateAdv : function() {
				try {

					var searchFieldAdv = document
							.getElementById("advStoreLocatorForm:searchStringAdv");
					this.log("searchFieldAdv: " + searchFieldAdv);
					var searchField = document
							.getElementById("searchForm:locationText");
					if (searchField == null || searchFieldAdv == null) {
						alert("Failed to find one or both search fields!");
						return false;
					}

					searchField.value = searchFieldAdv.value;

					// Duplicate code from geolocate()
					if (!geocoder) {
						geocoder = new GClientGeocoder();
						geocoder.setCache(new GFactualGeocodeCache());
					}

					var storeLocationString = searchFieldAdv.value;
					if (!this.validateInputs(storeLocationString)) {
						return false;
					}
					
					geocoder.setBaseCountryCode("us");
					geocoder
							.getLocations(
									storeLocationString + " US",
									function(response) {
										if (!response || !response.Placemark) {
											alert(errorMissingField);
										} else {
											var place = response.Placemark[0];
											if (!place) {
												alert(errorMissingField);
												return;
											}

											var accuracy = place.AddressDetails.Accuracy;
											var country = place.AddressDetails.Country.CountryNameCode;

											if ("US" != country) {
												alert(errorMissingField);
												return;
											}

											var point = new GLatLng(
													place.Point.coordinates[1],
													place.Point.coordinates[0]);
											Stores.log("New point: " + point);

											/*
											 * Populate hidden fields, return
											 * true to allow form submission.
											 */
											document
													.getElementById("advStoreLocatorForm:searchPointLat").value = point
													.lat();
											document
													.getElementById("advStoreLocatorForm:searchPointLng").value = point
													.lng();

											oamSubmitForm(
													'advStoreLocatorForm',
													'advStoreLocatorForm:locatorLink',
													null,
													[
															[ 'lat',
																	point.lat() ],
															[ 'lng',
																	point.lng() ], ]);
										}
									});

					/* Disallow form submission by returning false. */
					this.log("Geolocate returning false");
					return false;
				} catch (ex) {
					alert("Error: " + ex);
					return false;
				}

			}
		}

		/* End this class */
		);

var Stores = new StoresClass;
