//------------------------------------
// CommentMap
// Controls the map used when posting
// comments to the forum
//------------------------------------
CommentMap = new function() {
	var map;
	var pin = null;
	var self = this;
	var brandImage = '/images/general/vibeLogo.png';
	var defaultPosition = {Lat: -30.00, Lng: 145.00, Zoom: 3};

	this.BrandLocation = null;
	this.BrandPoPTitle = '';
	this.Origin = new GLatLng(defaultPosition.Lat, defaultPosition.Lng);
	this.Zoom = defaultPosition.Zoom;

	function handlePinDrag() {
		var point = pin.getLatLng();
		document.getElementById('CommentMap:lat').value = point.lat().toFixed(5);
		document.getElementById('CommentMap:lng').value = point.lng().toFixed(5);
		map.panTo(point);
	}

	function updateFromGeocode(point) {
		var lat, lng;

		if(!point) {
			alert("Sorry, that address could not be found.");
			return;
		}

		var lat = point.lat();
		var lng = point.lng();
		if(!isNaN(lat) && !isNaN(lng)) {
			document.getElementById('CommentMap:lat').value = lat.toFixed(5);
			document.getElementById('CommentMap:lng').value = lng.toFixed(5);
			if(self.updatePin()) {
				map.setZoom(15);
				// We have to re-position again because positioning at
				// lower zoom is not at a very high accuracy.
				self.updatePin();
			}
		}
	}

	this.start = function(){
		map = new GMap2(document.getElementById("CommentMap"), {size: new GSize(605, 350)});
		map.enableContinuousZoom();
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMenuMapTypeControl());

		if(self.BrandLocation) {
			self.Origin = self.BrandLocation;
			self.Zoom = 15;
		}

		minLat = maxLat = self.Origin.lat();
		minLng = maxLng = self.Origin.lng();
		map.setCenter(self.Origin, self.Zoom);

		if(self.BrandLocation) {
			// Add brand icon
			var hotelIcon = new GIcon();
			hotelIcon.image = brandImage;
			hotelIcon.iconSize =  new GSize(70, 52);
			hotelIcon.iconAnchor = new GPoint(14, 48);
			hotelIcon.shadow = null;
			map.addOverlay(new GMarker(self.BrandLocation, {'icon': hotelIcon, title: self.BrandPoPTitle, 'clickable' : false}));
		}

		// Add draggable icon
		var lat = parseFloat(document.getElementById('CommentMap:lat').value);
		var lng = parseFloat(document.getElementById('CommentMap:lng').value);
		if(isNaN(lat) || isNaN(lng)) {
			document.getElementById('CommentMap:lat').value = self.Origin.lat();
			document.getElementById('CommentMap:lng').value = self.Origin.lng();
		}
		
		self.updatePin();
	}

	this.updatePin = function() {
		var latInput = document.getElementById('CommentMap:lat');
		var lngInput = document.getElementById('CommentMap:lng');
		var lat = parseFloat(latInput.value);
		var lng = parseFloat(lngInput.value);

        if (isNaN(lat) || isNaN(lng)) {
			return false;
		}

		latInput.value = lat.toFixed(5);
		lngInput.value = lng.toFixed(5);

		var point = new GLatLng(lat, lng);
		if(!pin) {
			pin = new GMarker(point, {draggable: true, title: 'New location'});
			map.addOverlay(pin);
			GEvent.addListener(pin, "dragend", handlePinDrag);
		}
		else {
			pin.setLatLng(point);
		}
		map.panTo(point);
		return true;
	}

	this.lookupAddress = function() {
		var addressInput = document.getElementById('CommentMap:address');
		if(!addressInput || !addressInput.value) return;
		var geocoder = new GClientGeocoder();
		geocoder.setViewport(map.getBounds());
		geocoder.getLatLng(addressInput.value, updateFromGeocode);
	}


	return this;
}();

//------------------------------------
// DiscussionMap
// Controls the map seen at the top of
// disucssion and comment grids
//------------------------------------
DiscussionMap = new function(){
	var map;
	var self = this;
	var commentMarkers = new Array();
	var minLat, minLng, maxLat, maxLng;
	var brandImage = '/images/general/vibeLogo.png';
	var defaultPosition = {Lat: -30.00, Lng: 145.00, Zoom: 3};

	this.FirstCommentID = 0;
	this.BrandLocation = null;
	this.BrandPoPTitle = '';
	this.Origin = new GLatLng(defaultPosition.Lat, defaultPosition.Lng);
	this.Zoom = defaultPosition.Zoom;

	this.addCommentMarker = function(locationData) {
		commentMarkers.push(locationData);
	}

	this.start = function() {
		map = new GMap2(document.getElementById("DiscussionMap"), {size: new GSize(625, 350)});
		map.enableContinuousZoom();
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMenuMapTypeControl());

		if(self.BrandLocation) {
			self.Origin = self.BrandLocation;
			self.Zoom = 15;
		}
		else if(commentMarkers.length > 0) {
			self.Origin = new GLatLng(commentMarkers[0].Lat, commentMarkers[0].Lng);
			self.Zoom = 15;
		}

		minLat = maxLat = self.Origin.lat();
		minLng = maxLng = self.Origin.lng();
		map.setCenter(self.Origin, self.Zoom);

		if(self.BrandLocation) {
			// Add brand icon
			var hotelIcon = new GIcon();
			hotelIcon.image = brandImage;
			hotelIcon.iconSize =  new GSize(70, 52);
			hotelIcon.iconAnchor = new GPoint(14, 48);
			hotelIcon.shadow = null;
			map.addOverlay(new GMarker(self.BrandLocation, {'icon': hotelIcon, title: self.BrandPoPTitle, 'clickable' : false}));
		}

		// Add the locations
		plotLocations();
	}

	function plotLocations() {

		if(commentMarkers.length == 0) {
			return;
		}

		var baseIcon = new GIcon();
		var marker, point, lat, lng;
		baseIcon.image = 'http://maps.google.com/mapfiles/ms/micons/blue-dot.png';
		baseIcon.iconSize =  new GSize(32, 32);
		baseIcon.iconAnchor = new GPoint(16, 32);
		baseIcon.shadow = 'http://maps.google.com/mapfiles/ms/micons/msmarker.shadow.png';

		for(i = 0; i < commentMarkers.length; i++) {
			lat = commentMarkers[i].Lat;
			lng = commentMarkers[i].Lng;
			minLat = Math.min(lat, minLat);
			maxLat = Math.max(lat, maxLat);
			minLng = Math.min(lng, minLng);
			maxLng = Math.max(lng, maxLng);
			point = new GLatLng(lat, lng);
			if(commentMarkers[i].CommentID == self.FirstCommentID) {
				marker = new GMarker(point, {icon: (new GIcon(baseIcon, 'http://maps.google.com/mapfiles/ms/micons/red-dot.png')), clickable : true, title : commentMarkers[i].Title});
			}
			else {
				marker = new GMarker(point, {icon: baseIcon, clickable : true, title : commentMarkers[i].Title});
			}
			map.addOverlay(marker);
			GEvent.addListener(marker, 'click', function(id){return function(){document.location.hash='Item_'+id;};}(commentMarkers[i].RowNumber));
		}
		var minLatLng = new GLatLng(minLat, minLng);
		var maxLatLng = new GLatLng(maxLat, maxLng);
		var bounds = new GLatLngBounds(minLatLng, maxLatLng)
		var zoom = map.getBoundsZoomLevel(bounds);
		map.setCenter(bounds.getCenter(), zoom);
	}
	
}();

$(document).ready(function() {
	if(($('#CommentMap').length > 0) && CommentMap) {CommentMap.start();}
	if(($('#DiscussionMap').length > 0) && DiscussionMap) {DiscussionMap.start();}
});


