// Custom auto complete to work with ex ed and txd
// Author : Rob Mogridge
// Date : 06/05/2011
// Updated By :
// Date :

// Requires jQuery

	var siteID = 1; // defaults to 1 (exelement)
	var target = ''; // the target id for the search -- get this from the targeted widget
	var listLink = '/ajax/ajax_autocomplete_rendered_list_v3.php'; // uri of the list resource
	var resultPage = '/search.php'; // the default results page, if a permalink is not captured
// timer for time delaying events
	var c = 0;
	var t;
function timedCount(targeta){		
	if(c > 2){ // 2 * 500ms + 500ms to be greater than = 1.5 second delay. Halts on mouseenter
		$(targeta + ' .ac_display').fadeOut('normal');
		clearTimeout(t);
		c=0;
	}else{
		c=c+1;
		t=setTimeout("timedCount("+targeta+")",500);
	}		
}

$(document).ready(function(){
	
	
	
	// arrow key navigation
	var listPosition = -1;
	var storedLink = ''; // href set with key selection
	var name = '';
	var searchTerms = ''; // search page keywords, replace space with +
	var listLength = 0;
	var hasFocus = false;
	
	
	$(target + ' .ac_input').keyup(function(e){
		
		switch(e.keyCode){
			case 40: // down key
				// calculate the list length
				listLength = $(target + ' a[id^="acl_"]').length;
				// iterate through the list
				if(listPosition < listLength){ // change 10 to a count of list items
					// fade in the display, incase it's hidden
					$(target + ' .ac_display').fadeIn('fast');
					listPosition++;	
					storedLink = $(target + ' .autocomplete_list a#acl_'+listPosition).attr('href');
					name = $(target + ' .autocomplete_list a#acl_'+listPosition).attr('title');
					$(target + ' .ac_input').val(name);
				}
				break;
			case 38: // up key
				if(listPosition > -1){
					// fade in the display, incase it's hidden
					$(target + ' .ac_display').fadeIn('fast');
					listPosition--;
					storedLink = $(target + ' .autocomplete_list a#acl_'+listPosition).attr('href');
					name = $(target + ' .autocomplete_list a#acl_'+listPosition).attr('title');
					$(target + ' .ac_input').val(name);
				}
				break;
			case 13: // return key
				if(storedLink != ''){ // if we have a stored permalink
					window.location = storedLink;	
				}
				else{ // else visit search results page
					searchTerms = $(target + ' .ac_input').val();
					searchTerms = searchTerms.replace(/ /g,'+'); // replace all spaces with a plus
					window.location = resultPage+'?Keywords='+searchTerms;
				}
				break;
			default :
				$(target + ' .ac_display').load(listLink+'?SiteID='+siteID+'&r=0&q='+encodeURI($(this).val()),function(){
					storedLink = '';
					$(target + ' .ac_display').fadeIn('slow');	
					listPosition = -1;
				});	
		}
		$(target + ' .autocomplete_list a').removeClass('highlight');
		$(target + ' .autocomplete_list a#acl_'+listPosition).addClass('highlight');
	});
	// use blur trigger to fade out the list
	$(target + ' .ac_input').blur(function(){
		$(target + ' .ac_display').fadeOut('normal');	
		hasFocus = false;
	});
	$(target + ' .ac_input').focus(function(){
		target = $(this).parent().attr('id'); // assign the parent id to the current target
		target = '#' + target;
		listPosition = -1;
		$(target + ' .ac_display').fadeIn('slow');
		hasFocus = true;
		
		
	//	alert(target);
	});
	$(target + ' .ac_input').click(function(){
		$(target + ' .ac_display').fadeIn('slow');					 
	});
//	$('.autocomplete_list a').click(function(){
//		$('.ac_input').val($(this).text());					 
//	});
	
	// functions to reopen the drop down on hover but close it on mouse out IF we do not have focus
/*	$(target + ' .ac_container').mouseleave(function(){
		if(hasFocus == false){
			timedCount(target);
		}
		
	});
	$(target + ' .ac_container').mouseenter(function(){
		c = 0;
		clearTimeout(t);
		$(target + ' .ac_display').fadeIn('normal');
	});*/
	//using submit button
	$(target + ' .ac_submit').click(function(){
		if(storedLink != ''){ // if we have a stored permalink
			window.location = storedLink;	
		}
		else{ // else visit search results page
			searchTerms = $(target + ' .ac_input').val();
			searchTerms = searchTerms.replace(/ /g,'+'); // replace all spaces with a plus
			window.location = resultPage+'?Keywords='+searchTerms;
		}
	});
	//
});

