/* ALS_MEMBER_MAP
 * AussiesLivingSimply.com.au
 * An implementation of google maps to provide a member location map for
 * PHP Fusion sites as an infusion.
 * 
 * Version 0.2 - 2007-05-11
 * 
 * By Geoff Capper (geoff@aussieslivingsimply.com.au)
 * http://www.aussieslivingsimply.com.au/
 * Copyright: Geoff Capper 2007
 * Released for non-commercial use on a free basis as long as all notices and 
 * the ALS logo are kept intact.
 *
 * This comes with no warranty, and any problems/damage done by installing is
 * not our responsibility. You install this at your own risk.
 */

/* Object used to store information specific to a users marker on the map */
function UserMarker(userId, userName, lat, lng, msg, isCurrUser) {
	this.mUserId = userId;
	this.mUserName = userName;
	this.mLat = lat;
	this.mLng = lng;
	this.mMsg = msg;
	//this.marker = null;
	this.isCurrUser = isCurrUser;
	this.mPoint = new GLatLng(this.mLat, this.mLng);
	
	function getPoint() {
		return this.mPoint;
	}
	this.getPoint = getPoint;
	
	function getToolTip() {
		return this.mUserName;
		// + ' : ' + this.mPoint
	}
	this.getToolTip = getToolTip;
	
	function isDraggable() {
		return this.isCurrUser;
	}
	this.isDraggable = isDraggable;
	
	function getMsg() {
		if (this.mMsg == "" || this.mMsg == null || this.mMsg == "null") {
			return null;
		} else {
			return '<strong>' + this.mUserName + '</strong><br><p>' + this.mMsg + '</p>';
		}
	}
	this.getMsg = getMsg;
	
}

function load() {
	//alert('starting load');
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		//map.addMapType(G_SATELLITE_MAP);
		//map.setMapType(G_SATELLITE_MAP);
		
		map.addControl(new GLargeMapControl());
		
		map.setCenter(new GLatLng(START_LAT, START_LNG), 4, G_HYBRID_MAP);
		
		// Set up all the existing user markers.
		initMarkers(map);
		
		// Sett up click event on map.
		if (iMEMBER) {
			GEvent.addListener(map, "click", function(marker, point) {
				if (marker) {
					
					// if its the current users marker, they should be
					// able to edit it, otherwise, just center on the marker.
					if (marker.user.isCurrUser) {
						
						var oldMsg = marker.user.getMsg();
						var nMsg = prompt(M_MSG_PROMPT, oldMsg);
						
						marker.user.mMsg = nMsg;
						
						if (nMsg != oldMsg) {
							saveMarker(marker);
						}
						
					} else {
						
						centreOnPoint(marker.getPoint());
					}
					
				} else {
					
					if (currentUserHasMarker) {
						centreOnPoint(point);
					} else {
						// Create a marker for the user.
						createMarker(map, point);
					}
					
				}
			});
		}
	} else {
		alert(M_BRWS_INCMPT);
	}
	
}

// Centres the map on a particular location.
function centreOn(mlat, mlong) {
	centreOnPoint(new GLatLng(mlat, mlng));
}

function centreOnPoint(pt) {
	map.panTo(pt);
}

/* Creates a user marker ready for the map and returns it
 * Users are an amalgamation of the GMarker object and 
 * a user data object.
 */
function createUser(map, userId, userName, lat, lng, msg, isCurrUser) {
	
	var u = new UserMarker(userId, userName, lat, lng, msg, isCurrUser);
	
	var marker = new GMarker(u.getPoint(), {draggable: u.isDraggable(), bouncy: false, title: u.getToolTip() });
	marker.user = u;
	
	GEvent.addListener(marker, "dragstart", function() {
		map.closeInfoWindow();
	});
	
	GEvent.addListener(marker, "mouseover", function() {
			if (marker.user.getMsg() == null) {
				return;
			} else {
				marker.openInfoWindowHtml(marker.user.getMsg());
			}
		}
	);
	
	GEvent.addListener(marker, "mouseout", function() {
		map.closeInfoWindow();
	});
	
	GEvent.addListener(marker, "dragend", function() {
		var pt = marker.getPoint();
		marker.user.mLat = pt.lat();
		marker.user.mLng = pt.lng();
		
		saveMarker(marker);
		
	});
	
	map.addOverlay(marker);
	
	return marker;
	
}

// Sends data back to the server to be saved.
function saveMarker(marker) {
	
	user = marker.user;
	
	var data = 'userId=' + escape(user.mUserId) + '&';
	data = data + 'lat=' + escape(user.mLat) + '&';
	data = data + 'lng=' + escape(user.mLng) + '&';
	data = data + 'msg=' + escape(user.mMsg) + '';
	
	var params = data;
	
	var request = GXmlHttp.create();
	request.open("POST", SAVE_HREF, true);
	
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//request.setRequestHeader("Content-length", params.length);
	//request.setRequestHeader("Connection", "close");
	
	request.onreadystatechange = function() {
		if (request.readyState == 1) {
			//request.send(data);
		} else if (request.readyState == 2) {
			//alert('Information Sent');
		} else if (request.readyState == 3) {
			//alert('Receiving Data');
		} else if (request.readyState == 4) {
			//alert('Transmission Complete: ' + request.responseText);
		}
		
	}
	request.send(params);
	
}

// Sends data back to the server to be saved.
function doDeleteMarker(userId) {
	
	var data = 'userId=' + escape(userId) + '&';
	data = data + 'delete=1'
	
	var params = data;
	
	var request = GXmlHttp.create();
	request.open("POST", SAVE_HREF, true);
	
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//request.setRequestHeader("Content-length", params.length);
	//request.setRequestHeader("Connection", "close");
	
	request.onreadystatechange = function() {
		if (request.readyState == 1) {
			//request.send(data);
		} else if (request.readyState == 2) {
			//alert('Information Sent');
		} else if (request.readyState == 3) {
			//alert('Receiving Data');
		} else if (request.readyState == 4) {
			//alert('Transmission Complete: ' + request.responseText);
			for (var i = 0; i < users.length; i++) {
				if (users[i].user.isCurrUser) {
					map.removeOverlay(users[i]);
					alert('Mesajınız başarıyla silinmiştir.');
					break;
				}
			}
		}
		
	}
	request.send(params);
	
}

function centreMe() {
	for (var i = 0; i < users.length; i++) {
		if (users[i].user.isCurrUser) {
			centreOnPoint(users[i].getPoint());
			break;
		}
	}
}

