// JavaScript Document
var mapCmsPoiSearched = new Array();
henworx.MapCms.Search=function(){
	
	
	 this.init=function (){
		document.getElementById("searchform").onsubmit=this.henworx.MapCms.Search.getResults;
		/*
		document.getElementById("searchform").onsubmit=function (){
			if(document.getElementById('s').value != '')
				this.henworx.MapCms.Search.getResults;
			return false;
		};
		*/
	 }
	 
}

henworx.MapCms.Search.getResults=function(){
	
	//check if th search text is blank
	
	if(document.getElementById('s').value == '')
		return false;
	
	_lang = henworx.MapCms.Lang;
	//remove all the markers on the map
	if(typeof(gBigOpenWindow.location)!='undefined'){
		henworx.MapCms.BigWindow.hide();	
	}
	
	/**/
	gMapCms.map.clearOverlays();
	
	/*Linkrel*/
	//check if the linkrel is enabled 	
	if(typeof(objlinkrel)=="object"){	
		no_event=true				
		gMapCms.putMarker(objlinkrel.linkrelLocation,no_event);
		
		
	}	
	/*Linkrel*/
	restoreSearchedPoi();
	
	//check if the same catid is already opened
	henworx.MapCms.Status(_lang.getWord('Searching ...')+' <img src="'+gPluginUrl+'/content/images/searching.gif" align="absbottom"/>');
		//fetch new Category Data.
		gMapCmsDb.searchLocations=new Array();
		_special_chars = new Array('ä','ö','ü','Ä','Ö','Ü','ß','&');
		_general_chars = new Array('a','o','u','A','O','U','s','amp;');
		_s = document.getElementById("s").value;
		_s=encodeURI(_s);
						
		if(g_SearchingPOI!=""){
			range=g_SearchingPOI.split(",")
			//gMapCmsDb.getRangeAsXML(g_SearchingPOI);
			gMapCms.locations=gMapCmsDb.recentLocations;
			//addIcon
			_a=new henworx.MapCms.Address("",range[0],range[1])
			_l=new henworx.MapCms.Location(_a);			
			_l.title=decodeURI(_s);
			_l.setEmblem('linkrel');
			gMapCms.putMarker(_l,true);
			//store into array
			mapCmsPoiSearched[mapCmsPoiSearched.length] = _l;
			mapCmsPoiSearched.unique();
			
		}else{	
			for(i=0; i<=_special_chars.length; i++){
				_s = _s.replace(_special_chars[i],_general_chars[i]);			
			}
			gMapCmsDb.getSearchAsXML(_s,500);
			gMapCms.locations=gMapCmsDb.searchLocations;
		}
		
		
		if(gMapCms.locations.length>0){
			if(gMapCms.locations[0].id==gMapCms.locations[1].id){gMapCms.locations.pop()}
			gMapCms.centerAt=gMapCms.locations[0];
			gMapCms.zoom=13;
			if(g_SearchingPOI==""){
				gMapCms.setCenter(gMapCms.centerAt);
			}else{
				gMapCms.map.setCenter(new GLatLng(range[0],range[1]),gMapCms.zoom);
			}
			gMapCms.putAllMarker();
		}
		else{
			
			gMapCms.locations.length=0;
		}

	

	if(typeof(listViewObj)=="object"){
			listViewObj.reset();
			listViewObj.currentPage=1;
			
			
			if(g_SearchingPOI!=""){
				//fetch the data according to range if it is POI
				gMapCmsDb.getRangeAsXML(g_SearchingPOI,500);
				gMapCms.locations=gMapCmsDb.rangeLocations;
				//gMapCmsDebug.append("Range:"+g_SearchingPOI+" Length:"+gMapCmsDb.rangeLocations.length);//debug
			}	
			
			//normal search
			if(gMapCms.locations.length!=0){
				listViewObj.maxPage=Math.ceil((gMapCms.locations.length)/listViewObj.postsPerPage);
				if(g_SearchingPOI!=""){
					listViewObj.getData(true,'','',g_SearchingPOI);
				}
				else{
					for(i=0; i<=_special_chars.length; i++){
						_s = _s.replace(_special_chars[i],_general_chars[i]);			
					}
					listViewObj.getData(true,_s,'','');
				}
			}else{
				listViewObj.empty();
			}											
	
	}
	
	_s=decodeURI(_s);
	_s = _s.replace('amp;','&');
	
	if(gMapCms.locations.length!=0){
				_status_text = _lang.getWord("Searched for &#8220;#search_text#&#8221;&nbsp;#result_count#&nbsp;results found");
				if(g_SearchingPOI!=""){_status_text =_lang.getWord("Restaurants Near &#8220;#search_text#&#8221;")};				
				_status_text = _status_text.replace('#search_text#',_s);
				_status_text = _status_text.replace('#result_count#',gMapCms.locations.length);
				
				henworx.MapCms.Status(_status_text);
	}else{
		_status_text = _lang.getWord("Searched for &#8220;#search_text#&#8221; - results not found");
				_status_text = _status_text.replace('#search_text#',_s);				
				henworx.MapCms.Status(_status_text);
	}
	
	return false			
}

//put the markers that is stored in array
function restoreSearchedPoi(){
	if(mapCmsPoiSearched.length>0){
		for(i=0; i<mapCmsPoiSearched.length;i++){
			gMapCms.putMarker(mapCmsPoiSearched[i],true);
		}
	}
}

// removes the duplicate values from the array
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };