/*	Google Maps API HtmlControl v1.1.2
	based on code posted on Google Maps API discussion group
	last updated/modified by Martin Pearman 20th August 2008
	
	http://googlemapsapi.martinpearman.co.uk/htmlcontrol
	
	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
var currToggleCode = '11';
var currActiveMode = "";

function MySearchControl(options){

	this.isVisible=true;
	this.isPrintable=true;	
	this.isSelectable=true;
	if(options){
		this.isVisible=(options.visible===false)?false:true;
		this.isPrintable=(options.printable===true)?true:false;
		this.isSelectable=(options.selectable===true)?true:false;
	}
};

MySearchControl.prototype=new GControl();

MySearchControl.prototype.initialize=function(map){
	var controlWidth = '530px';
	this.div=document.createElement('div');
	this.div.style.width = controlWidth ;
	this.div.style.marginRight= "30px";						

	
	var divControls = document.createElement('div');	
	this.div.style.width = controlWidth ;	
	this.div.appendChild(divControls);			
	
	
	var div = document.createElement('div');	
	div.innerHTML = " ";
	div.style.textAlign = "LEFT";
	div.style.width = controlWidth ;
	div.style.paddingTop = "5px";	
	div.style.paddingLeft = "10px";	
	div.style.paddingBottom = "5px";		
	div.style.background= "#FFFFFF";		
	div.style.border = "1px solid #CCCCCC"	;	
	//div.style.borderBottom = "5px solid #CCCCCC"	;	
	divControls.appendChild(div);				
	

	var searchbox =  document.createElement("input");
	searchbox.id = "mySearchBox";
	searchbox.style.width = "325px";		
	div.appendChild(searchbox );	
	
	jQuery(searchbox).keydown(function(event) {
		if(event.keyCode ==13){
			if(currToggleCode =='11'){
				currToggleCode = '20';
				toggleSearchControlButt(currToggleCode);				
				PopulateSideBarSearchKeywords();				
			}
			else if(currToggleCode =='20'){
				PopulateSideBarSearchKeywords();								
			}
			else if(currToggleCode =='12'){
				eval(currActiveMode);
			}			
		}
	});
	
	

	jQuery(searchbox).autocomplete({

		
		source: function(req, add){
			
					  	var center = map.getCenter();  
						req.center_lat = center.lat();
						req.center_lng = center.lng();
						req.radius = getMapRadius();
						//pass request to server
						jQuery.getJSON(ROOT_URL+"?cmd=getKeywords", req, function(data) {
							//create array for response objects
							var suggestions = [];
							
							//process response
							//process response
							jQuery.each(data, function(i, val){								
								suggestions.push(val.name);
							});
							
							//pass array to callback
							add(suggestions);

							
							//pass array to callback
							//add(suggestions);
						});
					}

	});


	var searchbutt =  document.createElement("button");
	searchbutt.id = "mySearchButton";	
	searchbutt.value ="Search";
	searchbutt.innerHTML ="Search";	
	jQuery(searchbutt).attr('class',"search_control_button_standby");			
	
	GEvent.addDomListener(searchbutt, 'click', function() {

		//jQuery('#keyword_for_search').val(jQuery("#mySearchBox").val());
		
		//jQuery("search_type1").attr('checked', true);
		currToggleCode = '20';
		toggleSearchControlButt(currToggleCode);
		PopulateSideBarSearchKeywords();
	});						
	
	div.appendChild(searchbutt);		
	
	var filterbutt =  document.createElement("button");
	filterbutt.id = "myFilterButton";	
	filterbutt.value ="Filter";
	filterbutt.innerHTML ="Filter";	
	jQuery(filterbutt).attr('class',"search_control_button_standby");			
	div.appendChild(filterbutt);		

	GEvent.addDomListener(filterbutt, 'click', function() {

		//jQuery('#keyword_for_search').val(jQuery("#mySearchBox").val());
		
		//jQuery("search_type1").attr('checked', true);
		currToggleCode = '12';		
		toggleSearchControlButt(currToggleCode);		
		eval(currActiveMode);

	});		
	
	var resetbutt =  document.createElement("button");
	resetbutt.id = "mySearchControlResetButton";	
	resetbutt.value ="Reset";
	resetbutt.innerHTML ="Reset";	
	jQuery(resetbutt).attr('class',"search_control_button_standby");			
	div.appendChild(resetbutt);		

	GEvent.addDomListener(resetbutt, 'click', function() {

		//jQuery('#keyword_for_search').val(jQuery("#mySearchBox").val());
		//jQuery("search_type1").attr('checked', true);
		currToggleCode = '11';		
		toggleSearchControlButt(currToggleCode);		
	});		
		

	this.setVisible(this.isVisible);
	map.getContainer().appendChild(this.div);
	return this.div;
};

MySearchControl.prototype.getDefaultPosition=function(){
	return new GControlPosition(G_ANCHOR_TOP_LEFT , new GSize(130,7));
};

MySearchControl.prototype.selectable=function(){
	return this.isSelectable;
};

MySearchControl.prototype.printable=function(){
	return this.isPrintable;
};

MySearchControl.prototype.setVisible=function(bool){
	this.div.style.display=bool ? '':'none';
	this.isVisible=bool;
};

MySearchControl.prototype.visible=function(){
	return this.isVisible;
};

function searchControlForeignReset(){
	if(currToggleCode== '20'){
		currToggleCode = '11';
		toggleSearchControlButt(currToggleCode);
	}
}

function toggleSearchControlButt(toggleCode){

	switch(toggleCode){
		case '11' : jQuery('#mySearchButton').attr('class','search_control_button_standby');
					jQuery('#myFilterButton').attr('class','search_control_button_standby');				
					jQuery('#myFilterButton').removeAttr('disabled');
					break;
		case '20' : jQuery('#mySearchButton').attr('class','search_control_button_active');
					jQuery('#myFilterButton').attr('class','search_control_button_disabled');				
					jQuery('#myFilterButton').attr('disabled', 'true');
					break;		
		case '12' :	jQuery('#mySearchButton').attr('class','search_control_button_standby');
					jQuery('#myFilterButton').attr('class','search_control_button_active');				
					break;		
	}
}
