var C_findLocations=new Class ({
	numPage:0,
	pageSize:10,
	totalPages:0,
	map:null,
	toRegister:null,
	
	init:function(_map, pSize, register){
		if (_map) this.map=_map;
		if (pSize) this.pageSize=pSize;
		if (register) this.toRegister=true;
		else this.toRegister=false;
		
		$_("divError").empty();
		$_("divResults").empty(); 
		$_("searchTodosEnRedButton").onclick=function(){this.findLocationsFirstPage()}.bind(this);
		$_("index_leftArrow").style.display="none";
		$_("index_rightArrow").style.display="none";
	},
	
	findLocationsFirstPage:function(){
		this.numPage=0;this.totalPages=0;
		var textToFind = $_("searchTodosEnRed").$T("INPUT")[0].value;
		var divError = $_("divError").empty();
		
		if (textToFind.length<3){ $_("divResults").empty(); divError.innerHTML="<b>Ups... debe escribir al menos 3 caracteres</b>";$_("index_rightArrow").style.display="none";$_("index_leftArrow").style.display="none"; return;}
		this.findLocations(textToFind);
	},
	
	findLocations:function(textToFind){
		var divResults = $_("divResults").empty(); 
		ajaxPetition("../resources/PHP/API_locations.php", "command=searchLocations&nameLocation="+textToFind+"&pag="+this.numPage+"&pageSize="+this.pageSize+"&flag=text", function(ajax){
				var r = eval("("+ajax.responseText+")");
				if (r.errorCode!=0){alert(r.errorDescription); return;}
				//alert (print_r(r));
				this.totalPages = r.total;
				if (this.map) this.map.cleanMarkers();
				if (this.map) this.map.searchLocation(r.data);
				var i=0;
				for(e in r.data){
						var result = $C("DIV", {'.paddingBottom':'6px'}, divResults);
						var locationLink = null;
						if (this.toRegister) locationLink = $C("A", {className:"resultLocation", target:'_blank', href:"http://www.todosenred.es/"+r.data[e].location+"/registro/", innerHTML:"<b>"+r.data[e].locationName}, result);
						else locationLink = $C("A", {className:"resultLocation", target:'_blank', href:"http://www."+r.data[e].location, innerHTML:"<b>"+r.data[e].locationName}, result);
						$C("SPAN", {className:"resultLink", innerHTML:" - "+r.data[e].location+"</b>"}, locationLink);
						i++;
				}
				if (i==0){$_("divResults").empty(); $_("divError").innerHTML="<b>Ups... su búsqueda no produjo ningún resultado</b>";}
			
				if (parseInt(this.numPage)>0){
					var previousButton =	$_("index_leftArrow"); previousButton.style.display="block";
					previousButton.onclick=function(){this.numPage--;this.findLocations(textToFind);}.bind(this);
				}
				else  $_("index_leftArrow").style.display="none";
				if (this.totalPages>parseInt(this.pageSize)*(parseInt(this.numPage)+1)){
					var nextButton =	$_("index_rightArrow"); nextButton.style.display="block";
					nextButton.onclick=function(){this.numPage++;this.findLocations(textToFind);}.bind(this);
				}
				else $_("index_rightArrow").style.display="none"; 
		}.bind(this));
	}
});
	
