var n4=(document.layers);
var n6=(document.getElementById&&!document.all);
var ie=(document.all);
var o6=(navigator.appName.indexOf("Opera")!=-1);
var safari=(navigator.userAgent.indexOf("Safari")!=-1);
var currentSpan=new GBounds();
ajaxComplete = new Object();


function setCursor(container,cursor)
{
  try
  {
    container.style.cursor=cursor;
  } catch(c) {
    if(cursor=="pointer")
    setCursor("hand");
  }
};

function hideAllTooltips()
{
  tooltips = document.getElementsByClassName("googleMapTooltip");
  for(i = 0; i < tooltips.length; i++) {
    tooltips[i].style.display='none';
  }
}

function MyMarker(a,b,tooltipHtml,tooltipId,url)
{
  this.inheritFrom=GMarker;
  this.inheritFrom(a,b);
  if(!currentSpan.minX||a.x<currentSpan.minX)
  currentSpan.minX=a.x;
  if(!currentSpan.maxX||a.x>currentSpan.maxX)
  currentSpan.maxX=a.x;
  if(!currentSpan.minY||a.y<currentSpan.minY)
  currentSpan.minY=a.y;
  if(!currentSpan.maxY||a.y>currentSpan.maxY)
  currentSpan.maxY=a.y;
  if(typeof tooltipHtml!="undefined")
  {
    this.setTooltip(tooltipHtml);
  }
  if(typeof tooltipHtml!="undefined")
  {
    this.setTooltipId(tooltipId);
  }
  this.overlayUrl=url;
  //this.tooltip.toolTipTopHtml = '<table><tr><td style="background-image:url(bilder/gmap_ttip_tl.png)"><div style="width:23px; height:25px;"></div></td><td style="height:25px; background-image:url(bilder/themeStandard/gmap_ttip_tr.png);background-position:right;"><div style="float:right; height:16px; width:16px; cursor:pointer;" onclick="hideAllTooltips();"></div></td></tr><tr><td style="background-image:url(bilder/themeStandard/gmap_ttip_ml.png);"><div style="width:23px;"></td><td style="background-image:url(bilder/themeStandard/gmap_ttip_mr.png);background-position:right;padding-right:15px;">';
  //this.tooltip.toolTipBottomHtml = '</td></tr><tr><td style="background-image:url(bilder/gmap_ttip_bl.png)"><div style="width:23px; height:19px;"></div></td><td style="height:19px; background-image:url(bilder/themeStandard/gmap_ttip_br.png);background-position:right;"></td></tr></table>';
  this.tooltip.toolTipTopHtml = '';
  this.tooltip.toolTipBottomHtml = '';
}

MyMarker.prototype=new GMarker(new GLatLng(1,1));
MyMarker.prototype.setTooltip=function(string)
{
  this.removeTooltip();
  this.tooltip=new Object();
  this.tooltip.opacity=100;
  this.tooltip.contents=string;
};
MyMarker.prototype.setTooltipId=function(tooltipId)
{
  this.tooltip.tooltipId=tooltipId;
};
MyMarker.prototype.initialize=function(a)
{
  try
  {
    GMarker.prototype.initialize.call(this,a);
    this.MouseOverListener = GEvent.addListener(this,"mouseover",this.onMouseOver);
    this.MouseOutListener = GEvent.addListener(this,"mouseout",this.onMouseOut);
    this.ClickListener = GEvent.addListener(this,"click",this.onClick);
    this.map=a;
  }
  catch(e)
  {
    console.error(e);
  }
}

MyMarker.prototype.setCursor=function(cursor)
{
  var c=this.iconImage;
  if(n6&&this.icon.imageMap&&!safari)
  {
    c=this.imageMap;
  }
  else if(this.transparentIcon&&typeof this.transparentIcon!="undefined")
  {
    c=this.transparentIcon;
  }
}

MyMarker.prototype.remove=function(a)
{
  GMarker.prototype.remove.call(this);
  this.removeTooltip();
}

MyMarker.prototype.removeTooltip=function()
{
  if(this.tooltipElement)
  {
    try
    {
      document.body.removeChild(this.tooltipElement);
    }
    catch(e)
    {
      console.error(e);
    }

    this.tooltipElement=null;
  }
}

MyMarker.prototype.onInfoWindowOpen=function()
{
  this.hideTooltip();
  GMarker.prototype.onInfoWindowOpen.call(this);
}

MyMarker.prototype.onMouseOver=function()
{
  tooltips = document.getElementsByClassName("googleMapTooltip");
  for(i = 0; i < tooltips.length; i++) {
    tooltips[i].style.display='none';
  }
  this.showTooltip();
};

MyMarker.prototype.onClick=function()
{
    GEvent.removeListener(this.MouseOutListener);
    if(this.tooltip.tooltipId){
        tooltipAjaxContainer = this.tooltip.tooltipId;
        tooltipElement = this.tooltipElement;
        if(!ajaxComplete[tooltipAjaxContainer]){
            new Ajax.Request('/feeds/ajax_region_info.php?ttip='+this.tooltip.tooltipId,
              {
                method:'get',
                onSuccess: function(transport){
                  var response = eval("(" + transport.responseText + ")");
                  $(response.id).innerHTML = response.html;
                  ajaxComplete[response.id] = true;
                },
                onFailure: function(){ $(tooltipAjaxContainer).innerHTML = 'Es ist leider ein Fehler aufgetreten.<br />Bitte versuch es sp&auml;ter noch einmal.'},
                onLoading: function(){ $(tooltipAjaxContainer).innerHTML += '<br /><img src="../bilder/loading.gif" />'; }
              });
        }
      }
};

MyMarker.prototype.onMouseOut=function()
{
  this.hideTooltip();
};

MyMarker.prototype.showTooltip=function()
{
  if(this.tooltip)
  {
    if(!this.tooltipElement)
    {
      var opacity=this.tooltip.opacity/100;
      this.tooltipElement=document.createElement("div");
      this.tooltipElement.className="googleMapTooltip";
      //this.tooltipElement.id=this.tooltip.tooltipId;
      this.tooltipElement.innerHTML=this.tooltip.toolTipTopHtml+this.tooltip.contents+this.tooltip.toolTipBottomHtml;
      document.body.appendChild(this.tooltipElement);
    }

    markerPosition=calculateMarkerPosition(this.map,this.getPoint());
    try
    {
      this.tooltipElement.style.top=markerPosition.top-(this.getIcon().iconAnchor.y+0)+"px";
      this.tooltipElement.style.left=markerPosition.left+(this.getIcon().iconSize.width-this.getIcon().iconAnchor.x+5)+"px";
      this.tooltipElement.style.display="block";

    }
    catch(e)
    {
      console.error(e);
    }
  }
}

function calculateMarkerPosition(map,markerLatLngPoint)
{
  gmapTopLeftLatLng=map.fromContainerPixelToLatLng(new GPoint(0,0));
  gmapTopLeftPixel=map.fromLatLngToDivPixel(gmapTopLeftLatLng);
  myMarkerDivPixel=map.fromLatLngToDivPixel(markerLatLngPoint);
  markerTop=getAbsoluteTop(map.getContainer())-gmapTopLeftPixel.y+myMarkerDivPixel.y;
  markerLeft=getAbsoluteLeft(map.getContainer())-gmapTopLeftPixel.x+myMarkerDivPixel.x;
  return{top:markerTop,left:markerLeft};
}

MyMarker.prototype.hideTooltip=function()
{
  if(this.tooltipElement)
  {
    this.tooltipElement.style.display="none";
  }
}

function makeInterface(a)
{
  var b=a||window;
  b.MyMarker=MyMarker;
}

makeInterface();

function getAbsoluteTop(o)
{
  var oTop=o.offsetTop;
  var oParent;
  var o;
  while(o.offsetParent!=null)
  {
    oParent=o.offsetParent
    oTop+=oParent.offsetTop
    o=oParent
  }
  return oTop
}

function getAbsoluteLeft(o)
{
  var oLeft=o.offsetLeft;
  var oParent;
  var o;
  while(o.offsetParent!=null)
  {
    oParent=o.offsetParent
    oLeft+=oParent.offsetLeft
    o=oParent
  }
  return oLeft
}

function centerAndZoomOnBounds(bounds, map, maxZoom)
{
  var center = bounds.getCenter();
  var newZoom = map.getBoundsZoomLevel(bounds);
  if (maxZoom && newZoom > maxZoom)
  {
    newZoom = maxZoom;
  }
  if (map.getZoom() != newZoom)
  {

    map.setCenter(center, newZoom);
  }
  else
  {
    map.panTo(center);
  }
}


function createMarker(point,html,url,icon,tooltipId)
{
  var marker=new MyMarker(point,icon,html,tooltipId,url);
  if(url && !tooltipId){
    GEvent.addListener(marker,"click",function(){window.location=url; });
  }
  return marker;
}

function addPoint(map,bounds,html,url,latitude,longitude,icon,ttid){
  var point=new GLatLng(latitude,longitude);
  map.addOverlay(this.createMarker(point,html,url,icon,''));
  bounds.extend(point);
  return map;
}

function addRegion(map,bounds,html,latitude,longitude,icon,tooltipId,imgPath)
{
  var point=new GLatLng(latitude,longitude);
  map.addOverlay(this.createMarker(point,html,'',icon,tooltipId, imgPath));
  bounds.extend(point);
  return map;
}

function addClickRegion(map,bounds,html,url,latitude,longitude,icon)
{
  var point=new GLatLng(latitude,longitude);
  map.addOverlay(this.createMarker(point,html,url,icon,''));
  bounds.extend(point);
  return map;
}