function TGWMSOverlay(wmsurl,layernames,wmsbounds) 
{
    this.title = "";
    this.queryable = false;
    this.layers = new Array();
    this.active = true;
    this.subLayersVisible = true;
    this.wmsurl = wmsurl;
    this.layernames = layernames;
    this.wmsbounds=wmsbounds;
    this.image = null;
    this.dragEventHandle = null;
    this.opacity = 1;
	
	// Flagging für Nutzung von WebMercator-Koordinaten (z.B. bei ArcIMS)
	this.webmercator = false;
}

TGWMSOverlay.prototype = new GOverlay();


TGWMSOverlay.prototype.initialize = function(map) 
{
  this.image = document.createElement("IMG");
  this.image.onload = this.imageLoaded.bindAsEventListener(this);
  this.image.style.position = "absolute";
  this.image.style.opacity = this.opacity;
  this.image.style.filter = "alpha(opacity=" + this.opacity * 100 + ")";   
     
  map.getPane(G_MAP_MAP_PANE).appendChild(this.image);

  this.map = map;
  
  this.dragEventHandle = GEvent.bind(this.map,"dragend",this,this.dragend);
  this.zoomEventHandle = GEvent.bind(this.map,"zoomstart",this,this.zoomstart);
}

TGWMSOverlay.prototype.remove = function() 
{
  GEvent.removeListener(this.dragEventHandle);
  GEvent.removeListener(this.zoomEventHandle);
  this.image.parentNode.removeChild(this.image);
  this.image = null;
}


TGWMSOverlay.prototype.copy = function() 
{
  return new TGWMSOverlay(this.wmsurl,this.layernames,this.wmsbounds);
}

TGWMSOverlay.prototype.imageLoaded = function() 
{
  if(this.image)
  {
      this.image.style.display = "";

      if(debug)GLog.write("Image loaded");
  }
}

TGWMSOverlay.prototype.redraw = function(force) 
{
  if(debug) GLog.write("Redraw called with force: "+force);
  if (!force) 
  {
	return;
  }
  
  var mapBounds = this.map.getBounds();
  
  if(mapBounds.containsBounds(this.wmsbounds)) 
  {
     mapBounds = this.wmsbounds;
  }
  var ne = mapBounds.getNorthEast();  
  var sw = mapBounds.getSouthWest(); 
  
  //var width = parseInt(this.map.getContainer().style.width);
  //var wmsheight = parseInt(((ne.lat() - sw.lat()) / (ne.lng() - sw.lng()) ) * width);
  //var realheight = parseInt(this.map.getContainer().style.height);
  
  var c1 = this.map.fromLatLngToDivPixel(sw);
  var c2 = this.map.fromLatLngToDivPixel(ne);
  
  bbox = sw.lng()+ "," + sw.lat() + "," + ne.lng() + "," + ne.lat();
  if(debug) GLog.write("Original-BBox: "+bbox);
  if(this.webmercator) {
	  //bbox = bbox.replace(/,/g,"-");
	  // AJAX auf Umrechnung...
	 if(debug) GLog.write("Umrechnung auf Webmercator...");
     var obj = this;
	 var ajaxURL = 'http://www.googis.de/viewer/php/projprox.php?'+'Koords='+bbox;
	 if (debug) GLog.write(ajaxURL);
	 var myAjax = new Ajax.Request
   	 (
   		ajaxURL, 
    	  {
    		method: 'GET', 
    	    onLoading: function() {if(debug) GLog.write("Doing transformation...");},
    	    onSuccess: function(transport) {obj.mercatorCallback(transport,c1, c2);} ,
    	    onFailure: function() { if(debug) GLog.write("Could not connect to coordinates transscription"); }
    	  }
    );

  }
  else {
	  this.requestImage(bbox,c1,c2,"EPSG:4326");
  }
  
}

TGWMSOverlay.prototype.mercatorCallbackzwi = function(c1,c2) {
	if (debug) GLog.write("zwi");
	var obj = this;
	return function(transport) {
		if (debug) GLog.write(transport.responseText);
		obj.mercatorCallback(transport,c1,c2); 
	}
}


TGWMSOverlay.prototype.mercatorCallback = function(request,c1,c2) {
	//if (debug) GLog.write(request);
	bbox = request.responseText;
	if(debug) GLog.write(bbox);
	this.requestImage(bbox,c1,c2,'EPSG:54004');
}

TGWMSOverlay.prototype.requestImage = function(bbox,c1,c2,epsg) {
	var wu = this.wmsurl + "&LAYERS=" + this.layernames + "&SRS="+epsg+"&STYLES=&BBOX=" + bbox + "&WIDTH=" + (c2.x - c1.x) + "&HEIGHT=" + (c1.y - c2.y); 
	if(debug) GLog.write("requestImagae: "+wu);
	this.image.src = wu;
	this.image.style.left = c1.x + "px";
	this.image.style.top = c2.y + "px";
}

TGWMSOverlay.prototype.layersChanged = function(layernames)
{
    
    this.layernames = layernames;
}

TGWMSOverlay.prototype.dragend = function()
{
   this.image.style.display = "none";
   this.redraw(true);
}

TGWMSOverlay.prototype.zoomstart = function()
{
    this.image.style.display = "none";
}

TGWMSOverlay.prototype.setOpacity = function(value)
{
    this.opacity = value;
}

TGWMSOverlay.prototype.setWebMercator = function(value)
{
    this.webmercator = value;
}

function TGWMSOverviewMapControl(wmsLayerArray)
{
    this.element = null;
    this.overviewMap = null;
    this.wmsLayerArray = wmsLayerArray;
    this.map = null;
    this.imageArray = null;
    this.oldCenter = null;
}

TGWMSOverviewMapControl.prototype = new GControl();

TGWMSOverviewMapControl.prototype.printable = function()
{
    return new Boolean(false);
}

TGWMSOverviewMapControl.prototype.selectable = function()
{
    return new Boolean(false);
}

TGWMSOverviewMapControl.prototype.initialize = function(map)
{
    this.map = map;
    
    if(this.element == null)
    {
        this.element = document.createElement("div");
        this.element.style.width = "120px";
        this.element.style.height = "120px";
        this.element.style.backgroundColor = "#FFFFFF";
        this.element.style.borderTop = "1px solid #979797";
        this.element.style.borderLeft = "1px solid #979797";
        this.element.style.overflow = "hidden";
        
        this.overviewMap = document.createElement("div");
        this.overviewMap.style.position = "relative";
        this.overviewMap.style.top = "8px";
        this.overviewMap.style.left = "8px";
        this.overviewMap.style.width = "112px";
        this.overviewMap.style.height = "112px";
        this.overviewMap.style.border = "1px solid #979797";
        
        this.element.appendChild(this.overviewMap);
        
        map.getContainer().appendChild(this.element);
        
        GEvent.addListener(this.map,"zoomend",this.redraw.bind(this));
        GEvent.addListener(this.map,"moveend",this.redraw.bind(this));
    }

    return this.element;
    
}

TGWMSOverviewMapControl.prototype.redraw = function(moved)
{
    this.oldCenter = this.map.fromLatLngToDivPixel(this.map.getBounds().getCenter());
    
    this.overviewMap.innerHTML = "";
    
    this.imageArray = new Array();
    
    var cwidth = parseInt(this.map.getContainer().style.width);
    var cheight = parseInt(this.map.getContainer().style.height);
    
    var width = parseInt((cwidth/cheight) * 120.0);
    
    for(var i = 0;i < this.wmsLayerArray.length;i++)
    {
        var wmsMenu = this.wmsLayerArray[i];
        if(wmsMenu.subLayersActive(wmsMenu.layers))
        {
           var div = document.createElement("div");
           var img = document.createElement("img");
           img.onload = this.imageLoaded.bind(this,div);
           img.src = wmsMenu.overlay.getOverviewUrl(120,120);
           div.style.position = "absolute";
           div.style.width = width + "px";
           div.style.height = "120px";
           div.style.top = "0px";
           div.style.left = "0px";
    
           div.style.opacity = 0.8;
           div.style.filter = "alpha(opacity=" + 0.8 * 100 + ")";   
           div.appendChild(img);
           div.style.display = "none";
           this.overviewMap.appendChild(div);
           this.imageArray[this.imageArray.length] = div;
        }
    }      
}

TGWMSOverviewMapControl.prototype.imageLoaded = function(div)
{
    div.style.display = "";
}

TGWMSOverviewMapControl.prototype.getDefaultPosition = function()
{
    return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0,0));
}

