( function() {
   var trueName = '';
   for (var i = 16; i > 0; i--) { 
      trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97); 
   }
   window[trueName] = {};
   var $ = window[trueName];
   $.f = function() {
      return { 
         init : function(id) {
            $.foaf = {};
            $.matte = document.getElementById('matte');
            $.canvas = document.getElementById('canvas');
            $.signature = document.getElementById('signature');
            $.nav = document.getElementById('breadcrumbs');
            $.click = 0;
            $.breadcrumbs = [];
            $.maxToShow = 50;
            $.ident = document.createElement('SPAN');
            $.r = 8675309;
            $.f.svc = {
               "http://30boxes.com": { "name":"30 Boxes", "icon":"30boxes" },
               "http://activerain.com/": { "name":"ActiveRain", "icon":"activerain" },
               "http://bebo.com/": { "name":"Bebo","icon":"bebo"  },
               "http://del.icio.us/": { "name":"del.icio.us", "icon":"delicious", "api":"delicious", "count":"10"},
               "http://digg.com/": { "name":"Digg", "icon":"digg", "api":"digg", "count":"10" },
               "http://friendfeed.com/":  { "name":"FriendFeed","icon":"friendfeed"  },
               "http://openid.net/":  { "name":"OpenID", "icon":"openid" },
               "http://pownce.com/":  { "name":"Pownce", "icon":"pownce" },
               "http://secondlife.com/":  { "name":"Second Life", "icon":"secondlife" },
               "http://twitter.com/": { "name":"Twitter", "icon":"twitter", "api":"twitter", "count":"10"},
               "http://upcoming.yahoo.com/": { "name":"Upcoming", "icon":"upcoming", "api":"upcoming", "count":"10" },
               "http://wakoopa.com/": {"name":"Wakoopa", "icon":"wakoopa"}, 
               "http://wink.com/": {"name":"Wink", "icon":"wink"}, 
               "http://www.blogger.com/": { "name":"Blogger","icon":"blogger"  },
               "http://www.ebay.com/": { "name":"Ebay","icon":"ebay"  },
               "http://www.facebook.com/": { "name":"Facebook","icon":"facebook"  },
               "http://www.flickr.com/": { "name":"Flickr", "icon":"flickr", "api":"flickr", "count":"20"} ,
               "http://www.friendster.com/":  { "name":"Friendster","icon":"friendster" },
               "http://www.google.com/calendar/": { "name":"Google Calendar", "icon":"googlecalendar" },
               "http://www.jaiku.com/": { "name":"Jaiku", "icon":"jaiku" },
               "http://www.jumpcut.com/": { "name":"JumpCut","icon":"jumpcut"  },
               "http://www.kiva.org/": { "name":"Kiva","icon":"kiva"  },
               "http://www.last.fm/": { "name":"Last.fm", "icon":"lastfm", "api":"lastfm", "count":"10" },
               "http://www.linkedin.com/": { "name": "LinkedIn", "icon":"linkedin", "api":"linkedin", "count":"10" },
               "http://www.livejournal.com/":  { "name":"LiveJournal", "icon":"livejournal" },
               "http://www.multiply.com/":  { "name":"Multiply", "icon":"multiply" },
               "http://www.mybloglog.com/" : { "name":"MyBlogLog","icon":"mybloglog", "api":"mybloglog", "count":"5" },
               "http://www.myspace.com/":  { "name":"MySpace", "icon":"myspace" },
               "http://www.netflix.com/":  { "name":"Netflix", "icon":"netflix" },
               "http://www.plaxo.com/":  { "name":"Plaxo", "icon":"plaxo" },
               "http://www.shelfari.com/":  { "name":"Shelfari", "icon":"shelfari" },
               "http://www.stumbleupon.com/":  { "name":"StumbleUpon", "icon":"stumbleupon", "api":"stumbleupon", "count":"10" },
               "http://www.technorati.com/":  { "name":"Technorati", "icon":"technorati" },
               "http://www.thedjlist.com/":  { "name":"The DJ List","icon":"thedjlist"  },
               "http://www.trulia.com/":  { "name":"Trulia", "icon":"trulia" },
               "http://www.tumblr.com/":  { "name":"Tumblr", "icon":"tumblr", "api":"tumblr", "count":"10" },
               "http://www.typekey.com/": { "name":"TypeKey","icon":"typekey"  },
               "http://www.yelp.com/": {"name":"Yelp","icon":"yelp" },
               "http://www.zillow.com/": {"name":"Zillow","icon":"zillow" },
               "http://www.zorpia.com/": {"name":"Zorpia","icon":"zorpia"},
               "http://youtube.com/": {"name":"YouTube", "icon":"youtube", "api":"youtube", "count":"5" }
            }
            $.canvas.onmousemove = function(e) {
               $.theImages = $.canvas.getElementsByTagName('IMG');
               if (window.event) { 
                  e = window.event; 
               }
               var cx = ( e.x || e.clientX ) - $.canvas.offsetLeft + window.pageXOffset;
               var cy = ( e.y || e.clientY ) - $.canvas.offsetTop + window.pageYOffset;
               for (var i = 0; i < $.theImages.length; i++) {
                  el = $.theImages[i];
                  var currentSize = el.offsetHeight; 
                  var x = el.offsetLeft + currentSize/2;
                  var y = el.offsetTop + currentSize/2;
                  var dx = cx - x;
                  var dy = cy - y;
                  var dist = Math.sqrt(dx * dx + dy * dy);                  
                  if (dist < 150) { 
                     var m = Math.cos(0.5 * Math.PI * Math.abs(dist / 150));
                     var newDim = parseInt(m * 100, 10);                     
                     if (newDim > 25) {
                        el.style.zIndex = newDim;
                        var diff = parseInt((currentSize - newDim) / 2);
                        el.style.top = el.offsetTop + diff + 'px';
                        el.style.left = el.offsetLeft + diff + 'px';
                        el.style.height = el.style.width = newDim + 'px';
                     }             
                  } else {
                     if (el.style.offsetLeft !== $.foaf[el.id].x) {
                        el.style.height = el.style.width = '25px';
                        el.style.left = $.foaf[el.id].x + 'px';
                        el.style.top = $.foaf[el.id].y + 'px';
                     }
                  }
               }
            };            
            $.canvas.onmouseout = function(e) {
               $.theImages = $.canvas.getElementsByTagName('IMG');
               for (var i = 0; i < $.theImages.length; i++) {
                  el = $.theImages[i];
                  if (el.style.offsetLeft !== $.foaf[el.id].x) {
                     el.style.height = el.style.width = '25px';
                     el.style.left = $.foaf[el.id].x + 'px';
                     el.style.top = $.foaf[el.id].y + 'px';
                  }
               }
            };            
            $.f.getFoaf(id);
         },
         getFoaf : function(id) {
            var callback = trueName + '___' + id;
            $.ident.className = 'pondering';
            window[callback] = function(z) {
            if (z['foaf:Person']) {
               var requestedId = z['foaf:Person']['rdfs:seeAlso']['rdf:resource'].split('members/')[1].split('/foaf')[0];
               var callback = trueName + '___' + requestedId;
               $.f.removeScript(callback);
               delete callback;
               $.signature.style.display = 'block';
               if (!$.background) {
                  var img = document.createElement('IMG');
                  img.src = z['foaf:Person']['foaf:img']['rdf:resource'];
                  img.title = z['foaf:Person']['foaf:name'];
                  $.matte.appendChild(img);
                  $.background = img;
                  var svc = [];
                  if (z['foaf:Person']['foaf:holdsAccount']) {
                     svc = z['foaf:Person']['foaf:holdsAccount'];
                  }
                  $.f.makeNode(requestedId, img.src, img.title, svc);                  
                  $.f.updateSig(requestedId);
               }
               $.f.updateNav(requestedId);
               if (z['foaf:Person']['foaf:knows']) {
                  var k = z['foaf:Person']['foaf:knows'];
                  var toShow = k.length;
                  if (toShow > $.maxToShow) {
                     toShow = $.maxToShow;
                  }
                  for (var i = 0; i < toShow; i++) {
                     svc = k[i]['foaf:Person']['foaf:holdsAccount'];
                     if (svc && svc.length !== undefined) {
                        var id = k[i]['foaf:Person']['rdfs:seeAlso']['rdf:resource'].split('members\/')[1].split('\/foaf')[0];
                        $.f.makeNode(id, k[i]['foaf:Person']['foaf:img']['rdf:resource'], k[i]['foaf:Person']['foaf:name'], svc);
                        var img = document.createElement('IMG');
                        img.id = id;
                        img.title = $.foaf[id].name;
                        img.src = $.foaf[id].img;
                        img.style.top = $.foaf[id].y + 'px';
                        img.style.left = $.foaf[id].x + 'px';
                        img.onclick = function() {
                           $.background.src = this.src;
                           $.f.updateSig(this.id);
                           $.canvas.innerHTML = '';
                           $.f.getFoaf(this.id);                     
                        }
                        $.canvas.appendChild(img);
                     }
                  }
               } else {
                  alert('Sorry, ' + requestedId + ' has no public connections.');
               }
               $.ident.className = '';
               }
               else {
                  alert('Sorry, the MyBlogLog member name you asked for doesn\'t seem to exist.');
               }
            };
            var url = 'http://www.mybloglog.com/buzz/members/' + id + '/foaf?format=json&callback=' + callback;
            $.f.runScript(url, callback);
         },
         updateNav : function(id) {
            var dupe = 0;
            for (var i = 0; i < $.breadcrumbs.length; i++) {
               if ($.breadcrumbs[i].id == id) {
                  dupe = 1;
               }
            }
            if (!dupe) {
               $.breadcrumbs[$.click] = {};
               $.breadcrumbs[$.click].id = id;
               $.breadcrumbs[$.click].img = $.background.src;
               $.click++;
            }
            $.nav.innerHTML = '';
            for (var i = 0; i < $.click; i++) {
               $.nav.style.display = 'block';
               var img = document.createElement('img');
               img.src = $.breadcrumbs[i].img;
               img.id = img.title = $.breadcrumbs[i].id;
               img.onclick = function() {
                  $.background.src = this.src;
                  $.f.updateSig(this.id);
                  $.canvas.innerHTML = '';
                  $.f.getFoaf(this.id);                
               }
               $.nav.appendChild(img);
            }
         },
         updateSig : function(id) {
            $.signature.innerHTML = '';
            $.ident = document.createElement('SPAN');
            $.ident.innerHTML = $.foaf[id].name;
            $.signature.appendChild($.ident);
            if ($.foaf[id].svc) {
               for (var i = 0; i < $.foaf[id].svc.length; i++) {
                  if ($.foaf[id].svc[i]['foaf:OnlineAccount']['foaf:accountProfilePage'] && $.foaf[id].svc[i]['foaf:OnlineAccount']['foaf:accountServiceHomepage'] && $.foaf[id].svc[i]['foaf:OnlineAccount']['foaf:accountName']['rdf:resource'] !== '0') {
                     var s = $.foaf[id].svc[i]['foaf:OnlineAccount']['foaf:accountServiceHomepage']['rdf:resource'];
                     var a = document.createElement('A');
                     if ($.f.svc[s]) {
                        a.className = 'icon ' + $.f.svc[s].icon;
                        a.title = $.f.svc[s].name;
                        a.rel = $.foaf[id].svc[i]['foaf:OnlineAccount']['foaf:accountName']['rdf:resource'];
                        a.rev = $.foaf[id].svc[i]['foaf:OnlineAccount']['foaf:accountProfilePage']['rdf:resource'];
                        a.id = s;
                        a.onclick = function() {
                           $.f.getDetails(this);
                        }
                        $.signature.appendChild(a);
                     }
                  }
               }
            }
         },
         getDetails : function(svc) {
            var s = svc.id;
            if (typeof $.activity === 'object' && $.activity.parentNode) {
               $.activity.parentNode.removeChild($.activity);
            }
            $.activity = document.createElement('UL');
            $.activity.id = 'activity';
            $.signature.appendChild($.activity);
            $.activity.appendChild(document.createElement('BR'));
            var li = document.createElement('LI');
            var x = document.createElement('A');
            x.innerHTML = 'x';
            x.className = 'x';
            x.onclick = function() {
               $.activity.parentNode.removeChild($.activity);
            }
            li.appendChild(x);
            var a = document.createElement('A');
            a.className = 'hd';
            a.innerHTML = svc.rel + ' on ' + $.f.svc[s].name;
            a.href = svc.rev;
            a.target = '_blank';
            li.appendChild(a);
            $.activity.appendChild(li);
            if ($.f.svc[s].api) {
               $.f.getActivity($.f.svc[s].api, svc.rel, $.f.svc[s].count);
            }
         },
         getActivity : function(api, id, count) {
            var callback = trueName + '___' + api + '___' + id.replace(/@/gi, '_');
            window[callback] = function(z) {
               var t = callback.split('___');
               var api = t[1];
               var id = t[2];
               if (z && z.value && z.value.items) {
                  var r = z.value.items;
                  for (var i = 0; i < r.length; i++) {
                     var li = document.createElement('LI');
                     li.className = api;
                     var tx = r[i].t;
                     if (api == 'twitter') {
                        var tx = tx.replace(/http:\/\/([^\s,-]*)/gi, '<a href="http://$1" target="_blank">http://$1</a>').replace(/@([^\s,.!-]*)/gi, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
                        r[i].u = '';
                     }
                     var img = '';
                     if (r[i].u) {
                        var a = document.createElement('A');
                        a.href = r[i].u;
                        a.target = '_blank';
                        if (r[i].n) {
                           var img = document.createElement('IMG');
                           img.className = 'v';
                           if (api == 'flickr') {
                              img.className = 'q';
                              img.title = tx;
                           }
                           img.align = 'absmiddle';
                           img.src = r[i].n;
                           a.appendChild(img);
                           li.appendChild(a);
                           var a = document.createElement('A');
                           a.target = '_blank';
                           a.href = r[i].u;
                        }
                     } else {
                        var a = document.createElement('DIV');
                     }
                     if (!img) {
                        a.innerHTML = tx;
                        li.appendChild(a);
                     }
                     $.activity.appendChild(li);
                  }
                  $.activity.style.display = 'block';
               }
               $.f.removeScript(callback);
            };
            var url = 'http://pipes.yahoo.com/kentbrew/blogjuice_';
            if (api == 'mybloglog') {
               url = 'http://pipes.yahoo.com/kentbrew/foafster_';
            } 
            url += api + '?_render=json&_callback=' + callback + '&s=' + id + '&n=' + count;
            $.f.runScript(url, callback);
         },         
         makeNode : function(id, src, title, svc) {
            if (!$.foaf[id]) {
               $.foaf[id] = {};
               $.foaf[id].img = src;
               $.foaf[id].name = title;
               $.foaf[id].svc = svc;
               
               // magic: everybody's picture always shows up in the same spot
               $.r = ($.r * 9301 + 49297) % 233280;
               $.foaf[id].x = (Math.floor($.r / (233280.0) * 125) + 5) * 5;            
               $.r = ($.r * 9301 + 49297) % 233280;
               $.foaf[id].y = (Math.floor($.r / (233280.0) * 125) + 5) * 5;            
            }
         },
         runScript : function(url, id) {
            var s = document.createElement('script');
            s.id = id;
            s.type = 'text/javascript';
            s.charset = 'UTF-8';
            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 id = location.search.replace(/\?/, '');
   if (!id) {
      id = 'kentbrew';
   }
   var kickstart = function() { $.f.init(id); }
   if(typeof window.addEventListener !== 'undefined') {
      window.addEventListener('load', kickstart, false);
   } else if(typeof window.attachEvent !== 'undefined') {
      window.attachEvent('onload', kickstart);
   }
} )();
