/**
 * Functions related to search
 */

//Create the Profile namespace PRO
if (KM.SER == null || typeof(KM.SER) != "object") { KM.SER = new Object();}

KM.SER = {
		
	/*
	 * Initiate the datepicker for the search box
	 */
	 
	 initCalendar: function () {
		if ($('#date').length > 0) {
			$('#date').datepicker({
				dateFormat: 'yy-mm-dd',
	            firstDay: 1,
				numberOfMonths: 2,
			});
			$('#date').click(function() {
				//Hack for z-index bug, also exists with ui 1.8.4 seemingly http://dev.jqueryui.com/ticket/5765
				$('div#ui-datepicker-div').css("z-index", '99999'); 
			});
		}
	 
	 },

	/*
	 * Initiate the browse buttons
	 */
	 
	 initBrowse: function () {

		$('#activities-anchor').click(function(e) {
			e.preventDefault();
			$('div#activities').slideToggle('slow'); 
			$('a#activities-anchor').toggleClass('selected');
		});

		$('#locations-anchor').click(function(e) {
			e.preventDefault();
			$('div#locations').slideToggle('slow'); 
			$('a#locations-anchor').toggleClass('selected');
		});	 
	 },
	
	/*
	 * Initiate the forms to submit via AJAX using jquery.ajaxform.js
	 */
	initForms: function() {

		//Set forms to submit via AJAX and update search-results if that div exists on the page
		if ($('#search-results').length > 0) {
			var options = { 
		        target:         '#search-results',
		        clearForm: 	    false,
		        cache:			false,
		        beforeSend: 	function() {
					//$('#search-results').html('');
					$('#results-loading').show();
					$('#search-results').animate({ opacity: "0.4" }, "fast");
					$('.submit').animate({ opacity: "0.5" }, "fast");
				},
				complete:		function() {
					$('#results-loading').hide();
					$('#search-results').animate({ opacity: "1" }, "fast");
					$('.submit').animate({ opacity: "1" }, "fast");
				}
		    }; 
		 
		    // bind form using 'ajaxForm' 
		    $('#SupplierSearchForm').ajaxForm(options);
		    $('#ActivitySearchForm').ajaxForm(options);
		}
	},
	
	/*
	* Set the default content for the search box
	*/
	setDefaultContent: function() {
		$('#ActivityQuery').DefaultValue("activity, country");
		$('#ActivityQuery').focus(function() {
		    $(this).css("color","#333333");
		});
	},
	
	/*
	* Initiate the autocomplete function for the search box
	*/
	initAutocomplete: function() {

		function split( val ) {
			return val.split( /,\s*/ );
		}
		function extractLast( term ) {
			return split( term ).pop();
		}
		
		$("#ActivityQuery")
			// don't navigate away from the field on tab when selecting an item
			.bind( "keydown", function( event ) {
				if ( event.keyCode === $.ui.keyCode.TAB &&
						$( this ).data( "autocomplete" ).menu.active ) {
					event.preventDefault();
				}
			})
			.autocomplete({
				source: function( request, response ) {
					$.getJSON( "/search/suggest", {
						term: extractLast( request.term )
					}, response );
				},
				search: function() {
					// custom minLength
					var term = extractLast( this.value );
					if ( term.length < 3 ) {
						return false;
					}
				},
				focus: function() {
					// prevent value inserted on focus
					return false;
				},
				select: function( event, ui ) {
					var terms = split( this.value );
					// remove the current input
					terms.pop();
					// add the selected item
					terms.push( ui.item.value );
					// add placeholder to get the comma-and-space at the end
					terms.push( "" );
					this.value = terms.join( ", " );
					return false;
				}
			});
	},	
			
};

$(document).ready(function(){
	//KM.SER.initTabs();
	KM.SER.initForms();
	KM.SER.setDefaultContent();
	KM.SER.initCalendar();
	KM.SER.initBrowse();
	KM.SER.initAutocomplete();
});

