$(document).ready(function()
{

	var ooh_search_autocomplete = function($this)
	{
		$this.attr("autocomplete","off");
		
		$this.bind('focus', function() 
		{
			var this_id 			= $this.attr('id')
				suggest_id 			= this_id+'_suggest',
				$suggest 			= '',
				this_val 			= $this.val(),
				this_default_val 	= '',
				this_json_url 		= '',
				this_index = 0,		
				KEY = {
					UP: 38,
					DOWN: 40,
					DEL: 46,
					TAB: 9,
					RETURN: 13,
					ESC: 27,
					COMMA: 188,
					PAGEUP: 33,
					PAGEDOWN: 34,
					BACKSPACE: 8
				};
			
					
			if ( this_id == 'category_query') {
				this_default_val = "What?";
				this_json_url = '/ajax/categories-autocomplete?format=json&q=';
			} else {
				this_default_val = "Where?";
				this_json_url = '/ajax/location-autocomplete?format=json&q=';
			}
				
			if ( this_val === this_default_val) {
				$this.val('');
			}	
			
			if ( $suggest.length == 0 ) {
				$this.after($('<ul id="'+suggest_id+'" class="suggest"></ul>').hide());
				$suggest = $('#'+suggest_id);
			} else {
				if ($suggest.html().length > 0) {
					$suggest.show();
				}
			}

			$this.bind('keyup', function(event) 
			{
				var $this_suggest = $this.parent().find("ul.suggest"),
					val = $this.val();

				switch(event.keyCode) {
				
					case KEY.UP:
						event.preventDefault();

						$this_suggest.find("li:nth-child("+this_index+") a").removeClass('active');
						if ( this_index <= 1 ) {
							this_index = $this_suggest.find("li").length;
						} else {
							this_index--;
						}
						
						$this_suggest.find("li:nth-child("+this_index+") a").addClass('active');
						break;
				
					case KEY.DOWN:
						event.preventDefault();
						
						$this_suggest.find("li:nth-child("+this_index+") a").removeClass('active');
						if ( this_index >= $this_suggest.find("li").length ) {
							this_index = 0;
						}
						
						this_index++;
						$this_suggest.find("li:nth-child("+this_index+") a").addClass('active');
						
						break;
					case KEY.RETURN:	
						event.preventDefault();
						if ( $this_suggest.is(":visible") ) {
							$this.val($this_suggest.find('li a.active').text());	
							
							$this_suggest.hide();
							$this_suggest.find('li a.active').removeClass('active');
						}
						break;
						
					case KEY.TAB:
						if ( $this_suggest.is(":visible") ) {
							$this_suggest.hide();
						}
						break;
					default:
						if (val.length > 0) {
							jQuery.getJSON(this_json_url+encodeURI(val), function(data) 
							{
								var rtn = '';
								$(data).each(function(i) 
								{
									rtn += '<li><a href="#">' + data[i].name + '</a></li>';
								});
				
								$this_suggest.html(rtn);
								$this_suggest.show();
						
								$('#'+suggest_id+' li a').bind('click', function(e) 
								{ 
									$this.val($(this).text());
									
									$this_suggest.hide();
									e.preventDefault();
								});
								
								$('#'+suggest_id).bind('blur', function() 
								{ 
									$this_suggest.hide();
								});
							});
					}
					break;
				}
			});
			
			$this.bind(($.browser.opera ? "keypress" : "keydown"), function(event) 
			{	
				switch(event.keyCode) {
					case KEY.RETURN:
						if ($suggest.find('li a.active').length > 0) {
							event.preventDefault();
						}
						break;
				}
			});		
						
		});
	}
	
	ooh_search_autocomplete($('#category_query'));
	ooh_search_autocomplete($("#location_query"));

	var setMinDate = function()
	{
		var from_date = $("#date_from").datepicker("getDate");
		var to_date = $("#date_to").datepicker("getDate");		
		
		$("#date_to").datepicker("option", 'minDate', from_date);
		
		if (from_date > to_date) {		
			new_date = new Date();
			new_date.setDate(from_date.getDate() + 30);
			$("#date_to").datepicker("setDate", new_date);			
		}
	};
	
	$("#date_from").datepicker( 
	{
		"showOn": "focus",
		"dateFormat": "yy-mm-dd",
		"onSelect": function() { setMinDate() }
	});
	
	$("#date_to").datepicker(
	{
		"showOn": "focus",
		"dateFormat": "yy-mm-dd"
	});
	
	$('#date_from').parent().find('.hint a').bind('click', function(e) 
	{ 
		$('#date_from').val($(this).text());
		$(this).closest("form").submit();
		e.preventDefault();
	});
});
