FTS = function() {
	return {
		Store : function(theName, theStreet, theCity, theState, theZip, theTel, theLink, theLatitude, theLongitude) {
			var street = theStreet;
			var city = theCity;
			var state = theState;
			var zip = theZip;
			var latitude = theLatitude;
			var longitude = theLongitude;
			var name = theName;
			var link = theLink;
			var tel = theTel;
				
			return {
				getStreet : function() {
					return street;
				},
				
				getCity : function() {
					return city;
				},
				
				getState : function() {
					return state;
				},
				
				getZip : function() {
					return zip;
				},
				
				getLatitude : function() {
					return latitude;
				},
				
				getLongtitude : function() {
					return longitude;
				},
				
				getName : function() {
					return name;
				},
				
				getLink : function() {
					return link;
				},
				
				getTel : function() {
					return tel;
				},
				
				getFullAddress : function() {
					return (street + " " + city + ", " + state + " " + zip);
				}
			};
		},
		
		Direction : function(From, To, Direction) {
			var from = From;
			var to = To;
			var direction = Direction;
			
			return {
				getDirection : function() {
					return direction;
				},
				
				getFrom : function() {
					return from;	
				},
				
				getTo : function() {
					return to;
				}
			};
		},
		
		Map : function() {
			var draw_delivery_areas = true;
			var debug = false;

			var stores = [];
			var user_address = new Object();
		
			var store_marker_options;

			var geocoder = new GClientGeocoder();
			var directions = [];
			var map;
			
			return {
				addStore : function(newStore) {
					stores.push(newStore);	
				},
					
				init : function() {
					if (GBrowserIsCompatible()) {
						var directionsPanel = document.getElementById("addressSteps");
					
						map = new GMap2(document.getElementById("map_canvas"));
			
						var current_url = new String(window.location);
						current_url = current_url.substring(0, (current_url.lastIndexOf("/") + 1) );
			
						var pizzaSliceIcon = new GIcon(G_DEFAULT_ICON);
						pizzaSliceIcon.image = current_url + "images/map_marker.png";
						store_marker_options = { icon : pizzaSliceIcon, iconSize : new GSize(50, 30) };
				
						for (var i=0; i<stores.length; i++) {
							var store = stores[i];
							map.addOverlay(this.createMarker(store));
				
							if (i == 0) //Center the map to the first store
								map.setCenter(new GLatLng(store.getLatitude(), store.getLongtitude()), 9);
						}
			
						if (draw_delivery_areas)
							this.initDeliveryAreas();
	 	
						map.setUIToDefault();	
      				}
				},
				
				destroy : function() {
					GUnload();	
				},
				
				searchLocation : function(doGetClosest) {
					this.clearPreviousSearch();
					this.clearDirections();
	
					user_address.name = document.getElementById('addressInput').value;
					_instance = this;
					
					geocoder.getLatLng(user_address.name, function(point) {
						if (!point) {
       						alert(user_address.name + " not found");
							user_address = 'undefined';
   						} else {
       						map.setCenter(point, 12);
       						var marker = new GMarker(point);

							user_address.latLng = point;
							user_address.marker = marker;
       		
							map.addOverlay(marker);
       						marker.openInfoWindowHtml("Your address has been located here. <br> " + user_address.name + 
													  "<br><br>Click on any store around you to see online options.<br><span id='closestStore'></span><br>");
			
							if (doGetClosest)
								_instance.calcClosest();
				   		}
					});
				},
				
				calcClosest : function() {
					if (!user_address)
						return false;
		
					var closestStore;
					var dist = -1;
					for (var i=0; i<stores.length; i++) {
						var latLng = new GLatLng(stores[i].getLatitude(), stores[i].getLongtitude());
						var temp = latLng.distanceFrom(user_address.latLng);
						
						if (debug)
							alert('Calculating closest[ New distance from: ' + stores[i].getName() + ' to: ' +  user_address.name + ' is: ' + temp + ']');
						
						//Meters to miles * 0.000621371192
						if (dist == -1 || ( temp * 0.000621371192 ) < dist) {
							dist = ( temp * 0.000621371192 );
							closestStore = stores[i];
						}
					}
					
					//Max 50.0 miles away
					if (dist > 50.0)
						return false;
					
					if (closestStore) {
						var directionsPanel = document.getElementById("addressSteps");
						
						directions.push(new FTS.Direction(user_address.name, closestStore.getFullAddress(), new GDirections(map, directionsPanel)));
						var directionsRef = this.getDirection(user_address.name, closestStore.getFullAddress());
		
						directionsRef.getDirection().load("from: " + user_address.name + " to: " + closestStore.getFullAddress() + "");
						_instance = this;
						
						GEvent.addListener(directionsRef.getDirection(), "load", function() {
							_instance.hideDirectionMarkers(_instance.getDirection(user_address.name, closestStore.getFullAddress()));
						});
	
						delayedDisplay = function() { 
							document.getElementById("closestStore").innerHTML = "Closest store: <font color='red'>" + closestStore.getName() + "</font>";
						}
		
						setTimeout(delayedDisplay, 700);
					}
		
					if (debug)
						alert("The closest store is: " + closestStore.getName());
	
				},
				
				initDeliveryAreas : function() {
					for (var i=0; i<stores.length; i++) {
						var script = document.createElement('script');
						script.type = 'text/javascript';
						script.src = stores[i].getLink() + 'frontpage/mapPoints?callback=ftsMap.drawDeliveryArea';
						$("head").append(script);
				
						if (debug)
							alert(script.src);
					}
				},
				
				createMarker : function(store) {
					if (debug)
						alert('Creating marker for ' + store.getName());
						
					var point = new GLatLng(store.getLatitude(), store.getLongtitude());
					var marker = new GMarker(point, store_marker_options);
 					_instance = this;
					
					GEvent.addListener(marker, "click", function() {
						if (debug)
							alert("Marker clicked: " + marker);
					
							waitAndShowComments = function() { 
								showComments(store.getLink());
								if ( user_address.name != undefined)
									document.getElementById("addressDistance").style.display = "block";
								else
									document.getElementById("addressDistance").style.display = "none";
							};
				
						var content = document.createElement('div');
				
						content.innerHTML =
								 //"<img src='images/macianos_inside.jpg' width='350' height='170'/>" +
								 "<table>" +
								 "<tr><td><img src='images/map_logo.gif'/>" + 
				 
								 "<td style='padding-left: 10px'><font color='red'>" + store.getName() + "</font>" + 
								 "<br>" + store.getStreet() + ", " + 
								 "<br>" + store.getCity() + ", " + store.getState() + " " + store.getZip() +
								 "<br>" + store.getTel() + "" +
								 "</table>" +
				 
								 "<div id='featuredComments' style='font-size:medium;'></div>" +
							 	 "<span id='addressDistance' style='padding: 2px; background: lightGrey none repeat scroll 0% 0%; margin-top: 7px;'></span>" + 
				 
								 "<table align='center'>" +
								 "<tr>" +
								 "<td><a alt='Order Online' title='Order Online' href='" + store.getLink() + "menu'><img src='images/ordOnline.jpg' /></a>" +
							 //"<td><a alt='Contact us' title='Contact us' href='" + store.link + "contact'><img src='images/contactUs.jpg' /></a>" +
							 //"<td><a alt='Sign up' title='Sign up' href='" + store.link + "signup'><img src='images/signup.jpg' /></a>" +
								 "<td><img src='images/directions.gif' onClick='javascript: ftsMap.toggleDirections();'/>" +
								 "<td style='clear: both;'/>" +
								 "</table>";
					 
						if (user_address.name != undefined) {
							if (directions.length > 0)
								_instance.clearDirections();
						
						var directionsPanel = document.getElementById("addressSteps");
						directions.push(new FTS.Direction(user_address.name, store.getFullAddress(), new GDirections(map, directionsPanel)));
						var directionsRef = _instance.getDirection(user_address.name, store.getFullAddress());
				
						GEvent.addListener(directionsRef.getDirection(), "error", function() {
							if (debug)																	 
								alert("Failed to resolve address, error code: " + directionsRef.getDirection().getStatus().code);
		
							map.openInfoWindow(point, content);
							setTimeout("waitAndShowComments()", 500); //Let google create the panel first, before attaching any content.
						}); 
				
						GEvent.addListener(directionsRef.getDirection(), "load", function() {
							map.openInfoWindow(point, content);	
							setTimeout("waitAndShowComments()", 500); //Let google create the panel first, before attaching any content.
					
							document.getElementById("addressDistance").innerHTML = "Your distance to this store is: " + directionsRef.getDirection().getDistance().html;
							_instance.hideDirections(directionsRef);
						});  
			
						if (debug)
							alert("Distance : " + "from: " + user_address.name + " to: " + store.getFullAddress() + "");
							
						directionsRef.getDirection().load("from: " + user_address.name + " to: " + store.getFullAddress() + "");
						} 
						else  {
							map.openInfoWindow(point, content);	
							setTimeout("waitAndShowComments()", 500); //Let google create the panel first, before attaching any content.
						}								  
					});
		
				return marker;
				},
				
				getDirection : function(from, to) {
					for (var i=0; i<=directions.length; i++) {
						if (directions[i].getFrom() == from && directions[i].getTo() == to)
						return directions[i];
					}
	
					return null;
				},  
				
				clearPreviousSearch : function() {
					if (user_address) {
						if (user_address.marker)
							user_address.marker.remove();
			
						user_address = new Object();
					}
				},  
				
				clearDirections  : function() {
					while (directions.length > 0) {
						var direction = directions.pop();
						direction.getDirection().clear();
					}
				}, 
				
				hideDirectionMarkers : function(ftsDirections) {
					ftsDirections.getDirection().getMarker(0).remove(); 
					ftsDirections.getDirection().getMarker(1).remove();
				}, 
				
				toggleDirections : function(ftsDirections) {
					if (!ftsDirections)
						ftsDirections = directions[directions.length - 1];
	
					if (ftsDirections.getDirection().getPolyline()) {
						if(ftsDirections.getDirection().getPolyline().isHidden())
							this.showDirections(ftsDirections);
						else
							this.hideDirections(ftsDirections);
					}
				},  
				
				hideDirections : function(ftsDirections) {
					if (ftsDirections.getDirection().getPolyline()) {
						ftsDirections.getDirection().getPolyline().hide();
						this.hideDirectionMarkers(ftsDirections);
						document.getElementById("addressSteps").style.display = "none";
					}
				},  
				
				showDirections : function(ftsDirections) {
					if (ftsDirections.getDirection().getPolyline()) {
						ftsDirections.getDirection().getPolyline().show();
						this.hideDirectionMarkers(ftsDirections);
						document.getElementById("addressSteps").style.display = "block";
						map.closeInfoWindow();
					}
				},  
				
				drawDeliveryArea : function(mapShape) {
					var polygon = new GPolygon([], mapShape.fillColor, 2, 1, mapShape.fillColor, 0.4);
					for (var i=0; i<mapShape.mapPoints.length; i++)
						polygon.insertVertex(i, new GLatLng(mapShape.mapPoints[i].latitude, mapShape.mapPoints[i].longitude));

					map.addOverlay(polygon);
				}
			};
		}
	};
}();