﻿
    this.quicksearch = new quicksearch();

    function quicksearch()
    {

        var highlighted;
        var rowIndex = -1;
        
        function cancelBubbleFunction(e)
        {
            e.cancelBubble = true;
            document.getElementById("searchOutput").style.display = "none";
        }
        
        function clearSearchFunction(element)
        {
        
            if (element.value == "Movies, People, Videos")
                element.value = "";
        
        }
        
        function highlightFunction(element)
        {
        
            if (highlighted != null)
                highlighted.style.backgroundColor = "";
             
            highlighted = element;
            element.style.backgroundColor = "#DDDDDD";
        
        }

        
        function invokeSearch() {
            
            var txtSearch = window.document.getElementById("terms").getElementsByTagName("input")[0];
           
            if (txtSearch.value != "")
            {
			    if(txtSearch.value != "Movies, People, Videos")
			    {
			        document.location = BASE_URL + "search.aspx?keywords=" + encodeURIComponent(txtSearch.value);
				    saveSearch();
			    }
			    else
			    {
				    txtSearch.focus();
			    } 
            }
            return false;
	    }
        
        function navigateToLinkFunction(element)
        {
            
            document.getElementById("searchOutput").style.display = "none";
            document.getElementById("terms").getElementsByTagName("input")[0].value = element.getAttribute("title");
            self.location = element.getAttribute("link");
            saveSearch();
           
        
        }
        
        function populateSearchFieldFunction(e)
        {
        
            try
            {
            
                switch (e.keyCode)
                {
                
                    case 9: 
                    {
                       
                        if (document.getElementById("searchOutput").childNodes != null)
                        {
                            
                            if (document.getElementById("searchOutput").childNodes.length > 0)
                            {
                                
                                document.getElementById("terms").getElementsByTagName("input")[0].value = document.getElementById("searchOutput").childNodes[0].getAttribute("title");
                                navigateToLinkFunction(document.getElementById("searchOutput").childNodes[0]);
                                
                            }
                        }
                        break;
                    
                    }
                    case 13:
                    {
                       
                        searchFunction(e);
                        break;
                    
                    }
                    case 38:
                    {
                    
                        if (document.getElementById("searchOutput").childNodes != null && rowIndex > 0)
                        {
                            rowIndex--;
                            highlightFunction(document.getElementById("searchOutput").childNodes[rowIndex]);
                           
                        }
                        break;
                    
                    }
                    case 40:
                    {
                        if (document.getElementById("searchOutput").childNodes != null && rowIndex < document.getElementById("searchOutput").childNodes.length - 1)
                        {
                            rowIndex++;
                            highlightFunction(document.getElementById("searchOutput").childNodes[rowIndex]);
                           
                        }
                        break;
                    }
                }
            
            }
            catch(err) { }
            
        }
        
        function refreshResultsFunction(e)
        {
             
            if (document.getElementById("terms").getElementsByTagName("input")[0].value != "" && document.getElementById("terms").getElementsByTagName("input")[0].value != "Movies, People, Videos")
            {
              
                if (e.type == "keyup")
                {
                    
                    if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13)
                    {
                       
                        xmlHttp = getXmlHttpObject();
                        xmlHttp.onreadystatechange = refreshResultsCallback;
                        xmlHttp.open("GET", BASE_URL + "webservices/quicksearch.asmx/GetQuickSearchResults?keyword=" + encodeURIComponent(document.getElementById("terms").getElementsByTagName("input")[0].value), true);
                        xmlHttp.send(null);
                    }
               
                }
                else
                {
                        xmlHttp = getXmlHttpObject();
                        xmlHttp.onreadystatechange = refreshResultsCallback;
                        xmlHttp.open("GET", BASE_URL + "webservices/quicksearch.asmx/GetQuickSearchResults?keyword=" + encodeURIComponent(document.getElementById("terms").getElementsByTagName("input")[0].value), true);
                        xmlHttp.send(null);
                }
            }
        
        }
        
        function refreshResultsCallback()
        {
           
            if (xmlHttp.readyState == 4)
            {
                    
                if (xmlHttp.status == 200)
                {
                 
                     document.getElementById("searchOutput").innerHTML = getReturnValue("string");
                     
                    if (getReturnValue("string") != "")
                    {
                    
                        rowIndex = -1;
                      
                        document.getElementById("searchOutput").style.left = parseInt(document.getElementById("search").offsetLeft) + parseInt(document.getElementById("terms").getElementsByTagName("input")[0].offsetLeft) + "px";
                        document.getElementById("searchOutput").style.top = (parseInt(document.getElementById("search").offsetTop) + parseInt(document.getElementById("terms").getElementsByTagName("input")[0].offsetTop) + 30) + "px";
                        document.getElementById("searchOutput").style.display = "inline";
                        
                    }
                    else
                    {
                        document.getElementById("searchOutput").style.display = "none";
                    }
                    
                }
                   
            }

        }
        
        function saveSearch()
        {
        
           xmlHttp = getXmlHttpObject();
           xmlHttp.open("GET", BASE_URL + "webservices/quicksearch.asmx/SaveSearch?keyword=" + document.getElementById("terms").getElementsByTagName("input")[0].value, true);
           xmlHttp.send(null);
        
        }
        
        function searchFunction(e)
        {
          
           if ((document.getElementById("terms").getElementsByTagName("input")[0].value != "" && rowIndex == -1) || e.type == "mousedown")
           {
              
               invokeSearch();
               
           }
           else
           {
               
               navigateToLinkFunction(document.getElementById("searchOutput").childNodes[(rowIndex == -1 ? 0 : rowIndex)]);
                
           }
            
            return false;
        
        }
        
        this.cancelBubble = cancelBubbleFunction;
        this.clearSearch = clearSearchFunction;
        this.highlight = highlightFunction;
        this.navigateToLink = navigateToLinkFunction;
        this.populateSearchField = populateSearchFieldFunction;
        this.refreshResults = refreshResultsFunction;
        this.search = searchFunction;
        
}
