var app = app || {};

app.debugging = true;

app.init_done = false;

$(function(){
	app.globalsearch.init();
});

app.globalsearch = {
	init: function() {
		
		$(document).unbind('keydown.globalsearch').bind('keydown.globalsearch', function(e) {
			if (e.keyCode == 91 || e.keyCode == 224 || e.keyCode == 17) { /* cmd, ctrl */
				app.globalsearch.cmd_down = true;
			}
			
			if (e.keyCode == 70 && app.globalsearch.cmd_down === true) { /* cmd-f */
				$('#global-search input').focus();
				e.preventDefault();
			}
		});
		$(document).unbind('keyup.globalsearch').bind('keyup.globalsearch', function(e) {
			if (e.keyCode == 91 || e.keyCode == 224 || e.keyCode == 17) {
				app.globalsearch.cmd_down = false;
			}
		});
		
		$('#global-search input').unbind('keydown.globalsearch').bind('keydown.globalsearch', function(e) {
			if (e.keyCode == 27) { /* escape clears the search */
				$(this).val('');
			}
		});
	
		$('#global-search input')
			.unbind('blur.globalsearch').bind('blur.globalsearch', function(e) {
				if (app.globalsearch.hide_timeout != false) clearTimeout(app.globalsearch.hide_timeout);
				app.globalsearch.hide_timeout = setTimeout(function() { $('#global-search-results').fadeOut(100); }, 200);
			})
			.unbind('focus.globalsearch').bind('focus.globalsearch', function(e) {
				if (app.globalsearch.hide_timeout != false) clearTimeout(app.globalsearch.hide_timeout);
				if ($(this).val() != '' && $('#global-search-results .search-results li').length > 0) {
					$('#global-search-results').show();
				}
			})
			.unbind('keyup.globalsearch').bind('keyup.globalsearch', function(e) {
			
				if (e.keyCode == 27) { /* escape clears the search */
					if (app.globalsearch.timeout != false) clearTimeout(app.globalsearch.timeout);
					$('#global-search-results').empty().hide();
					$(this).val('');
					$(this).blur();
					e.preventDefault();
					return false;
				}
				
				else if (e.keyCode == 40) { /* arrow down */
					app.globalsearch.navigate_down(e);
					e.preventDefault();
					return false;
				}
				
				else if (e.keyCode == 38) { /* arrow up */
					app.globalsearch.navigate_up(e);
					e.preventDefault();
					return false;
				}
				
				else if (e.keyCode == 13) {
					app.globalsearch.navigate_into(e);
					e.preventDefault();
					return false;
				}
				
				else {
					if (app.globalsearch.timeout != false) clearTimeout(app.globalsearch.timeout);
					app.globalsearch.timeout = setTimeout(app.globalsearch.search, 200);
				}
			})
			.unbind('click.globalsearch').bind('click.globalsearch', function(e) {
				if ($(this).val()=='') {
					$('#global-search-results').hide();
					e.preventDefault();
				}
			});
		
		$('#global-search').die('submit').live('submit', function(e) {
			e.preventDefault();
		});
		
		$('#global-search-results .search-results li').die('mouseover.globalsearch').live('mouseover.globalsearch', function(e) {
			$(this).addClass('active').siblings('li').removeClass('active');
			app.globalsearch.active = $(this).data('index');
		});
		
	},
	timeout: false,
	hide_timeout: false,
	search: function() {
		$.ajax({
			type: 'POST',
			url: $('#global-search').attr('action'),
			data: $('#global-search').serialize(),
			success: function(results) {
				$('#global-search-results').empty();
				if ($(results).find('li').length > 0) {
					$(results).appendTo('#global-search-results');
					$('#global-search-results').css({
						top: '30px',
						right: $(window).width()-$('#global-search').position().left-$('#global-search').width()
					}).show();
					app.globalsearch.active = 0;
					app.globalsearch.results_total = $('#global-search-results .search-results').find('li').length;
					$('#global-search-results .search-results li').each(function(i, o) {
						$(this).data('index', i);
						//$(this).wijtooltip();
					});
				} else {
					$('#global-search-results').hide();
				}
			},
			cache: false,
			dataType: 'html'
		});
	},
	navigate_into: function(e) {
		if ($('#global-search-results .search-results li.active a').length) {
			var _url = $('#global-search-results .search-results li.active a').attr('href');
			document.location = _url;
		}
	},
	navigate_up: function(e) {
		var _new_index = app.globalsearch.active-1;
		if (app.globalsearch.results_total > 0) {
			if (_new_index < 0) _new_index = app.globalsearch.results_total-1;
			$('#global-search-results .search-results li').removeClass('active').eq(_new_index).addClass('active');
			app.globalsearch.active = _new_index;
		}
		app.debug(_new_index);
	},
	navigate_down: function(e) {
		var _new_index = app.globalsearch.active+1;
		if (app.globalsearch.results_total > 0) {
			if (_new_index > app.globalsearch.results_total-1) _new_index = 0;
			if (app.globalsearch.active == 0 && $('#global-search-results .search-results li').eq(0).hasClass('active') === false) _new_index = 0;
			$('#global-search-results .search-results li').removeClass('active').eq(_new_index).addClass('active');
			app.globalsearch.active = _new_index;
		}
		app.debug(_new_index);
	},
	active: 0,
	cmd_down: false
}
