var WIKISEARCH = function() {
   $ = {};
   return {
      init : function(el) {
         $.badge = document.getElementById(el);
         $.h = document.createElement('A');
         $.badge.appendChild($.h);
         $.h.className = 'home';
         $.h.target = '_blank';
         $.q = document.createElement('INPUT');
         $.badge.appendChild($.q);
         $.r = document.createElement('DL');
         $.r.style.display = 'none';
         $.badge.appendChild($.r);
         WIKISEARCH.ping = [];
         $.lastQuery = '';
         setInterval(WIKISEARCH.runSearch, 500);
      },
      runSearch : function(query) {
      
         if ($.q.value) {
            if ($.q.value != $.lastQuery) {
               $.lastQuery = $.q.value;
               
         var n = WIKISEARCH.ping.length;
         $.h.className = 'loading';
         WIKISEARCH.ping[n] = function(result) {
            $.h.href = 'http://en.wikipedia.org/wiki/' + $.q.value;
            $.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() {
                     WIKISEARCH.saveBookmark(this);
                  }
                  dt.appendChild(a);
                  var a = document.createElement('A');
                  a.rel = result.ResultSet.Result[i].Title;
                  a.innerHTML = result.ResultSet.Result[i].Title.split(' - ')[0];
                  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';
            delete WIKISEARCH.ping[n];
            WIKISEARCH.removeScript('WIKISEARCH.ping[' + n + ']');
         };
         var callback = 'WIKISEARCH.ping[' + n + ']';
         var url = 'http://search.yahooapis.com/WebSearchService/V1/webSearch?';
         url += '&appid=WikiSearch';
         url += '&site=en.wikipedia.org'; 
         url += '&results=20'; 
         url += '&output=json';
         url += '&query=' + $.q.value;
         url += '&callback=' + callback;
         WIKISEARCH.runScript(url, callback);
            }
         } else {
            if ($.lastQuery) {
               $.r.style.display = 'none';
               $.r.innerHTML = '';
               $.h.href = 'http://en.wikipedia.org';
            }
         }      
      },
      saveBookmark : function(el) {
         var ns = el.nextSibling;
         var url='http://del.icio.us/post/?';
         url += 'url=' + escape(ns.href);
         url += '&title=' + escape(ns.rel);
         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) {
         if (document.getElementById(id)) {
            var s = document.getElementById(id);
            s.parentNode.removeChild(s);
         }
      }
   };
}();

window.onload = function() {
   WIKISEARCH.init('wikisearch');
};