/* 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. */ 
/** 
* @requires OpenLayers/Layer/TMS.js 
*/ 
/** 
 * Class: OpenLayers.Layer.ArcGISCache   
 * Layer for accessing cached map tiles from an ArcGIS Server style mapcache. 
  * Tile MUST already be cached for this layer to access it. Does NOT require 
  * ArcGIS Server itself. It will work on any accessible directory structure 
  * that is the same as the one which ArcGIS Server creates. 
  *  
  *Inherits from: 
  *  - <OpenLayers.Layer.TMS>             
  */    
OpenLayers.Layer.ArcGISCache = OpenLayers.Class(OpenLayers.Layer.TMS, {   
     
     /** 
     * APIProperty: tileOrigin 
     * The X,Y origin of the tiles in map coordinates. 
     * {<OpenLayers.LonLat>} 
     */ 
    tileOrigin: null, 
     
     /** 
     * Constructor: OpenLayers.Layer.ArcGISCache 
     * Creates a new instance of this class 
     *  
     * Parameters: 
     * name - {String} 
     * url - {String} 
     * options - {Object} 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); 
    }, 
     
     
    /** 
     * APIMethod: clone 
     * Returns an exact clone of this OpenLayers.Layer.ArcGISCache       
     * 
     * Parameters: 
     * [obj] - {Object} optional object to assign the cloned instance to.             
     *  
     * Returns: 
     * {<OpenLayers.Layer.ArcGISCache>} clone of this instance 
     */ 
    clone: function (obj) { 
        if (obj == null) { 
            obj = new OpenLayers.Layer.ArcGISCache(this.name, 
                                           this.url, 
                                           this.options); 
        } 
        //get all additions from superclasses 
        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); 
        return obj; 
    },     
     
    /** 
     * APIMethod: getURL 
     * Gets the URL string for the tile specified by the passed bounds object. 
     *  
     * Parameters: 
     * bounds - {OpenLayers.Bounds} the bounds of the area the tile will fill.                      
     *  
     * Returns: 
     * {String} A string with the layer's url and specific path to the tile. 
     */ 
    getURL: function (bounds) { 
        var path,url,res,x,y,z,center,originTileX,originTileY; 
            res = this.map.getResolution(); 
            //get tile center 
            center = bounds.getCenterLonLat(); 
            originTileX = this.tileOrigin.lon + (res * this.tileSize.w/2); 
            originTileY = this.tileOrigin.lat - (res * this.tileSize.h/2); 
            //calculate x 
            x = (Math.round(Math.abs((center.lon - originTileX ) / (res * this.tileSize.w)))).toString(16); 
        while (x.length < 8) { 
          x = "0" + x; 
        } 
        x = "C" +x; 
        //calculate y 
            y = (Math.round(Math.abs((originTileY - center.lat) / (res * this.tileSize.h)))).toString(16); 
        while (y.length < 8) { 
          y = "0" + y; 
        } 
        y = "R" +y; 
        //calculate z 
        z = (this.serverResolutions != null) ? this.serverResolutions.indexOf(res) : this.map.getZoom(); 
        if (z <10) z = "L0"+z; 
        else z ="L"+z; 
            //concatenate path 
            path = "/" + this.layername + "/" + z + "/" + y + "/" + x + "." + this.type; 
        url = this.url; 
        if (url instanceof Array) { 
	            url = this.selectUrl(path, url); 
	        } 
	        return url + path;  
	    }, 
	 
	    CLASS_NAME: "OpenLayers.Layer.ArcGISCache" 
	}); 
