
var closeLocations = new Array();
var maxCloseLocations  = 6;
function mapsInitialize() {
	var mapDiv =document.getElementById("map_canvas")
 			
	if(mapDiv!=null){
		if (GBrowserIsCompatible()) {
			var zip = getQueryVariable('zipCode');
			var zoom = getQueryVariable('radius');
			var map = new GMap2(mapDiv);
			
			var directionsPanel =document.getElementById("route");
		  var directions = new GDirections(map, directionsPanel);
		  
			var geocoder = new GClientGeocoder();
			var degrees = 1;
			var nolocation  = 0;

			if(!zip || zip ==''){
				map.setCenter(new google.maps.LatLng(39,-76), 4);
				map.setUIToDefault();
				return;
			}
			zoom = parseInt(zoom);
			//degrees = miles/ 45;
			switch(zoom)
			{
				case 14:degrees = .2;break;
				case 13:degrees = .3;break;
				case 12:degrees = .4;break;
				case 11:degrees = .5;break;
				case 10:degrees = .75;break;
				default:degrees = 1;break;
			}
				
		
				
			geocoder.getLatLng(zip,
			function(point) {
				if (!point) {
						alert("We're sorry, we could not locate " + zip);
						map.setCenter(new google.maps.LatLng(39,-76), 4);
					} else {
						var tinyIcon = new GIcon();
						tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
						tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
						tinyIcon.iconSize = new GSize(12, 20);
						tinyIcon.shadowSize = new GSize(22, 20);
						tinyIcon.iconAnchor = new GPoint(6, 20);
						tinyIcon.infoWindowAnchor = new GPoint(5, 1);


						var marker = new GMarker(point,tinyIcon);
						map.addOverlay(marker);
						//marker.openInfoWindowHtml(zip);
						var json = getPoints();
							
						
						for(place in json.locations){
								AddStore(map,directions, json.locations[place],degrees,point,zip);
						} 

						if(closeLocations.length > 0){
							var closeLocationsDiv = document.getElementById('routedefault');
							
							closeLocationsDiv.innerHTML="<b>Locations Near You</b><br/>";
							closeLocations.sort(sortDistance);
							
							for(var jj=0;jj<closeLocations.length;jj++){
									if (jj <maxCloseLocations){
										var place = closeLocations[jj];
									closeLocationsDiv.innerHTML+="<table><tr><td style='width:100px'>"+place[0]+" miles </td><td>"+ place[1]+ "</td></tr></table><hr>"
									}
							}
						}
						map.setCenter(point, zoom);
						
					}
					
				}
				);
			}

			map.setUIToDefault();
		}
	}
	function sortDistance(a, b){
		return a[0] - b[0];
	}
	
	
	function AddStore(map,directions, the_location,absdegrees,point,start){
		
		if(Math.abs(the_location.lat-point.y) > absdegrees||
			Math.abs (the_location.long-point.x) > absdegrees){ return;}

			var latlng = new google.maps.LatLng(the_location.lat, the_location.long);
		  var clean = the_location.storename + "<br/>"+
				the_location.street + "<br/>"+
				the_location.city + ", "+
				the_location.state + " "+
				the_location.zip + "<br/>" +
				the_location.phone +"<br/>";
			var end = the_location.street+' ' + the_location.city+ ' ' +the_location.state+' ' +the_location.zip ;
		
		  if(Math.abs(the_location.lat-point.y) < .075 && Math.abs (the_location.long- point.x) < .075){ 
			  var distance =Math.round(point.distanceFrom(latlng) * 0.00621371192 ,2)/10;
			  closeLocations.push( new Array(distance,clean));
			}




		var tinyIcon = new GIcon();
		tinyIcon.image = "/global/images/hc_icon_32x32.png";
		tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		tinyIcon.iconSize = new GSize(32, 32);
		tinyIcon.shadowSize = new GSize(44, 40);
		tinyIcon.iconAnchor = new GPoint(6, 20);
		tinyIcon.infoWindowAnchor = new GPoint(5, 1);
				

		var store = new GMarker(latlng,tinyIcon);
		var printLink = document.getElementById('print');

		map.addOverlay(store);
		
		GEvent.addListener(store, "click", function() {
			store.openInfoWindowHtml("<img src='/global/images/logo_reflect.jpg' alt='hc logo' class='gm_hc_logo' /><br/>"+
				clean);
    	directions.load("from: "+start +" to: " +end);
			document.getElementById('routedefault').style.display = 'none';
			printLink.style.display= 'block';
			printLink.onclick = function(){window.open("http://maps.google.com/maps?f=d&source=s_d&saddr="+start +"&daddr="+end +"%28Hair+Cuttery%29&ie=UTF8&z=13&layer=c&pw=2",'mywindow','scrollbars=yes,width=800,height=600'); return false;}
 			
		  
	}); 
}
	
	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
	}
