/*
Google Maps
*/

function Site(siteID, name, latitude, longitude, image) {
	
	this.siteID = siteID;
	this.name = name;
	this.latitude = latitude;
	this.longitude = longitude;
	this.image = image;
	
}
//<![CDATA[
			var data = Array();
			
			data.push(new Site('1', 'Fastaid', '52.476472', '-1.894392', ''));

			//data.push(new Site('1', 'Fastaid Solihull', '52.412843', '-1.778548', ''), new Site('2', 'Fastaid Black Country', '52.590870', '-1.994052', ''));
//]]>




var map;
var normalProjection = G_NORMAL_MAP.getProjection();

function drawCircle(centre, colour, radius) {
	var centreInPixels = normalProjection.fromLatLngToPixel(centre, map.getZoom());
	var radiusInPixels = new GPoint((centreInPixels.x + radius), (centreInPixels.y + radius));
	
	var circle = Array();
	
	with(Math) {
		var radius = floor(sqrt(pow((centreInPixels.x-radiusInPixels.x),2) + pow((centreInPixels.y-radiusInPixels.y),2)));
	
		for (var i = 0; i < 361 ; i+=0.5 ) {
			var ithRadius = i*(PI/180);
			y = centreInPixels.y + radius * sin(ithRadius)
			x = centreInPixels.x + radius * cos(ithRadius)
			var ithPoint = new GPoint(x,y);
			circle.push(normalProjection.fromPixelToLatLng(ithPoint, map.getZoom()));
		}	
	}
	
	area = new GPolygon(circle,colour,1,1,colour);
	map.addOverlay(area);
}

function createMarker(point, html, markerOptions) {
	var marker = new GMarker(point, markerOptions);
	
	GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml(html);
  	});
	
	return marker;
}

function populate() {
	
	var colours = Array();
	colours.push('#346ABA', '#8E85ED', '#AF3F53', '#F7B25A', '#346ABA', '#8E85ED', '#AF3F53', '#F7B25A');
	
	for (var i=1; i < data.length; i++) {
		
		var mwicon = new GIcon(G_DEFAULT_ICON);
		mwicon.image = "/image_assets/map/site_" + (i%5) + ".png";
		
		
		markerOptions = { icon:mwicon };

		var point = new GLatLng(data[i].latitude, data[i].longitude);
		
		var name = data[i].name;
		var html = "<div class='site-text'><p>";
		
		if (name) {
			var html = html + data[i].name + "<br />";
		}
		
		html = html + "</p></div>";
		
		marker = createMarker(point, html, markerOptions);
		
		map.addOverlay(marker);
	}
	
	
	var point = new GLatLng('52.476472', '-1.894392');
	drawCircle(point, colours[1], 130);
	
	/*
	var point = new GLatLng('52.412843', '-1.778548');
	drawCircle(point, colours[0], 60);
	
	var point = new GLatLng('52.590870', '-1.994052');
	drawCircle(point, colours[1], 60);
	*/

	
}

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng('52.476472', '-1.894392'), 10);
		map.addControl(new GSmallMapControl());
		populate();
	}
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

addEvent(window, 'load', load, false);
addEvent(window, 'onunload', GUnload, false);