(function() {
  var scripts = document.getElementsByTagName('script'), script_tag,args={},debug=/[?&]ld=/.test(location.href);

  for(var i=scripts.length-1; i >= 0; i--) {
    if(scripts[i].src.indexOf("/plugin/show") > -1) {
      script_tag = scripts[i]
      break;
    }
  };

  var vars = script_tag.src.split("?").pop().split("&");

  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    args[pair[0]] = decodeURI(pair[1]).replace(/\+/g, ' ');  // decodeURI doesn't expand "+" to a space
  }

  if (debug) {
    for(var i=0; i<scripts.length; i++) { console.log(i + ". " + scripts[i].src);}
    console.log(script_tag);
    console.log(vars);
  }

  var
    listlyPlugin = { list_id: args.list, src: script_tag.src, target_id: args.target_id || "listly_" + args.list + "_" + Math.floor(Math.random()*10001),
    item_id: args.item, layout: args.layout || "full", show_tools: args.show_tools || "true", show_header: args.show_header || "true", page: args.page || 1,
    per_page: args.per_page || 25, show_item_numbers: args.show_item_numbers || "true", show_sharing: args.show_sharing || "true",
    show_author: args.show_author || "true", show_item_footer:  args.show_item_footer || "true", show_item_voting: args.show_item_voting || "true",
    show_item_relist: args.show_item_relist || "true", show_item_comments: args.show_item_comments || "true", show_item_timestamp: args.show_item_timestamp || "false", show_item_filter: args.show_item_filter || "true", show_item_tabs: args.show_item_tabs || "true",
    show_item_sort: args.show_item_sort || "true", show_item_layout: args.show_item_layout || true, show_item_search: args.show_item_search || "true", 
    show_list_headline: args.show_list_headline || "true", show_list_title: args.show_list_title || "true", show_list_description: args.show_list_description || "true",  show_list_tools: args.show_list_tools || "true",
    show_list_tools: args.show_list_tools || "true", show_list_stats: args.show_list_stats || "true", show_list_badges: args.show_list_badges || "true" };

  script_tag.setAttribute("rel", listlyPlugin.target_id);

  if (window.ListlyPluginOnPage) {
    if (Listly.ready) { new Listly.ListlyPlugin(listlyPlugin);} else { Listly.Queue.push(listlyPlugin);}
  } else {

    ListlyPluginOnPage = true;

    if (!window.Listly) {
      Listly={Site: false}
    }

    Listly.Settings = {
      ROOT: window.location.protocol + "//list.ly",
      CDN: "//d28efpdu2tk2gz.cloudfront.net",
      DOMAIN: ".list.ly"
    };

    Listly.Settings.JS = Listly.Settings.ROOT + "/assets/plugin_listly.js",
    Listly.Settings.CSS = Listly.Settings.ROOT + "/assets/plugin_listly.css"

    Listly.Queue = [];Listly.Net = [];Listly.Plugins = {};
    Listly.ListlyPlugin = listlyPlugin;

    /* Load Listly Widget Code */
    var lp = document.createElement("SCRIPT"); lp.type = "text/javascript"; lp.async = true;
    lp.src = Listly.Settings.ROOT + "/assets/plugin.js";

    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(lp);
  }

})();