YAHOO.namespace("BIZARRO");
(function(){

    YAHOO.BIZARRO.RemoteCustomRequest = function(el,cont) {
        // Use an XHRDataSource
        var oDS = new YAHOO.util.XHRDataSource("/search.php");
        // Cancel stale requests
        oDS.connXhrMode = 'cancelStaleRequests';
        // Set the responseType
        oDS.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
        // Define the schema of the JSON results
        oDS.responseSchema = {
            resultsList: "Results",
            fields: ["value","id","location"]
        };

        // Instantiate the AutoComplete
        var oAC = new YAHOO.widget.AutoComplete(el, cont, oDS);
        oAC.useShadow = true;
        oAC.autoHighlight = false;
        oAC.animSpeed = 0;
        oAC.minQueryLength = 1;
        oAC.allowBrowserAutocomplete = false;

        oAC.itemSelectEvent.subscribe(function(sType, aArgs) {
            var myAC = aArgs[0]; // reference back to the AC instance
            var elLI = aArgs[1]; // reference to the selected LI element
            var oData = aArgs[2]; // object literal of selected item's result data

            window.location = oData[2];
        });

        var getImgUrl = function(src) {
            var sUrl = src;
            return '<img src="' + sUrl + '" width="30" height="33">';
        };


        oAC.formatResult = function(oResultData, sQuery, sResultMatch) {

            var icon,type,supplemental,aka,markup = [], rating;

            markup.push('<div style="overflow:hidden;padding:2px 0;">');
            markup.push(icon);
            markup.push('<div style="overflow:hidden">');
            markup.push("<strong>"+sResultMatch+"</strong>");
            markup.push('</div>');
            markup.push('</div>');
            return markup.join("\n");
        };

        // Throttle requests sent
        oAC.queryDelay = .2;
        // The webservice needs additional parameters
        oAC.generateRequest = function(sQuery) {
            return "?term=" + sQuery ;
        };

        YAHOO.util.Event.addListener(el, 'focus', function(){
            oAC.sendQuery('  ');
        });

        return {
            oDS: oDS,
            oAC: oAC
        };
    };

    YAHOO.util.Event.onAvailable("search",function(){
        YAHOO.BIZARRO.RemoteCustomRequest(this,"myTopnavContainer");
    });

})();

