
/**
 * @requires core/Control.js
 */

/**
 * Class: eGV.Control.WMSmanager
 * This class implements the WMS Manager control. It contains the functionalities
 * for adding new layers from a given WMS service to the specified map.
 *
 * Inherits from:
 *  - <eGV.Control>
 */

eGV.Control.WMSManagerCustom = OpenLayers.Class(eGV.Control.WMSManager, {


     /**
     * Method: loadLayers
     * This method is in charge of processing the petition done by the user
     * after selecting the layers he/she wants to add to the map. It takes
     * the selected layers, draws them in the map, and also adds them to the
     * corresponding group in the layers manager, in order to be able to manage
     * them from there.
     * We have overloaded this method to obtain wms connections with "text/html" infoFormat parameter to enable info for external wms servers queries
     */
	loadLayers: function(){
        this.buttonGet.disabled = true;
        this.buttonGet.className = "egvControlWMSManagerButtonDisabled";
        this.loadingGet.style.display = "";

        var selectedLayers = [];
        var selectedNames = [];
        var selected = this.selectLayers.getElementsByTagName("input");
        var i,len;
        for(i=0,len=selected.length; i<len; i++){
            if(selected[i].checked){
                selectedLayers.push(selected[i].name);
                selectedNames.push(selected[i].value);
            }
        }

        if(selectedLayers.length == 0) return;

			var conn = new eGV.Connection.WMS(
			"UserConnection"+this.parent.id+(this.parent.connections+1),
			this.parent.serviceURL,
			{
                "layers":selectedLayers.toString(),
				"format":this.selectFormats.options[this.selectFormats.selectedIndex].value,
				"transparent":"true"
			},
			{"buffer":0, "transitionEffect":"resize","ratio":1,"singleTile":true},
			{"id":"userConn"+this.parent.id+(this.parent.connections+1),"infoFormat":"text/html"}
		);


        if(this.parent.map.groups.length > 0){
            if(this.parent.wmsManagerLayersGroup == null){
                this.parent.wmsManagerLayersGroup = new eGV.LayersGroup({
                    "id": "wmsmanagerlayersgroup"+this.parent.id,
                    "title": this.parent.layersGroupName,
                    "collapsed": true,
                    "showInLayerManager": true
                    }
                );
                this.parent.map.addGroup(this.parent.wmsManagerLayersGroup);
            }
        }

		for(i=0,len=selectedLayers.length; i<len; i++){
            var layer = new eGV.Layer({
                /*"id":selectedLayers[i],*/
                "name":selectedLayers[i],
                "title": selectedNames[i],
                "visible" : true,
                "queryable" : true
            });

            conn.addLayer(layer);

            if(this.parent.map.groups.length > 0)
                this.parent.wmsManagerLayersGroup.addLayer(layer);

        }

     	this.parent.map.addConnection(conn,this.checkBaseLayer.checked);

        this.parent.connections++;
        this.parent.map.events.triggerEvent("layeradded",{connection:conn});

        conn.olLayer.events.register("loadend",this.parent,this.parent.enableButtons);

	},


	
    CLASS_NAME: "eGV.Control.WMSManager"
});
