var topoMap = null;
var topoIconRed = null;
var topoIconBlue = null;
var topoIconBlack = null;
var infoWindowPadding = null;

$(document).ready( function() {

    topoMap_init();

});

function topoMap_init() {

    var topoMapCanvas;

    topoMapCanvas = document.getElementById('topoMap_canvas');

    if( topoMapCanvas==null )
        return;

    /*
	if( typeof(topoMap)!='undefined' && (topoMap instanceof GMap2) ) {
		console.log('previous topoMap destruction...');
		topoMap.clearOverlays();
		delete topoMap;
	}
	*/

    topoMap = new GMap2(  topoMapCanvas );

    topoMap.setMapType(G_PHYSICAL_MAP);
    topoMap.enableScrollWheelZoom();
    //topoMap.setUIToDefault();

    topoMap.addControl(new GScaleControl (), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3, 3)));
	topoMap.addControl(new UzikMapTypeControls());

    infoWindowPadding = new GSize(10, 0);

    topoIconRed = new GIcon(G_DEFAULT_ICON);
    topoIconRed.image = cdtRegistry.baseUrl + 'images/map/icon_red.png';
    topoIconRed.iconSize = new GSize(23, 29);
    topoIconRed.iconAnchor = new GPoint(11, 29);
    topoIconRed.shadow = cdtRegistry.baseUrl + 'images/map/ombre.png';
    topoIconRed.shadowSize = new GSize(38, 31);

    topoIconBlue = new GIcon(G_DEFAULT_ICON);
    topoIconBlue.image = cdtRegistry.baseUrl + 'images/map/icon_blue.png';
    topoIconBlue.iconSize = topoIconRed.iconSize;
    topoIconBlue.iconAnchor = topoIconRed.iconAnchor;
    topoIconBlue.shadow = cdtRegistry.baseUrl + 'images/map/ombre.png';
    topoIconBlue.shadowSize = topoIconRed.shadowSize;

    topoIconBlack = new GIcon(G_DEFAULT_ICON);
    topoIconBlack.image = cdtRegistry.baseUrl + 'images/map/icon_black.png';
    topoIconBlack.iconSize = topoIconRed.iconSize;
    topoIconBlack.iconAnchor = topoIconRed.iconAnchor;
    topoIconBlack.shadow = cdtRegistry.baseUrl + 'images/map/ombre.png';
    topoIconBlack.shadowSize = topoIconRed.shadowSize;

}


function drawTopoTrip(points, minlat, maxlat, minlng, maxlng) {

    var polyline = new GPolyline(points, "#8A75B0", 5, 1);

    var center_lat = (parseFloat(minlat) + parseFloat(maxlat)) / 2;
    var center_lng = (parseFloat(minlng) + parseFloat(maxlng)) / 2;
    var zoom = topoMap.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(minlat, minlng), new GLatLng(maxlat, maxlng)));
	zoom -= 1;
    topoMap.setCenter(new GLatLng(center_lat, center_lng), zoom);

    topoMap.addOverlay(polyline);


    var $topoMapContainer, $topoMapBg, $topoMapGMap, $topoMapCloseButton, $topoMapLegend;
    var topoMapX, topoMapY;
    var topoMapCloseButtonX, topoMapCloseButtonY;
    $topoMapContainer = $('#topoMapContainer');
    $topoMapBg = $topoMapContainer.find('.bg');
    $topoMapGMap = $topoMapContainer.find('#topoMap_canvas');
    $topoMapCloseButton = $topoMapContainer.find('.closeButton');
    $topoMapLegend = $topoMapContainer.find('.legend');

    $topoMapContainer.css( {
        'width': '100%',
        'height': '100%'
    } );
    $topoMapBg.css('opacity', 0 );
    $topoMapBg.fadeTo('fast', 0.5 );
    topoMapX = ( $(window).width()-$topoMapGMap.width() ) / 2 ;
    topoMapY = ( $(window).height()-$topoMapGMap.height() ) / 2 ;
    $topoMapGMap.css( {left: topoMapX, top: topoMapY} );

    $topoMapCloseButton.html('<a href="javascript:closeTopoMap();" alt="'+topoMapCloseButtonTitle+'" title="'+topoMapCloseButtonTitle+'"><img src="'+cdtRegistry.baseUrl+'images/map/close.png" /></a>');
    topoMapCloseButtonX = topoMapX  + parseInt( $topoMapGMap.width() ) - 10;
    topoMapCloseButtonY = topoMapY - 10;
    $topoMapCloseButton.css( {left: topoMapCloseButtonX, top: topoMapCloseButtonY} );
	
	$topoMapLegend.css( {left: topoMapX + ( $topoMapGMap.width() - $topoMapLegend.width() )/2, top: topoMapY + $topoMapGMap.height() } );

}


function addTopoMarker(position, name, desc, photo, link) {

    var marker = new GMarker(position, {
        icon: topoIconBlack
    });

    topoMap.addOverlay(marker);

	if( name.length>0 ) {

		marker.onMouseOver = function() {
			this.openExtInfoWindow(
					topoMap,
					'black_infoWindow',
				   'name : '+name
			);
		}
		  marker.onMouseOut = function() {
			this.closeExtInfoWindow(topoMap);
		};

		GEvent.addDomListener(marker, 'mouseover',	marker.onMouseOver );
		GEvent.addDomListener(marker, 'mouseout',	marker.onMouseOut );

	}

}
