// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.recornwall.org.uk/icons/_shadow.png";
baseIcon.iconSize = new GSize(12, 20);
baseIcon.shadowSize = new GSize(22, 20);
baseIcon.iconAnchor = new GPoint(6, 20);
baseIcon.infoWindowAnchor = new GPoint(5, 1);


var iconashp = new GIcon(baseIcon);  // takes baseIcon params for settings
iconashp.image = 'http://www.recornwall.org.uk/icons/pin-ashp.png';

var iconbiomass = new GIcon(baseIcon);
iconbiomass.image = 'http://www.recornwall.org.uk/icons/pin-biomass.png';

var icongshp = new GIcon(baseIcon);
icongshp.image = 'http://www.recornwall.org.uk/icons/pin-gshp.png';

var iconlfg = new GIcon(baseIcon);
iconlfg.image = 'http://www.recornwall.org.uk/icons/pin-lfg.png';

var icononshorewind = new GIcon(baseIcon);
icononshorewind.image = 'http://www.recornwall.org.uk/icons/pin-onshore-wind.png';

var iconsmallhydro = new GIcon(baseIcon);
iconsmallhydro.image = 'http://www.recornwall.org.uk/icons/pin-small-hydro.png';

var iconsolarpv = new GIcon(baseIcon);
iconsolarpv.image = 'http://www.recornwall.org.uk/icons/pin-solar-pv.png';

var iconsolarthermal = new GIcon(baseIcon);
iconsolarthermal.image = 'http://www.recornwall.org.uk/icons/pin-solar-thermal.png';

var iconsewagegas = new GIcon(baseIcon);
iconsewagegas.image = 'http://www.recornwall.org.uk/icons/pin-sewage-gas.png';

var iconwshp = new GIcon(baseIcon);
iconwshp.image = 'http://www.recornwall.org.uk/icons/pin-wshp.png';

var iconbio = new GIcon(baseIcon);
iconbio.image = 'http://www.recornwall.org.uk/icons/pin-bio.png';

var iconefw = new GIcon(baseIcon);
iconefw.image = 'http://www.recornwall.org.uk/icons/pin-efw.png';

var iconchp = new GIcon(baseIcon);
iconchp.image = 'http://www.recornwall.org.uk/icons/pin-chp.png';


// MapIconmaker - OPTIONS set in clustermarker js file


var customIcons = [];
customIcons["ashp"] = iconashp;
customIcons["biomass"] = iconbiomass;
customIcons["gshp"] = icongshp;
customIcons["lfg"] = iconlfg;
customIcons["onshore-wind"] = icononshorewind;
customIcons["small-hydro"] = iconsmallhydro;
customIcons["solar-pv"] = iconsolarpv;
customIcons["solar-thermal"] = iconsolarthermal;
customIcons["sewage-gas"] = iconsewagegas;
customIcons["wshp"] = iconwshp;

customIcons["bio"] = iconbio;
customIcons["efw"] = iconefw;
customIcons["chp"] = iconchp;

	

function load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(map_cent_lat, map_cent_lng), map_zoom);
		
		var markersArray=[];
		
		for (var i = 0; i < markers.length; i++) {
			var point = new GLatLng(parseFloat(markers[i].lat), parseFloat(markers[i].lng));
			var marker = createMarker(
								 point, 
								 markers[i].id, 
								 markers[i].name, 
								 markers[i].place, 
								 markers[i].technology, 
								 markers[i].installs, 
								 markers[i].capacity, 
								 markers[i].title,
								 markers[i].icon
								 );			
			markersArray.push(marker);
		}
		
		// we are using the clustermarker js to group icons
		// setting are in the cluster marker file
		// uses Google's mapiconmaker
		// http://gmaps-utility-library.googlecode.com/svn/trunk/mapiconmaker/1.1/docs/reference.html
		
		cluster=new ClusterMarker(map, {markers:markersArray, clusterMarkerTitle:'%count installations: click for details' , clusterMarkerClick:myClusterClick });
		
		cluster.refresh();
		
	} else {
		alert("Sorry, Google Maps is not compatible with this web browser");
	}
}

function xtractFile_sans(data){
	var m = data.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
	return {path: m[1], file: m[2]}
}
    
    
    
function myClusterClick(args) {
	
	cluster.defaultClickAction=function(){
		map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds))
		delete cluster.defaultClickAction;
	}
	
	// set height of box up to 8ems depending on number of items
	var clusterBoxHeight = args.clusteredMarkers.length + 2;
	if (clusterBoxHeight > 8) clusterBoxHeight = 8; 
	
	var html='<div style="height:' + clusterBoxHeight  + 'em; overflow:auto; width:20em"><span class="clusterBox"><b>'+args.clusteredMarkers.length+' installations:<\/b><br \/><ul>';
	
	for (i=0; i<args.clusteredMarkers.length; i++) {
		html+='<li class="';
		
		iconURL = args.clusteredMarkers[i].getIcon().image; // returns: http://www.recornwall.org.uk/icons/pin-ashp.png
		URLbits = xtractFile_sans(iconURL);
		iconCode = URLbits.file;
		html+=iconCode.substring(4);  // remove 'pin-' bit with substring - eg pin-ashp
		
		html+='">';
		html+='<a href="javascript:cluster.triggerClick('+args.clusteredMarkers[i].index+')">';
		html+=args.clusteredMarkers[i].getTitle();
		html+='<\/a>';
		html+='<\/li>';
	}
	
	html+= '<\/ul><\/span>';
	args.clusterMarker.openInfoWindowHtml(html);
} 


   

function createMarker(point, id, name, place, technology, installs, capacity, title, icon) {
	
	var marker = new GMarker(point, {title:title, icon:customIcons[icon]} );
	
	var html = '<p class="bubbleBox" style="background-image:url(/icons/big-'	+ icon + '.gif);">' 
	+ '<b>' + name + '<\/b><br\/>'
	+ place + '<br\/>'
	+ 'Installs: ' + installs + '<br\/>'
	+ 'Technology: ' + technology + '<br\/>'
	+ 'Capacity: ' + capacity + '<br\/>'
	+ '<\/p>';
	
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}
