var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();

// V1.0 - Brush
//
//icon.image = "http://www.pubtoilets.com/images/map-toilet-brush.png";
//icon.shadow = "http://www.pubtoilets.com/images/map-toilet-brush-shadow.png";
//icon.infoWindowAnchor = new GPoint(15,6);

//V2.0 - Toilet
//
icon.image = "http://www.pubtoilets.com/images/map-toilet.png";
icon.shadow = "http://www.pubtoilets.com/images/map-toilet-shadow.png";
icon.infoWindowAnchor = new GPoint(15,18);

icon.iconSize = new GSize(40, 32);
icon.shadowSize = new GSize(40, 32);
icon.iconAnchor = new GPoint(15, 31);

var markerY = 1;
var getY;

var dot = new GIcon();
dot.image = "http://www.pubtoilets.com/images/map-dot.png";
//dot.shadow = "http://www.pubtoilets.com/images/map-dot-shadow.png";
dot.infoWindowAnchor = new GPoint(3,3);

dot.iconSize = new GSize(7, 7);
//dot.shadowSize = new GSize(40, 32);
dot.iconAnchor = new GPoint(3, 3);

function usePointFromPostcode(postcode, country, callbackFunction, Y, usepubname, usepubtown, usepubid) {

	getY = Y;

	localSearch.setSearchCompleteCallback(null, 
		function(Y, usepubname, usepubtown, usepubid) {
			
			if (localSearch.results[0])
			{	
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				var goY = getY;
				callbackFunction(point, goY);
			}else{
				//alert("Postcode not found!");
			}
		});	
	
	localSearch.execute(postcode + ", " + country);
}

function createMarker2008(point1,title1,html1) {
	
	var opts1 = new Object();
	  opts1.clickable = true;
	  opts1.icon = icon;
	  opts1.title = title1;
	  
	  var markera1 = new GMarker(point1,opts1);
        return markera1;
}

function createMarkerDot(point1,title1,html1) {
	
	var opts1 = new Object();
	  opts1.clickable = true;
	  opts1.icon = dot;
	  opts1.title = title1;
	  
	  var markera1 = new GMarker(point1,opts1);
        return markera1;
}

function addToMap(pointAdd, titleAdd, textAdd)
{
	markerAdd = createMarker2008(pointAdd, titleAdd, textAdd);
	  map.addOverlay(markerAdd);
	  var htmlout=(""+ textAdd +"");
	  markerAdd.bindInfoWindowHtml(htmlout);
}

function addToMapDot(pointAdd, titleAdd, textAdd)
{
	markerAdd = createMarkerDot(pointAdd, titleAdd, textAdd);
	  map.addOverlay(markerAdd);
	  var htmlout=(""+ textAdd +"");
	  markerAdd.bindInfoWindowHtml(htmlout);
}

function setCenterToPoint(point, Y)
{
	map.setCenter(point, Y);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_NORMAL_MAP);
		
		map.addControl(new GOverviewMapControl());
			
		map.enableDragging();
		
		map.enableInfoWindow()

		
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(startMapping);
addUnLoadEvent(GUnload);

function startMapping()
{	
mapLoad();
zoomTown();
}

function postcodePlot(postcode, pubName, pubTown, pubCountry, pubID, theYear)
{
	markerY=markerY+1;
	pubname[markerY] = pubName;
	pubtown[markerY] = pubTown;
	pubid[markerY] = pubID;
	year = theYear;

usePointFromPostcode(postcode, placeMarkerAtPoint, markerY, pubName, pubTown, pubID);

}

function getZoom(town, country)
{
	if(country == "UK" && (town in {"Helston":1, "Wimborne":1, "Leeds":1, "Leyland":1}))
		return 11;
	else if(country == "UK" && (town in {"Hartfield":1, "Macclesfield":1, "Milton Keynes":1, "Seascale":1, "W1W 5QU":1, "Cheadle":1, "Preston":1, "Nottingham":1, "Leek":1, "Birmingham":1, "Cardiff":1, "Bakewell":1}))
		return 12;
	else if(country == "UK" && (town in {"Tytherington":1, "Speke":1, "Wilmslow":1, "Northwich":1, "Chapel-en-le-Frith":1, "Salford":1, "Blackpool":1, "Llandudno":1, "Bamford":1, "Poynton":1}))
		return 13;
	else if(country == "Germany" && (town in {"Munich":1}))
		return 13;
	else if(country == "Poland" && (town in {"Warsaw":1, "Lodz":1}))
		return 12;
	else if(country == "UK" && (town in {"Buxton":1}))
		return 15;
	else if(country == "Australia" && (town in {"Merimbula":1}))
		return 15;
	else if(country == "France" && (town in {"Bergerac":1}))
		return 12;
	else if(country == "UK" && (town in {"Keswick":1}))
		return 16;
	else if (country == "WORLD")
		return 1;
	else if (country == "AUS" && town == "Australia") // correct Aussie Map fault
		return 4;
	else if (country == "Canada" && country == town)
		return 4;
	else if (country == "Eire" && country == town)
		return 6;
	else if (country == "Portugal" && country == town)
		return 6;
	else if (country == "Germany" && country == town)
		return 5;
	else if (country == "The Gambia" && country == town)
		return 8;
	else if (country == "Wales" && country == town)
		return 7;
	else if (country == "England" && country == town)
		return 5;
	else if (country == "Scotland" && country == town)
		return 6;
	else if (country == "UK" && country == town)
		return 5;
	else if (country == "France" && country == town)
		return 5; //  Default Country Zoom
	else if (country == town)
		return 4; //  Default Country Zoom
	else		
		return 14; // Default Town Zoom
}

function townSet(town, country)
{
	if (country == "" || country == null)
		country = "UK";
		
	var tCountry = country;
	
	if (country == "England" || country == "Wales" || country == "Scotland" || country == "Northern Ireland")
		tCountry = "UK";

	if(town == "WORLD" && country == "WORLD")
		usePointFromPostcode("Morocco", "Morocco", setCenterToPoint, 1);
	else
		usePointFromPostcode(town, tCountry, setCenterToPoint, getZoom(town, country));
}

function SetMapToLatLng(town, country, lat, lng)
{
	var point = new GLatLng(lat,lng);
	map.setCenter(point, getZoom(town, country));
}


