/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD licence.
 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
 * for the full text of the license. */


/**
 * @class
 * 
 * @requires OpenLayers/Layer/Grid.js
 */
OpenLayers.Layer.AGS = OpenLayers.Class(OpenLayers.Layer.Grid, {

    
    reproject: false,
    isBaseLayer: true,
    tileOrigin: null,

    /**
    * @constructor
    *
    * @param {String} name
    * @param {String} url
    * @param {Object} params
    * @param {Object} options Hashtable of extra options to tag onto the layer
    */
    initialize: function(name, url, options) {
        var newArguments = [];
        newArguments.push(name, url, {}, options);
        OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
    },    

    /**
     * 
     */
    destroy: function() {
        // for now, nothing special to do here. 
        OpenLayers.Layer.Grid.prototype.destroy.apply(this, arguments);  
    },

    
    /**
     * @param {Object} obj
     * 
     * @returns An exact clone of this OpenLayers.Layer.TMS
     * @type OpenLayers.Layer.TMS
     */
    clone: function (obj) {
        
        if (obj == null) {
            obj = new OpenLayers.Layer.AGS(this.name,
                                           this.url,
                                           this.options);
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);

        // copy/set any non-init, non-simple values here

        return obj;
    },    
    
    /**
     * @param {OpenLayers.Bounds} bounds
     * 
     * @returns A string with the layer's url and parameters and also the 
     *           passed-in bounds and appropriate tile size specified as 
     *           parameters
     * @type String
     */
    getURL: function (bounds) {
        var res = this.map.getResolution();

        //var x = (Math.floor((bounds.left - this.tileOrigin.lon) / (res * this.tileSize.w))).toString(16);
        var x = (Math.round((bounds.left - this.tileOrigin.lon) / (res * this.tileSize.w))).toString(16);
        while (x.length < 8) {
        x = "0" + x;
        }
        x = "C" +x;

        //var y = (Math.floor((this.tileOrigin.lat - bounds.top ) / (res * this.tileSize.h))).toString(16);
        var y = (Math.round((this.tileOrigin.lat - bounds.top ) / (res * this.tileSize.h))).toString(16);
        while (y.length < 8) {
        y = "0" + y;
        }
        y = "R" +y;
        
        /*
        var x = (bounds.left - this.tileOrigin.lon) / (res * this.tileSize.w);
        var y = (bounds.bottom - this.tileOrigin.lat) / (res * this.tileSize.h);*/
        
        var z = this.map.getZoom();
        if (z <10) z = "L0"+z;
        else z ="L"+z;

		var url = this.url;
		var path= "/" + this.layername + "/" + z + "/" + y + "/" + x + "." + this.type; 

        if (url instanceof Array) {
            url = this.selectUrl(path, url);
        }

        return url + path; 


//        return this.url + "/" + this.layername + "/" + z + "/" + y + "/" + x + "." + this.type; 
    },

    /**
    * addTile creates a tile, initializes it, and 
    * adds it to the layer div. 
    *
    * @param {OpenLayers.Bounds} bounds
    *
    * @returns The added OpenLayers.Tile.Image
    * @type OpenLayers.Tile.Image
    */
    addTile:function(bounds,position) {
        var url = this.getURL(bounds);
        return new OpenLayers.Tile.Image(this, position, bounds, 
                                             url, this.tileSize);
    },

    /** When the layer is added to a map, then we can fetch our origin 
     *  (if we don't have one.) 
     * 
     * @param {OpenLayers.Map} map
     */
    setMap: function(map) {
        OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
		var res = map.maxResolution; 
		var tile_height = map.getTileSize().h; 
		var tile_height_meters = res * tile_height;
		var yorigin = this.maxExtent.top; 
		var ydiff = yorigin-map.maxExtent.bottom; 
		var nrows = Math.round(ydiff/tile_height_meters); 
		var yminorigin = yorigin-(nrows*tile_height_meters); 
		this.maxExtent.bottom = yminorigin ;
		
        if (!this.tileOrigin) { 
            this.tileOrigin = new OpenLayers.LonLat(this.map.maxExtent.left,
                                                this.map.maxExtent.bottom);

        }                                       
    },

    
    /** @final @type String */
    CLASS_NAME: "OpenLayers.Layer.AGS"
});

