
    SecondLifeLayer = OpenLayers.Class(
	OpenLayers.Layer.TMS, 

        // Overloading the base class starts here
        {
           initialize: function(name, url, options) {
		
	              options.maxResolution=128;
                      options.maxExtent=new OpenLayers.Bounds(500*256,500*256,1500*256,1500*256);
                      options.numZoomLevels=8;
                      options.type='jpg';


	              // Call parental constructor             
                      OpenLayers.Layer.TMS.prototype.initialize.apply(this, arguments);


           },

           getURL: function(bounds) {

              xAbs=bounds.top;
              yAbs=bounds.left;

              y=xAbs/256-1;
              x=yAbs/256;
        
              var z = this.map.getZoom();

//	      if (this.options.minZoomLevel>z) {
//                 alert("Can't display at this level "+this.options.minZoomLevel+" "+z);
//                 return "about:blank";
//              }

              z=8-z;
              
              pot=Math.pow(2, z-1);

              xCorr=(x%pot);
              x=x-xCorr;  


              yCorr=(y%pot);
              y=y-yCorr;


              var result = this.url;
              result=result.replace("$Z", z);
              result=result.replace("$X", x);
              result=result.replace("$Y", y);

              return result;
           },

            /** 
             * Method: calculateGridLayout
             * Generate parameters for the grid layout. This  
             *
             * Parameters:
             * bounds - {<OpenLayers.Bound>}
             * extent - {<OpenLayers.Bounds>}
             * resolution - {Number}
             *
             * Returns:
             * Object containing properties tilelon, tilelat, tileoffsetlat,
             * tileoffsetlat, tileoffsetx, tileoffsety
             *
             * Copied from Layer.Grid and modified slightly
             * so that the tile base is always at 0,0
             *
             */
            calculateGridLayout: function(bounds, extent, resolution) {

                var tilelon = resolution * this.tileSize.w;
                var tilelat = resolution * this.tileSize.h;
                
                var offsetlon = bounds.left;
                var tilecol = Math.floor(offsetlon/tilelon) - this.buffer;
                var tilecolremain = offsetlon/tilelon - tilecol;
                var tileoffsetx = -tilecolremain * this.tileSize.w;
                var tileoffsetlon = tilecol * tilelon;
                
                var offsetlat = bounds.top - tilelat;  
                var tilerow = Math.ceil(offsetlat/tilelat) + this.buffer;
                var tilerowremain = tilerow - offsetlat/tilelat;
                var tileoffsety = -tilerowremain * this.tileSize.h;
                var tileoffsetlat = tilerow * tilelat;
                        
                return { 
                  tilelon: tilelon, tilelat: tilelat,
                  tileoffsetlon: tileoffsetlon, tileoffsetlat: tileoffsetlat,
                  tileoffsetx: tileoffsetx, tileoffsety: tileoffsety
                };

            },
             
        } // End of overloads
    ); // End of class definition



    SLMouseGridPos = OpenLayers.Class(
	OpenLayers.Control.MousePosition, 
        {
           initialize: function(name, url, options) {
	   	// Call parental constructor             
                OpenLayers.Control.MousePosition.prototype.initialize.apply(this, arguments);
                this.numDigits=0;
           },

          formatOutput: function(lonLat) {
             var newHtml =
             this.prefix +
             parseInt(lonLat.lon/256) +
             this.separator +
             parseInt(lonLat.lat/256) +
             this.suffix;
             return newHtml;
          }, 

        CLASS_NAME: "SLMouseGridPos" 

        }
	
    ); // End of class

    SecondLifeGridLayer = OpenLayers.Class(
	SecondLifeLayer, 

        // Overloading the base class starts here
        {
           clickHandler: function(evt) {
               if (!evt.altKey) {
                   return;
               }
               var xy=evt.object.events.getMousePosition(evt);
               var lonlat = map.getLonLatFromViewPortPx(xy);
               var zoom=map.getZoom();
               window.open("manage.php?lat="+lonlat.lat+"&lon="+lonlat.lon+"&zoom="+zoom, "manage", 'width=800,height=600,scrollbars=yes');    

           },


           initialize: function(name, url, options) {

	       // Call parental constructor             
               SecondLifeLayer.prototype.initialize.apply(this, arguments);

               this.events.register("click", this, this.clickHandler);
            }
         } // End of overloading

    ); // End of class


