$(document).ready(function() {
	StoreLocator.initialize();
	$('.searchTerms').eq(0).blur()
	$('#myLocation').focus();
});

var StoreLocator = new function() {
	var self = this;
	var container = $('#storeLocator');
	this.map = null;
	this.geocoder = null;
	this.baseIcon = null;
	this.letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	this.markers = [];
	this.storeCache = {};
	this.userLocation = null;
	this.busy = false;

	/**
	  * Initialize the store locator widget
	  */
	this.initialize = function() {
		container = $('#storeLocator');
		self.map = new google.maps.Map2(document.getElementById('mapCanvas'));
		self.map.setUIToDefault();

		self.geocoder = new GClientGeocoder();

		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		self.baseIcon = new GIcon(G_DEFAULT_ICON);
		self.baseIcon.shadow = "/wcsstore/Sears/images/store-locator/markers/shadow.png";
		self.baseIcon.iconSize = new GSize(20, 34);
		self.baseIcon.shadowSize = new GSize(37, 34);
		self.baseIcon.iconAnchor = new GPoint(9, 34);
		self.baseIcon.infoWindowAnchor = new GPoint(9, 2);

		if (google.loader.ClientLocation != null) {
			var clientLoc = google.loader.ClientLocation;
			var lat = clientLoc.latitude;
			var lon = clientLoc.longitude;
			self.map.setCenter(new google.maps.LatLng(lat, lon), 8);
		}
		else {
			self.map.setCenter(new google.maps.LatLng(53.74871079689897, -93.515625), 3);
		}

		$('#myLocation', container).focus(function() {
			$('body').keypress(function(e) {
				if (e.keyCode == 13) {
					self.doSearch($.trim($('#myLocation', container).val()));
					return false
				}
			});
		}).blur(function() {
			$('body').unbind('keypress');
		});

		$('.btnSearch', container).click(self.doSearch);

		if ($('.findStores').length > 0) {
			$('.findStores').click(function() {
				var renderContent = function(html) {
					var popup = new eStore.UI.Window('chooseAddress');
					popup
						.prepend(html.children())
						.addClass('large')
						.removeClass('plain')
						.addDecorators('<span class="btmEdge"></span>')
						.closeButton(true)
						.blockUI(true)
						.clickToClear(true)
						.open();

					popup.find('.btnSelect').attr('href', '#');

					var canada = 'Canada';
					popup.find('.address').each(function() {
						if ($('.country', this).text()!= canada) {
							$('.btnSelect', this).hide();
						}
					});

					popup.find('.btnSelect').click(function() {
						var postalCode = $('.postalCode', $(this).siblings('.addressWrapper')).text();
						self.findByLocation(postalCode);
						$('#storeLocator #myLocation').val(postalCode);
					});
				}
				var addressBookUrl = '/stores/shop/AddressBookForm?catalogId=' + eStore.catalogId + '&langId=' + eStore.langId + '&storeId=' + eStore.storeId + '&mode=selectBillingAddress&ajax=true';
				$.ajax({
					url: addressBookUrl,
					type: 'GET',
					dataType: 'html',
					success: function(html) {
						var html = $(html);
						renderContent(html);
					}
				});
			});
		}
	}

	/**
	  *
	  */
	this.doSearch = function() {
		var query = $.trim($('#myLocation', container).val());
		if (query.match(/^\d{4}$/)) {
			self.findByStoreNum(query);
		}
		else {
			self.findByLocation(query);
		}
	}

	this.cacheResults = function(stores) {
		for (var i = 0; i < stores.length; i++) {
			var id = stores[i].identifier;
			if (self.storeCache[id] == undefined) {
				self.storeCache[id] = stores[i];
			}
		}
	}

	/**
	  * Find By Address or Postal Code
	  */
	this.findByLocation = function(address) {
		self.map.clearOverlays();
		if (self.mapPanListener != null) GEvent.removeListener(self.mapPanListener);
		if (!$.trim(address).match(/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d$)/) 
		 	& address.toLowerCase().indexOf('canada') == -1) {
			address += ', Canada';
		}
		self.geocoder.getLatLng(address, function(latlng) {
			if (latlng) {
				self.userLocation = latlng;
				//var url = '/stores/shop/StoreLocator?langId=1&storeId=10051&catalogId=10001&latitude=' + latlng.lat() + '&longitude=' + latlng.lng() + '&services=&limit=50';
				// if selecting an address from address book popup
				if ($('body .window#chooseAddress').length > 0) {
					$('.window .btnClose').click();
				}
				self.mapPanListener = GEvent.addListener(self.map, "moveend", self.onMapPan);
				//$.getJSON(url, function(stores) {
					//self.cacheResults(stores);
					//displayResults(stores, true);
					self.map.setCenter(latlng, 12);
				//});
			}
			else {
				alert(eStore.storeText.storelocator.store_not_found_alert);
			}
		});
	}

	/**
	  * Find By Store Number
	  */
	this.findByStoreNum = function(storeNum) {
		self.map.clearOverlays();
		var url = '/stores/shop/StoreLocator?langId=1&storeId=10051&catalogId=10001&storeNum=' + storeNum;
		$.getJSON(url, function(stores) {
			if (stores.length > 0) {
				displayResults(stores, true);
			}
			else {
				//alert('Pick-up location ' + storeNum + ' is not a number. Please re-enter the unit number a try again');
				alert(eStore.storeText.storelocator.invalid_pickup_location.replace(/\{\?\}/, storeNum));
			}
		});
	}

	/**
	  * Find the closest store using postal code
	  */
	this.findClosestStore = function(postalCode, fnCallback) {
		self.geocoder = new GClientGeocoder();
		self.geocoder.getLatLng(postalCode, function(latlng) {
			if (latlng) {
				var url = '/stores/shop/StoreLocator?langId=1&storeId=10051&catalogId=10001&latitude=' + latlng.lat() + '&longitude=' + latlng.lng() + '&services=&limit=1';
				$.getJSON(url, function(json) {
					alert(json);
					/*if (fnCallback != undefined || fnCallback != null) {
						fnCallback(json[0]);
					}*/
				});
			}
		});
	}

	/**
	  *
	  */
	this.onMapPan = function() {
		if (self.busy == false) {
			var zoomLevel = self.map.getZoom();
			var mapCenter = self.map.getCenter();
			//if (zoomLevel >= 10) {
				var url = '/stores/shop/StoreLocator?langId=1&storeId=10051&catalogId=10001&latitude=' + mapCenter.lat() + '&longitude=' + mapCenter.lng() + '&services=&limit=25' +
																						  '&origLatitude=' + self.userLocation.lat() + '&origLongitude=' + self.userLocation.lng();
				$.getJSON(url, function(stores) {
					self.cacheResults(stores);
					var mapBounds = self.map.getBounds();
					var stores = [];
					for (var store in self.storeCache) {
						var latlng = new google.maps.LatLng(self.storeCache[store].latitude, self.storeCache[store].longitude);
						if (mapBounds.containsLatLng(latlng)) {
							var distance = latlng.distanceFrom(self.userLocation) / 1000;
							self.storeCache[store].distance = distance;
							stores.push(self.storeCache[store]);
						}
					}
					self.map.clearOverlays();
					displayResults(stores, false);
				});
			/*}
			else {
				self.map.clearOverlays();
			}*/
		}
	}

	// Creates a marker whose info window displays the letter corresponding
	// to the given index.
	this.plotLetteredMarker = function(latlng, letter, infoWindowContent) {
		// Create a lettered icon for this point using our icon class
		var letteredIcon = new GIcon(self.baseIcon);
		//letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		letteredIcon.image = "/wcsstore/Sears/images/store-locator/markers/marker" + letter + ".png";

		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(latlng, markerOptions);

		self.map.addOverlay(marker);
		var setBusyToFalse = function() {
			self.busy = false;
		}

		GEvent.addListener(marker, "click", function() {
			self.busy = true;
			marker.openInfoWindowHtml(infoWindowContent);
			setTimeout(setBusyToFalse, 1500);
		});

		//return marker;
	}

	this.clearMarkers = function() {
		for (var i = 0; i < self.markers.length; i++) {
			self.markers[i].set_map(null);
		}
		self.markers = [];
	}


	var displayResults = function(stores, adjustPosition) {
		var bounds = new GLatLngBounds();

		//Omniture
		trackStoreLocator(stores.length);
		if (eStore.langId == 1){
			var storeTypes = {
				'A&M'	: 'Sears Appliance & Mattress Store',
				'CORE'	: 'Sears Department Store',
				'CORE- 1 Flr': 'Sears Department Store',
				'DEALER': 'Sears Dealer Store',
				'FLOOR'	: 'Sears Floor Covering Centre',
				'HIPS' 	: 'Sears HomeCentral Showroom',
				'HOME'	: 'Sears Home Store',
				'OUTLET': 'Sears Outlet Store',
				'PILOT'	: 'Sears Department Store',
				'PRS'	: 'Sears Parts & Repair Services',
				'SATELLITE': 'Sears Department Store',
				'SELECT': 'Sears Department Store',
				'SMALL'	: 'Sears Department Store',
				'TEMP OUTLET': 'Sears Outlet Store',
				'PICKUP': 'Catalogue Pickup Location'
			}
		}
		else{
			var storeTypes = {
				'A&M'	: 'Magasin de matelas et d\'&eacute;lectrom&eacute;nagers',
				'CORE'	: 'Grand magasin Sears',
				'CORE- 1 Flr': 'Grand magasin Sears',
				'DEALER': 'Magasins-concessions Sears',
				'FLOOR'	: 'Centre de rev&ecirc;tements de sol Sears',
				'HIPS' 	: 'Salle d\'exposition Centre du foyer Sears',
				'HOME'	: 'Magasin Sears D&eacute;cor',
				'OUTLET': 'Magasin de liquidation Sears',
				'PILOT'	: 'Grand magasin Sears',
				'PRS'	: 'Service des pi&egrave;ces et de l&rsquo;entretien Sears',
				'SATELLITE': 'Grand magasin Sears',
				'SELECT': 'Grand magasin Sears',
				'SMALL'	: 'Grand magasin Sears',
				'TEMP OUTLET': 'Magasin de liquidation Sears',
				'PICKUP': 'Comptoir de ramassage de catalogue'
			}
		}
				
		if (stores.length > 0) {
			$('.locations .stores', self.container).empty();
			for (var i = 0; i < stores.length; i++) {
				var store = stores[i];
				var letter = String.fromCharCode("A".charCodeAt(0) + i);
				if (i > 25) {
					letter = 'Blank';
				}
				var latlng = new google.maps.LatLng(store.latitude, store.longitude);

				// add store to listing
				var distance = (store.distance != null ? distance = Math.round(store.distance * 100) / 100 : 0);

				var address = '<p><strong>' + store.unitname + '</strong><br/>' +
							  store.address1 + ($.trim(store.address2) != '' ? '<br/>' + store.address2 : '') + '<br/>' +
							  store.city + ', ' + store.province + ' ' + store.postalcode + '<br/>';
				if (store.phone != '') {
					address += 'Phone: ' + store.phone + '<br/>';
				}
				if (store.fax != '') {
					'Fax: ' + store.fax + '</p>';
				}

				/* Displaying the store hours */
				// utility function format hour: 900 => 9am, 2200 => 10pm
				function hourFormat(hour) {
					var x = Number(hour) / 100;
					var h = parseInt(x);
					var m = parseInt((x - h) * 100);
					var postfix = 'am';
					if (h > 12) {
						postfix = 'pm';
						h = h - 12;
					}
					if (m == 0) {
						return h + postfix;
					}
					return h + ':' + m + postfix;
				}
				
				// utility function displayDate: 2010-12-31 => Dec 31, 2010
				function displayDate(date) {
					var splittedDate = date.split('-');
					var d = new Date(Number(splittedDate[0]), Number(splittedDate[1]), Number(splittedDate[2]));
					var month = eStore.storeText.storehours.months;
					var returnedDate = "";
					if (eStore.langId == '2') {
						returnedDate = '(' + d.getDate() + ' ' + month[d.getMonth() - 1] + ', ' + d.getFullYear() + ')';
					} else {
						returnedDate = '(' + month[d.getMonth() - 1] + ' ' + d.getDate() + ', ' + d.getFullYear() + ')';
					}
					return returnedDate;
				}

				// first step: creating a hash with day as a key and the merged store hours as a value
				// {"hours_sun_open": "800", "hours_sun_closed": "2200", "hours_mon_open": "900", "hours_mon_closed": "2100","hours_tue_open": "900", "hours_tue_closed": "2100"}
				// becomes
				// { "Sun": "800-2200", "Mon": "900-2100", "Tue": "900-2100"}
				function mergeHours(hash) {
					var mergedHours = {};
					
					var days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
					var daysDisplayed = eStore.storeText.storehours.days_displayed;
					var suffixOpen = '_open';
					var suffixClosed = '_closed';
					var prefixHours = 'hours_';
					for(j = 0; j < days.length; j++) {
						mergedHours[daysDisplayed[j]] = hash[prefixHours + days[j].toLowerCase() + suffixOpen] + '-' + hash[prefixHours + days[j].toLowerCase() + suffixClosed];
					}
					return mergedHours;
				}

				// second step: reversing the hash's key/values to get the days that have the same open-closed hours.
				// { "Sun": "800-2200", "Mon": "900-2100", "Tue": "900-2100"}
				// becomes
				// { "800-2200": "Sun", "900-2100": ["Mon", "Tue"] }
				function reverseHash(mergedHours) {
					var reversedHash = {};
					$.each(mergedHours, function(key,value){
						if (reversedHash[value] != null) {
							reversedHash[value].push(key);
						} else {
							reversedHash[value] = [];
							reversedHash[value].push(key);
						}
					});
					return reversedHash;
				}

				// Third step: displaying the store hours, grouping the same hours by day
				function displayRegularHours(reversedHash) {
					var storeHours = "";
					$.each(reversedHash, function(key,daysArray){
						if (daysArray.length > 2) {
							storeHours += daysArray[0] + " - " + daysArray[daysArray.length - 1];
						} else {
							storeHours += daysArray.join();
						}
						
						var h = key.split('-');
						if (h[0] == "" && h[1] == "") {
							storeHours += ': Closed<br/>';
						} else {
							storeHours += ': ' + hourFormat(h[0]) + ' - ' + hourFormat(h[1]) + '<br/>';
						}
					});
					return storeHours;
				}
				
				function displayHolidayHours(reversedHash) {
					var storeHours = "";
					var isClosed = new Boolean(true);
					$.each(reversedHash, function(key,daysArray){
						var h = key.split('-');
						if (h[0] != "" && h[1] != "") {
							storeHours += daysArray.join() + ': ' + hourFormat(h[0]) + ' - ' + hourFormat(h[1]) + '<br/>';
							isClosed = false;
						}
					});
					if (isClosed) storeHours += eStore.storeText.storehours.closed + '<br/>';
					return storeHours;
				}

				var storeHours = "";
				var storeHoursHash = store.storehours;
				for (sh in storeHoursHash) {
					var mh = mergeHours(storeHoursHash[sh]);
					var r = reverseHash(mh);
					if (storeHoursHash[sh]['description_en'] == 'Regular Store Hours') {
						storeHours += '<strong>' + storeHoursHash[sh][eStore.langId == '2' ? 'description_fr' : 'description_en'] + '</strong><br/>'
						storeHours += displayRegularHours(r);
					} else {
						storeHours += '<strong>' + storeHoursHash[sh][eStore.langId == '2' ? 'description_fr' : 'description_en'] + ' ' + displayDate(storeHoursHash[sh]['holiday_starts']) + '</strong><br/>';
						storeHours += displayHolidayHours(r);
					}
				}
				if (storeHours == "") storeHours = '&nbsp;';
				/* End Displaying the store hours */


				var infoWindowContent = '<p style="font-size:0.9em">' + address + '</p>';
				self.plotLetteredMarker(latlng, letter, infoWindowContent);
				if (i < 10) bounds.extend(latlng);


				/* Displaying the store services */
/*				var mystoreservices = store.storeservices;
				var listContainer = $('<div id="storeservices' + i + '"></div>');
				var storeServices = mystoreservices.length > 0 ? $('<ul></ul>') : '';
				
				for (s in mystoreservices) {
				    if (mystoreservices[s].service_name_en != '') {
						storeServices.append('<li>' + mystoreservices[s].service_name_en + '</li>');
				    }
				}
				listContainer.append(storeServices);
				storeServices = listContainer.html();
*/
				/* End Displaying the store services */

				/*var storeTypes = {
					D: 'Department Store',
					O: 'Sears Outlet Store',
					C: 'Sears Catalog Pickup Location',
					P: 'Sears Parts & Repair Service',
					H: 'Sears Home Store',
					A: 'Sears Appliance & Mattress Store',
					B: 'Dealer Store',
					F: 'Sears Floor Covering Center',
					S: 'Sears Home Central Showroom Location',
					L: 'Lands\' End Shop'
				}

				var serviceTypes = {
					W: 'Wheelchair Accessible',
					P: 'Photo Finishing'
				}

				var services = store.instore_services.split(',');
				var instoreServices = '<ul>';
				if (services.length > 0) {
					for (var j = 0; j < services.length; j++) {
						if (serviceTypes[services[j]] != null) {
							instoreServices += '<li>' + serviceTypes[services[j]] + '</li>';
						}
					}
				}
				instoreServices += '</ul>';*/
				var instoreServices = '&nbsp;';
				
				
				
				var storeInfo = $('<tr class="store">' +
									'<td class="storeType" valign="top">' + 
										//'<p>Sears Store Type<br/>' +
										(storeTypes[store.store_type] ? '<p>' + '<a class="glossaryTerm basic" href="/content/customer-service/shipping-and-delivery/store-types' + (eStore.langId == '2' ? '_fr' : '_en') + '">'+storeTypes[store.store_type]+'</a>'  : '<a class="glossaryTerm basic" href="/content/customer-service/shipping-and-delivery/store-types' + (eStore.langId == '2' ? '_fr' : '_en') + '">'+storeTypes['PICKUP']+'</a>') + '<br/>'+
										'<img class="marker" src="/wcsstore/Sears/images/store-locator/markers/marker' + letter + '.png" />' +
										(distance > 0 ? '<br/>' + distance + ' km</p>' : '') +
									'</td>' +
									'<td class="storeInfo" valign="top">' +
										address +
									'</td>' +
									'<td class="storeHours" valign="top"><p>' +
										storeHours + '</p>' +
									'</td>' +
									/* '<td class="services" valign="top">' +
										storeServices +
									'</td>' + */
								  '</tr>');
				$('.locations .stores', self.container).append(storeInfo);

				//var markerStr = latlng.lat() + ',' + latlng.lng() + ',blue' + letter.toLowerCase();
			}

			$('.locations').show();
			/*var staticMapUrl = 'http://maps.google.com/staticmap?size=512x250&maptype=mobile&markers=' + markers.join('%7C') +
							   '&key=ABQIAAAAmVWgEHeSLA96h7--pvqDjhS0cvn6zi3zYhBR31wcMlvbCslMkhT7AzRSRGyMobymDzyKI7Lg_gHoKQ&sensor=false';

			$('#staticMap').empty().append($('<img width="700" src="' + staticMapUrl + '" />'));*/

			if (adjustPosition) {
				var zoomLevel = 15;
				if (stores.length > 1) {
					zoomLevel = self.map.getBoundsZoomLevel(bounds);
				}
				var center = bounds.getCenter();
				self.map.setCenter(center, zoomLevel);
			}
		}
		else {
			// search by proximity
		}
	}
}
