var map;
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.chromaonline.com/design/chroma/images/ui/googlemaps/shadow50.png";
baseIcon.iconSize = new GSize(18, 33);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 33);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
var teacherIcon = new GIcon(baseIcon);
var workshopIcon = new GIcon(baseIcon);
teacherIcon.image = "http://www.chromaonline.com/design/chroma/images/ui/googlemaps/pin-gold.png";
workshopIcon.image = "http://www.chromaonline.com/design/chroma/images/ui/googlemaps/pin-purple.png";

// load the map depending on the search type selected
function loadMap(searchMapType, startLat, startLng, startZoom){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.setCenter(new GLatLng(startLat,startLng), startZoom);
		switch (searchMapType) {
			case 1:
			// keyword search for teachers only
			teachers.each(function(teacher){
				map.addOverlay(createTeacherMarker(teacher, true));
			});
			break;
			case 2:
			// keword search for classes/workshops only
			workshops.each(function(workshop){
				map.addOverlay(createWorkshopMarker(workshop, true));
			});
			break;
			case 3:
			// browse by teacher name
			teachers.each(function(teacher){
				map.addOverlay(createTeacherMarker(teacher, true));
			});
			break;
			default:
			// defalt show all teachers and classes/workshops
			teachers.each(function(teacher){
				map.addOverlay(createTeacherMarker(teacher, true));
			});			
			workshops.each(function(workshop){
				map.addOverlay(createWorkshopMarker(workshop, true));
			});			
		}
		Event.observe(window, 'unload', GUnload);
	}
}

function createTeacherMarker(teacher){
	var tmarker = new GMarker(new GLatLng(teacher["lat"], teacher["lng"]), teacherIcon);
	if (teacher["olat"] != '' && teacher["olng"] != '') {
		if(teacher["olat"] != teacher["lat"] && teacher["olng"] != teacher["lng"]) {
			var polyline = new GPolyline([
				new GLatLng(teacher["olat"], teacher["olng"]),
				new GLatLng(teacher["lat"], teacher["lng"])
			], "#FF0000", 2);
			map.addOverlay(polyline);
		}
	}
	GEvent.addListener(tmarker, "click", function() {
		if (map.getZoom() < 9){
			map.setZoom(9);
		}
		map.panTo(tmarker.getPoint());
		var infoHtml = "<p><strong><a href=\"" + teacher["url"] + "\">" + teacher["name"] + "</a></strong><br />";
		infoHtml = infoHtml + "<strong>" + teacher["title"] + "</strong><br />";
		infoHtml = infoHtml + "<strong>" + gmLang["address"] + "</strong> " + teacher["address"] + "<br /><br />";
		// this is just to make sure things are nice and tidy on output
		if (teacher["brands"] != '' || teacher["techniques"] != '' || teacher["mediums"] != '') {			
			if (teacher["brands"] != '') {
				infoHtml = infoHtml + "<strong>" + gmLang["brands"] + "</strong>" + teacher["brands"] + "<br />";
			}
			if (teacher["techniques"] != '') {
				infoHtml = infoHtml + "<strong>" + gmLang["techniques"] + "</strong>" + teacher["techniques"] + "<br />";
			}
			if (teacher["mediums"] != '' ) {
				infoHtml = infoHtml + "<strong>" + gmLang["mediums"] + "</strong>" + teacher["mediums"] + "<br />";
			}			
		}
		infoHtml = infoHtml + "<br /><a href=\"" + teacher["url"] + "\">" + gmLang["more"] + "</a>. <a href=\"javascript:map.setZoom(15);\">" + gmLang["zoomin"] + "</a> or <a href=\"javascript:map.setZoom(4);\">" + gmLang["zoomout"] + "</a>.</p><br /><br />";
		tmarker.openInfoWindowHtml(infoHtml);
	});
	return tmarker;
}
function createWorkshopMarker(workshop){
	var wmarker = new GMarker(new GLatLng(workshop['lat'], workshop['lng']), workshopIcon);
	if (workshop["olat"] != '' && workshop["olng"] != '') {
		if(workshop["olat"] != workshop["lat"] && workshop["olng"] != workshop["lng"]) {
			var polyline = new GPolyline([
				new GLatLng(workshop["olat"], workshop["olng"]),
				new GLatLng(workshop["lat"], workshop["lng"])
			], "#FF0000", 2);
			map.addOverlay(polyline);
		}
	}
	GEvent.addListener(wmarker, "click", function() {
		if (map.getZoom() < 12){
			map.setZoom(12);
		}
		map.panTo(wmarker.getPoint());
		var infoHtml = "<p><strong><a href=\"" + workshop["url"] + "\">" + workshop["name"] + "</a></strong><br /><br />";
		infoHtml = infoHtml + "<strong>Teacher: <a href=\"" + workshop["teacherUrl"] + "\">" + workshop["teacher"] + "</a></strong><br />";
		if (workshop["time"] != '') {
			infoHtml = infoHtml + "<strong>When</strong>: " + workshop["time"] + "<br />";
		}
		//if (workshop["description"] != '') {
		//	infoHtml = infoHtml + "<strong>What</strong>: " + workshop["description"] + "<br />";
		//}
		if (workshop["address"] != '') {
			infoHtml = infoHtml + "<strong>Where</strong>: " + workshop["address"] + "<br />";
		}
		infoHtml = infoHtml + "<br />";
		// this is just to make sure things are nice and tidy on output
		if (workshop["brands"] != '' || workshop["techniques"] != '' || workshop["mediums"] != '') {			
			if (workshop["brands"] != '') {
				infoHtml = infoHtml + "<strong>" + gmLang["brands"] + "</strong>" + workshop["brands"] + "<br />";
			}
			if (workshop["techniques"] != '') {
				infoHtml = infoHtml + "<strong>" + gmLang["techniques"] + "</strong>" + workshop["techniques"] + "<br />";
			}
			if (workshop["mediums"] != '' ) {
				infoHtml = infoHtml + "<strong>" + gmLang["mediums"] + "</strong>" + workshop["mediums"] + "<br />";
			}			
		}
		infoHtml = infoHtml + "<br />";
		infoHtml = infoHtml + "<a href=\"" + workshop["url"] + "\">" + gmLang["more"] + "</a>. <a href=\"javascript:map.setZoom(15);\">" + gmLang["zoomin"] + "</a> or <a href=\"javascript:map.setZoom(4);\">" + gmLang["zoomout"] + "</a>.</p><br /><br />";
		wmarker.openInfoWindowHtml(infoHtml);
	});
	return wmarker;
}

