	
function Locator(zip_code, google_site_uri, google_site_name, filters){
	this.g_image1 = preloadImages("http://maps.google.com/intl/ru_ALL/mapfiles/iw2.png");
	this.g_image2 = preloadImages("http://maps.google.com/intl/ru_ALL/mapfiles/iw_close.gif");
	this.g_image3 = preloadImages("http://maps.google.com/intl/ru_ALL/mapfiles/iws2.png"); //dark
	this.zip_code = (zip_code != null)? zip_code.toString(): null;
	this.site_uri = google_site_uri.toString();
	this.site_name = google_site_name.toString();
	this.filters = filters;
	this.finish_point = "";
	this.search_partner_term = this.site_name  +" site:"+ this.site_uri;
	this.search_string = this.zip_code; 
	this.map = new GMap2(document.getElementById("store-locator_map"));
	this.map.addControl(new GSmallMapControl());
	this.map.addControl(new GMapTypeControl() );
	this.local_search = new GlocalSearch();
	this.geo_finder = new GClientGeocoder(); 
 	this.local_search.setResultSetSize(GSearch.LARGE_RESULTSET); 
	this.local_search.setCenterPoint(this.map);
	this.local_search.setSearchCompleteCallback(null, wc_OnLocalSearch);
	this.current_target = null;
	this.state = new NormalState(this);
	this.input = new IElement("store-locator-search_address");
	this.right_error_address	= new Element("store-locator-error_window", "store-locator-error_message");
	this.left_info 				= new Element("store-locator-local_search_message", "store-locator-enter-your-caption");
	this.left_info_stores		= new Element("store-locator-local_search_caption");
	this.left_error_localsearch = new Element("store-locator_search_results_error", "wc-no-stores");
	this.left_loading_box 		= new Element ("store-locator_left_loading");
	this.map_loading_box 		= new Element ("store-locator_left_loading", "store-locator_left_loading");
	this.center_loading_box 	= new Element ("store-locator_left_loading");
	this.left_results_box		= new Element ("store-locator_search_results");
	
	this.left_dirs_loc_box      = new Element("store-locator_search_directions_block");
	this.left_dirs_loc_stores_at = new Element("store-locator-direction");
	this.left_dirs_loc_steps = new Element("store-locator-left-dirs-loc-steps");
	this.left_dirs_info =  new Element("store-locator-dirs-info");
	this.left_dirs_loc_stores_at.set("to " + this.site_name+" Store"  );
	this.bounds = null;
	this.local_search_results_filtered = [];
	this.local_search_len =0;
	this.results = new Results();
	this.current_route = null;
	this.dirs = new GDirections();

	GEvent.addListener(this.dirs, "load", function() {wc_locator.load_route();});
    GEvent.addListener(this.dirs, "error", handleErrors);
	
	this.center_loading_box.show();
	this.current_store = null;
	this.start_dirs_location = null;
	this.finish_dirs_location = null;
	this.current_address = null;
	this.current_address_directions = null;
	this.revers = false;
	this.start_html_point = null;
	this.finish_html_point = null;
	
	if(this.search_string!=null) this.search_string = this.search_string.trim(); else this.search_string='';
		
		if(this.search_string=='')
		{
			this.search_string=getCookie("wcGoogleLocation");

		}
		
	if(this.search_string!=null) this.search_string = this.search_string.trim(); else this.search_string='';	
	
		if (this.search_string==''){
			this.change_state(new EmptyState(this));
			this.state.error();
			} else {
				 this.check_zip();
				 this.input.set(this.search_string)
				 this.open_local_search();
			 }
	
}



Locator.prototype.change_state= function(state) { 
	this.state=state;

}

Locator.prototype.check_zip = function() { 
	this.zip_code = (is_zip(this.search_string))? this.search_string: null;
	parent.document.wcGoogleZipCode = (this.zip_code !=null)?this.zip_code: parent.document.wcGoogleZipCode;
}


Locator.prototype.open_address = function() { 
//	wc_hide_error_message();
	//hide(SL_RIGHT_ERR_INPUT_MESSAGE);
	//hide(SL_LEFT_INFO_EMPTY_LOCATION_MESSAGE);
	
	this.right_error_address.hide();
	
	this.geo_finder.getLocations(this.search_string, get_location);	 
}

Locator.prototype.load_stores = function() {
	this.map.setCenter(this.current_target,14);
	var text_caption="<b>"+this.site_name+"</b> stores near ";
	this.left_info_stores.set(text_caption+this.search_string_caption());
	this.left_info_stores.show();
	this.center_loading_box.show();
	this.local_search.execute(this.search_partner_term);
	this.left_info.hide();	
}

Locator.prototype.load_directions = function() {
	this.center_loading_box.hide();
	this.load_dirs_direct();
	
}

Locator.prototype.get_location = function(result) { 
	if (result.Status.code == G_GEO_SUCCESS) {
		this.left_loading_box.show();
		this.left_error_localsearch.hide();

		this.left_results_box.set("");
		this.left_loading_box.show();
		
		var rp = result.Placemark[0].Point.coordinates;
		this.current_target = new GLatLng(rp[1],rp[0]); 
		//this.map.setCenter(this.current_target,14);
		this.check_zip();
		this.state.post_loaded_address();
		
	
		this.current_address = this.search_string;
		setCookie("wcGoogleLocation", this.search_string);
		//this.change_state(new NormalState(this));
		//this.left_info.hide();			
		}
		else {
			this.state.error(result.Status.code);
		}
}	


Locator.prototype.check_coords = function (index) {
	var test_arr = this.local_search_results_filtered;
	var test_element = this.local_search.results[index];
		for (var i = 0; i < test_arr.length; i++) {
				elem = test_arr[i];
				
				if((elem.lng==test_element.lng)&&(elem.lat==test_element.lat))return false;
				
			}
	return true;
	

}	


Locator.prototype.add_point = function (index) {
	this.local_search_results_filtered.push(this.local_search.results[index]);
	
	res = this.results.add(this.local_search.results[index], this.site_name);
/* 	this.left_results_box.append(unselectedHtml_template(index, result, this.site_name));
	wc_create_result_template(locator.site_name, result, null)
	get_full_address(result);
	marker
	);
	 */

	this.left_results_box.append(res.getUNHTML());

	this.local_search_len++;
	//marer=createMarker(new GLatLng(parseFloat(this.local_search.results[index].lat), parseFloat(this.local_search.results[index].lng)), wc_get_locator_icon_index())
	this.bounds.extend(res.getCoords());
	
	this.map.addOverlay(res.getMarker());

}
Locator.prototype.check_filters = function (index) {
	
	var teststring = this.local_search.results[index].title;
	
	teststring = teststring.replace(/<.{1,2}>/g,"");
//alert(teststring);
	if (this.filters.length == 0) return this.add_point(index);
	
	for (var i = 0; i < this.filters.length; i++) {
		regex = new RegExp(this.filters[i], ""); 
		if (regex.test(teststring)==true)
			if (this.check_coords(index)){
					this.add_point(index);
					
				}
			
	}

	
}	

Locator.prototype.search_string_caption = function(search_string){

	return (is_zip(search_string))?search_string:"zip code " + search_string;
}

Locator.prototype.search_string_caption = function(){
	return (this.zip_code==null)?this.search_string:"zip code " + this.zip_code.toString();
}

Locator.prototype.execute_local_search = function (){
	if (!this.local_search.results) return;
		//delete this.results;
		this.results = new Results();
		this.local_search_results_filtered = [];
		this.left_results_box.clear();
		this.map.clearOverlays();
		this.local_search_len = 0;
     	this.bounds = new GLatLngBounds();
	 	
		this.bounds.extend(this.current_target);

		
		for (var i = 0; i < this.local_search.results.length; i++)this.check_filters(i); 		
		this.left_loading_box.hide();
		
		this.center_loading_box.hide();
		if (this.local_search_len >0 ) {
			this.left_results_box.show();
			
		} else 
		{	
			this.left_error_localsearch.show(); 
		}

		var new_zoom=this.map.getBoundsZoomLevel(this.bounds);
		var new_center=this.bounds.getCenter();
		
		this.map.setCenter(new_center,new_zoom);

	}	
	
function get_location(result) {
 /*AJAX FUNCTION*/
	wc_locator.get_location(result);
}	
Locator.prototype.open_local_search =function () {

	this.change_state(new NormalState(this));
	this.left_results_box.show();
	this.left_dirs_loc_box.hide();
	this.state.change_address();
	this.left_dirs_info.hide();
	this.left_info_stores.show();
}

Locator.prototype.load_route = function() {
	
	this.current_route = this.dirs.getRoute(0);
	this.change_state(new DirectionsState(this));
	this.start_dirs_location = this.start_dirs_location_pre;
	this.finish_dirs_location  = this.finish_dirs_location_pre;
	this.current_address_directions = this.search_string;
	setCookie("wcGoogleLocation", this.current_address_directions);
	//alert(this.current_address_directions);
 

		this.check_zip();
//this.left_dirs_loc_steps.set(html_template_step(this.dirs.getRoute(0).getStep(0),1));
		//alert("THIS:"+this.dirs.getRoute(0).getStep(0).getDescriptionHtml()); 
		var nums_steps = this.current_route.getNumSteps();
		var rp =this.current_route.getStartGeocode().Point.coordinates;
		
		this.start_dir_coords = new GLatLng(rp[1],rp[0]); 
		this.end_dir_coords = wc_locator.dirs.getRoute(0).getEndLatLng();
	if(!(this.reverse)){
		this.start_html_point = HTML_info_box("Your current location", "From:", HTML_info_address("("+this.search_string_caption(this.current_address_directions)+")"), -1, "res/store-locator/icons/icon_greenA.gif");
		this.finish_html_point = HTML_info_box(this.site_name, "To:", HTML_address_template(this.results.get(this.current_store_index).getResult()), -2, "res/store-locator/icons/icon_greenB.gif");
		
		//this.end_dir_coords = this.results.get(this.current_store_index).getCoords();
		this.start_index = -1;
		this.end_index = -2;
	}else {
		
		this.start_html_point = HTML_info_box(this.site_name, "From:", HTML_address_template(this.results.get(this.current_store_index).getResult()), -1, "res/store-locator/icons/icon_greenA.gif");
		this.finish_html_point = HTML_info_box("Your current location", "To:", HTML_info_address("("+this.search_string_caption(this.current_address_directions)+")"), -2, "res/store-locator/icons/icon_greenB.gif");
		//this.start_dir_coords = this.results.get(this.current_store_index).getCoords();
		//this.end_dir_coords =  new GLatLng(rp[1],rp[0]);
		this.start_index = -2;
		this.end_index = -1;
	}

	
	this.start_marker = new GMarker(this.start_dir_coords,wc_get_locator_icon_index(0));
	this.finish_marker = new GMarker(this.end_dir_coords,wc_get_locator_icon_index(1));
	
	GEvent.bind(this.start_marker, "click", this, function() { show_MapBlowup(-1);});
	GEvent.bind(this.finish_marker, "click", this, function() {show_MapBlowup(-2);});
	
	this.left_results_box.hide();
	this.left_info_stores.hide();
	this.left_dirs_info.show();
	this.left_dirs_loc_box.show();
	this.map.clearOverlays();
	this.map.addOverlay(this.start_marker);
	this.map.addOverlay(this.finish_marker);
	this.left_dirs_loc_steps.clear(); 	
	//getStartGeocode()
	this.dirs_table = new TableElement("store-locator-dirs-results");
	this.left_dirs_loc_steps.append(this.start_html_point.get());

	var duration_caption = new DivElement("store-locator-directions-duration",this.current_route.getSummaryHtml());
	var copyrights_caption = new DivElement("store-locator-directions-copyright",this.dirs.getCopyrightsHtml());
	this.left_dirs_loc_steps.append(duration_caption.get());
	

			for (var i = 0; i < nums_steps; i++) {
			
				this.dirs_table.append(HTML_template_step(this.current_route.getStep(i),i+1));
			
			
			}
			
			this.left_dirs_loc_steps.append(this.dirs_table.get());
			this.left_dirs_loc_steps.append(this.finish_html_point.get()); 
			this.left_dirs_loc_steps.append(copyrights_caption.get());
			var new_zoom=this.map.getBoundsZoomLevel(this.dirs.getBounds());
			var new_center=this.dirs.getBounds().getCenter();
		
		this.map.setCenter(new_center,new_zoom);
	
	this.map.addOverlay(this.dirs.getPolyline());

}
Locator.prototype.load_dirs = function(index){

	this.current_store = this.results.get(index).getCoords().lat()+", "+this.results.get(index).getCoords().lng();
	this.current_store_index = index;
	this.search_string = this.current_address;
	this.load_dirs_direct();


}


Locator.prototype.load_dirs_direct = function() {

	this.load_dirs_query(this.current_target.lat()+", "+this.current_target.lng(), this.current_store);
}

Locator.prototype.load_dirs_reverse = function() {


	this.reverse=!(this.reverse);
	this.load_dirs_query(this.finish_dirs_location, this.start_dirs_location);
}

Locator.prototype.load_dirs_query = function(start_dirs_location, finish_dirs_location ) { 
 
 var dirs_query = "from: " + start_dirs_location  + " to: " + finish_dirs_location;
 
 this.start_dirs_location_pre = start_dirs_location;
 this.finish_dirs_location_pre  = finish_dirs_location;
 
 this.dirs.load(dirs_query ,
                { 
				  "getSteps": true,
				  "getPolyline": true,
				  "preserveViewport": true
				
			});

}

Locator.prototype.change_search_string = function () {
	search_string = this.input.get().trim();
				
					if(search_string.length > 0){
						
						this.search_string = search_string;
						this.state.change_address();
					
					}
					this.input.set(search_string);
}


Locator.prototype.OnEnter = function (event) {
	if(event.keyCode==13)
			{
					this.change_search_string();
			}
			else 
			{
				this.right_error_address.hide();
			}

}		


	function wc_init_load() 
	{
		
		wc_locator =  new Locator(parent.document.wcGoogleZipCode, 
								  parent.document.wcGoogleSiteUrl, 
								  parent.document.wcGoogleSearchName, 
								  parent.document.wcGoogleResultsFilters 
								  ); 
		if ( parent.document.wcWrapperCssUrl!=null) apply_css_to_head(parent.document.wcWrapperCssUrl);
		
	}
	

	
    function wc_open_markerwindow(index){
		var res = wc_locator.results.get(index);
		res.getMarker().openInfoWindow(res.getHTML())
	}
	
	function wc_set_directions(index){
		
		wc_locator.load_dirs(index);

				
	}
	function show_MapBlowup(index){
		var coords;
		
		if (index == -1) coords = wc_locator.start_dir_coords;
		if (index == -2) coords = wc_locator.end_dir_coords;
		if (index > 0 )  coords = wc_locator.current_route.getStep(index-1).getLatLng();
		
		wc_locator.map.showMapBlowup(coords);
	}
	
	function wc_show_showMapBlowup(index){
		show_MapBlowup(index);
	}
	
	function wc_show_showMapBlowup(){
		show_MapBlowup(this.index);	
	}

		
		
  function get_reverse_directions() {
  
	wc_locator.load_dirs_reverse();
  }
   

	

	function set_wc_search_address(){
		wc_locator.change_search_string();
	}
	
	function wc_onpress(event)
	{ 
		wc_locator.OnEnter(event);
	}

	function back_to_stores() {
		wc_locator.open_local_search();
	}
	
	function wc_OnLocalSearch() 
	{  /**AJAX**/
		wc_locator.execute_local_search();
	}
	
	function onGDirectionsLoad() 
	{  /**AJAX**/
		wc_locator.load_route();
	}
	
	function handleErrors() 
	{  /**AJAX**/
		//wc_locator.execute_local_search();

		wc_locator.state.error(wc_locator.dirs.getStatus().code);
		
		
	}
