// needish badge --  updated 29Sep2008
// as always, use at your own risk
// copyright Gian Olivieri 2008
// based on twitterati   http://kentbrewster.com/twitterati
( 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)) {
                  $.a = {};
                  if (theScripts[i].innerHTML) {
                     $.a = $.f.parseJson(theScripts[i].innerHTML);
                  }
                  if ($.a.err) {
                     alert('bad json!');
                  }
                  $.f.loadDefaults();
                  $.f.buildStructure();
                  $.f.buildPresentation();
                  theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
                  theScripts[i].parentNode.removeChild(theScripts[i]);
                  break;
               }
            }         
         },
         parseJson : function(json) {
            this.parseJson.data = json;
            if ( typeof json !== 'string') {
               return {"err":"tratando de parsear un non-string JSON object"};
            }
            try {
               var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
                  'Array,String,Math,RegExp,Image,ActiveXObject;',
                  'return (' , json.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function&shy;') , ');'].join(''));
               return f();
            } catch (e) {
               return {"err":"Problema parseando JSON object"};
            }
         },
         loadDefaults : function() {
            $.d = { };
            for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
         },
          buildPresentation : function () {
            var ns = document.createElement('style');
            document.getElementsByTagName('head')[0].appendChild(ns);
            if (!window.createPopup) {
               ns.appendChild(document.createTextNode(''));
               ns.setAttribute("type", "text/css");
            }
            var s = document.styleSheets[document.styleSheets.length - 1];
            var rules = {"" : "{padding:15px 15px 0 15px}"};
            var ieRules = "";
            for (r in rules) {
               var selector = '.' + trueName + ' ' + r;
               if (!window.createPopup) {
                  var theRule = document.createTextNode(selector + rules[r]);
                  ns.appendChild(theRule);
               } else {
                  ieRules += selector + rules[r];
               }
            } 
            if (window.createPopup) { s.cssText = ieRules; }
         },
         buildStructure : function() {

            $.w = document.createElement('DIV');
			$.w.con = document.createElement('DIV');
            $.w.con.className = "content";
            $.w.appendChild($.w.con);
            $.f.runSearch();

         },
         runSearch : function() {
            if (!$.f.runFunction) { $.f.runFunction = []; }
            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.value.items); 
            };
            var url = 'http://pipes.yahoo.com/pipes/pipe.run?_id=vFWiDo5M3hG5x8TvPhJ3AQ&_render=json';
            url += '&_callback=' + id;
            $.f.runScript(url, id);
         },
         renderResult: function(r) { 
            $.w.con.hhh = document.createElement('H3');
			$.w.con.hhh.innerHTML = r[0].titulo;
			$.w.con.appendChild($.w.con.hhh);
			$.w.con.pp = document.createElement('P');
			$.w.con.pp.innerHTML = r[0].post.content + '...';
			$.w.con.appendChild($.w.con.pp);
			$.w.im  = document.createElement('IMG');
			$.w.im.src = "http://smallpod.co.uk/wp/wp-content/themes/smallpod/img/imgblog.jpg";
			$.w.im.alt = "latest from blog";
			$.w.appendChild($.w.im);
			$.w.ah  = document.createElement('A');
            $.w.ah.className = "learn-more";
			$.w.ah.href = r[0].link;
            $.w.ah.innerHTML = "learn more";
			$.w.appendChild($.w.ah);
         },
         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);
            }
         }         
      };
   }();
   var thisScript = /smallpod2.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); });
   }
} )();
