var defaultLat = 54.86297498266524;
var defaultLng = -1.4903640747070312;
var defaultZoom = 10;
var countryToQueryGoogleWith = "United Kingdom";

var golfClubIconURL = "http://www.newsquestnortheastgolf.co.uk/images/golf-mapping/golf-marker-1.png";
var golfClubIconSize = [37,44]; //width, height
var golfClubIconAnchor = [9,37]; //x, y
var golfClubIconInfoWindowAnchor = [11, 2]; //x, y
var golfClubIconShadowURL = "http://www.newsquestnortheastgolf.co.uk/images/golf-mapping/golf-shadow-1.png";
var golfClubIconShadowSize = [37,44]; //width, height
var golfClubIconImageMap = [9,1,0,7,0,13,10,21,20,19,20,9];

//end config

var map = null;
var geocoder = null;
var markers = [];
var bounds = null;
var golfClubIcon = null;
var inactiveIcon = null;
	
function initialiseCommon()
{
  map = new GMap2(document.getElementById("map_canvas"));
  map.setCenter(new GLatLng(defaultLat, defaultLng), defaultZoom);
  bounds = new GLatLngBounds();
  
  golfClubIcon = new GIcon(G_DEFAULT_ICON);
  golfClubIcon.image = golfClubIconURL;
  golfClubIcon.shadow = golfClubIconShadowURL;
  golfClubIcon.iconSize = new GSize(golfClubIconSize[0], golfClubIconSize[1]);
  golfClubIcon.shadowSize = new GSize(golfClubIconShadowSize[0], golfClubIconShadowSize[1]);
  golfClubIcon.iconAnchor = new GPoint(golfClubIconAnchor[0], golfClubIconAnchor[1]);
  golfClubIcon.infoWindowAnchor = new GPoint(golfClubIconInfoWindowAnchor[0], golfClubIconInfoWindowAnchor[1]);
  golfClubIcon.imageMap = golfClubIconImageMap;
 
	
}

function getMarkersFromXML()
{
  GDownloadUrl(xmlResultsDocument,
			   function(doc)
			   {
			     var xmlDocument = GXml.parse(doc);
			     markers = xmlDocument.documentElement.getElementsByTagName("marker");
			     for (var i=0;i<markers.length;i++)
			     {
			       var lat = parseFloat(markers[i].getAttribute("lat"));
			       var lng = parseFloat(markers[i].getAttribute("lng"));
			       var point = new GLatLng(lat,lng);
			       bounds.extend(point);
			       var label = markers[i].getAttribute("label");
			       var image = markers[i].getAttribute("image");
				   var listingType = markers[i].getAttribute("type");
				   var companyType = markers[i].getAttribute("companyType");
			      
			       var summary = markers[i].getAttribute("summary");
			       var businessDirectoryLink = markers[i].getAttribute("businessDirectoryLink");
				   var profilePageLink = markers[i].getAttribute("profilePageLink");
				   
		
				   if (listingType=="u")
				   {
				     if (profilePageLink!="") moreInfoLink = profilePageLink;
				     else moreInfoLink = businessDirectoryLink;
				   }
				   else moreInfoLink = businessDirectoryLink;
				   
			       var availability = markers[i].getAttribute("availability");
			       //if (companyType==1) iconType = "golfClub";
				   iconType = "golfClub";
				   
			       html = "<div class=\"popup\">";
				   if (image!="") html = html+"<a href=\""+moreInfoLink+"\" target=\"_top\"><img src=\""+image+"\" class=\"thumbnail\" /></a>";
				   html = html+"<div class=\"summary";
				   if (image!="") html = html+"Short";
				   html = html+"\">";
			       html = html+"<h2><a href=\""+moreInfoLink+"\" target=\"_top\">"+label+"</a></h2>";
			       html = html+"<p>"+summary+"</p>";
				   html = html+"</div>";
				   html = html+"<div class=\"clearer\">&nbsp;</div>";
			       if ((listingType=="u") && (profilePageLink!=""))
				   {
				     html = html+"<a href=\""+profilePageLink+"\" target=\"_top\"><img src=\"../images/golf-mapping/view-profile.gif\" width=\"104\" height=\"30\" class=\"profilePageLink\" /></a>";
				   }
				   
				   html = html+"<a href=\""+businessDirectoryLink+"\" target=\"_top\"><img src=\"../images/golf-mapping/contact-details.gif\" width=\"94\" height=\"30\" class=\"businessDirectoryLink\" /></a>";
				   
				   html = html+"<div class=\"clearer\">&nbsp;</div></div>";
			       addPoint(lat,lng,iconType,html);
			       }
			       if (markers.length>1) map.setZoom(map.getBoundsZoomLevel(bounds));
				   else map.setZoom(13);
			       map.setCenter(bounds.getCenter());
			     });
}

function initialiseMiniMap(forceZoomLevel)
{
  if (GBrowserIsCompatible())
  {
    initialiseCommon();
	map.addControl(new GSmallMapControl());
    getMarkersFromXML();
	if (forceZoomLevel==true) map.setZoom(3);
  }
}

function initialiseMainMap()
{
  if (GBrowserIsCompatible())
  {
    initialiseCommon();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMenuMapTypeControl());
    getMarkersFromXML();
  }
}

function addPoint(lat,lng,iconType,infoHTML)
{
  var marker = "null";
  var point = new GLatLng(lat, lng);
  //if (iconType=="golfClub") marker = new GMarker(point,{ icon:golfClubIcon });
  marker = new GMarker(point,{ icon:golfClubIcon });
  if (infoHTML!="") GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(infoHTML);});
  map.addOverlay(marker);
}
	

