MTV.Search = new function(){ var SEARCH_TERM_REGEX = /q=([^&|\s]+)/; this.init = function(){ var self = this; var search_forms = 'form[name="search"], form[name="searchF"], form[name="search404"]'; $j(search_forms).each(function(){ self.addEventListeners(this); self.setSearchTerm(this); }); } this.setSearchTerm = function(selector){ var query_string = document.location.search; if (query_string.length < 1){ return; } var search_term = query_string.match(SEARCH_TERM_REGEX); if (search_term){ search_term = search_term[1].replace("+", " "); search_term = unescape(search_term); $j(selector).find('input[type="text"]').val(search_term); } } this.addEventListeners = function(selector){ var self = this; $j(selector).each(function(){ var form = $j(this); // deactivate submit form.submit(function(){ return false; }); var goButton = form.find('button.mtv'); var webButton = form.find('button.web'); var input = form.find('input[type="text"]'); // clear input on click input.click(function(){ $j(this).val(''); return false; }); // watch for user enter input.keypress(function(event){ if (event.keyCode == 13){ var params = ""; if($j(this).parent().hasClass('searchNutch')){ params = "testgroup=B"; mboxUpdate('viamtv','searchclick=nutch'); }else if($j(this).parent().hasClass('searchFast')){ mboxUpdate('viamtv','searchclick=fast'); } self.submit(input.val(), 'mtv', params); return false; } }); // web submit webButton.click(function(){ self.submit(input.val(), 'web'); }); // mtv submit goButton.click(function(){ var params = ""; if($j(this).parent().hasClass('searchNutch')){ params = "testgroup=B"; mboxUpdate('viamtv','searchclick=nutch'); }else if($j(this).parent().hasClass('searchFast')){ mboxUpdate('viamtv','searchclick=fast'); } self.submit(input.val(), 'mtv', params); }); }); } this.submit = function(value, type, params){ value = value.replace(/^\s*|\s(?=\s)|\s*$/g, "").toLowerCase(); value = value.replace(" ", "+"); value = escape(value); if (value == "search" || value.length == 0){ return false; } if (type == 'web'){ var msUrl ='http://search.live.com/results.aspx?q=' + value + '&mkt=en-us&FORM=VCM004'; var msWindow = window.open(msUrl); com.mtvi.reporting.Dispatcher.setAttribute("eVar3","web"); com.mtvi.reporting.Dispatcher.sendLinkEvent({linkName:"MSN_Live_Search",linkType:"e",lnk:this}); } else{ if(typeof params != 'undefined' && params != "") value += "&" + params; window.location = this.getBaseUrl() + '/search/?q=' + value; } return false; } this.getBaseUrl = function(){ if( window.location.hostname.match(/(www.mtvx?-j?[dq].mtvi.com)/) ) return ''; else{ if( window.location.hostname.indexOf("mtv-d.mtvi.com") != -1 ) return 'http://www.mtv-d.mtvi.com'; else if (window.location.hostname.indexOf("mtv-q.mtvi.com") != -1 ) return 'http://www.mtv-q.mtvi.com'; else return 'http://www.mtv.com'; } } }