YAHOO.namespace("mzag.gmaps");

// marker + markerManager
YAHOO.mzag.gmaps.createMarker=function(id, latlng, options, infotext, markerClass)
{
  YAHOO.mzag.gmaps.markerClass=markerClass;
  var marker=window['YAHOO']['mzag']['gmaps']['markerClass']
    ? new window['YAHOO']['mzag']['gmaps']['markerClass'](latlng, options)
    : new GMarker(latlng,options);

  if(infotext)
  {
    marker.infotext=infotext;
    marker.showInfo=function(){this.openInfoWindowHtml(this.infotext);}
    marker.hideInfo=function(){this.closeInfoWindow();}
  }
  return marker;
}

YAHOO.mzag.gmaps.getMarker=function(id)
{
  /(.+)-.+/.exec(id);
  var manager=RegExp.$1;
  return YAHOO.mzag.gmaps.markerManagers[manager].getMarker(id);
}
YAHOO.mzag.gmaps.markerManager=function(map)
{
  this.map=map;
  this.markers={};
}
YAHOO.mzag.gmaps.markerManager.prototype.getMarker=function(id)
{
  return this.markers[id];
}
YAHOO.mzag.gmaps.markerManager.prototype.addMarker=function(marker, id)
{
  this.map.addOverlay(marker);
  this.markers[id]=marker;
}
YAHOO.mzag.gmaps.markerManager.prototype.hideAll=function()
{
  for(index in this.markers)
    this.markers[index].hide();
}
YAHOO.mzag.gmaps.markerManager.prototype.showAll=function()
{
  for(index in this.markers)
    this.markers[index].show();
}

//labeled marker:
//constructor
YAHOO.mzag.gmaps.markerLabeled=function(latlng, options)
{
  this.latlng=latlng;
  this.labelText=options.labelText || "";
  this.labelClass=options.labelClass || "mzag_gui_googlemaps_marker_labeled";
  this.labelOffset=options.labelOffset || new GSize(0, 0);
  GMarker.apply(this, arguments);
}
YAHOO.mzag.gmaps.markerLabeled.prototype=new GMarker(0);

//pass events from the label to the actual marker
YAHOO.mzag.gmaps.eventPassthru=function(object, eventName)                    {return function(){GEvent.trigger(object, eventName);}}

//initialization
YAHOO.mzag.gmaps.markerLabeled.prototype.initialize=function(map)
{
  GMarker.prototype.initialize.call(this, map);
  
  if(this.label)
   this.label.parentNode.removeChild(this.label);
  
  var label=document.createElement("div");
  label.className=this.labelClass;
  label.innerHTML=this.labelText;
  label.style.position="absolute";
  map.getPane(G_MAP_MARKER_PANE).appendChild(label);
  
  var events=new Array("click", "mouseover", "mouseout");
  for (name in events)
  {
    var event=events[name];
    GEvent.addDomListener(label, event, YAHOO.mzag.gmaps.eventPassthru(this, event));
  }
  
  // mouseover behaviour for the cursor.
  label.style.cursor="pointer";
  
  this.map=map;
  this.label=label;
}

//redraw
YAHOO.mzag.gmaps.markerLabeled.prototype.redraw=function(force) {
  GMarker.prototype.redraw.call(this, this.map);
  // we only need to do anything if the coordinate system has changed
  if (!force) return;
  var p=this.map.fromLatLngToDivPixel(this.latlng);
  var z=GOverlay.getZIndex(this.latlng.lat());
  this.label.style.left=(p.x+this.labelOffset.width)+"px";
  this.label.style.top=(p.y+this.labelOffset.height)+"px";
  this.label.style.zIndex=z+1; // directly in front of the marker image
};
YAHOO.mzag.gmaps.markerLabeled.prototype.hide=function() {
  GMarker.prototype.hide.call(this, this.map);
  this.label.style.display="none";
}
YAHOO.mzag.gmaps.markerLabeled.prototype.show=function() {
  GMarker.prototype.show.call(this, this.map);
  this.label.style.display="block";
}
//------------------------------------------------------------------------------


// clearing and resetting
YAHOO.mzag.gmaps.clear=function()
{
  YAHOO.mzag.gmaps.markers=new Array();
  YAHOO.mzag.gmaps.map.clearOverlays();
}

YAHOO.mzag.gmaps.resetViewport=function(maxZoomLevel)
{
  for(var i in YAHOO.mzag.gmaps.markers)
  {
    var minLat;
    latlng=YAHOO.mzag.gmaps.markers[i].getLatLng();
    if(!minLat)
    {
      minLat=maxLat=latlng.lat();
      minLng=maxLng=latlng.lng();
    }
    else
    {
      minLat=latlng.lat()<minLat?latlng.lat():minLat;
      minLng=latlng.lng()<minLng?latlng.lng():minLng;
      maxLat=latlng.lat()>maxLat?latlng.lat():maxLat;
      maxLng=latlng.lng()>maxLng?latlng.lng():maxLng;
    }
  }
  if(minLat)
  {
    bounds=new GLatLngBounds(
      new GLatLng(minLat,minLng),
      new GLatLng(maxLat,maxLng)
    );
    var boundsZoomLevel=(YAHOO.mzag.gmaps.map.getBoundsZoomLevel(bounds)-YAHOO.mzag.gmaps.map.boundsZoomReduction);
    var zoom=(boundsZoomLevel>=maxZoomLevel)
      ? maxZoomLevel
      : boundsZoomLevel;
    YAHOO.mzag.gmaps.map.panTo(bounds.getCenter());
    YAHOO.mzag.gmaps.map.setZoom(zoom);
    //TODO can setCenter be called back after panning to?
    YAHOO.mzag.gmaps.map.setCenter(bounds.getCenter());
  }
}
//------------------------------------------------------------------------------

// coords selector
YAHOO.mzag.gmaps.setCoords=function(latlng, panelLat, panelLng, icon)
{
  YAHOO.mzag.gmaps.clear();
  var options=icon ?{icon: icon} :null;
  YAHOO.mzag.gmaps.map.addOverlay(YAHOO.mzag.gmaps.createMarker(1, latlng, options));
  $(panelLat).value=latlng.lat();
  $(panelLng).value=latlng.lng();
}
//------------------------------------------------------------------------------

// area
// topLeft and bottomRight assuming that the area is being drawn from topLeft to bottomRight
YAHOO.namespace("mzag.gmaps.area");
YAHOO.mzag.gmaps.area.setCoords=function(latlngTL, latlngBR, panelLatTL, panelLngTL, panelLatBR, panelLngBR)
{
  YAHOO.mzag.gmaps.clear();
  YAHOO.mzag.gmaps.area.panels={};
  YAHOO.mzag.gmaps.area.panels.latTL=panelLatTL;
  YAHOO.mzag.gmaps.area.panels.lngTL=panelLngTL;
  YAHOO.mzag.gmaps.area.panels.latBR=panelLatBR;
  YAHOO.mzag.gmaps.area.panels.lngBR=panelLngBR;
  
  $(panelLatTL).value=latlngTL.lat();
  $(panelLngTL).value=latlngTL.lng();

  $(panelLatBR).value=latlngBR.lat();
  $(panelLngBR).value=latlngBR.lng();
}
YAHOO.mzag.gmaps.area.select=function(latlng, overlay, overlaylatlng)
{
  if(typeof latlng=='undefined') // clicked on GPolygon
    latlng=overlaylatlng;
  console.log(latlng);
  console.log(overlay);
  console.log(overlaylatlng);
  if(!YAHOO.mzag.gmaps.area.coordsTopLeft)// start selection -> top left first
  {
    YAHOO.mzag.gmaps.area.coordsTopLeft=latlng;
    YAHOO.mzag.gmaps.area.selectionEvent=GEvent.addListener(YAHOO.mzag.gmaps.map, 'mousemove', YAHOO.mzag.gmaps.area.moveCallback);
    YAHOO.mzag.gmaps.map.removeOverlay(YAHOO.mzag.gmaps.area.currentArea);
  }
  else
  {
    if((YAHOO.mzag.gmaps.area.coordsTopLeft.lat()-YAHOO.mzag.gmaps.area.coordsBottomRight.lat()<=0)||(YAHOO.mzag.gmaps.area.coordsTopLeft.lng()-YAHOO.mzag.gmaps.area.coordsBottomRight.lng()>=0))
    {
      alert(YAHOO.mzag.gmaps.area.selectionError);
      return;
    }
    GEvent.removeListener(YAHOO.mzag.gmaps.area.selectionEvent);
    $(YAHOO.mzag.gmaps.area.panels.latTL).value=YAHOO.mzag.gmaps.area.coordsTopLeft.lat();
    $(YAHOO.mzag.gmaps.area.panels.lngTL).value=YAHOO.mzag.gmaps.area.coordsTopLeft.lng();
    $(YAHOO.mzag.gmaps.area.panels.latBR).value=YAHOO.mzag.gmaps.area.coordsBottomRight.lat();
    $(YAHOO.mzag.gmaps.area.panels.lngBR).value=YAHOO.mzag.gmaps.area.coordsBottomRight.lng();
    YAHOO.mzag.gmaps.area.coordsTopLeft=null;
  }
}
YAHOO.mzag.gmaps.area.moveCallback=function(latlng)
{
  YAHOO.mzag.gmaps.area.coordsBottomRight=latlng;
  if(typeof YAHOO.mzag.gmaps.area.overlay!='undefined')
    YAHOO.mzag.gmaps.map.removeOverlay(YAHOO.mzag.gmaps.area.overlay);
  YAHOO.mzag.gmaps.area.overlay=YAHOO.mzag.gmaps.area.draw(YAHOO.mzag.gmaps.area.coordsTopLeft, YAHOO.mzag.gmaps.area.coordsBottomRight, YAHOO.mzag.gmaps.area.selectBorderColor, YAHOO.mzag.gmaps.area.selectBgColor);
}
YAHOO.mzag.gmaps.area.draw=function(latlngTL, latlngBR, borderColor, bgColor, borderOpacity, bgOpacity)
{
  var polygon=new GPolygon(YAHOO.mzag.gmaps.area.createVertices(latlngTL, latlngBR), borderColor, 3, borderOpacity, bgColor, bgOpacity);
  var events=new Array("click", "mouseover", "mouseout");
  YAHOO.mzag.gmaps.map.addOverlay(polygon);
  return polygon; 
}
YAHOO.mzag.gmaps.area.createVertices=function(topLeft, bottomRight)
{
  var topRight=new GLatLng(bottomRight.lat(), topLeft.lng());
  var bottomLeft=new GLatLng(topLeft.lat(), bottomRight.lng());
  return new Array(topLeft, bottomLeft, bottomRight, topRight, topLeft);
}

