
function SearchManager(opt_opts) {
	var me = this;
  
  me.options = opt_opts;
  me.markers = [];
  me.poi = [];
  me.screen = {width: '0px', height: '0px'};
  me.map = null;
  me.marker_manager = null;
  me.marker_bounds = null;
//  me.results = null;
  
};
SearchManager.prototype.bindGui = function() {
	var me = this; 
	(function($) {
		 if(GBrowserIsCompatible())
			{
			 me.iconMole = new GIcon();
			 $('input[name^=category_main_cb]').each(function(){
			 		$('#category_main_cb_'+$(this).val()).hide();
			 	});
			 	$('input[name^=category_main_cb]:checked').each(function(){
					$('#category_main_cb_'+$(this).val()).show();
				});
			 $('input[name^=category_main_cb]').click(function(){
			 	$('input[name^=category_main_cb]').each(function(){
			 		$('#category_main_cb_'+$(this).val()).hide();
			 	});
				$('#category_main_cb_'+$(this).val()).show();			 		
			 });
			 function regionClick(){
			 	$('input.regionItem').each(function(){
			 			$('#'+$(this).attr('id')+'Box').hide();
			 			$('#'+$(this).attr('id')+'Box input').attr('disabled','disabled');
		 		});
		 		$('input.regionItem:checked').each(function(){
		 			$('#'+$(this).attr('id')+'Box').show();
		 			$('#'+$(this).attr('id')+'Box input').attr('disabled',false);
		 		});
			 }
			 regionClick();
			 $('input.regionItem').each(function(){
			 	$(this).click(function(){
			 		regionClick();
			 	});
			 });
			 me.iconMole.image = me.options.imgUrl+'house.gif';
//			 me.iconMole.shadow = 'mole_down.png';
			me.iconMole.iconSize = new GSize(20, 20);
			me.iconMole.shadowSize = new GSize(20, 20);
			me.iconMole.iconAnchor = new GPoint(6, 20);
			me.iconMole.infoWindowAnchor = new GPoint(5, 1);
			 me.screen.width = $(me.options.map).parent().width();
			 me.screen.height = $(me.options.map).parent().height();
			 if(me.screen.height=='0'){
				 me.screen.height = Math.round(3*me.screen.width/4); 
			 }
			 me.screen.mapheight = me.screen.height;
			 me.screen.resultswidth = $(me.options.results).parent().width();
			 me.screen.mapwidth= me.screen.width - me.screen.resultswidth - 20;
			 $(me.options.map).css({width: me.screen.mapwidth+'px', height:me.screen.mapheight+'px'});
//			 $(me.options.map).css({position:relative, top: 0px});
			 
			$(me.options.map).append('<div id="googlemap" style="width: '+me.screen.mapwidth+'px; height:'+me.screen.mapheight+'px">here</div>');
			me.map = new GMap2(document.getElementById('googlemap'));
			me.map.enableScrollWheelZoom();
			me.map.setCenter(new GLatLng(me.options.mapCenter.lat,me.options.mapCenter.lng), 13);
			me.map.addControl(new GLargeMapControl3D());
//			me.map.addControl(new GLargeMapControl3D());
			me.marker_manager = new MarkerManager(me.map, {trackMarkers: true});
			}
			$(me.options.tabs).tabs();
			$(me.options.paging.reset).click(function(){
				me.reset();
			});
			$(me.options.paging.load_next_page).click(function(){
				var cp =$(me.options.paging.current_page);
				cp.val(1+parseInt(cp.val()));
				$(me.options.form).submit();
			});
			me.reset();
		  $(me.options.poi.form).submit(function(){
				$(this).ajaxSubmit({
					target:me.options.poi.target,
					url : me.options.poi.url,
					timeout:   600000
				});
				return false;
		  });
		  $(me.options.form).submit(function(){
			  $(this).ajaxSubmit({
				/*target:	me.options.results,*/ 
				url: me.options.formUrl,
				timeout:   60000,
				beforeSubmit:function(){
				  (function($) {
					  $(me.options.results).hide();
					  $(me.options.searching).show();
				  })(jQuery)
			  	},
				success: function(data,statusText,xhr,jqueryEl){
			  		(function($) {
			  			$(me.options.results).append(data);
			  			$(me.options.searching).hide();
			  			$(me.options.results).show();
			  		})(jQuery)
			  	}
				});
				return false;
		  });  
	  })(jQuery)
};
SearchManager.prototype.setResultPaging = function(results,min,max){
	var me = this;
	(function($) {
		$(me.options.paging.results).val(results);
		$(me.options.paging.load_next_page).attr('disabled', (results>0)?'':'disabled');
	})(jQuery)
};
SearchManager.prototype.drawResults = function(){
	var me = this;
	me.map.setCenter(me.marker_bounds.getCenter(),me.map.getBoundsZoomLevel(me.marker_bounds));
	(function($) {
	$(me.options.paging.displayed).val(me.markers.length);
//	$(me.options.results)
//		jQuery(document).ready(function() {
//		alert(me.screen.resultswidth);
//		alert(me.screen.height);
		//$('#contenttable').css({width:me.screen.resultswidth+'px', height: me.screen.height+'px'});
		/*document.body.clientHeight;*/
//			$(me.options.results+' table').Scrollable(me.screen.height, me.screen.resultswidth);
//		});

	})(jQuery)
};
SearchManager.prototype.reset = function(){
	var me = this;
	me.markers = null;
	me.markers = [];
	me.poi = [];
	me.marker_bounds = new GLatLngBounds();
	me.map.clearOverlays();
	if(me.marker_manager != null){
		me.marker_manager.clearMarkers();
	}
	me.setResultPaging(0,0,0);
	(function($) {
		$(me.options.paging.current_page).val(1);
	})(jQuery)
};
SearchManager.prototype.addPoi = function(id,geo,title) {
	var me = this;
	var posn = new GLatLng(geo["lat"], geo["long"]);
	var mark = new GMarker(posn, {
		title :title,
		icon: me.iconMole
	});
//	alert(me.options.imgUrl+'house.gif');
//	mark.setImage({url: me.options.imgUrl+'house.gif'});
//	mark.bindInfoWindowHtml(content);
//	alert(mark.getImage());
	me.map.addOverlay(mark);
	me.poi[me.poi.length] = mark;
}
SearchManager.prototype.addRow = function(id,geo,title,content,price) {
	var me = this;
	var posn = new GLatLng(geo["lat"], geo["long"]);
	
	var mark = new GMarker(posn, {
		title :title + " - " + price
	});
	mark.bindInfoWindowHtml(content);
	me.map.addOverlay(mark);
	me.marker_bounds.extend(posn);
	me.markers[id] = mark;
};
SearchManager.prototype.focusRow = function(id) {
	var me = this;
	var marker = me.markers[id];
	me.map.setCenter(marker.getLatLng());
	marker.show();
};

