/*

	
	This is the JS necessary to construct the autocomplete functionality in the message compose page

	Make sure that the HTML structure is as in message_compose.php. 
	
	Setting the user_ID from the form is not a security vulnerablity because it's only used for excluding IDs and these are cast to INT anyway.

	This implementation deviates from the way in which you would expect this to work based on the original documentation.
	Here there be dragons.	
	Particular attention should be paid to the binding of the keydown for the ENTER key

	Original Doc: http://www.pengoworks.com/workshop/jquery/autocomplete_docs.txt
	
	

*/

	var ac; //Autocompleter object
	var excludeObj = new Object(); //Excluding IDs object
	var user_ID;
	
	excludeObj['count'] = 0;
	
	
	function findValue(li) {	
		var li_val = li.innerHTML; //Get the HTML of the list item that was selected
		
		//Pull out the autocomplete ID out of the HTML
		var autocomplete_ID = li_val.substring(li_val.indexOf(">|",15)+2,li_val.lastIndexOf('|<')) * 1;
		
		appendToRecipients(autocomplete_ID);
	}
	
	function appendToRecipients(autocomplete_ID){
		//If this item has already been inserted, don't insert it again (the same ID shouldn't be shown twice [or they'd receive the email twice = bad])
		if(0 == $('ul#recipients li#'+autocomplete_ID).size()){
			if(document.forms[0].autocomplete.value.length>1){
				$('ul#recipients').append("<li class='to' id='"+autocomplete_ID+"'><input type='checkbox' name='to_user_ID"+$('ul#recipients li').size()+"' class='checkbox' value='"+autocomplete_ID+"' checked>"+document.forms[0].autocomplete.value+"</li>");		
				
				//Update the limit that will later be given to the email script so it knows how many people to email
				document.forms[0].limit.value = $('ul#recipients li').size();
				
				//Make sure this item isn't displayed again in subsequent searches
				excludeObj['count']++;
				excludeObj['e'+excludeObj['count']] = autocomplete_ID;
				
				ac[0].autocompleter.setExtraParams(excludeObj);
				ac[0].autocompleter.flushCache();
			}

		}
		
		clear();

	}
	
	function clear(){
		//Clear the autocomplete text box + cache
		document.forms[0].autocomplete.value = "";
		
		//Clear the results list
		if($('.ac_results').is(":visible"))
			$('.ac_results').hide();
	}
	
	function selectItem(li) {
		findValue(li);
	}
	
	function formatItem(row) {
		return row[0]+"<span class='hidden'>|"+row[1]+"|</span>";
	}
	$(document).ready(function() {
		//Kill off the enter key for the autocomplete => Not doing this will make for plenty of accidental emails
		$(document).bind("keypress keydown", function(e) {
			if (e.keyCode == 13 || e.which == 13){ //ENTER key
				if(document.forms[0].autocomplete.value !=''){
					 $.ajax({
						type: "GET",
						url: "includes/jquery_ac/find.php",
						data: "q="+document.forms[0].autocomplete.value,
						success: function(msg){
							var autocomplete_ID = msg*1;
							if(excludeObj['e'+excludeObj['count']] != autocomplete_ID){ //Don't bother adding it twice
								if(autocomplete_ID>0)
									appendToRecipients(autocomplete_ID);
								else
									clear();
							}
						}
					});
				}
				return false;
			}
		});

		user_ID = document.forms[0].user_ID.value * 1;
		excludeObj['e0'] = user_ID; //Make sure that the user doesn't seem themselves as a potential recipient

		ac = $("#autocomplete").autocomplete(
			"includes/jquery_ac/search.php",
			{
				delay:50,
				minChars:3,
				matchContains:1,
				extraParams:{e0:user_ID,count:0},
				cacheLength:10,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
		
	});
	
