// ==UserScript==
// @name         Bloglines2del.icio.us
// @description	 Adds "Post to del.icio.us" link under each item in Bloglines which tags item "_toread" and makes it "not shared".
// @namespace    http://www.longhighway.com/monkeyisland
// @include      http*://www.bloglines.com/myblogs_display*
// @include      http*://bloglines.com/myblogs_display*
// ==/UserScript==
// Based on      http://userscripts.org/scripts/show/3663

(function() {

  function tagIt(postUrl) {
    GM_xmlhttpRequest(
      {method: 'GET',
       url: postUrl,
       onload: function(response) {
         var parser = new DOMParser();
         var doc = parser.parseFromString(response.responseText, "text/xml");
         elements = doc.getElementsByTagName("result");
         if ((elements.length == 0) || (elements[0].getAttribute("code") != "done")) {
           alert("Post to del.icio.us failed.");
         };
       }
    });
  };

  function addDeliciousLinkToItems() {
    //all item links are in <h3><a> tags
    var links = document.evaluate("//h3/a", 
                                  document, 
                                  null,
                                  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                                  null);

    //loop through all the item links on the page
    for (var link = null, i = 0; link = links.snapshotItem(i); i++) { 

      //get actual HREF link
      var linkHREF = link.href;

      //get title of link
      var linkTitle = link.innerHTML;

      //go back up to the parent of the <h3> tag, and find the "Clip/Blog This" link
      // "../..//ul[li/@class='item_date']"
      var itemLinks = document.evaluate("../..//ul[li/a/text()='Clip/Blog This']",
					   link,
					   null,
					   XPathResult.FIRST_ORDERED_NODE_TYPE,
					   null);
       
      var itemLinksNode = itemLinks.singleNodeValue;

      //create new <a> for del.icio.us link
      var deliciousLink = document.createElement("a");
      deliciousLink.innerHTML = "Post to del.icio.us";

      //build del.icio.us Post URL
      var deliciousPostURL = "https://api.del.icio.us/v1/posts/add?&url=" + 
			      encodeURIComponent(linkHREF) + "&description=" +
			      encodeURIComponent(linkTitle) + "&tags=_toread" +
			      "&shared=no"

      deliciousLink.href = deliciousPostURL;
      deliciousLink.addEventListener('click', 
                                     function (event) {
                                       tagIt(event.originalTarget.toString());
                                       this.style.color = "grey";
                                       event.stopPropagation();
                                       event.preventDefault();
                                     },
                                     false);
      //create a new <li> for this link
      var deliciousListItem = document.createElement("li");
      deliciousListItem.appendChild(deliciousLink);
      
      //add this link after the other Item links
      itemLinksNode.appendChild(deliciousListItem);
    }//end for

  }//end addDeliciousLinkToItems()
  
  
  addDeliciousLinkToItems();

})();

