this.qsearch = new qsearch();
function qsearch() {
    var highlighted;
    var resultsOutput;
    var inputObject;
    var rowIndex = -1;
    var navigateObject = '';
    function cancelBubbleFunction(e) {
        if (resultsOutput != null)
            resultsOutput.style.display = "none";
    }       
    function clearSearchFunction(element, defaultString) {
        if (element.value == defaultString)
            element.value = "";
    }
    function highlightFunction(element) {
        if (highlighted != null)
            highlighted.style.backgroundColor = "";           
        highlighted = element;
        element.style.backgroundColor = "#DDDDDD";
    }              
    function exploreObjectFunction(element) {
        resultsOutput.style.display = "none";
        exploreMoreFunction(element.getAttribute("objectid"), element.getAttribute("objecttype"));
    }
    function navigateToLinkFunction(element) {
        resultsOutput.style.display = "none";
        inputObject.value = element.getAttribute("title");
        self.location = element.getAttribute("link");
        //saveSearch();      
    }
    function exploreMoreFunction( exploreNodeValue, type ) {
        var explorer = document.getElementById('movieMine');
        if (explorer != null) {
            if (type == 'Movie') {
                explorer.setExplorerMovie(exploreNodeValue);
                if (typeof removeReleaseDateFocus != 'undefined')
                    removeReleaseDateFocus();
            }
            else if (type == 'Person') {
            explorer.setExplorerPerson(exploreNodeValue);
            if (typeof removeReleaseDateFocus != 'undefined')
                removeReleaseDateFocus();
            }
            else if (type == 'releasedate')
                explorer.exploreReleaseDate(exploreNodeValue);
        }
        return false;
    }
    function populateSearchFieldFunction(e) {       
        try
        {
            switch (e.keyCode)
            {
                case 13:
                    {
                        if (resultsOutput != null && resultsOutput.childNodes != null) {
                            if (resultsOutput.childNodes.length > rowIndex) {
                                inputObject.value = resultsOutput.childNodes[rowIndex].getAttribute("title");
                                navigateFunction(resultsOutput.childNodes[rowIndex]);
                            }
                        }
                        break;
                    }
            case 38:
                {
                    if (resultsOutput != null && resultsOutput.childNodes != null && rowIndex > 0) {
                        rowIndex--;
                        highlightFunction(resultsOutput.childNodes[rowIndex]);
                    }
                    break;
                }
                case 40:
                {
                    if (resultsOutput != null && resultsOutput.childNodes != null && rowIndex < resultsOutput.childNodes.length - 1) {
                        rowIndex++;
                        highlightFunction(resultsOutput.childNodes[rowIndex]);
                    }
                    break;
                }
            }
        }
        catch(err) { }            
    }
    function refreshResultsFunction(obj, resultsOut, type, navigateFunctionName, e) {
        if (obj != null && obj.value != "") {
            navigateObject = navigateFunctionName;
            if (e.type == "keyup") {
                if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13) {
                    resultsOutput = resultsOut;
                    inputObject = obj;
                    var loadPage = '/services/Search/QuickSearch.aspx?format=html&type=' + type + '&keyword=' + encodeURIComponent(obj.value);
                    $(resultsOutput).load(loadPage, showNewContent); //, showNewContent).show('slow');
                }
                else {
                    populateSearchFieldFunction(e);
                }
            }
            else {
                    resultsOutput = resultsOut;
                    inputObject = obj;
                    var loadPage = '/services/Search/QuickSearch.aspx?format=html&type=' + type + '&keyword=' + encodeURIComponent(obj.value);
                    $(resultsOutput).load(loadPage, showNewContent);//, showNewContent).show('slow');
            }
        }
    }
    function showNewContent() { 
        if ($(resultsOutput) != null && $(resultsOutput).text() != "") {
            rowIndex = -1;
            //resultsOutput.style.top = (parseInt(inputObject.offsetTop) + 12) + "px";
            resultsOutput.style.display = "inline";
        } else if ($(resultsOutput) != null) {
            resultsOutput.style.display = "none";
        }
    }
    function navigateFunction(element) {
        inputObject.value = element.getAttribute("title");
        var objectId = $('input.object-id');
        if (objectId != null) objectId.val(element.getAttribute("objectid"));
        var objectType = $('input.object-type');
        if (objectType != null) objectType.val(element.getAttribute("objecttype"));

        if (navigateObject == "navigateToLink") {
            navigateToLinkFunction(element);
        }
        else if (navigateObject == "exploreObject") {
            exploreObjectFunction(element);
        }
    }
    this.cancelBubble = cancelBubbleFunction;
    this.clearSearch = clearSearchFunction;
    this.highlight = highlightFunction;
    this.navigateToLink = navigateToLinkFunction;
    this.exploreObject = exploreObjectFunction;
    this.populateSearchField = populateSearchFieldFunction;
    this.refreshResults = refreshResultsFunction;
    this.navigate = navigateFunction;
    this.exploreMore = exploreMoreFunction;
}
