// Add Article Titles to System iNetwork archives
// version 1.0  2009-04-09 
// --------------------------------------------------------------------
// Copyright (c) 2009, Scott C. Klement
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//     * Redistributions of source code must retain the above copyright 
//         notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright 
//         notice, this list of conditions and the following disclaimer 
//         in the documentation and/or other materials provided with the 
//         distribution.
//     * Neither the name of the Scott C. Klement nor the names of its 
//         contributors may be used to endorse or promote products derived
//         from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasespot.net/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "System iNetwork Colors", and click Uninstall.
//
// --------------------------------------------------------------------
// ==UserScript==
// @name           ArticleTitles
// @namespace      http://www.scottklement.com/scripts/articletitles
// @description    Add Article Titles to Newsletter Index
// @include        http://systeminetwork.com/archivesearch/issue/*
// @include        http://systeminetwork.com/sism
// @include        http://systeminetwork.com/sipt
// @include        http://systeminetwork.com/nd
// @include        http://systeminetwork.com/systeminews
// @include        http://systeminetwork.com/myi
// @include        http://systeminetwork.com/inwuk
// @include        http://systeminetwork.com/rpgcoder
// ==/UserScript==

GM_addStyle('.sckarticles { list-style-type: none; }');
GM_addStyle('.sckarticles li a{ color: #88801E; font-size: 90%; }');

var allLinks, thisLink;

   allLinks = document.evaluate(
       '//a[@href]',
       document,
       null,
       XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
       null);

   for (var i = 0; i < allLinks.snapshotLength; i++) {
       thisLink = allLinks.snapshotItem(i);
       if (thisLink.href.indexOf("/archivesearch/issue") != -1
           && thisLink.innerHTML.length>6 ) {
           var artTitles = GM_getValue(thisLink.href, "null");
           if (artTitles.length > 20) {
              setTitles(thisLink, artTitles);
           } else {
              get(thisLink.href, getTitles);
           }
       }
   }



function getTitles(xhr) {
   var text = xhr.responseText;
   var start = text.search("<ul class='resultissuearticles'>");
   var text2 = text.slice(start);
   var end   = text2.search("</ul>");
   var text3 = text2.slice(0, end+5);
   var text4 = text3.replace("resultissuearticles", "sckarticles");
   var allLinks, thisLink;

   allLinks = document.evaluate(
       '//a[@href]',
       document,
       null,
       XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
       null);

   for (var i = 0; i < allLinks.snapshotLength; i++) {
       thisLink = allLinks.snapshotItem(i);
       var testURL = xhr.finalUrl;
       if (testURL == thisLink.href) {
          GM_setValue(thisLink.href, text4);
          setTitles(thisLink, text4);
       }
   }
}

function setTitles(afterThis, titles) {
   var newdiv = document.createElement("div");
   newdiv.innerHTML = titles;
   afterThis.parentNode.appendChild(newdiv);
} 

function get(gurl, cb) {
  GM_xmlhttpRequest({
    method: "GET",
     url: gurl,
     onload: function(xhr) { cb(xhr); }
  });
}
