	function params(){

		this.urlDetails;
		this.urlBiography;
		this.noTenantsMessage;
		this.housesProvince;
		this.housesCouncil;
	}

	function selectProvince(value) {

		if(value=="-1"){
			
			return;
		}
				
		retrieveTenantsByProvince(value);
		document.getElementsByTagName("select")[1].options.selectedIndex = 0;
		hilites.destroyFeatures();
		hMarkers.map.zoomTo(0);
	}

	function selectCouncil(value,text) {
		
		if(value=="-1"){
			
			return;
		}
		
		hilites.destroyFeatures();
		highlightFeatures('topp:concello','cdmuni',value,highlight_them);
		retrieveTenantsByCouncil(value,text);
		hMarkers.map.zoomTo(0);
		document.getElementsByTagName("select")[0].options.selectedIndex = 0;
	}

	function setCenter(lon,lat,zoom,provinceName){
		
		hilites.destroyFeatures();
		hMarkers.markers.clearMarkers();
		//hMarkers.length = 0;
		document.getElementsByTagName("select")[0].options.selectedIndex = 0;
		document.getElementsByTagName("select")[1].options.selectedIndex = 0;
		hMarkers.map.setCenter(new OpenLayers.LonLat(lon,lat).transform(new OpenLayers.Projection("EPSG:23029"),new OpenLayers.Projection("EPSG:900913")),zoom);
		retrieveTenantsByProvince(provinceName);
	}
	
	function addFlag(lon,lat,street,number,city,province){

		if(popup != null){ 
			hMarkers.map.removePopup(popup);
			popup = null;
		}

		popup = new OpenLayers.Popup.FramedCloud("flag",
				    new OpenLayers.LonLat(lon,lat).transform(new OpenLayers.Projection("EPSG:23029"),new OpenLayers.Projection("EPSG:900913")),
				 	null,
					"<div id='info'>"+street+", "+number+"<br/>"+city+" ("+province+")"+"</div>",
				 	null, true, onPopupClose);

		hMarkers.map.addPopup(popup);
	}

	function onPopupClose(evt){
		
		for(var p in hMarkers.map.popups){

			if(hMarkers.map.popups[p].id == "flag") hMarkers.map.removePopup(hMarkers.map.popups[p]);
		}
	}
	
	function infoPopUp(href,label){

		if(label == "url"){
		
			if(href.substring(0,7)!="http://"){
				href = "http://"+href;}
		}
		
		var frog = window.open("","","width=700,height=400,scrollbars=1,resizable=0,top=150,left=300");
		frog.location.href =href;
	}
	
	function completUrl(href){
		
		if(href.substring(0,7)!="http://"){
			href = "http://"+href;}
		
		var frog = window.open("","","");
		frog.target = "_blank";
		frog.location.href =href;
	}
    
	function popUp(href,width,height){

		var frog = window.open("","","width="+width+",height="+height+",scrollbars=1,resizable=0,top=150,left=250");
		frog.location.href = href;
	}

	function onSelectClick(authorId,isMember){
        
        var chk=document.getElementById(authorId);

        if(chk.checked){
        	
        	HouseFacade.findAuthorById(authorId,isMember,author = function(data)
            {
              if (data != null && typeof data == 'object'){
                 for(i=0;i<data.houses.length;i++){
                   
                  if(data.houses[i].city == document.getElementsByTagName('input')[0].value||
                     data.houses[i].province == document.getElementsByTagName('input')[1].value||
                     (document.getElementsByTagName('input')[0].value =="" && document.getElementsByTagName('input')[1].value =="")){    
					hMarkers.addMarker(data.authorId,data.name,data.surname,data.houses[i].x,data.houses[i].y,data.houses[i].id);
                  }              	 
                 }
              }
              drawMarkers();
            });
        }
        else{
            hMarkers.removeMarker(authorId);
            drawMarkers();   	      
        }
	}
	
	function onNameClick(authorId,isMember){
		
		var chk=document.getElementById(authorId);
		chk.checked = ! chk.checked;
		onSelectClick(authorId,isMember);
	}
	
	function hMarkers(){
		this.markers = new OpenLayers.Layer.Markers("Markers",{displayInLayerSwitcher:false});
		//this.list = new Array();
		//this.length = 0;
		this.addMarker = addMarker;
		this.removeMarker = removeMarker;
		this.start = start;
		this.map;
	}
	
	function addMarker(id,name,surname,lon,lat,houseId){
		
		if(lon!="" && lat!=""){ 
            
	 		var size = new OpenLayers.Size(15,15);
	      	var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
	      	var icon = new OpenLayers.Icon('./icons/casa2.png',size,offset);               

      		aux = new OpenLayers.Marker.HouseMarker(new OpenLayers.LonLat(lon,lat).transform(new OpenLayers.Projection("EPSG:23029"),new OpenLayers.Projection("EPSG:900913")),icon,
      				id, name, surname, houseId, houseId); 

      		this.markers.addMarker(aux);

      		//this.list[this.length] = aux;
    		//this.length = this.length+1;	
    	}
	}

	function removeMarker(id){
		
		var i=0
		
		//while(i<this.length){
		
		while(i<this.markers.markers.length){
			
			incrementar = true;
			
			//if(this.list[i].authorId == id){
			if(this.markers.markers[i].authorId == id){
				
//				this.markers.removeMarker(this.list[i]);
				this.markers.removeMarker(this.markers.markers[i]);
				
//				for(j=i;j<this.length;j++){

//					if(this.list[j+1]!=null){
//						this.list[j].houseId = this.list[j+1].houseId;
//						this.list[j].authorName = this.list[j+1].authorName;
//						this.list[j].authorSurname = this.list[j+1].authorSurname;
//						this.list[j].subHouseIds = this.list[j+1].subHouseIds;
//						this.list[j].authorId = this.list[j+1].authorId;
//					}
//				}
//				this.length = this.length-1;
				incrementar = false;
			}
			
			if(incrementar){ i = i+1;}
		}
	}
	
	function drawMarkers(){
		
		//for(i=0;i<hMarkers.length;i++){
		
		for(i=0;i<hMarkers.markers.markers.length;i++){
			
			//hMarkers.list[i].setSingleMarker();
			hMarkers.markers.markers[i].setSingleMarker();
			
			for(j=0;j<i;j++){

				//var m = hMarkers.list[i].distance(hMarkers.list[j]);
				
				var m = hMarkers.markers.markers[i].distance(hMarkers.markers.markers[j]);
				
				if(m <= 12 && hMarkers.markers.markers[j].visible){
					
					hMarkers.markers.markers[j].setGroupMarker(hMarkers.markers.markers[i]);
				}
			}
		}	
	}
	
	function selectAllTenants(){
		
		inputs = document.getElementById("public_house_list").getElementsByTagName("input");
		
		var councilName = document.getElementsByTagName("input")[0].value;
		var provinceName = document.getElementsByTagName("input")[1].value;
		var councilId = document.getElementsByTagName("input")[2].value;

        for(var i = 0;i < inputs.length; i++){
        		
        	inputs[i].checked = true;
        }
        	
        if(councilId == "" && councilName == ""){
        		
        	retrieveTenantsByProvince(provinceName);
        }
        else{
        		
        	retrieveTenantsByCouncil(councilId, councilName);
        }
	}
	
	function deselectAllTenants(){
		
		inputs = document.getElementById("public_house_list").getElementsByTagName("input");

		for(var i = 0;i < inputs.length; i++){
    		
    		inputs[i].checked = false;
    	}
    	
    	hMarkers.markers.clearMarkers();
		//hMarkers.length = 0;
	}
	
	function addTenantToList(tenant){
		
		var div = document.createElement("div");
  	  	div.className = "listElement";
		var html ="";

		if(tenant.isMember){
			
			html = "<input checked='true' type='checkbox' onClick=javascript:onSelectClick('"+tenant.authorId+"',true); id='"+tenant.authorId+"'/>";
			
			if(tenant.name != ''){
				   html = html+"<a href=javascript:onNameClick('"+tenant.authorId+"',true);>"+tenant.surname+", "+tenant.name+"</a></input>";
			}
			else{
				html = html+"<a href=javascript:onNameClick('"+tenant.authorId+"',true);>"+tenant.surname+"</a></input>"; 
			}
		}
		else{
		   html = "<input checked='true' type='checkbox' onClick=javascript:onSelectClick('"+tenant.authorId+"',false); id='"+tenant.authorId+"'>";
		   
		   if(tenant.name != ''){
			   html = html + "<a href=\"javascript:onNameClick('"+tenant.authorId+"',false);\">"+tenant.surname+", "+tenant.name+"</a>";
		   }
		   else{
			   html = html + "<a href=\"javascript:onNameClick('"+tenant.authorId+"',false);\">"+tenant.surname+"</a>";
		   }

		   html = html + "</input>";
		}
		
		div.innerHTML = html;
		document.getElementById("public_house_list").appendChild(div);
	}

	function addEmptyToList(){
		
		var div = document.createElement("div");
  	  	div.className = "noElement";
		var html = params.noTenantsMessage +" "+ document.getElementById("city").value+
				   document.getElementById("province").value;
		
		div.innerHTML = html;
		document.getElementById("public_house_list").appendChild(div);
		document.getElementById("public_house_list").childNodes[1].style.display = 'none';
	}
	
	function removeTenantList(){
		
		var list = document.getElementById("public_house_list");

		if(list.hasChildNodes()){
			
			while(list.childNodes.length >= 3){
				list.removeChild(list.lastChild );
			}
		}
	}
	
	function addTitleToList(title){

		var div = document.getElementById("public_house_list");
  	    div.childNodes[0].getElementsByTagName("p")[0].innerHTML = title;
	}
	
	function retrieveTenantsByCouncil(councilId,name){
		
		hMarkers.markers.clearMarkers();
		//hMarkers.length = 0;
		
		document.getElementById("city").value = name;
		document.getElementById("cityId").value = councilId;
		document.getElementById("province").value = "";
		
		removeTenantList();
		addTitleToList(params.housesCouncil+" "+name);
		
		HouseFacade.findTenantsByCouncil(councilId,tenants = function(data)
	            {
	              if (data != null && typeof data == 'object' && data.length>0){
	            	  
	                 for(var i=0;i<data.length;i++){
	                	 addTenantToList(data[i]);
	                	 houses = data[i].houses;
	                	 
	  	               for(var j=0;j< houses.length; j++){
	  	                	if(houses[j].city == document.getElementsByTagName('input')[0].value||
	                             houses[j].province == document.getElementsByTagName('input')[1].value||
	                             (document.getElementsByTagName('input')[0].value =="" && document.getElementsByTagName('input')[1].value ==""))	 
	  	                		 	hMarkers.addMarker(data[i].authorId,data[i].name,data[i].surname,houses[j].x,houses[j].y,houses[j].id);
	  	               }
	                 }
	                 drawMarkers();
	                 document.getElementById("public_house_list").childNodes[1].style.display = 'block';
	              }
	              else{
	            	  addEmptyToList();
	              }
	    });
	}
	

	function retrieveTenantsByProvince(provinceName){
		
		hMarkers.markers.clearMarkers();
		hMarkers.length = 0;
		
		document.getElementById("city").value = "";
		document.getElementById("cityId").value = "";
		document.getElementById("province").value = provinceName;
		
		removeTenantList();
		
		if(provinceName != ""){
			addTitleToList(params.housesCouncil+" "+provinceName);
		}
		else{
			addTitleToList(params.housesProvince);
		}
		
		HouseFacade.findTenantsByProvince(provinceName,tenants = function(data)
	       {
	       if (data != null && typeof data == 'object' && data.length>0){

	           for(var i=0;i<data.length;i++){
	               addTenantToList(data[i]);
	               houses = data[i].houses;
	                	 
	               for(var j=0;j< houses.length; j++){
	                	if(houses[j].city == document.getElementsByTagName('input')[0].value||
                           houses[j].province == document.getElementsByTagName('input')[1].value||
                           (document.getElementsByTagName('input')[0].value =="" && document.getElementsByTagName('input')[1].value =="")){	 
	                		 	hMarkers.addMarker(data[i].authorId,data[i].name,data[i].surname,houses[j].x,houses[j].y,houses[j].id);
	                		}
	               }
	           }
	           document.getElementById("public_house_list").childNodes[1].style.display = 'block';
	           drawMarkers();
	       }
	       else{
	          addEmptyToList();
	       }
	     });
	}
	
	function showCouncilName(e){
		
		if(hMarkers.map.controls[0].controls[0].active &&
		   !hMarkers.map.controls[0].controls[0].dragPan.handler.dragging){
		
		var url =  hMarkers.map.layers[0].getFullRequestString(
                {
                    REQUEST: "GetFeatureInfo",
                    EXCEPTIONS: "application/vnd.ogc.se_xml",
                    BBOX: hMarkers.map.getExtent().toBBOX(),
                    X: e.xy.x,
                    Y: e.xy.y,
                    INFO_FORMAT: 'application/vnd.ogc.gml',
                    QUERY_LAYERS: hMarkers.map.layers[0].params.LAYERS,
                    FEATURE_COUNT: 50,
                    WIDTH: hMarkers.map.size.w,
                    HEIGHT: hMarkers.map.size.h
                },
                "/geoserver/wms"
        );

		OpenLayers.loadURL(url, "", null, function getCouncilName(req){
			
			g =  new OpenLayers.Format.GML();
			features = g.read(req.responseText);

			for(var feat in features){ 

				name = features[feat].attributes['nome'];
				cdmuni = features[feat].attributes['cdmuni'];
			}
			
			if(name != "" && name && cdmuni != "" && cdmuni){
				
				if(features[feat]){
					hilites.destroyFeatures();
					features[feat].geometry.transform(new OpenLayers.Projection("EPSG:23029"),new OpenLayers.Projection("EPSG:900913"));
					hilites.addFeatures(features[feat]);
					hilites.setVisibility(true);
					retrieveTenantsByCouncil(cdmuni,name);
					document.getElementsByTagName("select")[0].options.selectedIndex = 0;
					document.getElementsByTagName("select")[1].options.selectedIndex = 0;
				}
			}
		});
		
		}
	}

    function highlightFeatures(typename,attribute,value,fun) {
	     var wfsurl = '/geoserver/wfs?SERVICE=WFS&VERSION=1.0.0&REQUEST=getfeature&typename=' + typename +
	     '&Filter=<Filter><PropertyIsEqualTo><PropertyName>'+attribute+'</PropertyName><Literal>'+value+'</Literal></PropertyIsEqualTo></Filter>';
	     OpenLayers.loadURL(wfsurl,'',null,fun);
    }
    
    function highlight_them(response) {
    	
        var features = new OpenLayers.Format.GML().read(response.responseText);
        
        for(f in features){
        	
        	features[f].geometry.transform(new OpenLayers.Projection("EPSG:23029"),new OpenLayers.Projection("EPSG:900913"));
        }
        
        hilites.destroyFeatures();
        hilites.addFeatures(features);
        hilites.setVisibility(true);
	}
   
	function start(){

		OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
		
		var bounds = new OpenLayers.Bounds(
			        473197, 4628115,
			        711547, 4852145
		);
		
		bounds.transform(new OpenLayers.Projection("EPSG:23029"),new OpenLayers.Projection("EPSG:900913"));
		
		var panel = new OpenLayers.Control.NavToolbar({'div':OpenLayers.Util.getElement('paneldiv')});
		panel.controls[0].zoomWheelEnabled = false;

		councils = new OpenLayers.Layer.WMS("Vista normal",
	               "/geoserver/wms",
	               { 
	               transparent: 'true',
	               layers: 'topp:concello,topp:otrasprovincias0',
	               format: 'image/png'
	               },
	               {  
	               isBaseLayer: true,
	               visibility: true,
	               buffer: 0,
	               'alpha':true
	               }
	    );
	        
	    gmap = new OpenLayers.Layer.Google(
	                "Vista Google Rúas",{'sphericalMercator': true}
	    );

	    ghyb = new OpenLayers.Layer.Google(
	        		"Vista google",
	        		{type: G_HYBRID_MAP,'sphericalMercator': true}
	    );
	    
	    // define a blank Vector layer and add it to the map
        var highlight_style = { fillColor:'#EEE8AA', strokeColor: "red", strokeWidth: 1, fillOpacity: 0.3};
        hilites = new OpenLayers.Layer.Vector("Highlighted",
                      {isBaseLayer:false, features:[], visibility:true, style:highlight_style,displayInLayerSwitcher:false
                      }
        );
		
		this.map = new OpenLayers.Map('map',{
             controls: [
	                        panel,
	                        new OpenLayers.Control.LoadingPanel(),
	                        new OpenLayers.Control.LayerSwitcher(),
	                        new OpenLayers.Control.PanZoomBar({'zoomWorldIcon':true})
	                    ],
	        resolutions: [gmap.RESOLUTIONS[8],gmap.RESOLUTIONS[9],gmap.RESOLUTIONS[10],gmap.RESOLUTIONS[11],gmap.RESOLUTIONS[12]
	                      ,gmap.RESOLUTIONS[13],gmap.RESOLUTIONS[14],gmap.RESOLUTIONS[15],gmap.RESOLUTIONS[16],gmap.RESOLUTIONS[17],gmap.RESOLUTIONS[18]],
	        restrictedExtent: bounds,
			eventListeners:{"zoomend": drawMarkers},
			'minZoomLevel':8,
            'numZoomLevels':11,
	        maxExtent: bounds,
            projection: new OpenLayers.Projection("EPSG:900913"),
            units: 'm'});
		
		this.map.addLayers([councils,/*gmap,*/ghyb,hilites,this.markers]);
		
		this.map.events.register('click', this.map, function(evt) {showCouncilName(evt)});
		this.map.events.register('mouseover', this.map, function(evt) { hMarkers.map.div.style.cursor = "pointer";});
		this.map.events.register('mouseout', this.map, function(evt) { hMarkers.map.div.style.cursor = "default"; });
		
		this.map.zoomToExtent(bounds);
	}