var map = null;
var mgr = null;
var JSON = null;
var extbounds = null; //Extended Bounds (50% extra)

jQuery.fn.addEventListeners = function(data){
	//update the map if the user has moved and is now outside extbounds
	GEvent.addListener(map,"moveend", function(){
		if(!extbounds.containsBounds(map.getBounds())){
			jQuery.fn.updateMap();
		}
	});
	//update the map if the user has changed zoom level and is now outside extbounds
	GEvent.addListener(map,"zoomend", function(){
		if(!extbounds.containsBounds(map.getBounds())){
			jQuery.fn.updateMap();
		}
	});
}
jQuery.fn.geoCodeSearch = function(){
	var address = $('#search').val();
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address,
		function(point) {
	    	if (!point) {
	        	alert(address + " not found");
	      	} else {
		        map.setCenter(point, 13);
	      	}
	    }
	);
}
jQuery.fn.addMarkerEventListener = function(markers){
	$(markers).each(function(i,marker){
		GEvent.addListener(marker, "click", function(){
			$.each(JSON.list, function(j,hotspot){
					if(marker.getTitle()==(hotspot.name)){
						//(hotspot.category.category == undefined || hotspot.category.category.length == 0) ? type = "n/a" : type = hotspot.category.category;
						//(hotspot.coverage == undefined || hotspot.coverage.length == 0) ? coverage = "n/a" : coverage = hotspot.coverage;
						$('#hotspot_detail').html(
									'<h3>'+hotspot.name+'</h3>'+
							       	'<table><tr><td width="80">Address</td><td>'+hotspot.address+'</td></tr>'+
							            '<tr><td>Type</td><td>'+hotspot.category+'</td></tr>'+
							       	'</table>'
						);
					}		
			});
		});
	});
}
jQuery.fn.addMarkers = function(data){
	mgr = new MarkerManager(map);
	mgr.addMarkers(data,null);
	mgr.refresh();
}
jQuery.fn.showHotspots = function(data){
	$.getJSON(data,
		function(data){
			JSON = data;
			var markers = Array();
			$.each(data.list, function(i,hotspot){
					var coord = new GLatLng(hotspot.latitude,hotspot.longitude);
					hotspotInfo = {title:hotspot.name+":"+hotspot.id};
					hotspot = new GMarker(coord,hotspotInfo);
					//TODO hotspot set icon based on category(default for no category)
					markers.push(hotspot);
			});
			jQuery.fn.addMarkers(markers);
			jQuery.fn.addMarkerEventListener(markers);
		}
	);
}
jQuery.fn.setBoundboxes = function(data){
	var bounds = map.getBounds();
	var swlat = bounds.getSouthWest().lat();
	var swlng = bounds.getSouthWest().lng();
	var nelat = bounds.getNorthEast().lat();
	var nelng = bounds.getNorthEast().lng();
	var latDiff = ((nelat - swlat)/2);
	var lngDiff = ((nelng - swlng)/2);
	extbounds = new GLatLngBounds(
		new GLatLng((swlat - latDiff),(swlng - lngDiff)),
		new GLatLng((nelat + latDiff),(nelng + lngDiff)));
}
jQuery.fn.buildURL = function(){
	var sw = extbounds.getSouthWest();
	var ne = extbounds.getNorthEast();
	//You will need to change this to be what it needs to be
	return "http://www.tomizone.com/ajax/locations_by_box/"+sw.lat()+"/"+sw.lng()+"/"+ne.lat()+"/"+ne.lng()+"/";
	
}
jQuery.fn.updateMap = function(){
	jQuery.fn.setBoundboxes();
	jQuery.fn.showHotspots(jQuery.fn.buildURL());
}
jQuery.fn.mapInitialise = function(){
	map = new GMap2(document.getElementById('map'));
	var aucklandNZ = new GLatLng(-36.8517,174.758806);
	map.setCenter(aucklandNZ, 11);
	map.enableScrollWheelZoom();
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
}
$(document).ready(function(){
	jQuery.fn.mapInitialise();
	jQuery.fn.updateMap();
	jQuery.fn.addEventListeners();
});