		
		
		//globals
		
		_mPreferMetric=true;
		
		//var page=[];
		
		//page["Macit"] = "stores/MacStores.txt";
		//page["Lidlit"] = "stores/LiqStores.txt";
		
		
		//var target = "Macit";
		
		var infoText ="";
		var bounds = new GLatLngBounds();
		var marker = new Array();
		var pointer = new GMarker(new GLatLng(46.800059446787316,1.7578125));
		var closer = "<a href='javascript:zoom();'>See closer</a>"
		var kaikki = "<a href='javascript:fit();'>Show all</a>"
		var geocoder = new GClientGeocoder();
		var hits=0;//hits
		var yourLocation = ""
		var melbourne = "melbourne";
		
		
		// Store icons
		var you = "/img/store_icons/icon_you.png";
		var storeIcon = "/img/store_icons/icon_dreamfarm.png";
		var storeHover = "/img/store_icons/icon_dreamfarm_hover.png";
		
		var iconYou = new GIcon();
		iconYou.image = you;
		iconYou.shadow = "/img/store_icons/icon_shadow.png";
		iconYou.iconSize = new GSize(22, 34);
		iconYou.shadowSize = new GSize(52, 35);
		iconYou.iconAnchor = new GPoint(10, 34);
		iconYou.infoWindowAnchor = new GPoint(10, 1);
		
		var iconStoreHover = new GIcon();
		iconStoreHover.image = storeHover;
		iconStoreHover.shadow = "/img/store_icons/icon_shadow.png";
		iconStoreHover.iconSize = new GSize(22, 34);
		iconStoreHover.shadowSize = new GSize(52, 35);
		iconStoreHover.iconAnchor = new GPoint(10, 34);
		iconStoreHover.infoWindowAnchor = new GPoint(10, 1);
		
		var iconStore = new GIcon();
		iconStore.image = storeIcon;
		iconStore.shadow = "/img/store_icons/icon_shadow.png";
		iconStore.iconSize = new GSize(22, 34);
		iconStore.shadowSize = new GSize(52, 35);
		iconStore.iconAnchor = new GPoint(10, 34);
		iconStore.infoWindowAnchor = new GPoint(10, 1);
		//var hotIcon = new GIcon(G_DEFAULT_ICON);
		
		// create the map
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng( 0,0), 2);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl(1));
		map.addControl(new GScaleControl(300));
		GEvent.addListener(map, "click", function(){map.closeInfoWindow()});
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();
		
		//overview
		
		//var ovSize=new GSize(200, 150)
		//var ovMap=new GOverviewMapControl(ovSize);
		//map.addControl(ovMap);
		//var mini=ovMap.getOverviewMap();
		//setTimeout("mini.setMapType(G_SATELLITE_MAP);",1);
		//ovMap.hide();
		
		
		//Show hide controls
		map.hideControls();
		GEvent.addListener(map, "mouseover", function(){
		map.showControls();
		});
		GEvent.addListener(map, "mouseout", function(){
		map.hideControls(); 
		map.closeInfoWindow();
		});
		
		
		/// info window
		
		function showInfo(pin,html)
		{
		
			GEvent.addListener(pin, "click", function(){
			pin.openInfoWindowHtml(html,{maxWidth:300});
			map.setZoom(12);
			map.panTo(pin)
			})
		}
		
		
		function zoom()
		{	
			map.zoomIn();
		}
		
		
		/////// Process the text file
		
		var infoW=[];
		var distance=[];
		var stores=[];
		
		function load()
		{
			//alert("loading xml");
			var request = GXmlHttp.create();
			request.open("GET", "/stores_xml.cfm", true);
			
		//added from parse file
			var howMany = document.getElementById("maxStores").value*1;
			var radius = document.getElementById("radius").value*1000;
			hits=0;
			infoText = "";
			stores.length = 0;
		//from parseFILE
			
			request.onreadystatechange = function() {
			if (request.readyState == 4) 
			{
				//alert("got the xml, status: " + request.status);
				var xmlDoc = GXml.parse(request.responseText);
				
				var points = xmlDoc.documentElement.getElementsByTagName("store");
				//alert(points.length);
				for (var i = 0; i < points.length; i++) 
				{
					var storeName = xmlDoc.documentElement.getElementsByTagName("name")[i].firstChild.nodeValue;
					var storeAddress = xmlDoc.documentElement.getElementsByTagName("address")[i].firstChild.nodeValue;
					var storePhone = (xmlDoc.documentElement.getElementsByTagName("tel")[i].firstChild) ?  xmlDoc.documentElement.getElementsByTagName("tel")[i].firstChild.nodeValue : "";
					//kml
					var lati = xmlDoc.documentElement.getElementsByTagName("lat")[i].firstChild.nodeValue;
					var longi = xmlDoc.documentElement.getElementsByTagName("long")[i].firstChild.nodeValue;					
					
					//alert(storeName + ":" + lati + ":" + longi);
					
					//var point = new GLatLng(points[i].getAttribute("lat"),points[i].getAttribute("lng"));
					var point = new GLatLng(lati,longi);
					
				
					
					//Calculate distance betweeen your location and the new point
					distance = point.distanceFrom(pointer.getPoint());
		
					//populate array with the new point + attributes
					stores.push({distance:distance, point:point, storeName:storeName, storeAddress:storeAddress, storePhone:storePhone});
					
					//Create the marker but don't had it yet to the map
					var marker = new GMarker(point);
					//map.addOverlay(marker);
				}
				//sort points by distance from your location
				stores = stores.sort(function (a,b) {return (a.distance - b.distance)}); 
			}
			for (var j=0; j<stores.length && j<document.getElementById("maxStores").value*1; j++)
			{
				//if the distance from your location is < radius
				if (stores[j].distance < radius)
				{
					//GLog.write(stores[j].distance);
					
					//extends the bounds of the window from your location
					bounds.extend(pointer.getPoint());
					//add it to the hit array
					hits++;
					//Add a new marker
					marker[j] = new GMarker(stores[j].point,{icon:iconStore});
					var mar = marker[j]
					//show it on the map
					map.addOverlay(marker[j]);
					
					//extends the bounds of the window from each new store within the radius
					bounds.extend(marker[j].getPoint());
		
					
					//Set tooltip info
					html = "<p style='font-size:18px;color: #15a6d7;'>"+stores[j].storeName+"</p>";
					html += "<br/>";
					html += "<p.tooltip>"+stores[j].storeAddress+"</p>";
					/*
					html += "<br/>";
					html += closer;
					html += " | ";
					html += kaikki;
					*/	
					
					//Show tooltip info
					showInfo(marker[j],html);
								
					//Update html info window
					infoText += "<p class='result'>"+(j+1)+" - "
					infoText += "<a href='javascript:go("+j+")'"
					infoText += "onmouseover='heat("+j+")'"
					infoText += "onmouseout='cool("+j+")'>"
					infoText += stores[j].storeName
					infoText += "&nbsp;"
				
					infoText += "</a>";
					infoText += " <br /> "
					infoText += "<span class='store_address'>"+stores[j].storeAddress+"</span>"
					infoText += "<span class='km'>"+" ("+(stores[j].distance/1000).toFixed(1)+" km)"+"</span>"
					infoText += " <br /> "
					infoText += "<span class='store_address'>"+"T: "+stores[j].storePhone+"</span>"+"</p>"
					
					document.getElementById("results").innerHTML = infoText;
					document.getElementById("search_button").value = "Search for Stores";
					
					
				}
				else if(hits == 0)
					{
						document.getElementById("results").innerHTML ="<p>"+"Sorry no stores were found near your location"+"</p>";
					}
									
			} 
			// extends the map viewport to fit all the stores within he radius
			fit();	
		}
		request.send(null);
		
		}
		
		
		function go(num)
		{
			map.setZoom(12);
			GEvent.trigger(marker[num],"click");	
		}
		
		var attract;
		
		function heat(num)
		{
			attract = new GMarker(marker[num].getPoint(),iconStoreHover);
			map.addOverlay(attract);
			//map.closeInfoWindow();
		}
		
		function cool(num)
		{
			map.removeOverlay(attract);
		}
		
		///show all
		
		function fit()
		{
			bounds.extend(pointer.getPoint());
			var center = bounds.getCenter();
			map.closeInfoWindow();
			if(hits>0){map.setZoom(map.getBoundsZoomLevel(bounds));}
			map.setCenter(center);
			//map.panDirection(0,0.2);
		}
		
		
		///clear the small markers
		
		function clearMarkers()
		{
			for (var k=0; k<marker.length; k++) 
			{
				map.removeOverlay(marker[k]);
				//marker.length=0;
			}
		bounds = new GLatLngBounds();
		//document.getElementById("address").value=" ";
		}
		
		function doIt()
		{
			clearMarkers();
			load();
		}
		
		
		///Geocoding
		
		function showAddress(address){
			if (address=="")
			{
				document.getElementById("results").innerHTML = "<p class='error'>"+"Please enter an address."+"</p>";
			}
			else
			{
				document.getElementById("results").innerHTML = "<p>Searching...</p>";
				document.getElementById("search_button").value = "Searching...";
			
					geocoder.getLatLng(
					address,function(point)
					{
						if (!point)
						{	
							document.getElementById("results").innerHTML = "<div class'input'>"+"<p class='error'>"+"Sorry, this address doesn't seem to exist."+"</p>"+"</div>";
							
						}
						else
						{
							yourLocation = address;
							map.clearOverlays();
							bounds=new GLatLngBounds();
							
							pointer = new GMarker(point, {icon:iconYou, title:address, draggable:true});//pointer  {icon:iconYou, title:address, draggable:true}
							map.addOverlay(pointer);
							//pointer.enableDragging();
							GEvent.addListener(pointer, "dragend", function()
							{
								clearMarkers();
								//document.getElementById("address").value=" ";
								doIt();
								bounds.extend(pointer.getPoint());
							});
				
							bounds.extend(point);
							
							//var text = address;
							var text = "<p style='font-size:18px;color: #15a6d7;'>"+"YOU!"+"</p>";
							text += "<br/>";
							text += "<p style='font-size:12px;color: silver;'>"+address+"</p>";
							text += "<br/><br/>"
							//text += "<small>"
							text += point.toUrlValue();
							//text += "</small>"
							/*
							text += "<br/><br/>"
							text += closer;
							text += " | ";							
							text += kaikki;
							*/
							showInfo(pointer,text);
							
							doIt();
						}
					});
			}
		}
		
		function init()
		{
			doIt();
			//showAddress(document.getElementById("address").value,true);
		}
		
		
		//init();
		
	


