function GMon() {
	this.map = null;
	this.bounds = null;
	this.json = null;
	this.data = null;
	this.markers = new Array();
	this.myLoc = null;
	this.busy = false;
	this.filters = '';
	this.security = '';
}

GMon.prototype.drawMap = function(element,lat,lon,zoom) {
	var self = this;
	var element = document.getElementById(element);
	var origin = new google.maps.LatLng(lat,lon);
	var mapOptions = {
		zoom: zoom,
		center: origin,
		mapTypeControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	this.map = new google.maps.Map(element, mapOptions);
	
	// Load more markers as the map is altered
	google.maps.event.addListener(this.map,"idle",function() {
		self.bounds = self.map.getBounds();
		self.clearMarkers();
		self.loadMarkers(self.security,self.filters);
	});
}

GMon.prototype.centreMap = function(lat,lon,zoom) {
	this.map.panTo(new google.maps.LatLng(lat,lon));
	this.map.setZoom(zoom);
}

GMon.prototype.setLocation = function(lat,lon) {
	self = this;
	this.centreMap(lat,lon,15);
	this.myLoc = new google.maps.Marker({
		icon: iconLocation,
		position: new google.maps.LatLng(lat,lon),
		map: this.map
	});
	google.maps.event.addListener(this.myLoc,"click",function(){
		self.centreMap(lat,lon,15);
	});
}

GMon.prototype.loadMarkers = function() {
	var self = this;
	var params = {
		latN: this.bounds.getNorthEast().lat(),
		latS: this.bounds.getSouthWest().lat(),
		lonE: this.bounds.getNorthEast().lng(),
		lonW: this.bounds.getSouthWest().lng(),
		zoom: this.map.getZoom(),
		security: this.security,
		filters: this.filters
	};
	
	$("#map-busy").fadeIn();
	
	ajaxOb.postRequest('/modules/GMon/data.php',params,function(){
		var result = JSON.parse(ajaxOb.xmlResponseText);
		if(result.success=="true") {
			var nodes = result.nodes;
			var total = result.total;
			for(i in nodes) {
				var marker = new google.maps.Marker({
					icon: self.getIcon(nodes[i].security),
					position: new google.maps.LatLng(nodes[i].lat,nodes[i].lon),
					map: self.map
				});
				self.addNodeEvent(marker,nodes[i]);
				self.markers.push(marker);	
			}
			$("#gmon-results").html(total+" results");
		}
		
		$("#map-busy").fadeOut();
		
	});
}

GMon.prototype.addNodeEvent = function(marker,data) {
	google.maps.event.addListener(marker, "mouseover", function() {
		$("#gmon-ssid").html(data.ssid);
		$("#gmon-security").html(data.security);
		$("#gmon-channel").html(data.channel);
		$("#gmon-signal").html(data.signal);
		$("#gmon-make").html(data.make);
		$("#gmon-date").html(data.date);
	});
}

GMon.prototype.getIcon = function(type) {
	switch(type) {
		case "cluster":
			return iconCluster;
		case "Open":
			return iconOpen;
		case "Wep":
			return iconWep;
		case "WpaPsk":
			return iconWpa;
		case "WPA2":
			return iconWpa2;
		default:
			return iconDefault;
	}
}

GMon.prototype.clearMarkers = function() {
	if(this.busy) {
		alert("currently busy");
		return;
	}
	this.busy = true;
	for(i in this.markers)
		this.markers[i].setMap(null);
	this.markers = new Array();
	this.busy=false;
}


// Define the marker icons
var iconLocation = new google.maps.MarkerImage(
	"/modules/GMon/images/location.png",
	new google.maps.Size(16,16),
	new google.maps.Point(0,0),
	new google.maps.Point(8,0)
);
var iconCluster = new google.maps.MarkerImage(
	"/modules/GMon/images/cluster.png",
	new google.maps.Size(24,24),
	new google.maps.Point(0,0),
	new google.maps.Point(12,0)
);
var iconDefault = new google.maps.MarkerImage(
	"/modules/GMon/images/default.png",
	new google.maps.Size(6,6),
	new google.maps.Point(0,0),
	new google.maps.Point(3,0)
);
var iconOpen = new google.maps.MarkerImage(
	"/modules/GMon/images/open.png",
	new google.maps.Size(6,6),
	new google.maps.Point(0,0),
	new google.maps.Point(3,0)
);
var iconWep = new google.maps.MarkerImage(
	"/modules/GMon/images/wep.png",
	new google.maps.Size(6,6),
	new google.maps.Point(0,0),
	new google.maps.Point(3,0)
);
var iconWpa = new google.maps.MarkerImage(
	"/modules/GMon/images/wpa.png",
	new google.maps.Size(6,6),
	new google.maps.Point(0,0),
	new google.maps.Point(3,0)
);
var iconWpa2 = new google.maps.MarkerImage(
	"/modules/GMon/images/wpa2.png",
	new google.maps.Size(6,6),
	new google.maps.Point(0,0),
	new google.maps.Point(3,0)
);