/**
* @fileoverview Wrapper for Google Maps API v3
*/

// Create NetR namespace if it doesn't exist
if (typeof (NetR) === 'undefined') {
	var NetR = {};
}

NetR.GMaps = function() {
	var map = null;
	// Default options
	var options = {
		defaultCenter: [52.659924, 4.724121],
		defaultZoom: 1,
		mapTypeId: google.maps.MapTypeId.SATELLITE,
		showMapTypes: false,
		mapContainerId: '',
		mapElementId: 'map',
		mapClass: '',
		markerAtCenter: false,
		markers: [],
		markersXML: '',
		XMLMarkerNodeName: '',
		XMLMarkerLinkText: '',
		loadComplete: null
	};

	/**
	* Creates a marker on the map, with info bubble displaying on mouseover
	* @param lat Latitude for marker location
	* @param lon Longitude for marker location
	* @param url URL for more info about marker location
	* @param name Name of location
	* @param linkURLs URLs of links that have corresponding map markers
	*/
	function createMarker(lat, lon, url, name, body, icon) {
		//var point = new GLatLng(lat, lon);
		var point = new google.maps.LatLng(lat, lon);

		var image = new google.maps.MarkerImage('/i/mapmarker.png',
		// This marker is 20 pixels wide by 32 pixels tall.
			new google.maps.Size(16, 16),
		// The origin for this image is 0,0.
			new google.maps.Point(0, 0),
		// The anchor for this image is the base of the flagpole at 0,32.
			new google.maps.Point(8, 8)
		);

		var shadow = new google.maps.MarkerImage('/i/mapmarker-shadow.png',
		// The shadow image is larger in the horizontal dimension
		// while the position and offset are the same as for the main image.
			new google.maps.Size(28, 16),
			new google.maps.Point(0, 0),
			new google.maps.Point(8, 8)
		);

		var marker = new google.maps.Marker({
			position: point,
			map: this.map,
			shadow: shadow,
			icon: icon,
			title: name
		});

		var windowHtml = '<strong>' + name + '</strong><br />' + body;
		var bubble = $('<div />').addClass('info-bubble').html(windowHtml);
		$('#map-wrap').append(bubble);
		google.maps.event.addListener(marker, 'mouseover', function() {
			if(!$(bubble).is(':visible'))
    			$(bubble).show();
  		});
  		google.maps.event.addListener(marker, 'mouseout', function() {
    		$(bubble).hide();
  		});
		/*var infowindow = new google.maps.InfoWindow({
			content: windowHtml
		});

		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		}); */
	}

	/**
	* Creates markers from XML document on map
	* @param xml The XML document returned by an Ajax request
	*/
	function createMarkers(xml) {
		var points = xml.getElementsByTagName(options.XMLMarkerNodeName);
		var linkURLs = {};
		if (options.restaurantList) {
			var linkContainer = document.getElementById(options.restaurantList);
			if (linkContainer) {
				var links = linkContainer.getElementsByTagName("a");
				for (var i = 0, len = links.length; i < len; i++) {
					linkURLs[links[i].href] = links[i];
				}
			}
		}
		var point;
		for (var i = 0, len = points.length; i < len; i++) {
			point = points[i];
			createMarker(point.getAttribute('latitude'), point.getAttribute('longitude'), point.getAttribute('URL'), point.getAttribute('name'), point.getAttribute('body'), point.getAttribute('icon'));
		}
	}

	/**
	* Performs Ajax call to get XML data for markers
	* @requires jQuery
	* @param {string} XMLPath URL for XML document
	*/
	function loadMarkersFromXML(XMLPath) {
		$.ajax({
			type: 'GET',
			dataType: 'xml',
			url: XMLPath,
			success: function(xml) {
				createMarkers(xml);
			}
		});
	}

	/**
	* Loads map into element with id from options.mapContainerId
	*/
	function load() {
		if (document.getElementById(options.mapContainerId)) {
			// Create element
			var mapContainer = document.getElementById(options.mapContainerId);
			var mapElement = document.createElement('div');
			mapElement.id = options.mapElementId;

			if (options.mapClass && options.mapClass.length > 0) {
				mapElement.className = options.mapClass;
			}

			mapContainer.innerHTML = '';
			mapContainer.appendChild(mapElement);

			var centerPoint = new google.maps.LatLng(options.defaultCenter[0], options.defaultCenter[1]);

			// Init map
			this.map = new google.maps.Map(mapElement, {
				center: centerPoint,
				zoom: options.defaultZoom,
				mapTypeId: options.mapTypeId,
				mapTypeControl: options.showMapTypes,
				scrollwheel: false,
				mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }
			});

			// Marker at center point?
			if (options.markerAtCenter) {
				new google.maps.Marker({
					position: centerPoint,
					map: this.map
				});
			}

			// Add additional markers?
			if (options.markers && options.markers.length > 0) {
				for (var i = 0, len = options.markers.length; i < len; i++) {
					this.map.addOverlay(new GMarker(new GLatLng(options.markers[i][0], options.markers[i][1])));
				}
			}

			if (options.markersXML && options.markersXML.length > 0) {
				loadMarkersFromXML(options.markersXML, createMarkers);
			}

			// Execute loadComplete function (if it exists)
			if (typeof (options.loadComplete) === "function") {
				options.loadComplete(this.map);
			}
		}
	}

	/**
	* Accessor function for map object
	*/
	function getMap() {
		return this.map;
	}

	/**
	* Initialization
	*/
	function init(opts) {
		if (!document.getElementById || !document.createElement) {
			return;
		}
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		load();
	}

	return {
		init: init,
		createMarkers: createMarkers,
		getMap: getMap
	};
} ();
