
var mArray = Array();
var map;
var start;
var directionsPanel;
var directions;
var centerPoint = new GLatLng(54,-4);
var end = "n";

//function load() {
//	doLoad();
//	//alert(showAddress('225 Marsh Wall, London, E14 9FW, UK')+';My marker')
//	//showAddress('225 Marsh Wall, London, E14 9FW, UK','teaset');
//	//showAddress('631 Manchester Road, London, E14 3NU, UK','teaset');
//	mArray.push('225 Marsh Wall, London, E14 9FW, UK;My marker')
//	mArray.push('631 Manchester Road, London, E14 3NU, UK;My marker')
//	//mArray.push('48.87916700;2.37304700;New point')
//
//
//	//addMarkers();
//	showAddress();
//}

function doLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.removeMapType(G_SATELLITE_MAP);
		map.removeMapType(G_HYBRID_MAP);
		map.setCenter(centerPoint, 5);
		map.addControl(new GScaleControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		GEvent.addListener(map, 'click', mapClick);
		directionsPanel = document.getElementById("route");
      directions = new GDirections(map, directionsPanel);
     
		

	}
}

function addMarkers() {
	if (mArray.length) {
		var bounds = new GLatLngBounds();
		for (n=0 ; n < mArray.length ; n++ ) {
			var mData = mArray[n].split(';');
			var point = new GLatLng(mData[0],mData[1]);
			bounds.extend(point);
			var marker = createMarker(point, mData[2]);
			map.addOverlay(marker);
		}
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
	}
}

function createMarker(point, title, addr) {
	
	
	var marker = new GMarker(point,{title:"99p Stores " + title, icon: new GIcon(G_DEFAULT_ICON,'images/google.png')});
	
	GEvent.addListener(marker, "click", function() { end = point.y + ',' + point.x; });
	return marker;
}

function mapClick(marker, point) {
	if (!marker) {
		oLat = document.getElementById("lat");
		oLat.value = point.lat().toFixed(6);
		oLon = document.getElementById("lon");
		oLon.value = point.lng().toFixed(6);
		oDesc = document.getElementById("desc");
		oDesc.value = 'New point';
		
	}
	
}

	//function alrt(alrt){
//		alert(alrt);
//	}
//	GClientGeocoder.getLatLng("225 Marsh Wall, London, E14 9FW, UK", alrt)
	
	var bounds = new GLatLngBounds();
	var p = 0;
function showAddress() {
		
	if (mArray.length) {
		//var bounds = new GLatLngBounds();
		for (n=0 ; n < mArray.length ; n++ ) {
			var mData = mArray[n].split(';');
			//var point = new GLatLng(mData[0],mData[1]);
			//bounds.extend(point);
			//var marker = createMarker(point, mData[2]);
			//map.addOverlay(marker);
			geocoder = new GClientGeocoder();
			if (geocoder) {
				geocoder.setBaseCountryCode("uk");
				geocoder.getLatLng(
				mData[0],
				function(point) {
				if (!point) {
				  //alert(address + " not found");
				  p++;
				} else {
					//alert(point);
					//alert(mArray[p]);
					
					var addArr=mArray[p].split(';');
					//var bounds = new GLatLngBounds();
					//var point = new GLatLng(point);
					bounds.extend(point);
					var marker = createMarker(point, addArr[1], addArr[0]);
					
					map.addOverlay(marker);
					p++;
					//mArray.push(point+';My marker')
					//alert(point.replace(/,/,";"));
					
					//return point;
					//map.setCenter(point, 13);
					//var marker = new GMarker(point);
					//map.addOverlay(marker);
					//marker.openInfoWindowHtml(address);
				}
          	}
			//end function
        );
		if (n == mArray.length-1) {
			setTimeout("map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds))",5000);
		}
      }
			
	}
	
	}
}



    function locateAddress() {
      var address = document.getElementById("address").value + ", UK";
      var geocoder = new GClientGeocoder();
	  if (end == "n"){
	 
	  	alert("Please click on a destination branch");
		} else {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
              start = ""+point;
		
			  			var s = ""+start;
			s = s.replace(/[(]/g, "");
			s = s.replace(/[)]/g, "");
			var e = ""+end
			e = e.replace(/[(]/g, "");
			e = e.replace(/[)]/g, "");
			s = s + " to " + e; 
			
			  directions.load(s);
			  //var dir = start.slice(1, start.length-1) + " to 51.494067, -0.00811";
			  
			 // alert(dir);
			  //directions.load(dir);
            }
          }
        );
      }
	  }
    }
	
function setend(e) {
	end = e;
  alert(end);
}

    function showAdd(address) {
	var geocoder = new GClientGeocoder();
	//alert(address);
	var mData = address.split(';');
	//alert(mData[0]);
      if (geocoder) {
        geocoder.getLatLng(
          mData[0],
          function(point) {
            if (!point) {
              alert(mData[0] + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point,{title:"99p Stores", icon: new GIcon(G_DEFAULT_ICON,'images/google.png')});
              map.addOverlay(marker);
              marker.openInfoWindowHtml("99p Stores " + mData[1] + "<br>" + mData[2]);
			  end = point;
            }
          }
        );
		
      }
    }
	function showPoint(lat, lng, title, address) {
		//alert(address);
		//var mData = address.split(';');
		//alert(mData[0]);
		//alert(loc);
		//alert(title);
		//alert(address);
		var point = new GLatLng(lat, lng);
		//alert(point);
		map.setCenter(point, 13);
		//alert("set center");
		var marker = new GMarker(point,{title:"99p Stores " + title, icon: new GIcon(G_DEFAULT_ICON,'images/google.png')});
		map.addOverlay(marker);
		marker.openInfoWindowHtml("99p Stores " + title + "<br>" + address);
		end = point;
     }