MTV.Typeahead = new function(){ var default_options ={ minChars: 1, scrollHeight: 300, jsonpCallback: 'mtvSearchJSONP', matchSubset: false, selectFirst: false, formatItem: function(item){ return item; }, parse: function(data){ var parsed = []; $j.each(data, function(i){ var row = $j.trim(this); parsed[i] ={ data: row, value: row, result: row } }); return parsed; } }; this.init = function(){ MTV.Typeahead.autocomplete('form[name="search"] input[type="text"]'); MTV.Typeahead.autocomplete('form[name="searchF"] input[type="text"]'); MTV.Typeahead.autocomplete('form[name="search404"] input[type="text"]'); } this.autocomplete = function(selector, options, callback){ var typehead_options ={}; $j.extend(typehead_options, default_options, options ||{}); callback = callback || function(){ var input = $j(this); /* this blows- too tied to the "search" structure */ //input.closest('form').find('button.mtv').click(); var e = jQuery.Event("keypress"); e.keyCode = 13; input.trigger(e); }; $j(selector).each(function(){ var input = $j(this); // we subtract the padding of the autocomplete container class here // this is defined by .ac_results selector if (!typehead_options['width']){ typehead_options['width'] = parseInt(input.outerWidth()) - 8; } var url = "http://search.mtvnservices.com/typeahead/suggest/?siteName=mtv&format=json"; if($j(this).parent().hasClass('searchNutch')) url += "&testgroup=B"; input.autocomplete(url, typehead_options).result(callback); }); } }