/**
 * @requires OpenLayers/Layer/Grid.js
 * @requires OpenLayers/Tile/Image.js
 * @requires OpenLayers/Tile/WMTS.js
 */

/**
 * Class: OpenLayers.Layer.WMTSGeograma
 * Instances of OpenLayers.Layer.WMS are used to display data from OGC Web
 *     Mapping Services. Create a new WMS layer with the <OpenLayers.Layer.WMS>
 *     constructor.
 * 
 * Inherits from:
 *  - <OpenLayers.Layer.WMTS>
 *
 * @Author Ignacio Gámez Ramírez
 */
OpenLayers.Layer.WMTSGeograma = OpenLayers.Class(OpenLayers.Layer.WMTS, {

    
    /**
     * Property: reproject
     * *Deprecated*. See http://trac.openlayers.org/wiki/SphericalMercator
     * for information on the replacement for this functionality. 
     * {Boolean} Try to reproject this layer if its coordinate reference system
     *           is different than that of the base layer.  Default is true.  
     *           Set this in the layer options.  Should be set to false in 
     *           most cases.
     */
    reproject: false,
 
    /**
     * APIProperty: isBaseLayer
     * {Boolean} Default is true for WMS layer
     */
    isBaseLayer: true,
    
    /**
     * APIProperty: encodeBBOX
     * {Boolean} Should the BBOX commas be encoded? The WMS spec says 'no', 
     * but some services want it that way. Default false.
     */
    encodeBBOX: false,
    /**
     * APIProperty: numSelection
     */
    numSelection: "",
    /**
     * APIProperty: rotuloSelection
     */
    rotuloSelection: "",
    /**
     * APIProperty: nombre de la capa
     */
    layerName: "",
    /**
     * APIProperty: leyenda
     */
    leyenda: "",
    
 
    /**
     * Constructor: OpenLayers.Layer.WMS
     * Create a new WMS layer object
     *
     * Example:
     * (code)
     * var wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
     *                                    "http://wms.jpl.nasa.gov/wms.cgi", 
     *                                    {layers: "modis,global_mosaic"});
     * (end)
     *
     * Parameters:
     * name - {String} A name for the layer
     * url - {String} Base url for the WMS
     *                (e.g. http://wms.jpl.nasa.gov/wms.cgi)
     * params - {Object} An object with key/value pairs representing the
     *                   GetMap query string parameters and parameter values.
     * options - {Ojbect} Hashtable of extra options to tag onto the layer
     */
    initialize: function(name, url, params, options) {
        var newArguments = [];
        //uppercase params
        params = OpenLayers.Util.upperCaseObject(params);
        newArguments.push(name, url, params, options);
        OpenLayers.Layer.WMTS.prototype.initialize.apply(this, newArguments);
        OpenLayers.Util.applyDefaults(
                       this.params, 
                       OpenLayers.Util.upperCaseObject(this.DEFAULT_PARAMS)
                       );


        //layer is transparent        
        if (this.params.TRANSPARENT && 
            this.params.TRANSPARENT.toString().toLowerCase() == "true") {
            
            // unless explicitly set in options, make layer an overlay
            if ( (options == null) || (!options.isBaseLayer) ) {
                this.isBaseLayer = false;
            } 
            
            // jpegs can never be transparent, so intelligently switch the 
            //  format, depending on teh browser's capabilities
            if (this.params.FORMAT == "image/jpeg") {
                this.params.FORMAT = OpenLayers.Util.alphaHack() ? "image/gif"
                                                                 : "image/png";
            }
        }
        
        // Guardamos el nombre de la capa porque es posible que varie al añadir leyendas
        this.nameLayer = this.params['LAYER'];

    },    

    /**
     * Method: destroy
     * Destroy this layer
     */
    destroy: function() {
        // for now, nothing special to do here. 
        OpenLayers.Layer.WMTS.prototype.destroy.apply(this, arguments);  
    },

    
    /**
     * Method: clone
     * Create a clone of this layer
     *
     * Returns:
     * {<OpenLayers.Layer.WMS>} An exact clone of this layer
     */
    clone: function (obj) {
        
        if (obj == null) {
            obj = new OpenLayers.Layer.WMTSGeograma(this.name,
                                           this.url,
                                           this.params,
                                           this.options);
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WMTS.prototype.clone.apply(this, [obj]);

        // copy/set any non-init, non-simple values here

        return obj;
    },    
    
    /**
     * Method: getURL
     * Return a GetMap query string for this layer
     *
     * Parameters:
     * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox for the
     *                                request.
     *
     * 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 requestString = OpenLayers.Layer.WMTS.prototype.getURL.apply(this,arguments);
      
      requestString += "&NUMSEL=" + this.numSelection + "&ROTULOSEL=" + this.rotuloSelection;
      
      return requestString;
    },

    
    // IGámez: METODOS ESPECIFICOS DEL SERVICIO WEB MAP SERVICE TILING
    
    setNumSelection:function(numSel)
    {
      if(numSel != null)
        this.numSelection = escape(numSel);
    },
    
    getNumSelection:function()
    {
      return this.numSelection;
    },
    
    setRotuloSelection:function(rotuloSel)
    {
      if(rotuloSel != null)
        this.rotuloSelection = escape(rotuloSel);
    },
    
    getRotuloSelection:function()
    {
      return this.rotuloSelection;
    },
    
    setLeyenda: function(leyenda)
    {
      if(leyenda != null && leyenda!=""){
        this.params['LAYER'] = this.nameLayer + "#" + leyenda;
        this.leyenda = leyenda;
      }
      else{
        this.params['LAYER'] = this.nameLayer;
        this.leyenda = "";
      }
    },
    
    getLeyenda: function()
    {
        return this.leyenda;
    }, 
    
    setVista: function(vista)
    {
      if(vista != null && vista!=""){
        this.nameLayer = vista;
        this.numSelection = "";
        this.rotuloSelection = "";
        this.params['LAYER'] = escape(this.nameLayer);
      }      
    },
    
    CLASS_NAME: "OpenLayers.Layer.WMTSGeograma"
});
