( 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.buildStructure();
                  theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
                  theScripts[i].parentNode.removeChild(theScripts[i]);
                  break;
               }
            }         
         },
         // Hedger's safer JSON parser: 
         // http://www.hedgerwow.com/360/dhtml/js_safe_eval/demo.php
         parseJson : function(json) {
            this.parseJson.data = json;
            if ( typeof json !== 'string') {
               return {"err":"trying to parse a 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":"trouble parsing JSON object"};
            }
         },         
         buildStructure : function() {
            var args = [ "user", "height", "width" ];
            var defaults = { 
               "user":"",
               "height":"350",
               "width":"250" };
            var validStyle = [
               "border",
               "background",
               "color",
               "font",
               "height", 
               "width",
			   "user",
			   "urmariti"];
            for (var i = 0; i < args.length; i++) {
               if (typeof defaults[args[i]] !== typeof $.a[args[i]] ) {
                  $.a[args[i]] = defaults[args[i]];
               }
            }
            if (!document.getElementById('ciripBadge')) {
               var link = document.createElement('LINK');
               link.id = 'ciripBadge'; 
               link.rel = 'stylesheet';
               link.type = 'text/css';
               link.href = 'http://www.cirip.ro/widget/cirip.css';
               document.getElementsByTagName('HEAD')[0].appendChild(link);
            }
            $.w = document.createElement('DIV');
            $.w.className = 'ciripBadge';
            $.w.h = document.createElement('H3');
            $.w.h.innerHTML = 'Messages on cirip.ro:' + $.a.user;
            if ($.a.headerBackground) {
               try {
                  $.w.h.style.background = $.a.headerBackground;
               } 
               catch(err) { }
            }
            if ($.a.headerColor) {
               try {
                  $.w.h.style.color = $.a.headerColor;
               } 
               catch(err) { }
            }
            $.w.appendChild($.w.h);
            $.w.r = document.createElement('UL');
            $.w.r.style.height = $.a.height - 50 + 'px';
            $.w.appendChild($.w.r);
            
            $.w.f = document.createElement('P');
            $.w.f.style.height = '15px';
            var a = document.createElement('A');
            a.innerHTML = 'I want on cirip too';
            a.target = '_blank';
            a.href = 'http://www.cirip.ro/';
            if ($.a.headerColor) {
               try {
                  a.style.color = $.a.headerColor;
               } 
               catch(err) { }
            }            
            $.w.f.appendChild(a);
            if ($.a.headerBackground) {
               try {
                  $.w.f.style.background = $.a.headerBackground;
               } 
               catch(err) { }
            }
            $.w.appendChild($.w.f);
            for (var i = 0; i < validStyle.length; i++) {
               if (typeof $.a[validStyle[i]] !== undefined) {
                  var styleToApply = $.a[validStyle[i]];
                  if (styleToApply - 0 == styleToApply) {
                     styleToApply += 'px';
                  }
                  try {
                     $.w.style[validStyle[i]] = styleToApply;
                  } 
                  catch(err) { }
               }
            }
            $.f.runSearch();
         },
         runSearch : function() {
            $.w.h.innerHTML = '<a href="http://www.cirip.ro/u/' + $.a.user + '" target="_blank">' + $.a.user + '</a> on <a href="http://www.cirip.ro" target="_blank">Cirip.eu</a>';
            $.w.r.innerHTML = '';
            var n = $.f.runFunction.length;
// ugh, trouble with Twitter's callbacks:
// 1) no dots, so we can't use our nice safe trueName
// 2) no square brackets, so we can't do arrays of callbacks
// 3) they seem to be caching replies, including callbacks
// so instead of this:
//            var id = trueName + 'runFunction';
// we have to do this:
            $.f.callback = 'twitBack';
            window[$.f.callback] = function(r) {
               delete($.f.callback);
               $.f.removeScript($.f.callback);
               $.f.renderResult(r);
            };
// and if anybody has hit the same friends_timeline with a different callback,
// our badge will blow chunx
// hopefully soon they will fix this....
            var url = 'http://www.cirip.ro/widget/json.php?user=';
            url += $.a.user;
			url += '&limit=';
			url += $.a.limit;
			url += '&urmariti=';
			url += $.a.urmariti;
            //url += '.json?callback=' + $.f.callback;
			//var url ='http://www.cirip.ro/widget/cami13.json';
            $.f.runScript(url, $.f.callback);
         },
         renderResult: function(r) {
            for (var i = 0; i < r.length; i++) {	
               var li = document.createElement('LI');
               li.style.width = $.a.width - 20 + 'px';
               var icon = document.createElement('A');
               icon.className = 'icon';
               icon.style.backgroundImage = 'url(' + r[i].user.profile_image_url + ')';
               icon.rel = r[i].user.screen_name;
               icon.title = 'Status ' + icon.rel;
               icon.alt = 'Status ' + icon.rel;
               icon.style.cursor = 'pointer';
			   icon.href = r[i].user.url;
			   icon.target = '_blank';
			   
			   /*
               icon.onmouseup = function() {
                  $.a.user = this.rel;
                  $.f.runSearch();
               };
			   */
               li.appendChild(icon);
               var user = document.createElement('A');
               user.innerHTML = r[i].user.name;
               if (r[i].user.url) {
                  //user.href = r[i].user.url;
				  user.href = 'http://www.cirip.ro/status/' + r[i].id;
                  user.target = '_blank';
               }
               user.style.fontWeight = 'bold';
               if ($.a.userColor) {
                  try {
                     user.style.color = $.a.userColor;
                  } 
                  catch(err) { }
               }                 li.appendChild(user);
               li.appendChild(document.createElement('BR'));
               var t = document.createTextNode(r[i].text.replace(/&quot;/gi, "\""));
               li.appendChild(t);
               if ( i % 2) {
                  li.className = 'odd';
                  if ($.a.oddBackground) {
                     try {
                        li.style.background = $.a.oddBackground;
                     } 
                     catch(err) { }
                  }
               } else {
                  if ($.a.evenBackground) {
                     try {
                        li.style.background = $.a.evenBackground;
                     } 
                     catch(err) { }
                  }
               }
               if ($.a.userBorder) {
                  try {
                     li.style.borderTop = $.a.userBorder;
                  } 
                  catch(err) { }
               }
               $.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) {
            if (document.getElementById(id)) {
               var s = document.getElementById(id);
               s.parentNode.removeChild(s);
            }
         }         
      };
   }();
   var thisScript = /^https?:\/\/[^\/]*www.cirip.ro\/widget\/cirip_en.js$/;
// brothercake onload function
   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); });
   }
} )();
