// twitterati badge --  updated 10Jun2008
// as always, use at your own risk
// copyright Kent Brewster 2008
// see http://kentbrewster.com/twitterati for info
( function() 
  { 
    var trueName = '';
    for (var i = 0; i < 16; i++) 
    { 
       trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97); 
    }
    window[trueName] = {};
    var $ = window[trueName];

    if ($.executed)
    {
        return;
    }

    $.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.replace(/\/\/ <\!\[CDATA\[/g, '').replace(/\/\/ \]\]&gt;/g, ''));
                             }
                             if ($.a.err) 
                             {
                                 alert('bad json! ' + theScripts[i].innerHTML.replace(/\/\/ <\!\[CDATA\[/g, '').replace(/\/\/ \]\]&gt;/g, ''));
                             }
                             $.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":"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"};
                          }
                     },
          loadDefaults : function() 
                         {
                             $.d = { 
                                      "list_id":46,
                                      "item_number":5,
                                      "height" : 300,
                                      "width" : 200,
                                      "padding": 2,
                                      "background" : "#fff",
                                      "color": "#333",
                                      "frame_color":"#ddc"
                                   };
                             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 = {
                                               "" : "{zoom:1;margin:0;padding:0;width:" + ($.a.width) + "px;background:" + $.a.background + ";border:2px solid " + $.a.frame_color + ";font:11px tahoma, veranda, arial, helvetica, clean, sans-serif;*font-size:small;*font:x-small;text-transform:none;}",
                                               "a" : "{cursor:pointer;text-decoration:none;}",
                                               "img":"{border:0;}",
                                               "h3" : "{font-size:140%;font-family:tahoma,veranda,arial,helvetica,clean,sans-serif;line-height:normal;text-align:left;border:0px;margin:0!important;padding:2px 2px 4px 22px;font-weight:bold;background:" + $.a.frame_color + " url('http://media.librairiedialogues.fr/39/style/images/favicon.png') " + $.a.padding + "px 2px no-repeat; color:"+$.a.color+"}",
                                               "h3.loading" : "{background-image:url('http://l.yimg.com/us.yimg.com/i/us/my/mw/anim_loading_sm.gif')}",
                                               "h4, h4 a.shoplink, h4 a.shoplink:visited" : "{font-size:100%;font-weight:normal;font-family:tahoma,veranda,arial,helvetica,clean,sans-serif;background:" + $.a.frame_color + ";text-align:right;margin:0;padding:" + $.a.padding + "px;border:0px;color:"+$.a.color+";text-transform:none;}",
                                               "h4 a.shoplink:hover":"{padding:" + $.a.padding + "px;text-decoration:underline;}",
                                               "ul.p_list":"{margin:0!important; padding:0!important; height:" + $.a.height + "px;width:" + $.a.width + "px;overflow:auto;}",
                                               "ul.p_list li":"{font-size:100%;margin:0!important;padding:"+$.a.padding+"px!important;list-style:none;background:transparent;border:0px;line-height:normal;}",
                                               "ul.p_list li.p_list_item":"{float:left;width:" + ($.a.width - 22) + "px;}",
                                               "div.bookcover":"{padding:0;float:left;width:60px;}",
                                               "div.bookcover a, div.bookcover a:hover":"{padding:0;background:transparent;border:0px;}",
                                               "ul.bookdesc":"{margin:0 0 0 62px;padding:0;border-bottom:1px solid #ccc;text-align:left;}",
                                               "li.bookdesc a":"{padding:0;background:transparent;color:#666;border:0px;text-align:left;}",
                                               "li.bookdesc a:hover":"{padding:0;text-decoration:underline;}",
                                               "li.booktitle a":"{padding:0;background:transparent;color:#000;font-size:110%;text-align:left;}"
                                              };
                                  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.id=trueName;
                               $.w.h = document.createElement('H3');
                               $.w.h.innerHTML = '...';
                               $.w.appendChild($.w.h);
                               $.w.r = document.createElement('UL');
                               $.w.r.className = "p_list";
                               $.w.appendChild($.w.r);
                               $.w.f = document.createElement('H4');
                               var a = document.createElement('A');
                               a.innerHTML = 'librairiedialogues.fr';
                               a.target = '_blank';
                               a.className="shoplink";
                               a.href = 'http://www.librairiedialogues.fr';
                               $.w.f.appendChild(a);
                               $.w.appendChild($.w.f);
                               $.f.runSearch();
                          },
         runSearch : function() 
                     {
                         $.w.h.className = 'loading';
                         $.w.r.innerHTML = '';
                         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); 
                         };
                         var url = 'http://www.librairiedialogues.fr/ws/widgets/list/';
                         url += $.a.list_id + '/';
                         url += $.a.item_number + '/';
                         url += id + '/';
                         if ($.a.affiliate_key) 
                         {
                              url += "?affiliate_key=" + $.a.affiliate_key;
                         }
                         
                         $.f.runScript(url, id);
                     },
         renderResult: function(r)
                       { 
                           $.w.h.className = '';
                           if ($.a.title)
                           {
                               $.w.h.innerHTML = $.a.title;
                           } 
                           else
                           {
                               $.w.h.innerHTML = r.screen_name + " / " + r.list_title;
                           }
                          for (var i = 0; i < r.items.length; i++) 
                          {
                               var product = r.items[i];
                               var li = document.createElement('LI');
                               li.className = "p_list_item";

                               var cover = document.createElement('DIV');
                               cover.className = 'bookcover';
                               var a = document.createElement('A');
                               a.href = product.url;
                               a.target = '_blank'; 
                               var img = document.createElement('IMG');
                               img.src = product.cover_url;
                               img.title = product.title;
                               a.appendChild(img);
                               cover.appendChild(a);
                               li.appendChild(cover);

                               var desc = document.createElement('UL');
                               desc.className = 'bookdesc';
                               var metadatas = ['title', 'author', 'publisher'];

                               for(var metadata=0; metadata<metadatas.length; metadata++ )
                               {
                                   var descli = document.createElement('LI');
                                   descli.className="bookdesc book" + metadatas[metadata];
                                   var a = document.createElement('A');
                                   a.innerHTML = product[metadatas[metadata]]; 
                                   a.href = product.url;
                                   descli.appendChild(a);
                                   desc.appendChild(descli);
                               }
                               li.appendChild(desc);

                               $.w.r.appendChild(li);
                          }         

                          $.executed = true;
                       },
          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 = /userlist.js$/;

  if(document.getElementById('sandboxexportlist'))
  {
     $.f.init(thisScript);
  }
  
  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); });
    }
} 
)();
