/** 
 * @requires OpenLayers/Control.js
 * 
 * Class: OpenLayers.Control.LayerSwitcherList
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 */
Roja.Control.LayerSwitcherList = 
  OpenLayers.Class(OpenLayers.Control, {
    /** 
     * Property: div
     * {Object)}
     */
    div: null,
    
    /** 
     * Property: fotogramma layer name 
     * {Object)}
     */
    fotoLayerName: "Fotogramma",

    /** 
     * Property: tabIndex 
     * {Integer)}
     */
    tabIndex: -1,
    
       /** 
     * Property: doubleMapControl
     * {Roja.Control.DoubleMap} double map control 
     */
    doubleMapControl:null,	   

    /**
  	* Property: messageDiv component
  	* message div component 
	* {DOMElement}
	*/
    //messageDiv: null,
    
    /**
     * Constructor: OpenLayers.Control.LayerSwitcherList
     * 
     * Parameters:
     * options - {Object}
     */
    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    /**
     * APIMethod: destroy 
     */    
    destroy: function() {
        
        OpenLayers.Event.stopObservingElement(this.select);

        OpenLayers.Control.prototype.destroy.apply(this, arguments);
    },

    /**
     * Method: draw
     *
     * Returns:
     * {DOMElement} A reference to the DIV DOMElement containing the 
     *     switcher tabs.
     */  
    draw: function() {
    	if (this.select==null && this.map!=null)
    	{
        	//this.map.events.register('addlayer', this, this.redraw.bind(this));     
        	this.map.events.register('changebaselayer', this, this.redraw.bind(this));     
        	this.map.events.register('removelayer', this, this.redraw.bind(this));     
        	this.select = document.createElement("select");
        	this.select.title = "Seleziona il layer";
        	this.select.setAttribute('tabIndex',this.tabIndex);
        	this.div.appendChild(this.select);
        	var context = {
                map: this.map
    			//doubleMapControl:this.doubleMapControl,
    			//messageDiv:this.messageDiv
            };
    	
	    	if (this.map!=null) {
	       		 OpenLayers.Event.observe(this.select, "change", 
	        			OpenLayers.Function.bindAsEventListener(this.changeValue, context));
	    	}		        
    	}
	
	  /* if (this.map!=null && this.map.baseLayer!=null && this.map.baseLayer.name==this.fotoLayerName)
        {
    		var option = document.createElement("option");
    		option.value = "Nessuno";
    		option.innerHTML = "Mosaici";
    		this.select.appendChild(option);
        }*/
               
        if (this.map!=null) {
	    	var layers = this.map.layers;
	    /*	var typeL = layers[0].typeLayer;
   			var optionG = document.createElement("optgroup");
   			optionG.label = layers[0].typeLayer;
   			this.select.appendChild(optionG)*/
	    	for (var i=0; i<layers.length;i++)
	    	{
	    		if (layers[i].displayInLayerSwitcher == true && layers[i].isBaseLayer == true)
	    		{/*
	    			if (layers[i].typeLayer != typeL) {
		    			var optionG = document.createElement("optgroup");
		    			optionG.label = layers[i].typeLayer;
		    			this.select.appendChild(optionG)
	    				var typeL = layers[i].typeLayer;
	    			}   */ 			
	    			var option = document.createElement("option");
	    			option.value = layers[i].name;
	    			option.innerHTML = layers[i].name;
	    			if (this.map.baseLayer.name==layers[i].name)
	    			{
	    				option.selected="selected";
	    			}
	    			this.select.appendChild(option);
	    		}
	    	}
        }
    	
        return this.div;
    },

    /**
     * Method: redraw
     * 
     * Redraw control
     *
     */  
    redraw: function() {
    	if (this.select!=null)
    	{
    		this.select.innerHTML="";    	
    	}
    	
    	this.draw();
    },
    
    /**
     * Method: changeValue
     * 
     * @param evt event on change value
     *
     */  
    changeValue: function(evt) {

		//this.messageDiv.className=this.messageDiv.className.replace('display','hidden');
    	var value = null;
    	//If IE
    	if (evt.srcElement!= null)
    	{
    		value = evt.srcElement.value;
    	}
    	else
    	{
    		value = evt.currentTarget.value;
    	}
    	this.map.setBaseLayer(this.map.getLayerByName(value));
    	
    	if (this.map.getLayerByName(value).backgroundColor!=null)
    	{
			this.map.viewPortDiv.style.backgroundColor = this.map.getLayerByName(value).backgroundColor;
    	}
    	
    /*	if (value!='Nessuno' && this.doubleMapControl!=null) {
    		this.doubleMapControl.hideFotogramma();
    		this.doubleMapControl.coordinateButtonRight.deactivate();
    	}*/
 
   	   	var layersfondo;
   	   	  
 
   	   	for (i=0;i<this.map.layers.length;i++) {
  			if(!this.map.layers[i].isBaseLayer && this.map.layers[i].visibility 
   				&& ( this.map.layers[i].CLASS_NAME == "OpenLayers.Layer.TMS"
   				|| this.map.layers[i].CLASS_NAME == "OpenLayers.Layer.WMS")
   				&&  this.map.layers[i].name != "QuadroUnione"
   				) {				layersfondo = this.map.layers[i].name;
			        this.map.getLayerByName(layersfondo).setVisibility(false);
   				}
   	   	}
    	   		    
	    if (this.map.getLayerByName(value).overlay)
	    {
	        this.map.getLayerByName(this.map.getLayerByName(value).overlay).setVisibility(true);
	    }
	    
    	correctZoombarPosition()
		creaLink();
/*   	
    	if (this.map.getLayerByName(value).copyrightTitle!=null)
    	{
 			if (this.map.getLayerByName(value).typemap == "left") {
 				this.map.removeControl(copyrightLeft);
			      // initialize a new UrlOnMap object
				options2 = {
					text : this.map.getLayerByName(value).copyrightTitle,
					href: this.map.getLayerByName(value).copyrightUrl
				};
				copyrightLeft = new Roja.Control.UrlOnMap(options2);
				this.map.addControl(copyrightLeft);
				//copyrightLeft.setUrl(this.map.getLayerByName(value).copyrightUrl,this.map.getLayerByName(value).copyrightTitle);
 				//copyrightLeft.show();   		
 			}
 			else {
 				this.map.removeControl(copyrightRight);
			      // initialize a new UrlOnMap object
				options2 = {
					text : this.map.getLayerByName(value).copyrightTitle,
					href: this.map.getLayerByName(value).copyrightUrl
				};
				copyrightRight = new Roja.Control.UrlOnMap(options2);
				this.map.addControl(copyrightRight);
//				copyrightRight.setUrl(this.map.getLayerByName(value).copyrightUrl,this.map.getLayerByName(value).copyrightTitle);
// 				copyrightRight.show();   		
			}
    	}
    	else 
    	{
 			if (this.map.getLayerByName(value).typemap == "left")
 				copyrightLeft.hide();   
 			else
 				copyrightRight.hide();   						
    	}
   */ 	
     },

    CLASS_NAME: "Roja.Control.LayerSwitcherList"
});

