/**
*	Search string suggestions
**/
SearchStringSuggestions = {
	//Variables
	PanelHtml : "<TABLE id=\"tableSearchStringSuggestions\" style=\"visibility:hidden\" class=\"search-string-suggestions\"  cellSpacing=0 cellPadding=0><TBODY></TBODY></TABLE>" 
	,SearchStringInputId 	: "global_search_string"
	,SearchFormId			: "global_search"	
	,SearchStrings : [] 
	,MaxCount : 10 //How many suggestions should be displayed at once?
	,Active: false //true on display
	
	//Objects
	,Panel				: null
	,SearchStringInput 	: null
	,SearchForm			: null
	,HidingTimer		: null
	,SelectedRow		: null
	
	//Methods
	,Init: function ()
	{
		//Import stylesheets
		importStylesheet (getVirtualPath() + "intranet/css/searchList.css", "searchList");
		
		//Get the search strings
		SearchStringSuggestions.GetSearchStrings();
		//Init the search strings list panel
		SearchStringSuggestions.Panel = $("tableSearchStringSuggestions");
		if (!SearchStringSuggestions.Panel)
		{
			document.body.insertAdjacentHTML("beforeEnd", SearchStringSuggestions.PanelHtml);
			SearchStringSuggestions.Panel = $("tableSearchStringSuggestions");
		}
		
		//Init the search string input box
		SearchStringSuggestions.SearchStringInput =  $(SearchStringSuggestions.SearchStringInputId);
		if (!SearchStringSuggestions.SearchStringInput) return;
		
		//Init the global search form
		SearchStringSuggestions.SearchForm =  $(SearchStringSuggestions.SearchFormId);
		if (!SearchStringSuggestions.SearchForm) return;
		
		//Deactivate automcomplete
		try{SearchStringSuggestions.SearchStringInput.autocomplete="off"}catch(err){};
		
		//Add ontype event
		addEvent (SearchStringSuggestions.SearchStringInput, ["ontype", "onkeyup", "onkeydown"], SearchStringSuggestions.CheckKey);
		
		//Add onblur event
		addEvent (SearchStringSuggestions.SearchStringInput, "onblur", SearchStringSuggestions.StartHiding);
	}
	//Get search strings
	,GetSearchStrings : function ()
	{
		if (!isMS()) 
		{
			if (window.XMLHttpRequest)
	        {
	            var xmlHttpRequest = new XMLHttpRequest();
	            
	            if (xmlHttpRequest.readyState == null)
	            {
	                xmlHttpRequest.readyState = 1;
	                xmlHttpRequest.addEventListener("load",
	                    function()
	                    {
	                        xmlHttpRequest.readyState = 4;
	                        if (typeof(xmlHttpRequest.onreadystatechange) == "function")
	                        {
	                            xmlHttpRequest.onreadystatechange();
	                        }
	                    },
	                    false);
	            }
	            
	            xmlHttpRequest.open("POST",getVirtualPath() + "intranet/WebService/Search.asmx/SearchStrings", true);
	            xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    			
	            xmlHttpRequest.onreadystatechange = function()
		        {
		            if (xmlHttpRequest.readyState == 4)
		            {
		                var jsText = "SearchStringSuggestions.SearchStrings= [" + 
							xmlHttpRequest.responseText + "]";
							
						try {
						
						SearchStringSuggestions.SearchStrings = window.eval (jsText);}catch (err){
							}
		            }
		        }
		        
		        xmlHttpRequest.send();
	        }
		
		}
		else
		{
			var xmlHttpRequest =new ActiveXObject("Microsoft.XMLHTTP");
			xmlHttpRequest.open("POST",getVirtualPath() + "intranet/WebService/Search.asmx/SearchStrings" ,true);
			xmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
			xmlHttpRequest.onreadystatechange = function (e) {
				if (xmlHttpRequest.readyState == 4)
				{
					var jsText = "SearchStringSuggestions.SearchStrings= [" + 
						xmlHttpRequest.responseText + "]";
					
					try {SearchStringSuggestions.SearchStrings = window.eval (jsText);}catch (err){}
				}
			};
			xmlHttpRequest.send();
		}
	}
	//Check key pressed
	,CheckKey : function (e)
	{
		if (!e) return; 
		//Check key type

		if (e.keyCode == JSO.EventManager.KeyCodes.down) 		//Navigate down
		{
			if (e.type!="keydown")
			{
				SearchStringSuggestions.Navigation.Down();
				return false;
			}
			else
				return true;
			
		}
		else if (e.keyCode == JSO.EventManager.KeyCodes.up) 		//Navigate up
		{
			if (e.type!="keydown")
			{
				SearchStringSuggestions.Navigation.Up();
				return false;
			}
			else
				return true;
		}
		else if (e.keyCode == JSO.EventManager.KeyCodes.enter) 		//Select
		{
			if (e.type=="keydown")
			{
				e.cancelBubble = true;
				SearchStringSuggestions.Navigation.Select();
				return true;
			}
			else
				return true;
			
		}
		else if (e.type!="keyDown")
			SearchStringSuggestions.DisplaySuggestions(e);
		
	}
	//Displaying the suggestions list
	,DisplaySuggestions : function (e)
	{
		if (!SearchStringSuggestions.Panel) return;
		if (!SearchStringSuggestions.SearchStrings) return;
		
		//Stop the previous hiding timer
		SearchStringSuggestions.StopHiding();
		
		//Get the typed string
		var typedString = (SearchStringSuggestions.SearchStringInput.value||"").toLowerCase();
		if (typedString=="") return SearchStringSuggestions.StartHiding();
		
		SearchStringSuggestions.Panel.removeChild (SearchStringSuggestions.Panel.firstChild);
		SearchStringSuggestions.Panel.appendChild (document.createElement ("TBODY"));
		
		var foundCount = 0;
		for (var i=0; i < SearchStringSuggestions.SearchStrings.length; i++)
		{
			
			var searchString = SearchStringSuggestions.SearchStrings [i].toLowerCase();
			//Check if matches
			if (!searchString.startsWith (typedString)) continue;
			var tr = SearchStringSuggestions.Panel.insertRow (SearchStringSuggestions.Panel.rows.length);
			tr.searchString = searchString;
			
			var td = tr.insertCell (0);
			td.innerHTML = searchString;
			
			//Add events
			addEvent (tr, ["mouseover","mouseout"], SearchStringSuggestions.MouseEvent);
			addEvent (tr, ["click"], function (e){SearchStringSuggestions.SelectItem (e,null,true);});
			
			foundCount ++;
			if (foundCount >= SearchStringSuggestions.MaxCount) break;
		}
		if (foundCount>0)
		{
			//Close button
			var tr = SearchStringSuggestions.Panel.insertRow (SearchStringSuggestions.Panel.rows.length);
			tr.className = "close"
			var td = tr.insertCell (0);
			
			td.innerHTML = "<span onclick=\"SearchStringSuggestions.StartHiding()\">" + 
				"Schließen</span>";
			SearchStringSuggestions.PositionPanel ()
			SearchStringSuggestions.Panel.style.visibility = "visible";
			SearchStringSuggestions.Active = true;
		}	
		else
		
			SearchStringSuggestions.StartHiding();
		
	}
	//Hide the list
	,PositionPanel : function ()
	{
		if (!SearchStringSuggestions.Panel) return;
		var pos = getObjectPosition3 (SearchStringSuggestions.SearchStringInput);
		var left = pos [0];
		var top =  pos [1];
		var inputSize = getObjectSize (SearchStringSuggestions.SearchStringInput);
		top += inputSize[1];
		
		var panelSize = getObjectSize (SearchStringSuggestions.Panel);
		var clientSize = getClientSize();
		if ( (panelSize [0] + left) > clientSize [0])
			left = pos [0] + inputSize [0] - panelSize [0];
			
		SearchStringSuggestions.Panel.style.left = left;
		SearchStringSuggestions.Panel.style.top = top;
	}
	//Hide the list
	,HideSuggestions : function ()
	{
		if (!SearchStringSuggestions.Panel) return;
		SearchStringSuggestions.Panel.style.visibility = "hidden";
		SearchStringSuggestions.Active = false;
	}
	//Start the hiding timer	
	,StartHiding : function ()
	{
		SearchStringSuggestions.StopHiding();
			
		SearchStringSuggestions.HidingTimer = window.setTimeout (SearchStringSuggestions.HideSuggestions, "300");	
	}
	//Stop the hiding timer
	,StopHiding : function ()
	{
		if (SearchStringSuggestions.HidingTimer)
			window.clearTimeout (SearchStringSuggestions.HidingTimer);
	}
	
	//Navigating through the list
	,Navigation : {
		
		Down : function()
		{
			if (!SearchStringSuggestions.Panel) return;
			if (SearchStringSuggestions.Panel.rows.length<=1) return;
			
			var nextRow = null;
			//Unselect the previous row
			if (SearchStringSuggestions.SelectedRow)
				SearchStringSuggestions.SelectedRow.className = " ";
				
			if (!SearchStringSuggestions.SelectedRow || SearchStringSuggestions.Panel.rows.length == 2)
				nextRow = SearchStringSuggestions.Panel.rows [0];
			else 
			{
				if (SearchStringSuggestions.SelectedRow.nextSibling && SearchStringSuggestions.SelectedRow.nextSibling.className == "close")
					nextRow = SearchStringSuggestions.Panel.rows [0];
				else
					nextRow = SearchStringSuggestions.SelectedRow.nextSibling;
			}
			nextRow.className = "highlight";
			
			SearchStringSuggestions.SelectedRow = nextRow;
		}
		,Up : function()
		{
			if (!SearchStringSuggestions.Panel) return;
			if (SearchStringSuggestions.Panel.rows.length<=1) return;
			
			var nextRow = null;
			//Unselect the previous row
			if (SearchStringSuggestions.SelectedRow)
				SearchStringSuggestions.SelectedRow.className = " ";
				
			if (!SearchStringSuggestions.SelectedRow || SearchStringSuggestions.Panel.rows.length == 2)
				nextRow = SearchStringSuggestions.Panel.rows [0];
			else 
			{
				if (!SearchStringSuggestions.SelectedRow.previousSibling)
					nextRow = SearchStringSuggestions.Panel.rows [SearchStringSuggestions.Panel.rows.length-2];
				else
					nextRow = SearchStringSuggestions.SelectedRow.previousSibling;
			}
			nextRow.className = "highlight";
			
			SearchStringSuggestions.SelectedRow = nextRow;
		}
		,Select : function()
		{
			if (!SearchStringSuggestions.Active) return;
				if (SearchStringSuggestions.SelectedRow)
					SearchStringSuggestions.SelectItem(null, SearchStringSuggestions.SelectedRow)
			
		}
	}
	//Events
	,MouseEvent: function (e)
	{
		if (!e)return;
		//Type?
		var eventType = e.type;
		var tr = getEventSource(e);
		if(!tr) return;
		if (tr.tagName=="TD") tr = tr.parentNode;
		
		//Highlight
		if (eventType =="mouseover")
		{
			tr.className = "highlight";
			SearchStringSuggestions.SelectedRow = tr;
		}
		else
		{
			if (SearchStringSuggestions.SelectedRow == tr)
				SearchStringSuggestions.SelectedRow = null;
			tr.className = " ";
		}
	}
	//Select item
	,SelectItem : function (e, tr, submit)
	{
		if (!SearchStringSuggestions.Active) return;
		
		SearchStringSuggestions.StopHiding();
		
		if (!e && !tr) return;
		if (e && !tr)
		{
			var tr = getEventSource(e);
			if(!tr) return;
			if (tr.tagName=="TD") tr = tr.parentNode;
		}
		var searchString = tr.searchString;
		
		SearchStringSuggestions.SearchStringInput.value = searchString;
		
		SearchStringSuggestions.StartHiding();
		
		if (submit)
			SearchStringSuggestions.SearchForm.submit();
	}
};

if (!window.document || window.document.readyState!="complete")
	addEvent (window, "onload", SearchStringSuggestions.Init);
else
	SearchStringSuggestions.Init ();


