var map;
var active_id;
var active_icon;
var tooltip;
var center_icon;
var points = [];


// Get the correct XML.
function getXMLEdit(map, id, lang_id) {
	getXML(map, id, lang_id, 1);
}

function getXMLUser(map, id, lang_id, userid) {
	getXML(map, id, lang_id, 0, userid);
}

function getXML(map, id, lang_id, edit_mode, userid) {
	if (map) {
		$('#loading').show();	
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var map_el = document.getElementById('map');

		var url = "/getxml.php?width="+map_el.offsetWidth+"&left="+southWest.lng()+"&right="+northEast.lng()+"&top="+southWest.lat()+"&bottom="+northEast.lat()+"&lang_id="+lang_id;

		if (edit_mode)
			url += '&only_id='+id;

		if (userid)
			url += '&userid='+userid;

		readMap(url, map, id, edit_mode);
	}
}

// A function to read the data
function readMap(url, map, id, edit_mode) {
	gmarkers = [];
	htmls = [];
	points = [];
	GDownloadUrl(url, function(data, responseCode) {
		var xmlDoc = GXml.parse(data);

		// obtain the array of markers and loop through it
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");

		var act_id = active_id; // Keep it while refreshing

		// hide the info window, otherwise it still stays open where the removed marker used to be
		map.getInfoWindow().hide();
		hideTooltipSmall()

		map.clearOverlays();

		// empty the array
		gmarkers = [];

		for (var i = 0; i < markers.length; i++) {
			var marker = markers[i];

			// Create our "tiny" marker icon
			var icon = new GIcon();
			icon.infoWindowAnchor = new GPoint(5, 1);

			if (marker.getAttribute("latitude") && marker.getAttribute("longitude")) {
				// obtain the attribues of each marker
				var lat = parseFloat(marker.getAttribute("latitude"));
				var lng = parseFloat(marker.getAttribute("longitude"));
				var point = new GLatLng(lat,lng);

				var html = "<h1>"+marker.getAttribute("name")+"</h1>";
				var reg = '';
					
				var type = marker.getAttribute("type");
				var cnt = marker.getAttribute("cnt");
				if (type == 'group') {
					reg = '/' + marker.getAttribute('regionurl') + '/';
					if (cnt > 1) {
						var size = marker.getAttribute("size");
						icon.iconSize = new GSize(size, size);
						icon.iconAnchor = new GPoint(size/2, size/2);
						icon.image = '/img/group_'+size+'.png';
					} else {
						icon.iconSize = new GSize(17, 25);
						icon.iconAnchor = new GPoint(6, 25);
						icon.image = '/img/group_single.png';
					}

				} else if (type == 'club') {
					icon.iconSize = new GSize(22, 35);
					icon.iconAnchor = new GPoint(3, 35);
					if (id != marker.getAttribute("id")) {
						icon.image = '/img/flag_5.png';
					} else {
						icon.image = '/img/flag_1.png';
					}
					icon.shadow = '/img/flag_shadow.png';

				} else if (type == 'pitch_putt') {
					icon.iconSize = new GSize(22, 35);
					icon.iconAnchor = new GPoint(3, 35);
					if (id != marker.getAttribute("id")) {
						icon.image = '/img/flag_4.png';
					} else {
						icon.image = '/img/flag_1.png';
					}
					icon.shadow = '/img/flag_shadow.png';

				} else if (type == 'club_small') {
					icon.iconSize = new GSize(17, 25);
					icon.iconAnchor = new GPoint(9, 25);
					if (id != marker.getAttribute("id")) {
						icon.image = '/img/flag_4_small.png';
					} else {
						icon.image = '/img/flag_1_small.png';
					}

				} else if (type == 'hotel') {
					icon.iconSize = new GSize(26, 37);
					icon.iconAnchor = new GPoint(13, 37);
					if (id != marker.getAttribute("id")) {
						icon.image = '/img/flag_hotel.png';
					} else {
						icon.image = '/img/flag_hotel_active.png';
					}

				} else if (type == 'airport') {
					icon.iconSize = new GSize(26, 37);
					icon.iconAnchor = new GPoint(13, 37);
					if (id != marker.getAttribute("id")) {
						icon.image = '/img/flag_airport.png';
					} else {
						icon.image = '/img/flag_airport_active.png';
					}
				}

				var attributes = marker.getElementsByTagName("attribute");
				if (attributes) {
					for (var a = 0; a < attributes.length; a++) {
						html += "<li>";
						html += "<strong>"+attributes[a].getAttribute("att_name")+"</strong>: ";
						if (attributes[a].getAttribute("att_prepend"))
							html += attributes[a].getAttribute("att_prepend")+' ';
						html += attributes[a].getAttribute("value");
						if (attributes[a].getAttribute("att_append"))
							html += ' '+attributes[a].getAttribute("att_append");
						html += "</li>";
					}
				}
				if (marker.getAttribute('description')) {
					html += '<li>' + marker.getAttribute('description') + '</li>';
				}

				if (marker.getAttribute('url')) {
					//html += '<li><a href="/' + marker.getAttribute('url') + '/">' + marker.getAttribute('detail_text') + '</a></li>';
					if (!reg)
						//reg = '/' + marker.getAttribute('url') + '/';
						reg = '#';
				}

				// create the marker
				var m = createMarker(point, icon, marker.getAttribute("id"), cnt, html, reg, edit_mode);
				map.addOverlay(m);

				if (!edit_mode && act_id && marker.getAttribute('id') == act_id) {
					showTooltipSmall(act_id);
				}
			}
			$('#loading').hide();	
		}

		if (!gmarkers.length && edit_mode) {
			// Create our "tiny" marker icon
			var icon = new GIcon();
			icon.iconSize = new GSize(34, 49);
			icon.iconAnchor = new GPoint(13, 49);
			icon.image = '/img/flag_1.png';

			// obtain the attribues of each marker
			var point = map.getCenter();

			// create the marker
			var m = createMarker(point, icon, id, '', '', '', edit_mode);
			map.addOverlay(m);
		}
	});
}

// A function to create the marker and set up the event window
function createMarker(point, ic, id, cnt, html, url, edit_mode) {
	var marker = new GMarker(point, ic);
	if (edit_mode) {
		marker = new GMarker(point, {icon: ic, draggable: true});
		GEvent.addListener(marker, "dragstart", function() {
			//map.closeInfoWindow();
		});

		GEvent.addListener(marker, "dragend", function() {
			var lat  = marker.getPoint().lat();
			var long = marker.getPoint().lng();
			GDownloadUrl('/putxml.php?club_id='+id+'&latitude='+lat+'&longitude='+long);
		});
	} else {
		if (url && url != window.location.pathname) {
			GEvent.addListener(marker, "click", function() { window.location.href = url; } );
		} else {
			GEvent.addListener(marker, "click", function() { myclick(id, cnt, point) } );
		}
		GEvent.addListener(marker, "mouseover", function() { showTooltipSmall(id) } );
		if (url && url != window.location.pathname) {
			GEvent.addListener(marker, "mouseout", function() { hideTooltipSmall() } );
		}
	}

	gmarkers[id] = marker;
	htmls[id] = html;
	points[id] = point;

	return marker;
}

// This function picks up the click and opens the corresponding info window
function myclick(i, cnt, p) {
	active_id = i;

	if (cnt) {
		hideTooltipSmall();
		var z = map.getZoom();
		if (z < 6) {
			z += 3;
		} else if (z < 9) {
			z += 2;
		} else if (z < 20) {
			z += 1;
		}
		map.setCenter(p, z);
	} else {
		map.panTo(p);
		showTooltipSmall(i);
	}
}

function showTooltipSmall(id) {
	active_id = id;
	var tt = document.getElementById('tooltip_small');
	var pixelPoint = map.fromLatLngToDivPixel(points[id]);
	if (tt) {
		off_x = gmarkers[id].getIcon().iconSize.width  - gmarkers[id].getIcon().iconAnchor.x;// + 5;
		off_y = gmarkers[id].getIcon().iconAnchor.y;// + 5;

		tt.innerHTML = '<a onclick="hideTooltipSmall()" class="x_tooltip"><img src="/img/x_tooltip.png" alt=""/></a>' + htmls[id];
		tt.style.display = "block";
		tt.style.visibility = 'visible';
		tt.style.left = (pixelPoint.x + off_x - 55) + 'px';
		tt.style.top = (pixelPoint.y - tt.offsetHeight - off_y + -5) + 'px';
		map.getPane(G_MAP_MARKER_PANE).appendChild(tt);
	}
}

function hideTooltipSmall() {
	var tt = document.getElementById('tooltip_small');
	if (tt) {
		tt.style.display = "none";
	}
	active_id = undefined;
}

// Only needed for the maps.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


