var map;
var geocoder;
var melbourne_cbd = new google.maps.LatLng(-37.815, 144.970);
/**
 * The OpenLargeMap adds a control to the map that opens
 * a larger map with the shop address in a new window. This constructor takes
 * the control DIV as an argument.
 */

function OpenLargeMap(controlDiv, map) {

  // Setting padding to 5 px will offset the control
  // from the edge of the map
  controlDiv.style.padding = '5px';

  // Set CSS for the control border
  var controlUI = document.createElement('DIV');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to open large Map';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior
  var controlText = document.createElement('DIV');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = 'Large Map';
  controlUI.appendChild(controlText);

  google.maps.event.addDomListener(controlUI, 'click', function() {
    window.open('http://maps.google.com/maps?q='+ addr);
  });
}
/**
 * initalize() loads up a google map object
 * and loads it up with the shop address.
 * If the address fails to geocode, it opens up a 
 * bubble stating the address could not be found.
 * Also adds custom button "Large map".
 */

function initialize() {
    geocoder = new google.maps.Geocoder();
    var mapDiv = document.getElementById('map-canvas');
    var mapOptions = {
         zoom: 15,
         mapTypeControl: false,
         navigationControlOptions: {
                   position: google.maps.ControlPosition.TOP_LEFT
         },
         mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapDiv, mapOptions);

    if (geocoder) {
      geocoder.geocode( { 'address': addr}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
	    var melb_marker = new google.maps.Marker({
		    map: map,
		    position: melbourne_cbd,
		    title:"Melbourne"
		});
	    var contentString = 'Address could not be located';
	    var infowindow = new google.maps.InfoWindow({
                               content: contentString
                            });
	    map.setCenter(melbourne_cbd);
	    infowindow.open(map,melb_marker);
       } 
      });
    }
    var openLargeMapDiv = document.createElement('DIV');
    var largeMapControl = new OpenLargeMap(openLargeMapDiv, map);

    openLargeMapDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_RIGHT].push(openLargeMapDiv);
}
google.maps.event.addDomListener(window, 'load', initialize);

function setDirections(fromAddress, toAddress) {
    window.open('http://maps.google.com.au/maps?f=d&source=s_d&saddr='+fromAddress+'&daddr='+toAddress);
}