// JavaScript Document

/* global map object
----------------------------------------------- */
tmWebmap = {

	// public properties

	initLat: 40.753808,
	initLon: -73.995924,
	initZoom: 13,
	initType: G_NORMAL_MAP,

	showZoom: true,
	showType: true,
	showOverview: false,

	enableSearch: false,

	// private properties

	_map: false,

	_contextMenu: false,
	_contextOpacity: 0.8,
	_showingContextMenu: false,

	_data: '',
	_dataPath: 'data/data.js',

	// customizable ids
	_ta: 'tarOutput',

//	_curOverlay: false,
//	_curPoint: false,

	// public methods

	init: function() {
		// test to see if browser supports google maps
		if (GBrowserIsCompatible()) {
			// init non-map elements
			this._contextMenu = $('mapContextMenu');
			// create and render map
			var map = new GMap2($('map'));
			if (this.showZoom) { map.addControl(new GSmallMapControl()); }
			if (this.showType) { map.addControl(new GMapTypeControl()); }
			if (this.showOverview) { map.addControl(new GOverviewMapControl()); }
			map.setCenter(new GLatLng(this.initLat, this.initLon), this.initZoom, this.initType);
			map.enableDoubleClickZoom();
			map.enableContinuousZoom();
			map.enableScrollWheelZoom();
			if (this.enableSearch) { map.enableGoogleBar(); }
			// first set of options is for the visual overlay.
//			var boxStyleOpts = {
//				opacity: .2,
//				border: '2px solid red'
//			}
			// second set of options is for everything else
//			var otherOpts = {
//				buttonHTML: '<img src="images/zoom-button.gif" />',
//				buttonZoomingHTML: '<img src="images/zoom-button-activated.gif" />',
//				buttonStartingStyle: {width: '24px', height: '24px'}
//			};
			// third set of options specifies callbacks
//			var callbacks = {
//				buttonclick: function(){GLog.write("Looks like you activated DragZoom!")},
//				dragstart: function(){GLog.write("Started to Drag . . .")},
//				dragging: function(x1,y1,x2,y2){GLog.write("Dragging, currently x="+x2+",y="+y2)},
//				dragend: function(nw,ne,se,sw,nwpx,nepx,sepx,swpx){GLog.write("Zoom! NE="+ne+";SW="+sw)}
//			};
//			map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, callbacks));
			// store map in object
			this._map = map;
			// add event listeners
//			Event.observe('map', 'click', tmWebmap.captureClick.bindAsEventListener(tmWebmap), false);
			GEvent.addListener(map, 'click', function(overlay, point) {
				tmWebmap._hideContextMenu();
				GLog.write(overlay);
				GLog.write(point);
			});
			GEvent.addListener(map, 'singlerightclick', function(point, image, containter) {
				GLog.write(point.x);
				GLog.write(image);
				GLog.write(containter);
				tmWebmap._hideContextMenu(point);
//				tmWebmap._showContextMenu(point);
//				if (tmWebmap._hideContextMenu()) {
//					var menu = $('mapContextMenu');
//					menu.style.left = point.x + 'px';
//					menu.style.top = point.y + 'px';
//					menu.style.display = 'block';
//					tmWebmap._showingContextMenu = true;
//				}
			});
//			Event.observe('exportJson', 'click', tmWebmap.exportJson.bindAsEventListener(tmWebmap), false);
//			Event.observe('exportXml', 'click', tmWebmap.exportXml.bindAsEventListener(tmWebmap), false);
//			Event.observe('exportKml', 'click', tmWebmap.exportKml.bindAsEventListener(tmWebmap), false);
//			Event.observe('exportGps', 'click', tmWebmap.exportGps.bindAsEventListener(tmWebmap), false);
			// download markers
			GDownloadUrl(this._dataPath, function(data, responseCode) {
				if (responseCode == 200) {
					var objData = data.evalJSON(true);
					tmWebmap._data = objData.markers;
					var j = tmWebmap._data.length;
					for (var i = 0; i < j; i++) {
						tmWebmap.addMarker(tmWebmap._data[i].lat, tmWebmap._data[i].lon, tmWebmap._data[i].id);
					}
				}
			});
		}
	},

	addMarker: function(fltLat, fltLon, intId) {
		var objMarker = new GMarker(new GLatLng(fltLat, fltLon));
		if (intId) {
			objMarker.tmId = 'marker' + intId;
		}
		GLog.write(this);
		this._map.addOverlay(objMarker);
		// add event listeners
//		GEvent.addListener(objMarker, 'click', function(a, b) { 
//			GLog.write(a);
//			GLog.write(b);
//		});
		Event.observe(objMarker, 'click', test, false);
	},

//	captureClick: function(e) {
//		GLog.write('click');
//		GLog.write(this._curOverlay);
//		GLog.write(this._curPoint);
//	},

	exportGps: function(e) { 
		$(this._ta).value = this.map.getGpx();
		if (e.preventDefault) { e.preventDefault(); } else { return false; }
	},

	exportJson: function(e) { 
		$(this._ta).value = this.map.getJSON();
		if (e.preventDefault) { e.preventDefault(); } else { return false; }
	},

	exportKml: function(e) { 
		$(this._ta).value = this.map.getKml();
		if (e.preventDefault) { e.preventDefault(); } else { return false; }
	},

	exportXml: function(e) { 
		$(this._ta).value = this.map.getXml();
		if (e.preventDefault) { e.preventDefault(); } else { return false; }
	},

	// private methods

	_hideContextMenu: function(point) {
		if (tmWebmap._showingContextMenu) {
			Effect.Fade(tmWebmap._contextMenu, { duration: 0.1, from: tmWebmap._contextOpacity, to: 0.0, afterFinish:function() { 
				tmWebmap._showingContextMenu = false;
				if (point) {
					tmWebmap._showContextMenu(point);
				}
			} });
//			$('mapContextMenu').style.display = 'none';
			return false;
		} else {
			if (point) {
				tmWebmap._showContextMenu(point);
			}
			return true;
		}
	},

	_showContextMenu: function(point) {
		tmWebmap._contextMenu.style.left = point.x + 'px';
		tmWebmap._contextMenu.style.top = point.y + 'px';
		Effect.Appear(tmWebmap._contextMenu, { duration: 0.1, from: 0.0, to: tmWebmap._contextOpacity, afterFinish:function() { 
			tmWebmap._showingContextMenu = true;
		} });
	}

//	_captureRightClick: function(e) {
//		if (Event.element(e).up('#map')) {
//			GLog.write('got it');
//			GEvent.trigger(tmWebmap._map, 'click');
//			return false;
//		}
//	}
//		GLog.write(Event.element(e));
//		if (Event.element(e)..id == 'map') {
//			GLog.write('right click');
//			return false;
//		}
//	}

}

function test(e) { 
			alert('here');
		}

// http://maps.google.com/?ie=UTF8&ll=40.704847,-73.945885&spn=0.094737,0.181274&t=k&z=13&om=1

/* event listeners
----------------------------------------------- */
Event.observe(window, 'load', tmWebmap.init.bindAsEventListener(tmWebmap), false);
Event.observe(window, 'unload', GUnload, false);
//document.oncontextmenu = tmWebmap._captureRightClick;