/* 
 * JavaScript functions used by map.html, including those called from other frames.
 */
var map      = null ; //Link to the GoogleMap
var mgr      = null ; // markermanager
var mapsArr = new Array() ; // the chosen maps to show on map
var cLat     = 56.0 ; // the chosen latitude  also the first center of the map
var cLng     = 12.0 ; // the chosen longitude also the first center of the map

var focusIcon = new GIcon(); // The red X to mark the fokus of the search
var srchDoc = parent.srchFrame.document ;

// Data and setup for the big red X
focusIcon.image      = "galleri/fokus_icon.png";
focusIcon.iconSize   = new GSize(32, 32);
focusIcon.iconAnchor = new GPoint(16, 16);
fokusMarker          = new GMarker(focusIcon.iconAnchor, {icon:focusIcon, draggable: true, bouncy: false, title:'Find kort\'s fokus'});
fokusMarker.enableDragging();
fokusMarker.hide() ;

// Initialise the GoogleMap and do the setup for start image
function initMap() {
  if( ! GBrowserIsCompatible() ) {
      // Errormessage
  } ; // noncompatible

  map = new GMap2( document.getElementById("mapCanvas") ) ;
  map.setCenter( new GLatLng(cLat, cLng), 6 ) ; //long, lat, zoom
  map.setMapType( G_HYBRID_MAP ) ;
  map.addControl( new GLargeMapControl() ) ;
  map.addControl( new GScaleControl() ) ;
  mgr = new MarkerManager(map) ;
  map.addOverlay(fokusMarker);

  // what to do when a user clicks on the worldmap
  GEvent.addListener(map, 'click',
    function( overlay, point ) {
      onMapClick( point ) ;
    }
  ) ; // addListener
  // handle the movement of the focusMarker
  GEvent.addListener( fokusMarker, 'dragend',
    function() {
      onMapClick( fokusMarker.getLatLng() ) ;
    }
  ) ; // addListener

} // initMap

/**
 * Called when the user clicks on the GoogleMap.
 * Update chosen position & move the focusMarker
 */
function onMapClick( point ) {
  cLat = point.y ;
  cLng = point.x ;
  srchDoc.getElementById("pos" ).innerHTML = formatLatLng( cLat, cLng ) ;
  srchDoc.getElementById("pos2").innerHTML = formatLatLng( cLat, cLng ) ;
  fokusMarker.setLatLng( point ) ;
  fokusMarker.show() ;
} // onMapClick

/** Returns the chosen latitude of the search point*/
function getLat() { return cLat ; }

/** Returns the chosen longitude of the search point*/
function getLng() { return cLng ; }

// move the worldmap to center on a new position
function goTo( lat,lng ) {
    map.panTo(new GLatLng(lat, lng));
} //goTo

//function zoomTo( key ) {    zoom2map( key );} // zoomTo

// remove all maps
function clearOverlays() {
    //document.getElementById("pos").innerHTML = "clearOverlays " + mapsArr.length ;
    map.clearOverlays() ;
    mgr.clearMarkers() ;
    map.addOverlay( fokusMarker ) ;
    mapsArr = new Array() ;
} // clearOverlays

// add a map to mapsArr and then calls showChart to draw it.
function addChart( key, N,S,E,W, txt, offset, popupinfo ) {
    var chart ;
    var points = [ new GLatLng( S, W ),
                   new GLatLng( N, W ),
                   new GLatLng( N, E ),
                   new GLatLng( S, E ),
                   new GLatLng( S, W ) ] ;
    var colour = "#ffff33" ;

    //alert("Chart : " + key + " : " + N + ", " + S + ", " + E + ", " + W ) ;
    if( mapsArr[key] ) {
        chart = mapsArr[key] ;
    } else {
      chart = new Object() ;
      chart.colour = colour ;
      chart.weight = 2 ;
      chart.frame  = new GPolyline( points, chart.colour, chart.weight ) ;
      //chart.frame.setNumLevels(20) ;
      chart.marker = new GMarker( new GLatLng( S, W ) , {title: popupinfo });
      chart.marker.htmlStr = txt ;
      chart.marker.bindInfoWindowHtml( txt, {maxWidth:400} ) ;
      mapsArr[key] = chart ;
    }
    showChart( key ) ;

} // addChart


// Activates a map from the mapsArr and adds it to the GoogleMap image
function showChart( key ) {
    var chart = mapsArr[key] ;

    map.addOverlay( chart.frame ) ;
    mgr.addMarker( chart.marker, 0 ) ;

    chart.shown = true ;
    mgr.refresh() ;

} // showChart

/** Returns an array of keys for all the active maps. */
function getMapKeys() {
  var keyArr = new Array() ;
  for( key in mapsArr )
    if( mapsArr[key].shown )
        keyArr.push(key) ;
  return keyArr ;
} // getMapKeys

/** Zoom the GoogleMap image so all active maps can be seen */
function zoom2all() {
  var bounds = new GLatLngBounds() ;
  var chart ;
  for( key in mapsArr ) {
    //alert( "KORT" + key ) ;
    chart = mapsArr[key] ;
    if( chart.shown )
        for( var i = 0 ; i < chart.frame.getVertexCount() ; i++ )
          bounds.extend( chart.frame.getVertex(i) ) ;
  }
  map.setCenter( bounds.getCenter() ) ;
  map.setZoom( map.getBoundsZoomLevel( bounds ) ) ;
} // zoom2all

function zoom2map( key ) {
    var bounds = new GLatLngBounds() ;
    var chart  = mapsArr[key] ;
    if( chart == null )
        return ;
    var pLine  = chart.frame ;

    for( var i = 0 ; i < pLine.getVertexCount() ; i++ ) {
        bounds.extend( pLine.getVertex(i) ) ;
    }
    map.setCenter( bounds.getCenter() ) ;
    map.setZoom( map.getBoundsZoomLevel( bounds ) ) ;
} // zoom2map


// Delete the chosen map
// The info-pin can not be deleted, so it is moved to the northpole
function hideMap( key ) {
    var output = '';
    var chart = mapsArr[key] ;
    chart.marker.hide() ;
    chart.marker.bindInfoWindowHtml( null ) ;
    map.removeOverlay( chart.marker ) ;
    map.removeOverlay( chart.frame ) ;
    mgr.removeMarker( chart.marker) ;
    //chart.marker = null ;
    chart.shown = false ;
    //mapsArr[key] = null ;
} // hideMap

