/*
	AutoComplete.js
	
	Required for autocomplete functionality (unless you really want to build this again and again).
	
	Expected HTML:
	
	-- Field (ID and styling are variable)
	<input type="text" name="fAddRecipient" id="fAddRecipient" size="15" maxlength="200" class="StandardField" />
	
	-- List (ID and class are variable):
	<div class="RecipientAddList">
		<ul id="AddRecipientList"></ul>
	</div>
	
	Expected CSS:
	
		.RecipientAddList
		{
			position: absolute;				-- Required
			display: none;					-- Required
			width: 150px;					-- Property Required; Value Variable
			height: 200px;					-- Property Required; Value Variable
			padding: 5px 5px;				-- Variable
			border: 1px solid #999999;		-- Variable
			z-index: 900;					-- Required
			background-color: #FFFFFF;		-- Property Required; Value Variable
		}
		-- The following are required but the colors and spacing are variable.
		.RecipientAddList li
		{
			position: relative;
			margin: 2px 0px 2px 0px;
			padding: 3px 3px;
			display: block;
			color: #333333;
			cursor: pointer;
			background-color: #FFFFFF;
		}
		.RecipientAddList li.over
		{
			background-color: #333333;
			color: #FFFFFF;
		}
	
	
	
	
	Requirements:
	- Requires JQuery 1.3.x or better.
	
	Revision History:
	
	07/11/2009	kn		Created.	

*/

// Globals ================================
var gbACKeepListOpen = false;
var MessageTagsListLength = 50;


function gAutoC_Object( inListID, inFieldID, inFieldUpdateCallback, inSearchCallback )
{
	this.ListID = inListID;
	this.FieldID = inFieldID;
	this.FieldUpdateCallback = inFieldUpdateCallback;
	this.SearchCallback = inSearchCallback;
	
}//end gAutoC_Object definition

function gAutoC_InitList( inACObj )
{
	var sLIID = "";
	var sFID = "";
	
	if (inACObj == null)
		return;
	
	sLIID = "#" + inACObj.ListID;
	sFID = "#" + inACObj.FieldID;
	
	
	$(sFID).unbind("keyup")
	.bind("keyup", function() {
		
		inACObj.SearchCallback();
		
	});//end bind keyup
	
	$(sFID).unbind("blur")
	.bind("blur", function() {
		
		if (!gbACKeepListOpen)
			$(sLIID).parent().hide();
		
	});//end bind keyup
	
	$(sFID).unbind("focus")
	.bind("focus", function() {
		
		gbACKeepListOpen = true;
		inACObj.SearchCallback();
		
	});//end bind keyup
	
	
}//end gAutiC_InitList

function gAutoC_LoadList( inACObj, data, inType )
{
	var sHTML = '';
	
	var sLIID = "";
	
	if (inACObj == null || data == null)
		return;
		
	sLIID = "#" + inACObj.ListID;

	for(var i=0; i < data.length; i++)
	{
		var sItem = '';
		
		var sUID = data[i].ID;
		var sName = data[i].Name;
		var sSearchTerm = data[i].SearchTerm;
		
		
		var regex = new RegExp(sSearchTerm, "i");
		
		if (sName.match(regex) != null && sName.match(regex).length > 0)
		{
			var piece = sName.match(regex)[0];
			
			sName = sName.replace(piece, '<strong>' + piece + '</strong>');
			
		}//end if
		
		//sName = sName.replace(sSearchTerm, '<strong>' + sSearchTerm + '</strong>');
		switch(inType)
		{
			case "user" : 
				var photoID = data[i].PhotoID;
				var photoExt = data[i].PhotoExt;
				//var sPhotoURL = gMakeUserProfileAvatar(sUID, photoID, photoExt);
				var location = data[i].Location;
				sItem = '<li id="AD' + sUID + '"><span class="name">' + sName + 
					'</span><input type="hidden" class="PID" value='+photoID+
					' /><input type="hidden" class="PExt" value='+photoExt+
					' /><br /><span class="location" style="font-size: 11px;">'+location+'</span></li>\r\n';
				break;
			default : 
				sItem = '<li id="AD' + sUID + '"><span class="name">' + sName + '</span>'+
				'</li>\r\n';
		}
		
		
		sHTML += sItem;
		
	}//end for
	if(inType='tag' && data.length==MessageTagsListLength)
	{
		sHTML += '<li id="ignore">plus '+(data[0].TotalRows-MessageTagsListLength)+' more matches</li>';
	}
	
	if (sHTML.length == 0)
	{
		sHTML = '<li id="ignore">No matches found.</li>';
		
	}//end if
	
	$(sLIID).html(sHTML );
	
	gAutoC_RefreshHooks( inACObj );
	
	$(sLIID).parent().show();

}//end gAutoC_LoadList

function gAutoC_RefreshHooks( inACObj )
{
	var sLIID = "";
	var sFID = "";
	
	if (inACObj == null)
		return;
	
	sLIID = "#" + inACObj.ListID + " li";
	sFID = "#" + inACObj.FieldID;
	

	$(sLIID).unbind("mouseover mouseout blur click");
	$(sLIID).bind("mouseover", function() {
		
		if ($(this).attr("id") != "ignore")
			$(this).addClass("over");
				
		gbACKeepListOpen = true;
			
	})//end over
	.bind("mouseout", function() {
		
		$(this).removeClass("over");
		gbACKeepListOpen = false;

		
	})//end out
	.bind("click", function() {
		
		var sID = $(this).attr("id");
		var sName = $(this).find(".name").html();
		
		var sPhotoID = $(this).find(".PID").val();
		var sPhotoExt = $(this).find(".PExt").val();
		
		if (sID == "ignore")
			return;
			
		sID = sID.replace("AD", "");
		
		var regex = /<[\/]*strong>/gi;
		
		sName = sName.replace(regex, '');
		
		
		if (inACObj.FieldUpdateCallback != null)
			inACObj.FieldUpdateCallback( sName, sID, sPhotoID, sPhotoExt );
		else
			$(sFID).val(sName);
		
		$(sLIID)
		.parent()	// UL
		.parent()	// DIV
		.hide();
	
	});//end click


}//end gAutoC_RefreshHooks

