/**
 * @desc Gathers onReady functions that can be applied
 * to jQuery's queue.
 * 
 * @author Steven Rosato
 */
$(document).ready(function() {
	/**
	 * Bouncing
	 */
	
	/* Bounce all flash messages, then ide them using the idle plugin */
	$(".flash-messages")
		.effect("bounce")
		.idle(30000)
		.fadeTo('slow',0.001,function(){ 
			$(this).slideUp('fast', function() {
				 $(this).remove(); 
			});
		});
	
	/**
	 * Input text hints
	 */
	
	/* Make all input who have a title appear as a hint inside it */
	$('input.hint[title!=""]').hint("hint", true);
	
	/**
	 * Tooltips
	 */
	
	$('a.tooltip[title!=""]').tooltip({showURL: false, fade: 250});
	
	/* show extra infos on listed articles */
	$(".article-link").tooltip({
		showURL: false,
		delay: 500,
		fade: 250,
		bodyHandler: function() {
			return $(this).next().html();
    	}
	});
	
	/**
	 * Autocomplete
	 */
	
	$("#s").autocomplete(Application.url + '/search/ajax/', {
		minChars: 3,
		width: 250,
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.text,
					result: row.text
				}
			});
		},
		formatItem: function(row, i, max, term) {
			string = row.text.replace(new RegExp("(" + term + ")", "gi"), "<strong>$1</strong>") + "<br /><span style='font-size: 80%;'>Type: " + row.type + "</span>";
			
			if( row.free && row.free == 1 )
				string += ' <span style="color:green;">' + row.freeTranslated + '!</span>';
			return string;
		}
	}).result( function(event, data, formatted) {
		if( data ) {
			$('#search_submit').attr('disabled', 'disabled');
			window.location = data.url;
		}
	});
});
