( function() { 
   var trueName = '';
   for (var i = 0; i < 16; i++) { 
      trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97); 
   }
   window[trueName] = {};
   var $ = window[trueName];
   $.f = function() {
      return { 
         runFunction : [],
         init : function(target) {
            var theScripts = document.getElementsByTagName('SCRIPT');
            for (var i = 0; i < theScripts.length; i++) {
               if (theScripts[i].src.match(target)) {
                  if (theScripts[i].innerHTML) {
                     if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$/.test(theScripts[i].innerHTML.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) {
                        try {
                           $.a = eval( '(' + theScripts[i].innerHTML + ')' );
                        }
                        catch(err) { }
                     }
                  }
                  if (typeof $.a !== 'object') {
                     $.a = {};
                  }
                  $.f.buildStructure();
                  theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
                  theScripts[i].parentNode.removeChild(theScripts[i]);
                  break;
               }
            }         
         },
         buildStructure : function() {
            $.w = document.createElement('DIV');
            $.w.q = document.createElement('INPUT');
            if ($.a.query) {
               $.w.q.value = $.a.query;
            }
            $.w.q.onkeypress = function(e) {
               if ( (e ? e.which : event.keyCode) == 13) {
                   $.f.runSearch();
               }
            };
            $.w.appendChild($.w.q);
            $.w.b = document.createElement('BUTTON');
            $.w.b.innerHTML = 'Search';
            if ($.a.site) {
               $.w.b.innerHTML += ' ' + $.a.site;
            }
            $.w.b.onmouseup = function() {
               $.f.runSearch();
            };
            $.w.appendChild($.w.b);
            $.w.r = document.createElement('UL');
            $.w.appendChild($.w.r);
         },
         runSearch : function() {
            $.w.r.innerHTML = '';
            if ($.w.q.value) {
               var n = $.f.runFunction.length;
               var id = trueName + '.f.runFunction[' + n + ']';
               $.f.runFunction[n] = function(r) {
                  delete($.f.runFunction[n]);
                  $.f.removeScript(id);
                  $.f.renderResult(r);
               }
               var url = 'http://search.yahooapis.com/WebSearchService/V1/webSearch?';
               url += '&appid=YahooSearch';
               url += '&results=5';
               url += '&output=json';
               url += '&query=' + $.w.q.value;
               url += '&callback=' + id;
               if ($.a.site) {
                  url += '&site=' + $.a.site;
               }               
               $.f.runScript(url, id);
            }
         },
         renderResult: function(r) {
            for (var i = 0; i < r.ResultSet.Result.length; i++) {
               var li = document.createElement('LI');
               var a = document.createElement('A');
               a.innerHTML = r.ResultSet.Result[i].Title;
               a.href = r.ResultSet.Result[i].ClickUrl;
               a.target = '_blank';
               li.appendChild(a);
               $.w.r.appendChild(li);
            }
         },
         runScript : function(url, id) {
            var s = document.createElement('script');
            s.id = id;
            s.type ='text/javascript';
            s.src = url;
            document.getElementsByTagName('body')[0].appendChild(s);
         },
         removeScript : function(id) {
            var s = '';
            if (s = document.getElementById(id)) {
               s.parentNode.removeChild(s);
            }
         }         
      };
   }();
   var thisScript = /^https?:\/\/[^\/]*r8ar.com\/che.js$/;
   if(typeof window.addEventListener !== 'undefined') {
      window.addEventListener('load', function() { $.f.init(thisScript); }, false);
   } else if(typeof window.attachEvent !== 'undefined') {
      window.attachEvent('onload', function() { $.f.init(thisScript); });
   }
} )();
