function WMSLayer(mapurl)
{
    this.mapurl = mapurl;
    this.image = document.createElement("img");    
    this.image.style.position = "absolute";
    this.added = false;
	this.moveHandler = null;
	this.mercator = false;
}

WMSLayer.prototype.addTo = function(map)
{
    //alert("add map request");
	if(!this.added)
    {
        this.map = map;
        this.moveHandler = GEvent.addListener(this.map,"moveend",this.getMap.bind(this)); 
        this.zoomHandler = GEvent.addListener(this.map,"zoomend",this.getMap.bind(this));    
        var mapnode = this.map.getPane(G_MAP_MAP_PANE);
        if(mapnode.hasChildNodes())
        {
            mapnode.insertBefore(this.image,mapnode.firstChild);
        }
        else
        {
           mapnode.appendChild(this.image);
        }
		
        this.image.onload = this.mapLoaded.bind(this);
        
        this.getMap();
        
        this.added = true;
    }
}


WMSLayer.prototype.addToW = function(map)
{
    //alert("add map request");
	if(!this.added)
    {
        alert("omapinst");
		this.map = map;
		//mmap = this.getMap;
        //this.moveHandler = GEvent.addListener(this.map,"moveend",mmap.bind(this)); 
        //this.zoomHandler = GEvent.addListener(this.map,"zoomend",this.getMap.bind(this));    
        alert("Pane..." );
		var mapnode = this.map.getPane(G_MAP_PANE);
		alert("pane");
        if(mapnode.hasChildNodes())
        {
            mapnode.insertBefore(this.image,mapnode.firstChild);
			alert("ib");
        }
        else
        {
           mapnode.appendChild(this.image);
		   alert("ac");
        }
		
        this.image.onload = this.mapLoaded.bind(this);
        alert("bind");
        this.getMap();
		alert("geetmap");
        
        this.added = true;
		alert("true");
    }
}


WMSLayer.prototype.remove = function()
{
   if(this.added)
   {
	   this.image.onload = function(){};
       GEvent.removeListener(this.moveHandler);
       GEvent.removeListener(this.zoomHandler); 
       
       Element.remove(this.image);
       this.map = null;
       
       this.added = false;
   }
}

WMSLayer.prototype.copy = function()
{
    return new WMSLayer(this.mapurl);
}

WMSLayer.prototype.setMercator = function(mswitch) {
	this.mercator = mswitch;
}

WMSLayer.prototype.getMap = function()
{
    var bounds = this.map.getBounds();
    
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var bbox = sw.lng()+ "," + sw.lat() + "," + ne.lng() + "," + ne.lat();
    var size = this.map.getSize();
	if (this.mercator) {
		// Koordinaten transformieren
		 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,size.width,size.height);} ,
				onFailure: function() { if(debug) GLog.write("Could not connect to coordinates transscription"); }
			  }
		);	
	} else {
		// Bild anzeigen
		this.requestedWMS(bbox,size.width,size.height,"EPSG:4326");
	}
}

WMSLayer.prototype.mercatorCallback = function(transport,width, height) {
	bbox = transport.responseText;
	if(debug) GLog.write("Neue BBox: "+bbox);
	this.requestedWMS(bbox,width,height,'EPSG:54004');
}

WMSLayer.prototype.requestedWMS = function(bbox,width,height,epsg) {
	
    var url = this.mapurl + "&BBOX=" +bbox+"&WIDTH=" + width + "&HEIGHT=" + height+"&SRS="+epsg;
	if(debug)
		 GLog.write("Requested WMS: "+url);
    
    this.image.style.display = "none";
    
    this.image.src = url;
     
    this.image.width = width;
       
    this.image.height = height;
}

WMSLayer.prototype.mapLoaded = function()
{
    var bounds = this.map.getBounds();
    
    var p1 = bounds.getSouthWest();
    var p2 = bounds.getNorthEast();
    
    var pos = this.map.fromLatLngToDivPixel(new GLatLng(p2.lat(),p1.lng()));
       
    this.image.style.top = pos.y + "px";
    this.image.style.left = pos.x + "px";
    
    this.image.style.display = "";
    
    
}
