var geocoder;
var TPC = {
  map: null,
  infoWindow: null
};

var image = new google.maps.MarkerImage('/shared/img/markers/image.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    new google.maps.Size(24,36),
    // The origin for this image is 0,0.
    new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    new google.maps.Point(0,36));
var shadow = new google.maps.MarkerImage('/shared/img/markers/shadow.png',
    // The shadow image is larger in the horizontal dimension
    // while the position and offset are the same as for the main image.
    new google.maps.Size(42,36),
    new google.maps.Point(0,0),
    new google.maps.Point(0, 36));
    // Shapes define the clickable region of the icon.
    // The type defines an HTML <area> element 'poly' which
    // traces out a polygon as a series of X,Y points. The final
    // coordinate closes the poly by connecting to the first
    // coordinate.
var shape = {
	coord: [1,1,1,24,32,24,32,1],
	type: 'poly'
};

function CornerControl(controlDiv, imgSrc) {
  // Set CSS for the control border
  var controlUI = document.createElement('DIV');
  controlDiv.appendChild(controlUI);
  // Set CSS for the control interior
  var controlText = document.createElement('DIV');
  controlText.innerHTML = '<img src="img/'+imgSrc+'" width="7" height="7" border="0" />';
  controlUI.appendChild(controlText); 
}

function codeAddress(addname,hlink,content) {
	var address;
	if (geocoder) {
	    geocoder.geocode( { 'address': addname}, function(results, status) {
		    if (status == google.maps.GeocoderStatus.OK) {
		        //map.setCenter(results[0].geometry.location);
		        //marker = new google.maps.Marker({map: EnP.map, position: results[0].geometry.location});
				var project3 = new google.maps.Marker({
				    map: TPC.map,
				    shadow: shadow,
    				icon: image,
    				shape: shape,
    			    title: hlink,
				    position: results[0].geometry.location
				});
			  	// Register event listeners to each marker to open a shared info
			    // window displaying the marker's position when clicked or dragged.
			    google.maps.event.addListener(project3 , 'click', function() {
			      TPC.openInfoWindow(project3,hlink,content,addname);
			    });
				/*document.getElementById(hlink).onclick = function() {
			      TPC.openInfoWindow(project3,hlink,content,addname);					  
			      TPC.map.setZoom(8);
			    };				    */		          	
	        } else {
	          alert("Geocode was not successful for the following reason: " + status);
	        }
	    });
	}
}  	

/**
 * Called when clicking anywhere on the map and closes the info window.
 */
TPC.closeInfoWindow = function() {
  TPC.infoWindow.close();	  
};

TPC.doZoomOut = function() { 
	var centerLatLng = new google.maps.LatLng(51.5010, -0.038);
	TPC.map.setCenter(centerLatLng);
	TPC.map.setZoom(12); 
};

/**
 * Opens the shared info window, anchors it to the specified marker, and
 * displays the marker's position as its content.
 */
TPC.openInfoWindow = function(marker,hlink,etc,postcode) {
  var markerLatLng = marker.getPosition();
  TPC.infoWindow.setContent([
    '<b>Venue:</b> '+hlink+'<br/>',
    '<b>Address:</b> '+etc+'<br/>',	    
    '<b>Postcode</b> : '+postcode+'<br/>'
  ].join(''));
  TPC.infoWindow.open(TPC.map, marker);
  TPC.map.setZoom(15);
},

/**
 * Called only once on initial page load to initialize the map.
 */
TPC.init = function() {
  //Map Style
  
  var mapStyleView = [ 
  	/*{ 	featureType: "administrative", 
  		elementType: "all", 
  		stylers: [ { visibility: "off" } ] },*/
  	{ 	featureType: "landscape", 
  		elementType: "all", 
  		stylers: [ { visibility: "simplified" } ] },
  	{ 	featureType: "water", 
  		elementType: "all", 
  		stylers: [ { visibility: "simplified" } ] },
  	{ 	featureType: "all", 
  		elementType: "geometry", 
  		stylers: [ 	{ hue: "#FFF" }, 
  					{ saturation: 12 }, 
  					{ lightness: -30 }, 
  					{ visibility: "simplified" } ] },
  	{ 	featureType: "road.highway", 
  		elementType: "labels", 
  		stylers: [ { visibility: "off" } ] },
  	 ];

  if (navigator.appVersion.indexOf("MSIE") != -1)
	  mapStyleView = '';

  // Create single instance of a Google Map.
  geocoder = new google.maps.Geocoder();
  var centerLatLng = new google.maps.LatLng(51.5010, -0.038);
  
  var mapOptions = {
    zoom: 12,
    center: centerLatLng,    
    mapTypeControl: false	    
  };
    
  TPC.map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var htmlControl, html, corners=[{cls:'TL',anchor:google.maps.ControlPosition.TOP_LEFT, img:'map_corner_top_left.png'}, {cls:'TR',anchor:google.maps.ControlPosition.TOP_RIGHT, img:'map_corner_top_right.png'}, {cls:'BL',anchor:google.maps.ControlPosition.BOTTOM_LEFT, img:'map_corner_bottom_left.png'}, {cls:'BR',anchor:google.maps.ControlPosition.BOTTOM_RIGHT, img:'map_corner_bottom_right.png'}];
	
  for(var corner in corners){
	  var homeControlDiv = document.createElement('DIV');
	  var homeControl = new CornerControl(homeControlDiv, corners[corner].img);

	  homeControlDiv.index = -2147000000;
	  homeControlDiv.id = corners[corner].cls;
	  TPC.map.controls[corners[corner].anchor].push(homeControlDiv);
  }
	
  var styledMapOptions = {
      map: TPC.map,
      name: "NVG"
  }

  var laserMapType = new google.maps.StyledMapType(mapStyleView, styledMapOptions);
  
  TPC.map.mapTypes.set('NVG', laserMapType);
  TPC.map.setMapTypeId('NVG');
  
  // Create a single instance of the InfoWindow object which will be shared
  // by all Map objects to display information to the user.
  TPC.infoWindow = new google.maps.InfoWindow();

  // Make the info window close when clicking anywhere on the map.
  google.maps.event.addListener(TPC.map, 'click', TPC.closeInfoWindow);
  
  // Make the map zoom out when closing info window
  google.maps.event.addListener(TPC.infoWindow, 'closeclick', TPC.doZoomOut);
  
  //codeAddress('E14 9SF','The Hangar','Unit 4 Wood Wharf Business Park, Prestons Road');
  codeAddress('SE7 8NJ','Bunker 51','3 Herringham Road');
  codeAddress('W1D 7DH','Star Command','7 to 14 Coventry Street, Piccadilly Circus');
}

if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", TPC.init(), false);
}
