// JavaScript Document
// Multi Icon Javascript Library for use with Googlemaps
// V1.0 7/3/2007 Copyright Mike Walsh dBace.

//The delcarations in capitals are constants available for use by calling programs
//They control various aspects of how the map is displayed

var SHOW_ZOOM_CONTROL=true;
var NO_SHOW_ZOOM_CONTROL=false;

var LARGE_ZOOM=false;
var SMALL_ZOOM=true;

var SHOW_TYPE=true;
var NO_SHOW_TYPE=false;

var SHOW_OVERVIEW=false;
var NO_SHOW_OVERVIEW=true;

var map;
var iconpath='';

window.onunload = unloadmap;  //Specify a function to call when the page unloads

function unloadmap(){         //Call this function when page unloads to ensure no memory leaks affect Google Map
	GUnload();
}

function registericonpath(p){  //Short cut function to regiter the icon path to save repating it in each call to
	iconpath=p;				   //drawicon
}

//This function must be called before any attempt to draw icons on the map

//lat		the latitude as a decimal 
//lng		the longitude as a decimal
//zlevel	the starting zoomlevel for the map 
//showz		whether to show the zoom control (use the constants above to set the value)
//zlarge	the size of the soom control (use constants above)
//showt		whether to show the Map Type control (use constants above)
//showo		whether to show the Overview control (use constants above)

//Example  startmap(51.6516,-1.9046, 13,SHOW_ZOOM_CONTROL,SMALL_ZOOM,SHOW_TYPE,SHOW_OVERVIEW);


function startmap(lat,lng, zlevel,showz,zlarge,showt,showo){
	if (GBrowserIsCompatible())
	{
		var point = new GLatLng(lat,lng);
		map = new GMap2(document.getElementById("map"));
		map.setCenter(point,zlevel);
		
	if (showz)								 		 //Will we show the zoom/pan control
		if (zlarge)										//Large or small zoom/pan control
			map.addControl(new GLargeMapControl());
		else
			map.addControl(new GSmallMapControl());
		
		if (showt)								  	//Will we show the map type control
		map.addControl(new GMapTypeControl());
	
	if (showo)										//Will show the Overview control
		map.addControl(new GOverviewMapControl());
	}
}

//This function places an icon marker on the map

//id	 numeric (the id of the icon - not currently used and I might remove this)
//info	 Popup Info (string) used to set the text displayed when an icon is clicked may be left blank which means the icon will not respond to clicks
//iname	 string - the filename of the icon file ( if registerpath has been used) or the full path name URL to the icon file
//iw	 numeric - the width of the icon in pixels
//ih	 numeric - the height of the icon in pixels
//sname	 string - the filename of the shadow icon file ( if registerpath has been used) or the full path name URL to the shadow icon file (may be left blank)
//sw	 numeric - the width of the shadow icon
//sh	 numeric - the height of the shadow icon
//lat	 numeric - the latitude of the shadow icon
//lng	 numeric - the longitude of the shadow icon


function drawicon(id,info,iname,iw,ih,sname,sw,sh,lat,lng){
	
	var icw=iw/2;		//Calculate the centre of the icon in pixels
	var ich=ih/2;
	//var point=new GLatLng(lat,lng); //Create a point object for the position
	var point=new GLatLng(lat,lng); //Create a point object for the position
	
	var img=new GIcon();		//Cretae the icon object
	img.image=iconpath+iname;	//assign it's image pathname URL
	img.iconSize=new GSize(iw,ih);	//Set the icon size
	img.iconAnchor=new GPoint(icw,ich);	//set the icon anchor point
	img.infoWindowAnchor=new GPoint(icw,ich);	//set the info popup anchor point

	if (sname!=""){							//Do we want to display a shadow
		img.shadow = iconpath+sname;
		img.shadowSize =  new GSize(sw, sh);
		}

	var marker = new GMarker(point,img); 	//Create the marker object with the coordinates and icon

	map.addOverlay(marker);		//Add it to the map
	
	if (info!=''){				//Is there text in the info parameter
		GEvent.addListener(marker, "click", function() {	//Yes: Create the info window object
    	marker.openInfoWindowHtml(info);
  		});
	}
}

// Helper function to geocode postcode into lat lng.
// Makes an asynchronous call to Google local search passing postcode
// and then calls drawicon with translated lat,lng

//If the postcode code not be translated it is ignored

function drawiconFromPostcode(id,info,iname,iw,ih,sname,sw,sh,postcode, dicallbackFunction) {
 var localSearch = new GlocalSearch();
 
  localSearch.setSearchCompleteCallback(null, 
    function() {
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
		dicallbackFunction(id,info,iname,iw,ih,sname,sw,sh,resultLat,resultLng);
      }else{
        // alert("Postcode not found!"); //This is commented out so as not to generate an error when postcode lookup fails
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}

// Helper function to create a Map object from postcode instead of lat lng.
// Makes an asynchronous call to Google local search passing postcode
// and then calls candrawicon() to telling main program that the Map object is ready


function startmapFromPostcode(postcode,zlevel,showz,zlarge,showt,showo, smcallbackFunction) {
 var localSearch = new GlocalSearch();
 
  localSearch.setSearchCompleteCallback(null, 
    function() {
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
		smcallbackFunction(resultLat,resultLng,zlevel,showz,zlarge,showt,showo);
		candrawicons();
      }else{
        // alert("Postcode not found!"); //This is commented out so as not to generate an error when postcode lookup fails
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}


//This function places an icon marker on the map using a postcode location
//It has the same parameters as drawicon with the exception that the Lat, Lng paramaters
//are replaced by a postcode.

function drawiconpc(id,info,iname,iw,ih,sname,sw,sh,postcode){
	drawiconFromPostcode(id,info,iname,iw,ih,sname,sw,sh,postcode,drawicon);
}

// This function enables an initial map area to be defined centred on a postcode in contrast to startmap which
// required a lat and lng to be supplied.

// **************************** N O T E ********************************************

// If this function is used to start the map then it is necessary to take special steps to "drawicons" on to the map 
// in question. Because the geocode lookup in Google is asynchronous the calling code has to know that the lookup
// is complete and that the Map object has been created before calling any drawicon code. 
//
// This is achieved by making a callback to a hardcoded function name candrawicons() once the Map object is ready.
// Thus all drawicon code in the main program should be "wrapped" in the function candrawicons().

// Here is an example of how to call these routines from the main program which should make everything clear

// startmappc('SN5 6PX', 13,SHOW_ZOOM_CONTROL,SMALL_ZOOM,SHOW_TYPE,SHOW_OVERVIEW);
//
// function candrawicons(){
//   registericonpath('http://www.filterfeeder.co.uk/images/');
//   drawicon(1,'Weblinks','office.png',42,48,'shad.png',0,0,51.65160,-1.90460);
//   drawicon(1,'You can fish here','fishing.png',32,40,'',0,0,51.65740,-1.91040);
//   drawicon(1,'Bird watching','duck.png',28,19,'',0,0,51.66280,-1.93480);
//   drawicon(1,'','house.png',30,22,'shad.png',0,0,51.65000,-1.90970);
//   drawiconpc(1,'Computer','best_robust.jpg',60,70,'',0,0,'SN6 6PX');
//   drawiconpc(1,'Cow','cow.png',40,30,'',0,0,'SN6 6PQ');
// }

function startmappc(postcode,zlevel,showz,zlarge,showt,showo){
	startmapFromPostcode(postcode,zlevel,showz,zlarge,showt,showo,startmap);
}