// JavaScript Document

var img_url = '/images';
var map;
var geocoder;

function initialize() 
{
  var map_options = 
  {
    zoom: 6,
    center: new google.maps.LatLng(54.559322587438636, -4.21875),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("googleMap"), map_options);
  geocoder = new google.maps.Geocoder();
}

function initializePrint() 
{
  var map_options = 
  {
    zoom: 6,
    center: new google.maps.LatLng(54.559322587438636, -4.21875),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  map = new google.maps.Map(document.getElementById("googleMap"), map_options);
  geocoder = new google.maps.Geocoder();
}
		
// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) 
{
  map.clearOverlays();

  if (!response || response.Status.code != 200) 
  {
    alert("Sorry, we were unable to geocode that address");
  } 
  else 
  {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br />' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
  }
}

function showAddress(address) 
{
  if (geocoder) 
  {
    geocoder.getLatLng(
      parseAddress(address),//  take out telephone number
      function(point) 
      {
        if (!point) 
        {
          alert(address + " not found");
        } 
        else 
        {
          map.setCenter(point, 12);
          //var marker = new GMarker(point);
          //  map.addOverlay(marker);

          var perrysIcon = new GIcon(G_DEFAULT_ICON);
	  var marker = new GMarker(point,perrysIcon);
			 
	  perrysIcon.iconSize = new GSize(30,49);
	  perrysIcon.iconAnchor=new GPoint(0,49);
	  perrysIcon.shadowSize = new GSize(98,49);
      	  perrysIcon.infoWindowAnchor=new GPoint(25,10);
	  //   map.reset(point, null, 400, null, null);reset(point, tabs, size, offset?, selectedTab?)

          perrysIcon.image = img_url+"/googlemaps/icon_marker.png";
          perrysIcon.shadow = img_url+"/googlemaps/marker_shadow.png";

	  //marker = { icon:perrysIcon };
	  // map.addOverlay(new GMarker(point, perrysIcon));
			
	  map.addOverlay(marker);
          marker.openInfoWindowHtml('<div>'+address.replace(/,/g,'<br />')+'</div>',{pixelOffset:new GSize(32,5), maxWidth:540});
	 
      	  //perrysIcon.openInfoWindowHtml(address);
        }
      }
    );
  }
}

/*function showPoint(glat,glong,address) {*/
function showPoint(glong, glat, address) 
{
  var point = new google.maps.LatLng(glat, glong);
  map.setCenter(point);
  map.setZoom(12);
  //var marker = new GMarker(point);
  //  map.addOverlay(marker);

  /*var perrysIcon = new GIcon(G_DEFAULT_ICON);
  var marker = new GMarker(point,perrysIcon);*/

  var perrysIcon = new google.maps.MarkerImage(img_url + '/googlemaps/icon_marker.png',
                                               new google.maps.Size(30,49),
                                               new google.maps.Point(0,0),
                                               new google.maps.Point(0,49));
  var perrysShadow = new google.maps.MarkerImage(img_url + '/googlemaps/marker_shadow.png',
                                                 new google.maps.Size(98,49),
                                                 new google.maps.Point(0,0),
                                                 new google.maps.Point(0,49));
			 
			 
  /*perrysIcon.iconSize = new GSize(30,49);
  perrysIcon.iconAnchor=new GPoint(0,49);
  perrysIcon.shadowSize = new GSize(98,49);
  perrysIcon.infoWindowAnchor=new GPoint(25,10);*/
  //   map.reset(point, null, 400, null, null);reset(point, tabs, size, offset?, selectedTab?)
  //marker = { icon:perrysIcon };
  // map.addOverlay(new GMarker(point, perrysIcon));

  var marker = new google.maps.Marker({
    position: point,
    map: map,
    shadow: perrysShadow,
    icon: perrysIcon
  });

  if (address != '') {
    var infoContent = '<div>' + address.replace(/,/g,'<br />') + '</div>';

    var infoWindow = new google.maps.InfoWindow({
      content: infoContent,
      pixelOffset: new google.maps.Size(32,5),
      maxWidth:540
    });

    infoWindow.open(map, marker);
  }
			
//  map.addOverlay(marker);
/*
  marker.openInfoWindowHtml('<div>'+address.replace(/,/g,'<br />')+'</div>',{pixelOffset:new GSize(32,5), maxWidth:540});
*/			 
  //perrysIcon.openInfoWindowHtml(address);
}

function showPointPrint(glong, glat) 
{
  var point = new GLatLng(glat, glong);
  map.setCenter(point, 12);
                      
  var perrysIcon = new GIcon(G_DEFAULT_ICON);
  var marker = new GMarker(point,perrysIcon);
			 
  perrysIcon.iconSize = new GSize(30,49);
  perrysIcon.iconAnchor=new GPoint(0,49);
  perrysIcon.shadowSize = new GSize(98,49);
  //perrysIcon.infoWindowAnchor=new GPoint(25,10);
  perrysIcon.image = img_url+"/googlemaps/icon_marker.png";
  perrysIcon.shadow = img_url+"/googlemaps/marker_shadow.png";
  map.addOverlay(marker);
}
    
/* Set onclick events for dealer lins */
function setDlrLinks()
{
  var dlrArr = document.getElementById('dlrMap').getElementsByTagName('a');

  for(var i =0; i < dlrArr.length; i++)
  {
    dlrArr[i].onclick=function()
    {
      //alert(this.getAttribute('href'));
      //alert(this.getAttribute('title'));
      //findLocation(this.getAttribute('title'));
      showAddress(this.getAttribute('title'));
      return false;
    }
  }
}

function parseAddress(addStr) 
{
  // remove telephone number
  var addressHTML = addStr.replace(/(Tel : [\d\-\s]*)$/,'');
  return addressHTML;
}

