// JavaScript Document

if (window.location.hostname.indexOf('dev') == -1 && window.location.hostname.indexOf('uat') == -1) {
  var img_url = 'http://cdn.perrys.co.uk/d/perrys/images';
} else {
  var img_url = 'http://'+window.location.hostname+'/images';
}

/*function makeMap(){
			var ele = document.createElement('div');
			ele.setAttribute('id','googleMap');
			document.getElementById('dlrMap').appendChild(ele);
			document.getElementById('googleMap').style.position='relative';
}*/
var map;
var geocoder;

function initialize() 
{
  //setDlrLinks();
  //	makeMap();
  map = new GMap2(document.getElementById("googleMap"));
  map.setCenter(new GLatLng(54.559322587438636, -4.21875), 6);
	
  geocoder = new GClientGeocoder();
  //var mapControl = new GMapTypeControl();
  //map.addControl(mapControl);
  map.addControl(new GLargeMapControl());
}

function initializePrint() 
{
  map = new GMap2(document.getElementById("googleMap"));
  map.setCenter(new GLatLng(54.559322587438636, -4.21875), 6);
	
  geocoder = new GClientGeocoder();
}
		
// 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 GLatLng(glat, glong);
  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 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;
}
