/* ütf-8 marker */
/**
 * Simple Google Map
 *
 * @package     widgets
 * @author      Dieter Raber <raber@h2a.lu>
 * @copyright   h2a.lu 26.07.2007 11:07:39
 *
 * $Id: simpleGmap.js 9 2007-07-26 09:15:38Z dieter $
 */
function simpleGmap(cwp) { 

  var wp = {
    mapId             : cwp.mapId             || 'google-map',
    mapWidth          : cwp.mapWidth          || 270,
    mapHeight         : cwp.mapHeight         || 250,
    mapBorder         : cwp.mapBorder         || '1px #ccc solid',
    waiting           : cwp.waiting           || 'Chargement...',
    latitude          : cwp.latitude          || 0,
    longitude         : cwp.longitude         || 0,
    zoom              : cwp.zoom              || 15,
    showInfo          : cwp.showInfo          || true, 
    departAddress     : cwp.departAddress     || '', 
    hintClass         : cwp.hintClass         || 'hint',  
    searchLabel       : cwp.searchLabel       || 'Itinéraire vers ce lieu :',
    searchBtn         : cwp.searchBtn         || 'Envoyer',
    searchText        : cwp.searchText        || 'Lieu de départ', 
    searchLeft        : cwp.searchLeft        || 0,
    searchTop         : cwp.searchTop         || 0,
    searchHeight      : cwp.searchHeight      || 55,  
    searchWidth       : cwp.searchWidth       || 145,
    searchBg          : cwp.searchBg          || 'white',
    searchFont        : cwp.searchFont        || '11px helvetica,arial,sans-serif',
    searchColor       : cwp.searchColor       || '#333'
  };
  
  if(!GBrowserIsCompatible()) { return; }  
  
  if(!document.getElementById(wp.mapId)) { throw 'Map container does not exist'; }    
  if(!wp.latitude || !wp.longitude) { throw 'Latitude or longitude are missing'; } 
  if(wp.showInfo && !wp.departAddress) { throw 'Departure address is missing'; }  
  if(wp.showInfo && typeof(inputHint) != 'function') { throw 'Class inputHint not found'; }  
  if(wp.mapWidth < 270) { throw 'Map must be a lest 270px wide to display the copyrights properly'; }
  
  $('#' + wp.mapId).css({width   : wp.mapWidth + 'px',
                        height   : wp.mapHeight + 'px',
                        overflow : 'hidden',
                        border   : wp.mapBorder});
    
  $('<div id="' + wp.mapId + '-contents" style="width: 100%; height: 100%">' 
    + wp.waiting + '</div>').appendTo('#' + wp.mapId); 
  
  $(window).unload(function() { GUnload(); })
  this.map    = new GMap2(document.getElementById(wp.mapId));
  this.map.setCenter(new GLatLng(wp.latitude,wp.longitude), wp.zoom); 
  this.map.addControl(new GSmallMapControl());
  
  this.marker = new GMarker(new GLatLng(wp.latitude,wp.longitude));
  this.map.addOverlay(this.marker);
  
  if(wp.showInfo) {
    var searchCode = '<div id="' + wp.mapId + '-search" style="border:1px #ccc solid;">\n'
                 + '<form action="http://maps.google.com/maps" method="get" target="_blank">\n'
                 + '<label style="text-align:left; color:' + wp.searchColor + '">' + wp.searchLabel
                 + '<input type="text" '
                 + 'style="border:1px #ccc solid;background:#efefef;'
                 + 'font: ' + wp.searchFont + '" '
                 + 'width: ' + (wp.searchWidth - 20) + 'px" '
                 + 'name="saddr" '
                 + 'class="' + wp.hintClass + '" '
                 + 'alt="' + wp.searchText + '" />\n'
                 + '</label>\n'
                 + '<button type="submit" '
                 + 'style="border:1px #999 solid;background:#efefef;'
                 + 'display:block;margin:5px auto;'
                 + 'color:' + wp.searchColor + ';'
                 + 'font: ' + wp.searchFont + '">' + wp.searchBtn + '</button>\n'
                 + '<input type="hidden" name="daddr" value="' + wp.departAddress + '" />\n'      
                 + '</form>\n'               
                 + '</div>\n';
      
    //gets the position of the marker relative to the map container
    var tmpLatLng     = this.map.fromContainerPixelToLatLng(new GPoint(0,0), true);
    var tmpDivPixel   = this.map.fromLatLngToDivPixel(tmpLatLng);
    var pointDivPixel = this.map.fromLatLngToDivPixel(this.marker.getPoint());  
    var markerPos     = new GPoint(pointDivPixel.x-tmpDivPixel.x, pointDivPixel.y-tmpDivPixel.y);
      
    //get the icon width and height to offset the searchWindow
    var iconWidth  = this.marker.getIcon().iconSize.width;
    var iconHeight = this.marker.getIcon().iconSize.height;   
  
    var absMarkerPosTop  = 0 + markerPos.y;
    var absMarkerPosLeft = 0 + markerPos.x;
      
    /*if we have a height for the searchwindow, adapt the position so 
      the lower left corner of the searchwindow is on the marker
      if we do not have a height, the upper left corner is on the marker
    */    
    if(!isNaN(wp.searchHeight) && (absMarkerPosTop - wp.searchHeight - iconHeight) >= 0) {
      absMarkerPosTop  = absMarkerPosTop - wp.searchHeight - iconHeight;
      absMarkerPosLeft = absMarkerPosLeft + iconWidth/2;
    }
          
    //display the window
    $(searchCode).appendTo('#' + wp.mapId).css({display    : 'block',
                                                background : wp.searchBg,
                                                position   : 'absolute',
                                                top        : wp.searchLeft + absMarkerPosTop + 'px',
                                                left       : wp.searchTop + absMarkerPosLeft + 'px',
                                                width      : wp.searchWidth - 20 + 'px',
                                                height     : wp.searchHeight + 'px',
                                                padding    : '10px',
                                                font       : wp.searchFont});
    
    new inputHint();
  }  
}

          



