$(document).ready(function(){
					
		   
	// Add invalid but really useful auto complete off attribute
	$('#liveSearchInput').attr('autocomplete', 'off');
	$('#liveSearchInput.events').val('Search Artist/Promoter').width(200);
	$('#liveSearchInput.gallery').val('Search Galleries').width(200);
	$('#liveSearchSubmit').css('display', 'none');
	// Editable vars
	var minSearchChars = 2;
	function keyInput(e) {
		if(window.timeout) { window.clearTimeout(window.timeout); }
		
		// Check min search chars and exit if too short
		if(($('#liveSearchInput').val().length+1) < minSearchChars) {
			return true;
		}
		
	
		// Get key presses
		var key = 0;
		if (e == null) {
			key = event.keyCode;
		} else { // mozilla
			key = e.which;
		}
		
		switch(key) {
			// Left and right
			case 37:
			case 39: 
			  return false;
			 break;
			case 38: 
			  navagate('up');
			 break;
			case 40: 
			  navagate('down');
			 break;
			// Enter key
			case 13:
			  var href = $('#liveSearchResults li.active a').attr('href');
			 
			  if(href) {
			 	 window.location = href;
				 return false;
			  } else {
				return false;
			  }
			 break;
			case 27:
				searchLoseFocus();
			 break;
			default:
			 // only query the database every 200 milliseconds
			 window.timeout = window.setTimeout(function(){queryDB();},200);
			break
		}
	}

	function queryDB() {
		// Exit if nothing to search for
		if($('#liveSearchInput').val() == '') { searchLoseFocus(); return true; }
		
		// Display ajax indicator
		$('#liveSearchInput').addClass('searching');
		
		if(window.timeout) { window.clearTimeout(window.timeout); }
		var searchFor = $('#liveSearchInput').val();
	
		$.ajax({
			url: $('#liveSearchUrl').val(),
			data: {'s': searchFor, 'type' : 'ajax'},
			dataType: 'html',
			success: function(data) {
				$('#liveSearchInput').removeClass('searching');
				$('#liveSearchResults').html(data);
				$('#liveSearchResults').fadeIn('fast');
				mouseControl();
			},
			timeout: 12000,
			error: function() {
				$('#liveSearchInput').removeClass('searching');
				$('#liveSearchResults').html('<span class=\'cat\'>Error: please try searching again</span>');
				$('#liveSearchResults').fadeIn('fast');
			}
		});
	}
	
	// Keyboard navigation
	function navagate(direction) {
		
		if(!$('#liveSearchResults li').hasClass('active')) {
			$('#liveSearchResults li:first').addClass('active');
		} else {
			var currentDD = $('#liveSearchResults li.active');
			
			// Which direction?
			switch(direction) {
				case 'up':
					if($('#liveSearchResults li:first').hasClass('active')) {
						$('#liveSearchResults li:last').addClass('active');
					} else {
						currentDD.prev().addClass('active');
					}
				break;
				case 'down':
					if($('#liveSearchResults li:last').hasClass('active')) {
						$('#liveSearchResults li:first').addClass('active');
					} else {
						currentDD.next().addClass('active');
					}
				break;
			}
			currentDD.removeClass('active');
		}
	}
	
	// Mouse navigation
	function mouseControl() {
		$('#liveSearchResults li a').mouseover(function() {
			var currentDD = $('#liveSearchResults li.active');
			$(this).parent().addClass('active');
			
			if($('#liveSearchResults li').hasClass('active')) {
				currentDD.removeClass('active');
			}
		});
	}
	
	
	function searchLoseFocus() {
		$("#liveSearchResults").fadeOut("fast");
	}
	
	$('#liveSearchSubmit').click(function() {
		var query = $('#liveSearchInput').val();
		if(query == "Search..." || !query) {
			window.location = '/music-events/search/';
		} else {
			window.location = '/music-events/search/?s='+query;
		}
	});

	// Capture all key presses on the input field
	$('#liveSearchInput').keydown(keyInput);
	
	// Hide results when focus is lost
	$('#liveSearchInput').blur(function() {
		
		
		
		searchLoseFocus();
		
	});
	
	// Show results when focus is back
	$('#liveSearchInput').focus(function() {
		
			this.value = '';
		
			//$('#liveSearchResults').fadeIn('fast');
		
	});
});
