﻿var livecamMap = {};

livecamMap.map;
livecamMap.visibleMarkers = [];
livecamMap.markers = [];
//livecamMap.geo;
globalLat = 47.3877586;
globalLon = 9.7508211;
tooltip = "";

var unselected_marker_url = '/SysRes/GLOBALSkin/MyTownSkin/Groups/ContentMapBox/marker-red.png';
var selected_marker_url = '/SysRes/GLOBALSkin/MyTownSkin/Groups/ContentMapBox/marker-blue.png';

livecamMap.init = function(f) {
	if(GBrowserIsCompatible()) {
		//remove LoadingProcess and append Google Map container
		$("#MapArea").empty().append('<div id="' + livecamMapId + '" style="height:350px;width:570px;" />');

		livecamMap.map = new GMap2(document.getElementById(livecamMapId));
		livecamMap.map.setUIToDefault();
		
		//livecamMap.geo = new GClientGeocoder();
		bounds = new GLatLngBounds();
		livecamMap.map.enableScrollWheelZoom();
		livecamMap.map.enableDoubleClickZoom();
		livecamMap.map.enableContinuousZoom();
		
		//livecamMap.map.setCenter(new GLatLng(globalLat, globalLon),15); 
		
		//exectue callback function
		if (typeof f == "function") f();
		
		$("#" + livecamMapId).css("visibility","visible");
		
		
		// ====== set up marker mouseover tooltip div ======
		tooltip = document.createElement("div");
		livecamMap.map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
		tooltip.style.visibility="hidden";
	}
	return true;
};

livecamMap.update = function() {
	//remove all markers
	livecamMap.map.clearOverlays();

	$("#LivecamsContainer li[id^='livecam-']").each(function() {
		livecamId = $(this).attr("id");
		livecamName = $(this).attr("name");
		
		lat = $("#" + livecamId + "-lat");
		lon = $("#" + livecamId + "-lon");
		
		point = new GLatLng(lat.val(), lon.val());
		
		marker = livecamMap.createMarkerObj(point, livecamId, livecamName);
		livecamMap.map.addOverlay(marker);
	});
	
	livecamMap.map.setCenter(bounds.getCenter(), livecamMap.map.getBoundsZoomLevel(bounds));
}

livecamMap.createMarkerObj = function(point, livecamId, livecamName) {
	livecamMap.markers[livecamId] = new GMarker(point);
	bounds.extend(point);
	
	GEvent.addListener(livecamMap.markers[livecamId], 'click', function() {
		livecamId = getLivecamId($('#' + livecamId + ' a.LivecamLink'));
		displayLivecam(livecamId);
	});
	
    GEvent.addListener(livecamMap.markers[livecamId], 'mouseover', function(e) { 
        this.setImage(selected_marker_url);
		showTooltip(livecamMap.markers[livecamId], livecamName);
    });
	GEvent.addListener(livecamMap.markers[livecamId], 'mouseout', function() {
        this.setImage(unselected_marker_url);
		tooltip.style.visibility="hidden";
    });
	
	return livecamMap.markers[livecamId];
}


// ====== This function displays the tooltip ======
// it can be called from an icon mousover or a side_bar mouseover
function showTooltip(marker, livecamName) {
	tooltip.innerHTML = '<div class="LivecamMapTooltip">' + livecamName + '</div>';
	var point=livecamMap.map.getCurrentMapType().getProjection().fromLatLngToPixel(livecamMap.map.fromDivPixelToLatLng(new GPoint(0,0),true),livecamMap.map.getZoom());
	var offset=livecamMap.map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),livecamMap.map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}
/*
livecamMap.getLatLngFromAddress = function(address, id, url) {
	if (address != '') {
		livecamMap.geo.getLocations(address, function(result){
			if(result.Status.code == G_GEO_SUCCESS) {
				var point = result.Placemark[0].Point.coordinates;
				point = new GLatLng(point[1], point[0]);
				marker = livecamMap.createMarker(point, address, id, url);
				livecamMap.map.addOverlay(marker);
			}
		});
	}
}
*/
function unloadMap() {
	GUnload();
}
