var map;
var isMarker = false;
var boundsMove = false;
var isZoom = false;
var isDragging = false;
var isClicked = false;
var dragged = false;
var pointBounds;
var pMarkers = [];
var bDateLineHandle = false;
var pIcons = [];
var spillCenter = [28.7366694444444, -88.3871611111111];
var kml;
var pics;
var picsOverlay;
var kmlOverlay;

function initIcons() {
	//build all the base icon classes
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(25, 28);
	baseIcon.iconAnchor = new GPoint(2, 28);
	baseIcon.infoWindowAnchor = new GPoint(12, 14);
	var dotIcon = new GIcon();
	dotIcon.iconSize = new GSize(8,8);
	dotIcon.iconAnchor = new GPoint(4,4);
	dotIcon.infoWindowAnchor = new GPoint(4,4);
	
	bIcon = new GIcon(baseIcon);
	bIcon.image = "http://www.boatus.com/ServiceLocator/images/markers/buoyMarker.gif";
	pIcons.boatus = bIcon;
	
	xIcon = new GIcon(baseIcon);
	xIcon.image = "http://www.boatus.com/ServiceLocator/images/markers/explosion.gif";
	pIcons.explosion = xIcon;
}

function createCenterMarker() {
	point = new GLatLng(parseFloat(spillCenter[0]),parseFloat(spillCenter[1]));
	var marker = new GMarker(point, {icon:pIcons.explosion,title: "Spill Center"});
	marker.cat = "explosion";
	marker.name = "Spill Center";
	marker.title = "Spill Center";
	marker.iconCat = "X";
	marker.iconID = 1;
	marker.markerID = 0;
	marker.point = point;
	map.addOverlay(marker);
	pMarkers.push(marker);
	return marker;
}

function createWebMarker(input,id) {
	point = new GLatLng(parseFloat(input.reportLat),parseFloat(input.reportLng));
	var marker = new GMarker(point, {icon:pIcons.boatus, title: input.reportTitle});
	marker.cat = "web";
	marker.name = input.reportTitle;
	marker.title = input.reportTitle;
	marker.iconCat = input.reportSource;
	marker.iconID = 1;
	marker.markerID = id;
	marker.serviceID = input.reportSourceID;
	marker.point = point;
	map.addOverlay(marker);
	pMarkers.push(marker);
	return marker;
}

function createMarinaMarker(input,id) {
	point = new GLatLng(parseFloat(input.reportLat),parseFloat(input.reportLng));
	var marker = new GMarker(point, {icon:pIcons.boatus, title: input.reportTitle});
	marker.cat = "web";
	marker.name = input.reportTitle;
	marker.title = input.reportTitle;
	marker.iconCat = input.reportSource;
	marker.iconID = 1;
	marker.markerID = id;
	marker.serviceID = input.reportSourceID;
	marker.point = point;
	pMarkers.push(marker);
	map.addOverlay(marker);
	return marker;
}

function createTowerMarker(input,id) {
	point = new GLatLng(parseFloat(input.reportLat),parseFloat(input.reportLng));
	var marker = new GMarker(point, {icon:pIcons.boatus, title: input.reportTitle});
	marker.cat = "web";
	marker.name = input.reportTitle;
	marker.title = input.reportTitle;
	marker.iconCat = input.reportSource;
	marker.iconID = 1;
	marker.markerID = id;
	marker.serviceID = input.reportSourceID;
	marker.point = point;
	pMarkers.push(marker);
	map.addOverlay(marker);
	return marker;
}

function createAdminMarker(input,id) {
	point = new GLatLng(parseFloat(input.reportLat),parseFloat(input.reportLng));
	var marker = new GMarker(point, {icon:pIcons.boatus, title: input.reportTitle});
	marker.cat = "web";
	marker.name = input.reportTitle;
	marker.title = input.reportTitle;
	marker.iconCat = input.reportSource;
	marker.iconID = 1;
	marker.markerID = id;
	marker.serviceID = input.reportSourceID;
	marker.point = point;
	pMarkers.push(marker);
	map.addOverlay(marker);
	return marker;
}

function parseJson(doc) {
	json = doc;
	recCount = json.markers.length;
	for (var i = 0; i < recCount; i++) {
		switch(json.markers[i].reportSource) {
			case "W":
				createWebMarker(json.markers[i], i);
			break;
			case "M":
				createMarinaMarker(json.markers[i], i);
			break;
			case "T":
				createTowerMarker(json.markers[i], i);
			break;
			case "A":
				createAdminMarker(json.markers[i], i);
			break;
		}
	}
	lastSearchQuery = "";
}

function loadPoints() {
	map.clearOverlays();
	pMarkers = [];
	createCenterMarker();
	pointBounds = new GLatLngBounds();
	bounds = map.getBounds();
	southWest = bounds.getSouthWest();
	northEast = bounds.getNorthEast();
	boundsN = northEast.lat();
	boundsS = southWest.lat();
	boundsW = southWest.lng();
	boundsE = northEast.lng();
	
	//fix the IDL problem
	saveLng1 = boundsW;
	saveLng2 = boundsE;
	if (boundsW >= 0 && boundsE < 0) {
		bDateLineHandle = true;
		boundsW = -179.999;
	} else {
		bDateLineHandle = false;
	}
	ajaxError = false;
	$j.getJSON('/ServiceLocator/mapData.asp',{
			'A': 'loadOilData',
			'bN': boundsN,
			'bS': boundsS,
			'bE': boundsE,
			'bW': boundsW,
			'R': Math.random()
		}, function(d, T) {
			parseJson(d);
	});
	map.addOverlay(kml);
	//map.addOverlay(pics);
	GEvent.clearListeners(kml, "click");
	GEvent.clearInstanceListeners(kml);
}

function centerAndZoom(point,zoom) {
	if (zoom < 16) {
		newZoom = zoom+1;
		map.setCenter(point,newZoom);
	} else {
		("Sorry, you cannot zoom in any further");
	}
}

$j(document).ready(function() {
	if (GBrowserIsCompatible()) {
		initIcons();
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(28.729130483430154, -86.517333984375), parseFloat(5));
		GEvent.addListener(map, "click", function(marker,point) {
			window.location = "map.asp";
		});
		//kml = new GGeoXml("http://www.boatus.com/oilSpill/spillOverlay.xml?k=35fthhtfh");
		//pics = new GGeoXml("http://picasaweb.google.com/data/feed/base/user/101447164930137557937/albumid/5472041656568973857?alt=kml&kind=photo&authkey=Gv1sRgCMPM99CX-7ySGg&hl=en_US");
   		loadPoints();
   } else {
	   alert("Your browser does not support Google maps. Please upgrade to the latest version.");
   }
});
