var fulltext_timeout;
var search_timeout;
var panto_timeout;

function classSearch() {
	//}
	var thisO = this;
	
	this.map_center;
	this.arr_marker = new Array();
	this.arr_polyline = new Array();
	this.highlight_color = "#00FF00";
	this.search_hobby = false;
	this.search_location = false;
	this.search_fulltext = false;
	this.search_starttime = false;
	this.search_profil_picture = false;
	this.search_member_detail = false;
	
	this.profil_search = 0;
	
	if ( arguments.length ) {
		var search = arguments[0];
		for ( var i in search ) {
			if ( i == 'hobby' ) {
				this.search_hobby = search[i] ? true : false;
			} else if ( i == 'location' ) {
				this.search_location = search[i] ? true : false;
			} else if ( i == 'fulltext' ) {
				this.search_fulltext = search[i] ? true : false;
			} else if ( i == 'starttime' ) {
				this.search_starttime = search[i] ? true : false;
			} else if ( i == 'profilPicture' ) {
				this.search_profil_picture = search[i] ? true : false;
			} else if ( i == 'memberDetail' ) {
				this.search_member_detail = search[i] ? true : false;
			}
		}
	}
	
	
	this.search = function() {
		window.clearTimeout(fulltext_timeout);
		var page = 0;
		if ( arguments.length ) {
			page = arguments[0];
		}
		var poststr = 	"action=" 			+ encodeURIComponent( "search" ) + 
						"&page=" 			+ encodeURIComponent( page );
		
		if( this.search_hobby ) {
			var hobby = $("#search_hobby_id").val();
			poststr += 	"&hobby_id=" 		+ encodeURIComponent( hobby );
		}
		if( this.search_location ) {
			var vicinity = $("#search_vicinity").val();
			var location_lat = $("#search_location_lat").val();
			var location_lng = $("#search_location_lng").val();
			poststr += 	"&vicinity=" 		+ encodeURIComponent( vicinity ) + 
						"&location_lat=" 	+ encodeURIComponent( location_lat ) + 
						"&location_lng=" 	+ encodeURIComponent( location_lng );
		}
		if( this.search_fulltext ) {
			var fulltext = $("#search_fulltext").val();
			poststr += 	"&fulltext=" 		+ encodeURIComponent( fulltext );
		}
		if( this.search_starttime ) {
			var starttime_day		= $("#search_starttime_day").val();
			var starttime_month		= $("#search_starttime_month").val();
			var starttime_year		= $("#search_starttime_year").val();
			var starttime_hour		= $("#search_starttime_hour").val();
			var starttime_minute	= $("#search_starttime_minute").val();
			var starttime = starttime_year + '-' + starttime_month + '-' + starttime_day + ' ' + starttime_hour + ':' + starttime_minute + ':00';
			poststr += 	"&starttime=" 		+ encodeURIComponent( starttime );
		}
		if( this.search_profil_picture ) {
			var profil_picture = $('#search_profil_picture').attr("checked");
			profil_picture = profil_picture == true ? 1 : 0;
			poststr += 	"&profil_picture=" 	+ encodeURIComponent( profil_picture );
		}
		if( this.search_member_detail ) {
			var age_from = $('#search_age_from').val();
			var age_to = $('#search_age_to').val();
			age_from = parseInt(age_from, 10);
			age_to = parseInt(age_to, 10);
			var female = $("#search_gender_female").attr("checked");
			var male = $("#search_gender_male").attr("checked");
			
			var gender = 3;
			if( female == true ) {
				gender = 2;
			} else if( male == true ) {
				gender = 1;
			}
			
			poststr += 	"&gender=" 		+ encodeURIComponent( gender ) + 
						"&age_from=" 	+ encodeURIComponent( age_from ) + 
						"&age_to=" 		+ encodeURIComponent( age_to );
		}
		ajaxRequest.ajax(1, poststr, 'search_result', thisO.handleResponse, 1);
	}
	
	this.result = function() {
		this.removeAllOverlay();
		var i = 0;
		var from = new GLatLng($("#search_location_lat").val(), $("#search_location_lng").val());
		while ( $("#result_id_"+i).length ) {
			var location_lat = $("#result_location_lat_"+i).val() == '' ? 0 : parseFloat($("#result_location_lat_"+i).val());
			var location_lng = $("#result_location_lng_"+i).val() == '' ? 0 : parseFloat($("#result_location_lng_"+i).val());
			var center = new GLatLng(location_lat, location_lng);
			
			temp_polyline = new GPolyline( [from, center], "#ff0000", 2, 0.5, {geodesic: true});
			var dist = temp_polyline.getLength();
			map.addOverlay(temp_polyline);
			this.arr_polyline.push(temp_polyline);
			
			dist = Math.round(dist/10);
			dist = dist/100;
			
			var last = this.arr_marker.length;
			var event_id = $("#result_id_"+i).val();
			var title = $("#result_name_"+i).val() + ' --- ' + dist + ' km';
			var marker = createMarker(center, title, event_id);
			this.arr_marker.push(marker);
			
			map.addOverlay(this.arr_marker[last]);
			i++;
		}
		var num_page = $('#num_result_page').val();
		num_page = parseInt(num_page, 10);
		if ( num_page ) {
			var current_page = $('#result_current_page').val();
			current_page = parseInt(current_page, 10);
			var show_page = current_page + 1;
			$('#slider_show_page').text(show_page);
			$('#page_slider').show();
			if ( typeof $('#page_select_slider').slider == 'function' &&1==0 ) {
				$('#page_select_slider').slider(
					{
						steps: num_page, 
						minValue: 0, 
						maxValue: num_page, 
						startValue: current_page, 
						slide: function(e,ui) {
							var page = ui.value + 1;
							$('#slider_show_page').text(page);
						}, 
						change: function(e,ui) {
							//$('#page_slider').hide();
							thisO.search(ui.value);
						}
					}
				);
			} else {
				var html = '';
				html += 'Seite: ';
				var page = num_page + 1;
				if( current_page == 0 ) {
					html += '<span style="color: #666666;">[Zur&uuml;ck]</span> - ';
				} else {
					var page_back = current_page -1;
					html += '<a href="javascript: Search.search('+page_back+');">[Zur&uuml;ck]</a> - ';
				}
				for ( var i = 0; i<page; i++ ) {
					var show_page = i + 1;
					if( i != current_page ) {
						html += ' <a href="javascript: Search.search('+i+');">'+show_page+'</a> ';
					} else {
						html += ' <span style="font-weight: bold; color: #333333;">'+show_page+'</span> ';
					}
				}
				if( current_page == (page-1) ) {
					html += ' - <span style="color: #666666;">[Vorw&auml;rts]</span>';
				} else {
					var page_next = current_page+1;
					html += ' - <a href="javascript: Search.search('+page_next+');">[Vorw&auml;rts]</a>';
				}
				$('#page_slider').html(html).show();
			}
		}
	}
	
	this.removeAllOverlay = function() {
		for ( i in this.arr_marker ) {
			map.removeOverlay(this.arr_marker[i]);
		}
		this.arr_marker =  new Array();
		for ( i in this.arr_polyline ) {
			map.removeOverlay(this.arr_polyline[i]);
		}
		this.arr_polyline = new Array();
	}
	
	this.showAllMarker = function() {
		for ( i in this.arr_marker ) {
			this.arr_marker[i].show();
		}
		for ( i in this.arr_polyline ) {
			this.arr_polyline[i].show();
		}
		this.map_center = new GLatLng($('#search_bounceback_lat').val(), $('#search_bounceback_lng').val());
		panto_timeout = window.setTimeout(function(){map.panTo(thisO.map_center);}, 400);
	}
	
	this.hideAllMarker = function() {
		for ( i in this.arr_marker ) {
			this.arr_marker[i].hide();
		}
		for ( i in this.arr_polyline ) {
			this.arr_polyline[i].hide();
		}
	}
	
	this.showMarker = function(i) {
		if ( !this.map_center ) this.map_center = map.getCenter();
		window.clearTimeout(panto_timeout);
		this.hideAllMarker();
		if ( this.arr_marker[i] ) {
			this.arr_marker[i].show();
			center = this.arr_marker[i].getLatLng();
			map.panTo(center);
		}
		if ( this.arr_polyline[i] ) {
			this.arr_polyline[i].show();
		}
	}
	
	this.highlight = function( id ) {
		if ( getObject("list_result_"+id) ) {
			getStyleObject("list_result_"+id).backgroundColor = this.highlight_color;
		}
	}
	
	this.unHighlight = function( id ) {
		if ( getObject("list_result_"+id) ) {
			getStyleObject("list_result_"+id).backgroundColor = "#FFFFFF";
		}
	}
	
	this.handleResponse = function() {
		$('#search_result').html(ajaxRequest.responseText());
		thisO.result();
	}
	
	this.setStarttime = function() {
		var starttime = strtotime($('#datepicker_start').val());
		var y = starttime.getFullYear();
		var m = starttime.getMonth();
		var d = starttime.getDate();
		m++;
		$("#search_starttime_year_input").val(y);
		$("#search_starttime_month_input").val(m);
		$("#search_starttime_day_input").val(d);
		this.search();
	}
}

function createMarker(posn, title, id) {
	var marker = new GMarker(posn, {title: title});
	GEvent.addListener( marker, "mouseover", function(){Search.highlight(id);} );
	GEvent.addListener( marker, "mouseout",  function(){Search.unHighlight(id);});
	GEvent.addListener( marker, "click",  function(){ window.location = "?view="+id; });
	return marker;
}
