( function() {

  // create a random global variable to run everything under
  var trueName = "";
  for (var i = 0; i < 16; i++) { trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97); }
  window[trueName] = {};
  // create a private pointer so you don't have to keep saying window[trueName]
  var $ = window[trueName];

  // $.f is your main function
  $.f = function() {
    return {
      runFunction : [],
      init : function(target) {
        var theScripts = document.getElementsByTagName("SCRIPT");
        for (var i = 0; i < theScripts.length; i++) {
          // if you find yourself
          if (theScripts[i].src.match(target)) {
            // make a container for user arguments
            $.a = {};
            // parse the innerHTML into arguments
            if (theScripts[i].innerHTML) { $.a = $.f.parseJson(theScripts[i].innerHTML); }
            // create a DIV to run inside
            $.w = document.createElement("DIV");
            theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
            // remove yourself
            theScripts[i].parentNode.removeChild(theScripts[i]);
            // make and run the rest of the badge
            $.f.buildStructure();
            // quit looping; you are done
            break;
          }
        }
      },
      // safe JSON parser
      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,Data,Object,Function,",
              "Array,String,Math,RegExp,Image,ActiveXObject;",
              "return (" , json.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function') , ");"].join(""));
          return f();
        } catch(e) {
          return {"err":"trouble parsing JSON object"};
        }
      },
      // insertCSS
      insertCSS : function(rules) {
        var ns = document.createElement("style");
        document.getElementsByTagName("head")[0].appendChild(ns);
        // Safari does not see the new stylesheet unless you append something to it
        // However IE will blow chunks, so ... filter it thusly:
        if (!window.createPopup) {
          ns.appendChild(document.createTextNode(""));
          ns.setAttribute("type", "text/css");
        }
        var s = document.styleSheets[document.styleSheets.length - 1];
        var ieRules = "";
        for (var r in rules) {
          var selector = "." + trueName + " " + r;
          if (!window.createPopup) {
            var rule = document.createTextNode(selector + rules[r]);
            ns.appendChild(rule);
          } else {
            ieRules += selector + rules[r];
          }
        }
        if (window.createPopup) {
          s.cssText = ieRules;
        }
      },
      // build chrome
      buildStructure : function() {
        $.d = {
          "title" : "TOP BUZZ",
          "width" : "300px",
          "height" : "auto",
          "border" : "0px solid #DBDBDB",
          "backgroundColor" : "transparent",
          "padding" : 0,
          "titleColor" : "#404040",
          "titleBackgroundColor" : "#ffffff",
          "titleBackgroundImage" : "http://us.i1.yimg.com/us.yimg.com/i/us/shc/gr/buzz_widget_gradient.png",
          "cornerBackgroundImage" : "http://l.yimg.com/us.yimg.com/i/us/shc/gr/rc5.png"
        };
        // assign defaults if not present in user args
        for (var k in $.d) {
          if ($.a[k] === undefined) {
            $.a[k] = $.d[k];
          }
        }
        $.w.className = trueName;
        $.w.style.display = 'none';
        if ($.a.addClassName !== undefined) { try { $.w.className += " " + $.a.addClassName; } catch(eClassName) { } }
        if ($.a.id !== undefined) { try { $.w.id = $.a.id; } catch(eId) {} }
        if ($.a.width !== undefined) { try { 
          if (/^\s*\d+\s*$/.test($.a.width)) { $.a.width += "px"; }
          $.w.style.width = $.a.width;
        } catch(eWidth) { } }
        if ($.a.height !== undefined && $.a.height != 'auto') { try {
          if (/^\s*\d+\s*$/.test($.a.height)) { $.a.height += "px"; }
          $.w.style.height = $.a.height;
        } catch (eHeight) { } }

        var rules = {
          "" : "{zoom:1; background-color:" + $.a.backgroundColor + ";border:" + $.a.border + ";text-align:left;}",
          "*" : "{font:normal 11px arial;line-height:1.0em;margin:0;padding:0;}",
          "a" : "{cursor:pointer;text-decoration:none;}",
          "a:hover" : "{text-decoration:underline;}",
          ".ycsc" : "{margin:" + $.a.padding + "px;}",
          ".ycsb" : "{margin:0;text-align:center;line-height:1.0em;border-left:5px solid #DBDBDB;border-right:5px solid #DBDBDB;}",
          ".ycst" : "{margin:0;padding:5px 8px;display:block;font:normal 13px arial;text-align:left;}",
          ".ycst img" : "{vertical-align:middle;margin:0 6px 0 0;padding:0;border:0;display:inline;}",
          ".ycst #ycstt" : "{font-size:13px;font-weight:bold;position:relative;top:2px;color:"+$.a.titleColor+";}",
          ".ycstc" : "{position:relative;line-height:0;font-size:0;height:5px}",
          ".ycstcl" : "{width:5px;height:5px;margin:0;padding:0;background-color:transparent;background-image:url(" + $.a.cornerBackgroundImage + "); background-position:top left; background-repeat: repeat-none;position:absolute;left:0px;top:0px;line-height:0;font-size:0;}",
          ".ycstcc" : "{height:5px;margin:0;padding:0;background-color:#DBDBDB;margin:0px 5px;line-height:0;font-size:0;}",
          ".ycstcr" : "{width:5px;height:5px;margin:0;padding:0;background-color:transparent;background-image:url(" + $.a.cornerBackgroundImage + "); background-position:top right; background-repeat: repeat-none;position:absolute;right:0px;top:0px;line-height:0;font-size:0;}",
          ".ycsh" : "{margin:0;padding:0;background-color:" + $.a.titleBackgroundColor + ";background-image:url(" + $.a.titleBackgroundImage + "); background-position:bottom left; background-repeat: repeat-x; color:" + $.a.titleColor + ";border-left:5px solid #DBDBDB;border-right:5px solid #DBDBDB;border-bottom:5px solid #DBDBDB;position:relative;}",
          ".ycsh .ycst a" : "{text-decoration:none;color:" + $.a.titleColor + ";}",
          ".ycsh .ycst a:hover" : "{text-decoration:none;color:" + $.a.titleColor + ";}",
          ".ycsh .ycst a:visited" : "{text-decoration:none;color:" + $.a.titleColor + ";}",
          ".ycsf" : "{margin:0;padding:0;font:normal 10px verdana;background-color:#f2f2f2;text-align:left;border-left:5px solid #DBDBDB;border-right:5px solid #DBDBDB;}",
          ".ycsfa" : "{margin:0;padding:2px 8px;border-top:1px solid #ccc;}",
          ".ycsc .ycsf a" : "{color:#666;text-decoration:none;}",
          ".ycsc .ycsf a:hover" : "{color:#666;text-decoration:underline;}",
          ".ycsbc" : "{position:relative;line-height:0;font-size:0;height:5px;}",
          ".ycsbcl" : "{width:5px;height:5px;margin:0;padding:0;background-color:transparent;background-image:url(" + $.a.cornerBackgroundImage + ");background-position:bottom left;background-repeat:repeat-none;position:absolute;left:0px;bottom:0px;line-height:0;font-size:0;}",
          ".ycsbcc" : "{height:5px;margin:0;padding:0;background-color:#DBDBDB;overflow:hidden;margin:0px 5px;line-height:0;font-size:0;}",
          ".ycsbcr" : "{width:5px;height:5px;margin:0;padding:0;background-color:transparent;background-image:url(" + $.a.cornerBackgroundImage + ");background-position:bottom right;background-repeat:repeat-none;position:absolute;right:0px;bottom:0px;line-height:0;font-size:0;}"
        };
        this.insertCSS(rules);

        // inner container
        $.w.c = document.createElement("DIV");
        $.w.c.className = "ycsc";
        // top corner
        $.w.tc = document.createElement("DIV");
        $.w.tc.className = "ycstc";
        // top left corner
        $.w.tcl = document.createElement("DIV");
        $.w.tcl.className = "ycstcl";
        $.w.tc.appendChild($.w.tcl);
	// top center
        $.w.tcc = document.createElement("DIV");
        $.w.tcc.className = "ycstcc";
        $.w.tc.appendChild($.w.tcc);
        // top right corner
        $.w.tcr = document.createElement("DIV");
        $.w.tcr.className = "ycstcr";
        $.w.tc.appendChild($.w.tcr);
        $.w.c.appendChild($.w.tc);
        // header
        $.w.h = document.createElement("DIV");
        $.w.h.className = "ycsh";
        // title
        if ($.a["title"] !== undefined) {
          $.w.t = document.createElement("SPAN");
          $.w.t.innerHTML = "<a href=\"http://buzz.yahoo.com/\" target=\"_blank\"><img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/shc/gr/buzz-icon_new.png\" width=\"16\" height=\"16\"/><span id=\"ycstt\">" + $.a["title"] + "</span></a>";
          $.w.t.className = "ycst";
	  
          $.w.h.appendChild($.w.t);
        }
        $.w.c.appendChild($.w.h);
        // body
        $.w.b = document.createElement("DIV");
        $.w.b.className = "ycsb";
        $.w.c.appendChild($.w.b);
        // footer
        $.w.f = document.createElement("DIV");
        $.w.f.className = "ycsf";
	var url = window.location;
	var trackingLink = '<img src="http://fe.shortcuts.search.yahoo.com/widget/v1/track.php?wid=ym63v4ow' + '&domain=' + url.hostname + '&url=' + encodeURIComponent(url) + '&category=all&ts=' + new Date().getTime() + '" alt="" style="display:none"></img>';
        $.w.f.innerHTML = "<div class=\"ycsfa\"><a href=\"http://fe.shortcuts.search.yahoo.com/widget/buzz.php\">Get this widget...</a>" + trackingLink + "</div>";
	
        $.w.c.appendChild($.w.f);
        $.w.appendChild($.w.c);
        // bottom corner
        $.w.bc = document.createElement("DIV");
        $.w.bc.className = "ycsbc";
        // bottom left corner
        $.w.bcl = document.createElement("DIV");
        $.w.bcl.className = "ycsbcl";
        $.w.bc.appendChild($.w.bcl);
	// bottom center
        $.w.bcc = document.createElement("DIV");
        $.w.bcc.className = "ycsbcc";
        $.w.bc.appendChild($.w.bcc);
        // bottom right corner
        $.w.bcr = document.createElement("DIV");
        $.w.bcr.className = "ycsbcr";
        $.w.bc.appendChild($.w.bcr);
        $.w.c.appendChild($.w.bc);

        // module content 
        $.w.i = $.m.render();
        if ($.w.i != null) {
          $.w.b.appendChild($.w.i);
          $.w.style.display = "block";
          $.w.i.style.overflow = "hidden";
        }

        //$.w.i.style.width = ($.w.clientWidth - 2 * $.a["padding"]) + "px";
        //$.w.i.style.height = ($.w.clientHeight - 2 * $.a["padding"] - $.w.h.offsetHeight - $.w.f.offsetHeight) + "px";
      },
      // create a new script node
      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);
      }
    };
  }();
  // module content
  $.m = {
    render : function() {
      if (this.data.length == 0) {
        return null;
      }

      // shortcuts to document to save typing
      var d = document;

      // styles
      var css = {
        "#ycs-buzz table a" : "{color:#005a92;text-decoration:none;text-transform:none;}",
        "#ycs-buzz table a:hover" : "{color:#005a92;text-decoration:underline;}",
        "#ycs-buzz" : "{background-color:#fff;padding:3px 8px 0 8px;margin:0;zoom:1;}",
        "#ycs-buzz table" : "{border-bottom:1px solid #e2e2e2;padding:0;width:100%}",
        "#ycs-buzz .ycs-img" : "{vertical-align:top;padding:5px 5px 5px 0;width:90px;overflow:hidden;}",
        "#ycs-buzz .ycs-img img" : "{width:86px;height:64px;padding:1px;border-top:1px solid #009eff;border-left:1px solid #009eff;border-right:1px solid #0060ac;border-bottom:1px solid #0060ac;}",
        "#ycs-buzz .ycs-foot" : "{margin:0;padding:4px 0;text-align:right;font:normal 10px verdana;}",
        "#ycs-buzz .ycs-foot em" : "{font-weight:bold;font-style:normal;}",
        "#ycs-buzz .ycs-foot a" : "{color:#808080;}",
        "#ycs-buzz .ycs-foot a:hover" : "{color:#808080;text-decoration:underline;}",
        "#ycs-buzz .ycs-pub *" : "{font:normal 10px verdana;line-height:12px;display:inline;}",
        "#ycs-buzz .ycs-pt" : "{font:normal 10px verdana;color:#999;}",
        "#ycs-buzz .ycs-st" : "{padding:5px 0;color:#333;vertical-align:top;text-align:left;}",
        "#ycs-buzz .ycs-ab" : "{font:normal 11px verdana;color:#333;}",
        "#ycs-buzz .ycs-more a" : "{font:bold 10px verdana;}",
        "#ycs-buzz .h3" : "{margin:0;padding:0;border:0;min-width:0;display:inline;line-height:1.0em;}",
        "#ycs-buzz .h3 *" : "{font:bold 13px arial;color:#333;display:inline;}",
        "#ycs-buzz .h3 a" : "{color:#005a92;}",
        "#ycs-buzz .h3 a:hover" : "{color:#005a92;}"
      }
      $.f.insertCSS(css);

      // wrapper
      var a = d.createElement("DIV");
      a.id = "ycs-buzz";
      for (var i = 0; i < this.data.length; i++) {
        var da = this.data[i];
        if (!da['title'] || !da['link']) { continue; }
        // the story element
        // it's lame to use a table; but i couldn't make IE6 
        // happy otherwise
        var s = d.createElement("TABLE");
        s.cellSpacing = 0;
        var tb = d.createElement("TBODY");
        var tr = d.createElement("TR");
        tb.appendChild(tr);
        s.appendChild(tb);

        // the image
        var imgD = null;
        if (da['thumbnail'] !== undefined) {
          imgD = d.createElement("TD");
          imgD.className = "ycs-img";
          var imgLink = d.createElement("A");
          imgLink.href = da['link'];
          var imgElem = d.createElement("IMG");
          imgElem.src = da['thumbnail'];
          imgLink.appendChild(imgElem);
          
          imgD.appendChild(imgLink);
          tr.appendChild(imgD);
        }

        // the title
        var storyDiv = d.createElement("TD");
        storyDiv.className = "ycs-st";
        var t = d.createElement("DIV");
        t.className = "h3";
        var ta = d.createElement("A");
        ta.href = da['link'];
        ta.innerHTML = da['title'];
        
        t.appendChild(ta);
        storyDiv.appendChild(t);
        var publisher = d.createElement("SPAN");
        publisher.className = "ycs-pub";
        publisher.innerHTML = " -&nbsp;<a href=\"" +da['publisherUrl'] + "\">" + da['publisher'].replace(' ', '&nbsp;') + "</a>";
        
        storyDiv.appendChild(publisher);
        storyDiv.appendChild(d.createElement("BR"));

        // abstract
        if (da['description'] !== undefined) {
          var ab = d.createElement("DIV");
          var pt = d.createElement("SPAN");
          pt.className = "ycs-pt";
          pt.innerHTML = da['pubDate'] + " ";
          ab.appendChild(pt);

          var abstract = d.createElement("SPAN");
          abstract.className = "ycs-ab";
          abstract.innerHTML = da['description'];
          ab.appendChild(abstract);

	  /*
          var more = d.createElement("SPAN");
          more.className = "ycs-more";
          var moreLink = d.createElement("A");
          moreLink.href = da['link'];
          moreLink.innerHTML = " &raquo;&nbsp;More";
          more.appendChild(moreLink);
          ab.appendChild(more);
	  */

          storyDiv.appendChild(ab);
        }
        tr.appendChild(storyDiv);

        // append to wrapper
        a.appendChild(s);
      }
      // footer
      var ft = d.createElement("DIV");
      ft.className = "ycs-foot";
      ft.innerHTML = "<a href=\"http://buzz.yahoo.com/\" target=\"_blank\">&raquo;&nbsp;more on <em>Yahoo! Buzz</em></a>";
      
      a.appendChild(ft);
      return a;
    },
    data : [{"title":"Dem officials: Daschle accepts HHS Cabinet...","link":"http:\/\/news.yahoo.com\/s\/ap\/20081119\/ap_on_el_pr\/obama_health_daschle","publisher":"Yahoo! News","publisherUrl":"http:\/\/news.yahoo.com","pubDate":"5 hours ago","thumbnail":"http:\/\/l.yimg.com\/ds\/orion\/us\/y_news\/local\/mc27dv6jzya6u547sf66b45tu4\/thumbnail_image.jpg","description":"AP - Former Senate Majority Leader Tom Daschle has accepted President-elect..."},{"title":"Celebrating Mickey Mouse's 80th Birthday...","link":"http:\/\/www.trendhunter.com\/trends\/mickey-mouse-80","publisher":"Trend Hunter","publisherUrl":"http:\/\/www.TrendHunter.com","pubDate":"5 hours ago","thumbnail":"http:\/\/l.yimg.com\/ds\/orion\/us\/trend_hunter537\/local\/572gct42bykq4endoqqu5qtwee\/thumbnail_image.jpg","description":"Mickey Mouse turned 80, marking a huge anniversary in the history of cartoons...."},{"title":"Auto Industry Heads Flew Private Jets To D.C.","link":"http:\/\/businesssheet.alleyinsider.com\/2008\/11\/auto-industry-heads-flew-private-jets-to-d-c-bailout-begathon","publisher":"Businesssheet.com","publisherUrl":"http:\/\/www.businesssheet.com","pubDate":"3 hours ago","thumbnail":"http:\/\/l.yimg.com\/ds\/orion\/us\/businesssheet264\/local\/7r43shrwtiwbakbw5rzmobop5i\/thumbnail_image.jpg","description":"The U.S.'s three major carmakers may be so close to filing for bankruptcy that..."},{"title":"Nate Silver Cursed Out By Right-Winger","link":"http:\/\/www.huffingtonpost.com\/2008\/11\/19\/nate-silver-cursed-out-by_n_144877.html","publisher":"Huffington Post","publisherUrl":"http:\/\/www.huffingtonpost.com","pubDate":"6 hours ago","thumbnail":"http:\/\/l.yimg.com\/ds\/orion\/us\/huffington_post\/local\/bw4pe4zwgixzmm3okhxppwvnca\/thumbnail_image.jpg","description":"Nate Silver of FiveThirtyEight interviewed John Ziegler, who commissioned a Zogby..."},{"title":"Israel boycotts UN racism conference, claims...","link":"http:\/\/www.salon.com\/wires\/ap\/world\/2008\/11\/19\/D94I6O7O0_ml_israel_un_conference\/index.html?source=rss&aim=wires","publisher":"Salon.com","publisherUrl":"http:\/\/www.salon.com","pubDate":"2 hours ago","thumbnail":"http:\/\/l.yimg.com\/us.yimg.com\/i\/us\/shc\/gr\/buzz_no_img.gif","description":"Israel will boycott a United Nations conference on racism next year, Foreign..."},{"title":"Somali pirates transform villages into...","link":"http:\/\/news.yahoo.com\/s\/ap\/af_pirate_boomtown","publisher":"Yahoo! News","publisherUrl":"http:\/\/news.yahoo.com","pubDate":"7 hours ago","thumbnail":"http:\/\/l.yimg.com\/ds\/orion\/us\/y_news\/local\/6mecjqlu32kkdi3vjz5nidutxe\/thumbnail_image.jpg","description":"AP - Somalia's increasingly brazen pirates are building sprawling stone houses,..."}]
  };
  var thisScript = /(yahoo|yimg)\.com\/(jq\/)?widget\/v1\/ow\/ym63v4ow/;
  var init = function() { $.f.init(thisScript); };
  window.setTimeout(init, 0);
} )();