/* Copyright (c) 2006-2009 MetaCarta, Inc., published under the Clear BSD
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */

/**
 * @requires OpenLayers/Layer/Grid.js
 */

/**
 * Class: OpenLayers.Layer.ZYX
 * The ZYX class is designed to make it easier for people who have tiles
 * arranged by a standard ZYX grid.
 */
OpenLayers.Layer.ZYX = OpenLayers.Class(OpenLayers.Layer.Grid, {

    /**
     * APIProperty: isBaseLayer
     * Default is true, as this is designed to be a base tile source.
     */
    isBaseLayer: true,

    /**
     * APIProperty: sphericalMecator
     * Whether the tile extents should be set to the defaults for
     *    spherical mercator. Useful for things like OpenStreetMap.
     *    Default is false, except for the OSM subclass.
     */
    sphericalMercator: false,

    /**
     * Constructor: OpenLayers.Layer.OSM
     *
     * Parameters:
     * name - {String}
     * url - {String}
     * options - {Object} Hashtable of extra options to tag onto the layer
     */
    initialize: function(name, url, options) {
        if (options && options.sphericalMercator || this.sphericalMercator) {
            options = OpenLayers.Util.extend({
                maxExtent: new OpenLayers.Bounds(
                    -128 * 156543.0339,
                    -128 * 156543.0339,
                    128 * 156543.0339,
                    128 * 156543.0339
                ),
                maxResolution: 156543.0339,
                numZoomLevels: 19,
                units: "m",
                projection: "EPSG:900913",
                originX: 0,
                originY: 0
            }, options);
        }
        url = url || this.url;
        name = name || this.name;
        var newArguments = [name, url, {}, options];
        OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
    },

    /**
     * APIMethod: clone
     * Create a clone of this layer
     *
     * Parameters:
     * obj - {Object} Is this ever used?
     *
     * Returns:
     * {<OpenLayers.Layer.Grid>} An exact clone of this OpenLayers.Layer.Grid
     */
    clone: function (obj) {

        if (obj == null) {
            obj = new OpenLayers.Layer.ZYX(this.name,
                                            this.url,
                                            this.options);
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.HTTPRequest.prototype.clone.apply(this, [obj]);

        // copy/set any non-init, non-simple values here
        if (this.tileSize != null) {
            obj.tileSize = this.tileSize.clone();
        }

        // we do not want to copy reference to grid, so we make a new array
        obj.grid = [];

        return obj;
    },

    /**
     * Method: getUrl
     *
     * Parameters:
     * bounds - {<OpenLayers.Bounds>}
     *
     * Returns:
     * {String} A string with the layer's url and parameters and also the
     *          passed-in bounds and appropriate tile size specified as
     *          parameters
     */
    getURL: function (bounds) {
        var res = this.map.getResolution();
        var z = this.map.getZoom();
        var limit = Math.pow(2, z);
        var levels = {
            0: [516,300],
            1: [1033,600],
            2: [2067,1200],
            3: [4135,2401],
            4: [8271,4802],
            5: [16542,9604],
            6: [33084,19209],
            7: [66169,38419],
            8: [132396,76873]};
        //var xoffset = 300;//levels[z][1];
        //var yoffset = 516;//levels[z][0];
        var xoffset = 0;//levels[z][1];
        var yoffset = 0;//levels[z][0];

        //~ var x = (xoffset * Math.pow(2,z)) + Math.round((bounds.left - this.maxExtent.left)
            //~ / (res * this.tileSize.w));
        //~ var y = (yoffset * Math.pow(2,z)) + Math.round((this.maxExtent.top - bounds.top)
            //~ / (res * this.tileSize.h));
        var x = (xoffset ) + Math.round((bounds.left - this.maxExtent.left)
            / (res * this.tileSize.w));
        var y = (yoffset ) + Math.round((this.maxExtent.top - bounds.top)
            / (res * this.tileSize.h));


        var url = this.url;
        var s = '' + x + y + z;
        if (url instanceof Array)
        {
            url = this.selectUrl(s, url);
        }

        var path = OpenLayers.String.format(url, {'x': x, 'y': y, 'z': z});

        return path;
    },

    /**
     * Method: addTile
     * addTile creates a tile, initializes it, and adds it to the layer div.
     *
     * Parameters:
     * bounds - {<OpenLayers.Bounds>}
     * position - {<OpenLayers.Pixel>}
     *
     * Returns:
     * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image
     */
    addTile:function(bounds,position) {
        return new OpenLayers.Tile.Image(this, position, bounds,
                                         null, this.tileSize);
    },

    /* APIMethod: setMap
     * When the layer is added to a map, then we can fetch our origin
     *    (if we don't have one.)
     *
     * Parameters:
     * map - {<OpenLayers.Map>}
     */
    setMap: function(map) {
        OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
        if (!this.tileOrigin) {
            this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
                                                this.maxExtent.bottom);
        }
    },

    CLASS_NAME: "OpenLayers.Layer.ZYX"
});