/* ütf-8 marker */
/**
 * Simple Google Map
 *
 * @package     widgets
 * @author      Dieter Raber <raber@h2a.lu>
 * @copyright   h2a.lu 26.07.2007 11:07:39
 *
 * $: simpleGmap.js 21 2007-08-10 14:39:35Z dieter $
 */

if(jQuery.browser.msie && document.namespaces) {
  document.namespaces.add('v', 'urn:schemas-microsoft-com:vml');
  var styleSheet = document.createStyleSheet()
  styleSheet.addRule('v\\:*', 'behavior:url(#default#VML)');
}

function simpleGmap(cwp) {
  cwp = 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           || '',
    latitude          : cwp.latitude          || false,
    longitude         : cwp.longitude         || false,
    zoom              : cwp.zoom              || 15,
    showInfo          : cwp.showInfo          || true,
    directionsId      : cwp.directionsId      || 'google-directions',
    locale            : cwp.locale            || 'fr_FR',
    noDirections      : cwp.noDirections      || '',
    departAddress     : cwp.departAddress     || '',
    hintClass         : cwp.hintClass         || 'hint',
    searchLabel       : cwp.searchLabel       || '',
    searchBtn         : cwp.searchBtn         || '',
    searchText        : cwp.searchText        || '',
    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(typeof(GBrowserIsCompatible) != 'function'  || !GBrowserIsCompatible()) { return; }

  if(!document.getElementById(wp.mapId)) { return }
  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 < 220) { throw 'Map must be at least 220px wide to display the copyrights properly'; }


  jQuery('#' + wp.mapId).css({width    : wp.mapWidth + 'px',
                              height   : wp.mapHeight + 'px',
                              overflow : 'hidden',
                              border   : wp.mapBorder});

  jQuery('<div id="' + wp.mapId + '-contents" style="width: 100%; height: 100%">'
        + wp.waiting + '</div>').appendTo('#' + wp.mapId);

  jQuery(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" '
                 + 'onclick="this.blur()" '
                 + '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
    jQuery(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',// this takes the padding in account
                                                     height     : wp.searchHeight + 'px',
                                                     padding    : '10px',
                                                     font       : wp.searchFont});

    new inputHint({hintClass : wp.hintClass});

    if(document.getElementById(wp.directionsId)) {

      //tell the map where to put the directions and instantiate directions object
      var drivingDiv = document.getElementById(wp.directionsId);
      var directions = new GDirections(this.map, drivingDiv);

      //show an error message
      GEvent.addListener(directions, "error", function() {
        drivingDiv.innerHTML = wp.noDirections;
      });

      GEvent.addListener(directions, "load", function() {
        drivingDiv.innerHTML = '';
      });
      //tell the form to call the direction loader on submit
      jQuery('#' + wp.mapId + ' form').submit(function() {
        if(this.saddr.value) {
          directions.load("from: " + this.saddr.value + " to: " + this.daddr.value, { "locale": wp.locale });
        }
        else if(this.saddr.alt) {
          this.saddr.value = this.saddr.alt;
        }
        return false;
      });
    }
  }
}



