var YAHOOSEARCH = function() {
   $ = {};
   return {
      init : function(el) {
         $.badge = document.getElementById(el);
         // add move handle -- only do this if we're in an AIR window
         if (window.nativeWindow){
            $.handle = document.createElement('DIV');
            $.handle.style.height = '4px';
            $.handle.style.width = '180px';
            $.handle.style.position = 'absolute';
            $.handle.style.top = '-2px';
            $.handle.style.cursor = 'move';
            $.handle.onmousedown = function() {
               window.nativeWindow.startMove();
            }
            $.badge.appendChild($.handle);
         }
         $.h = document.createElement('A');
         $.h.className = 'home';
         $.h.title = 'Visit Yahoo! Search';
         $.h.target = '_blank';
         $.badge.appendChild($.h);
         $.q = document.createElement('INPUT');
         $.badge.appendChild($.q);
         $.x = document.createElement('A');
         $.x.title = 'close';
         $.x.className = 'x';
         $.x.onmouseup = function() {
           self.close();
         }
         $.badge.appendChild($.x);
         $.r = document.createElement('DL');
         $.r.style.display = 'none';
         $.badge.appendChild($.r);
         YAHOOSEARCH.ping = [];
         $.lastQuery = '';
         setInterval(YAHOOSEARCH.runSearch, 500);
      },
      runSearch : function(query) {
         if ($.q.value) {
            if ($.q.value != $.lastQuery) {
               $.lastQuery = $.q.value;
               var n = YAHOOSEARCH.ping.length;
               $.h.className = 'loading';
               YAHOOSEARCH.ping[n] = function(result) {
                  delete YAHOOSEARCH.ping[n];
                  $.h.href = 'http://search.yahoo.com/search?p=' + $.q.value;
                  YAHOOSEARCH.removeScript('YAHOOSEARCH.ping[' + n + ']');
                  $.r.innerHTML = '';
                  $.r.style.display = 'block';
                  if (result.ResultSet.Result.length) {
                     for (var i = 0; i < result.ResultSet.Result.length; i++) {
                        var dt = document.createElement('DT');
                        var a = document.createElement('A');
                        a.className = 'bookmark';
                        a.title = 'save to del.icio.us';
                        a.onmouseup = function() {
                           YAHOOSEARCH.saveBookmark(this);
                        }
                        dt.appendChild(a);
                        var a = document.createElement('A');
                        a.innerHTML = result.ResultSet.Result[i].Title;
                        a.href = result.ResultSet.Result[i].Url;
                        a.target = '_blank';
                        a.onmouseover = function() {
                           this.parentNode.nextSibling.style.display = 'block';
                        }
                        a.onmouseout = function() {
                           this.parentNode.nextSibling.style.display = 'none';
                        };
                        dt.appendChild(a);
                        if (i % 2) { dt.className = 'odd'; }
                        $.r.appendChild(dt);
                        var dd = document.createElement('DD');
                        var p = document.createElement('P');
                        p.innerHTML = result.ResultSet.Result[i].Summary;
                        dd.appendChild(p);
                        dd.style.zIndex = i + 100;
                        $.r.appendChild(dd);
                     }
                  } else {
                     var dt = document.createElement('DT');
                     dt.innerHTML = 'Nothing found, sorry!';
                     $.r.appendChild(dt);
                  }
                  $.h.className = 'home';
               };
               var callback = 'YAHOOSEARCH.ping[' + n + ']';
               var url = 'http://search.yahooapis.com/WebSearchService/V1/webSearch?';
               url += '&appid=YahooAirSearch';
               url += '&results=10';
               url += '&output=json';
               url += '&query=' + $.q.value;
               url += '&callback=' + callback;
               YAHOOSEARCH.runScript(url, callback);
            }
         } else {
            if ($.lastQuery) {
               $.r.style.display = 'none';
               $.r.innerHTML = '';
               $.h.href = 'http://search.yahoo.com';
            }
         }
      },
      saveBookmark : function(el) {
         var ns = el.nextSibling;
         var url='http://del.icio.us/post/?';
         url += 'url=' + escape(ns.href);
         url += '&title=' + escape(ns.innerHTML);
         url += '&jump=no';
         window.open(url,'popup','width=720px,height=420px',0);
      },
      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);
         }
      }
   };
}();
window.onload = function() {
   YAHOOSEARCH.init('yahooSearch');
};