var map_element;

var map_obj = null;
var small_map = null;

var map_default_lat;
var map_default_long;
var map_default_zoom = null;
var map_projection = G_NORMAL_MAP.getProjection();
var circleLine;
var circleLine2;
var mapPoints = Array();
var centrePoint;
var searchRadiusPoint;
var markerManager;
var map_directions;
var map_directions_panel = null;
var map_directions_to = null;
var map_directions_from = null;

function getLatLongBounds() {
	
	return new GLatLngBounds(centrePoint, searchRadiusPoint);
	
}

function initMap(map_element) {
	
	if (map_element == 'gmapSmall') {
		var small_map = true;
	}
	
	map_element = $(map_element);
	
	if (!map_element) {
		
		return false;
		
	}
	
	if (!GBrowserIsCompatible()) {
		return false;
	}

	map_obj = new GMap2(map_element);
	
	if (map_default_lat && map_default_long) {
		
		map_obj.setCenter(new GLatLng(map_default_lat, map_default_long), 7); // initial zoom level will get set below
		
	}
	else {
		
		map_obj.setCenter(new GLatLng(56.072035, -101.425781), 3); // initial zoom level will get set below
		
	}
	
	if (!small_map) {
		map_obj.addControl(new GLargeMapControl());
		map_obj.addControl(new GMapTypeControl());
		map_obj.addControl(new GOverviewMapControl());
	}
	else {
		map_obj.addControl(new GSmallMapControl());
	}
	
	GEvent.addListener(map_obj, "zoomend", zoomEnd);
	
	drawCircles();
	
	// set the initial zoom level
	if (circleLine) {
		map_obj.setZoom(map_obj.getBoundsZoomLevel(circleLine.getBounds()));
	}
	else if (map_default_zoom) {
		map_obj.setZoom(map_default_zoom);
	}
		
	if (!mapPoints.length) {
		
		return true;
		
	}
	
	markerManager = new GMarkerManager(map_obj);
	
	for (i=0; i < mapPoints.length; i++) {
		
		// set this based 
		var recentre = (!map_default_lat || !map_default_long) && mapPoints.length == 1 ? true : false;
			
		createMarker(mapPoints[i], i, markerManager, recentre);
		
	}
	
	if (map_directions_from) {
		
		get_directions(map_directions_from);
		
	}
	
	return true;
	
}

function get_directions(starting_point) {
	
	if (!map_directions_panel || !map_directions_to) {
		
		return false;
		
	}
	
	panel = document.getElementById(map_directions_panel);
	
	if (!map_directions) {
		
		map_directions = new GDirections(map_obj, panel);
		
	}
	
	map_directions.load(starting_point + ' to ' + map_directions_to);
	
}

var baseIcon = new GIcon();
baseIcon.shadow = 'images/map_point_shadow.png';
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

function createMarker(obj, index, manager, recentre) {

	var geocoder = new GClientGeocoder();
	
	var icon = new GIcon(baseIcon);
	icon.image = obj.private ? 'images/icong_private.png' : 'images/icong.png';
	
	if (!obj.address) {
		obj.address = null;
	}
	
	geocoder.getLatLng(obj.address, function(point) {
		
		if (!point) {
			
			point = new GLatLng(obj.latitude, obj.longitude);
			
		}
		
		var marker = new GMarker(point, {title: obj.title, icon: icon});
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(obj.html);
		});
		
		manager.addMarker(marker, 1);
		
		obj.marker = marker;
		
		if (recentre) {
			map_obj.setCenter(point, 14);
		}
		
	});
	
	return obj.marker;
	
}

function drawCircles() {
	
	if (centrePoint && searchRadiusPoint) {
		
		drawCircle();
		drawFilledCircle();
		
	}
	
}

function hideCircles() {
	
	if (circleLine) {
		circleLine.hide();
	}
	
	if (circleLine2) {
		circleLine2.hide();
	}
	
}

function drawCircle() {
	var map = map_obj;
	var normalProj = map_projection;
	
	var zoom = map.getZoom();
	var centerPt = normalProj.fromLatLngToPixel(centrePoint, zoom);
	var radiusPt = normalProj.fromLatLngToPixel(searchRadiusPoint, zoom);
	
	var circlePoints = Array();

	with (Math) {
		var radius = floor(sqrt(pow((centerPt.x-radiusPt.x),2) + pow((centerPt.y-radiusPt.y),2)));

		for (var a = 0 ; a < 361 ; a+=10 ) {
			var aRad = a * (PI/180);
			y = centerPt.y + radius * sin(aRad)
			x = centerPt.x + radius * cos(aRad)
			var p = new GPoint(x,y);
			circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
		}
		
		if (circleLine) {
			map.removeOverlay(circleLine);
		}
		
		circleLine = new GPolyline(circlePoints,'#009900', 2, 1);
		map.addOverlay(circleLine);
	}
	
	return circleLine;
}

function drawFilledCircle() {
	var map = map_obj;
	var normalProj = map_projection;
	
	var zoom = map.getZoom();
	
	var centerPt = normalProj.fromLatLngToPixel(centrePoint, zoom);
	var radiusPt = normalProj.fromLatLngToPixel(searchRadiusPoint, zoom);
	
	var circlePoints = Array();

	with (Math) {
		var radius = floor(sqrt(pow((centerPt.x-radiusPt.x),2) + pow((centerPt.y-radiusPt.y),2)));
		var thickness = min(255,radius);
		
		for (var n = 1 ; n < floor(2* radius / thickness)+1 ; n++ ) {
			radiusB = radius - (thickness / 2) * n;
			
			for (var a = 0 ; a < 361 ; a+=10 ) {
				var aRad = a*(PI/180);
				y = centerPt.y + radiusB * sin(aRad)
				x = centerPt.x + radiusB * cos(aRad)
				var p = new GPoint(x,y);
				circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
			}
			
			if (circleLine2) {
				map.removeOverlay(circleLine2);
			}
			
			circleLine2 = new GPolyline(circlePoints,'#009900', thickness, 0.2);
			map.addOverlay(circleLine2);
		}
	}
}

function toggleRadiusOverlay(on) {
	
	if (on) {
		
		drawCircles();
		
	}
	else {
		
		hideCircles();
		
	}
	
}



function zoomEnd(oldZoom,newZoom) {
	
	// need to redraw this on zoom
	
	if (circleLine && map_obj.getBoundsZoomLevel(circleLine.getBounds()) +1 < map_obj.getZoom()) {
		
		toggleRadiusOverlay(false);
		
	}
	else if (circleLine && true) {
		
		toggleRadiusOverlay(true);
		
	}
	
	
}

window.onunload = GUnload;


