
var QueryACContext =
{
    HealthInterestDataSource:null,
    HealthInterestAutoComplete:null,
    init:function()
    {
        // An XHR DataSource
        //this.HealthInterestDataSource = new YAHOO.widget.DS_XHR('getHealthInterestAC.do', ["\n", "\t"]);
        this.HealthInterestDataSource = new YAHOO.util.XHRDataSource('/vitamin-supplement/getHealthInterestAC.do');
        //this.HealthInterestDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
        this.HealthInterestDataSource.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;
        this.HealthInterestDataSource.responseSchema = {recordDelim: "\n", fieldDelim: "\t"};
        //this.HealthInterestDataSource.queryMatchCase = false;
        this.HealthInterestDataSource.maxCacheEntries = 30;
        //this.HealthInterestDataSource.queryMatchSubset = false;
/*
        this.HealthInterestDataSource.queryQuestionMark = false;
        this.HealthInterestDataSource.scriptQueryParam = "prefix";
        this.HealthInterestDataSource.scriptQueryAppend = "query="+CurrentQueryContext.composeQuery();
*/
        this.HealthInterestAutoComplete = new YAHOO.widget.AutoComplete("HIACInput","HIACContainer",QueryACContext.HealthInterestDataSource);
        this.HealthInterestAutoComplete.queryMatchCase = false;
        this.HealthInterestAutoComplete.queryMatchSubset = false;
        this.HealthInterestAutoComplete.maxResultsDisplayed = 30;
        this.HealthInterestAutoComplete.minQueryLength = 1;
        this.HealthInterestAutoComplete.queryDelay = 0;
        this.HealthInterestAutoComplete.autoHighlight = true;
        this.HealthInterestAutoComplete.prehighlightClassName = 'yui-ac-prehighlight';
        this.HealthInterestAutoComplete.useShadow = true;
        this.HealthInterestAutoComplete.forceSelection = true;
        this.HealthInterestAutoComplete.typeAhead = false;
        this.HealthInterestAutoComplete.allowBrowserAutocomplete = false;
        this.HealthInterestAutoComplete.itemSelectEvent.subscribe(QueryACContext.SelectItemHandler);
        this.HealthInterestAutoComplete.formatResult = QueryACContext.formatResult;
        this.HealthInterestAutoComplete.animHoriz = false;
        this.HealthInterestAutoComplete.animVert = false;

        this.HealthInterestAutoComplete.generateRequest = QueryACContext._generateQuery;
        //this.HealthInterestAutoComplete.textboxKeyEvent.subscribe(QueryACContext._textboxKeyHandler);

        //this.HealthInterestAutoComplete.containerCollapseEvent.subscribe(QueryACContext._containerCollapseHandler);
        //this.HealthInterestAutoComplete.textboxChangeEvent.subscribe(QueryACContext._textboxChangeHandler);

        this.HealthInterestAutoComplete.textboxBlurEvent.subscribe(QueryACContext._TextboxBlurHandler);

        YAHOO.util.Event.on(document.getElementById("HIAutoComplete"), "click", QueryACContext._clickHandler);

        this.HealthInterestAutoComplete.dataReturnEvent.subscribe(QueryACContext._OnDataReturnHandler);

        //CurrentQueryContext.ChangeQueryEvent.subscribe(QueryACContext._ChangeQueryEventHandler);

        //document.getElementById("queryAC").style.visibility="visible";
    },
/*
    _textboxChangeHandler:function(oSelf)
    {
        alert('['+YAHOO.util.Dom.get('HIACInput').value+']');
    },
*/
/*
    _containerCollapseHandler:function(oSelf)
    {
        alert('['+YAHOO.util.Dom.get('HIACInput').value+']');
    },
*/
/*
    _textboxKeyHandler:function(oSelf, nKeycode)
    {
        var obj1 = oSelf;
        var obj2 = nKeycode;
    },
*/
    _generateQuery:function()
    {
        return ("/prefix="+YAHOO.util.Dom.get('HIACInput').value+"/query="+CurrentQueryContext.composeTopicQuery());
    },
    _clickHandler:function(e)
    {
        var elTarget = YAHOO.util.Event.getTarget(e);
        var oButton = null;
        while (elTarget.id != "HIAutoComplete")
        {
            if(elTarget.nodeName.toUpperCase() == "INPUT")
            {
                var acInput = YAHOO.util.Dom.get('HIACInput');
                if (acInput.value == 'Enter health interest') acInput.value = "";
                //setTimeout(function(){QueryACContext.HealthInterestAutoComplete.sendQuery("");},0);
                break;
            }
            elTarget = elTarget.parentNode;
        }
    },
    _OnDataReturnHandler:function(sType, aArgs)
    {
        var oAutoComp = aArgs[0];
        var sQuery = aArgs[1];
        var aResults = aArgs[2];
        
        if(aResults.length == 0) oAutoComp.setBody("<div id='NoResult'>No matching results</div>");
    },
    _TextboxBlurHandler:function(oSelf)
    {
        this._oContainer._oContent.style.display="none";
    },
    formatResult:function(aResultItem, sQuery)
    {
        var pattern = new RegExp("((^|\\s)"+sQuery+")", "ig");
        return("<div class='"+aResultItem[1]+"'>"+aResultItem[0].replace(pattern, "<span>$1</span>")+"</div>");
    },
    SelectItemHandler:function(sType, aArgs)
    {
        var value = aArgs[2][0];
        var type = aArgs[2][1].trim();
        if (type == 'H')
        {
            var tQuery = CurrentQueryContext.composeTopicQuery();
            window.location = "/vitamin-supplement/topic=" + tQuery + encodeURIComponent((tQuery=='')?value:"%2B" + value) + "/page=1";
        }
        else if (type == 'I')
        {
            alert('TO-DO: link to a new page by Ingredient');
            //PLBlockContext.showProductListingByIngredient(BlockContext.HEALTH_INTEREST_BLOCK, value);
        }
        else if (type == 'B')
        {
            alert('TO-DO: link to a new page by Brand name');
            //PLBlockContext.showProductListingByBrand(BlockContext.HEALTH_INTEREST_BLOCK, value);
        }
        YAHOO.util.Dom.get('HIACInput').value="";
    }
/*
    _ChangeQueryEventHandler:function(type, args)
    {
        QueryACContext.HealthInterestDataSource.scriptQueryAppend = "query="+args[0];
        QueryACContext.HealthInterestDataSource.flushCache();
        if (CurrentQueryContext.NumQueryItem >= 4)
            document.getElementById("queryAC").style.display="none";
        else
            document.getElementById("queryAC").style.display="block";
    }
*/
};

YAHOO.util.Event.onDOMReady
(
    function ()
    {
        QueryACContext.init();
    }
);