// LayerSwitcher d'un layer en funció als diferents valors una propietat

var LayerSwitcherGuia = OpenLayers.Class(OpenLayers.Control, {
	guiaArray: null,
	divDesti: null,
	layer: null,
	EVENT_TYPES: ["canvidefiltre"],
	initialize: function(options) {
        this.EVENT_TYPES =
            LayerSwitcherGuia.prototype.EVENT_TYPES.concat(
            OpenLayers.Control.prototype.EVENT_TYPES
        );
		OpenLayers.Control.prototype.initialize.apply(this, arguments);
		this.guiaArray=options['guiaArray'];
		this.divDesti=options['divDesti'];
	},
	setMap: function(map) {
		OpenLayers.Control.prototype.setMap.apply(this, arguments);

		this.map.events.on({
			"addlayer": this.redraw,
			"changelayer": this.redraw,
			"removelayer": this.redraw,
			"changebaselayer": this.redraw,
			scope: this
		});
		//this.layer.mergeNewParams( {CQL_FILTER:this.capesGuiaFiltreCql()} );
		//console.debug( this.layer ); //capesGuiaFiltreIds
		//this.layer.filter = this.capesGuiaFiltre();
		//this.layer.protocol.options.params = {categoria: this.capesGuiaFiltreIds()};

		//this.layer.refresh(); 
	},
	draw: function() {
		this.redraw();
		OpenLayers.Event.observe(this.divDesti, "mouseup", OpenLayers.Function.bindAsEventListener(this.onInputClick, this));
		OpenLayers.Event.observe(this.divDesti, "click", this.ignoreEvent);
		OpenLayers.Event.observe(this.divDesti, "mousedown", OpenLayers.Function.bindAsEventListener(this.mouseDown, this));
		OpenLayers.Event.observe(this.divDesti, "dblclick", this.ignoreEvent);


	},
	redraw: function() {
		var divCapesGuia
		divCapesGuia = document.createElement("div");
		divCapesGuia.id = "divCapesGuia";

		for(var i=0; i<this.guiaArray.length; i++){
			var inputElem = document.createElement("input");
			inputElem.id = "input_" + this.guiaArray[i].id;
			inputElem.name =  "input_" + this.guiaArray[i].id;
			inputElem.type = "checkbox";

			if( this.guiaArray[i].visible == true ){
				inputElem.setAttribute("checked", true );
				inputElem.defaultChecked = true;
			}
			var labelSpan = document.createElement("span");
			labelSpan.innerHTML = "<img src=\""+this.guiaArray[i].imatge+"\" class=\"pngfix\" />" + this.guiaArray[i].nom;
			var br = document.createElement("br");
			divCapesGuia.appendChild(inputElem);
			divCapesGuia.appendChild(labelSpan);
			divCapesGuia.appendChild(br);
		}

		this.divDesti.innerHTML = divCapesGuia.innerHTML;

	},
	ignoreEvent: function(evt) {
		OpenLayers.Event.stop(evt);
	},

	onInputClick: function(e){
		//Collons d'ie
		var element = e.target != undefined ? e.target : e.srcElement;
		if(element.type == "checkbox"){
			var id = element.id;
			var estat = !element.checked;
			id  = id.replace("input_", "");
			for(var i=0; i < this.guiaArray.length; i++){
				if(this.guiaArray[i].id == id){
					this.guiaArray[i].visible = estat;
				}
			}
			this.redraw();
			this.events.triggerEvent("canvidefiltre");
			this.ignoreEvent(e);
		}else{
			return false;
		}
		
	},



	mouseDown: function(e) {
		var element = e.target != undefined ? e.target : e.srcElement;
		if(element.type == "checkbox"){
			this.isMouseDown = true;
			this.ignoreEvent(e);
		}else{
			return false;
		}
	},
	
	mouseUp: function(evt) {
		if (this.isMouseDown) {
			this.isMouseDown = false;
			this.ignoreEvent(evt);
		}
	}
	,
	capesGuiaFiltreCql: function(){
		var cond = [];
		for(var i=0; i<this.guiaArray.length; i++){
			if(this.guiaArray[i].visible) cond.push( "(categoria_id="+ this.guiaArray[i].id +")" );
		}
		var resposta = "(categoria_id=-1)";
		if (cond.length>0)
		{
		resposta = cond.join(" OR ");
		}
		return resposta;
	},
	capesGuiaFiltreIds: function(){
		var cond = [];
		for(var i=0; i<this.guiaArray.length; i++){
			if(this.guiaArray[i].visible) cond.push( this.guiaArray[i].id );
		}
		var resposta = "";
		if (cond.length>0)
		{
			resposta = cond.join(",");
		}
		return resposta;
	},

	capesGuiaFiltre: function(){
		
		var cond = [];
		if(this.guiaArray.length>0){
			for(var i=0; i<this.guiaArray.length; i++){
				if(this.guiaArray[i].visible){
				//cond.push( "(categoria_id="+ this.guiaArray[i].id +")" );
					cond.push( new OpenLayers.Filter.Comparison(
								{
									type: OpenLayers.Filter.Comparison.EQUAL_TO,
									property: "categoria_id",
									value: this.guiaArray[i].id
								})
					);
				}
			}
		}else{
				cond.push( new OpenLayers.Filter.Comparison(
							{
								type: OpenLayers.Filter.Comparison.EQUAL_TO,
								property: "categoria_id",
								value: -1
							})
					);
		}
		var filtre =  new OpenLayers.Filter.Logical(
								{
									type: OpenLayers.Filter.Logical.OR,
									filters: cond
								});
				
		return filtre;
	},
	CLASS_NAME: "LayerSwitcherGuia"
});