// ?D Shader, released under the GPL
// http://www.gnu.org/copyleft/gpl.html
//
// ==UserScript==
// @name           Hatena Diary Shader
// @description    Allows users to shade diary sections, comments, trackbacks, referers, and neighbors.
// @include        http://d.hatena.ne.jp/*
// @include        http://*.g.hatena.ne.jp/*
// ==/UserScript==
//
// 2007-04-10 (v0.2) Changed the strategy to locate the check-box.
// 2005-08-23 (v0.1) Initial Version.

(function(){
  const sections = document.evaluate('/descendant::DIV[@class="section"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  const comments = document.evaluate('/descendant::DIV[@class="comment" or @class="refererlist"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  const hyperlinks = document.createExpression('descendant::*[@href]', null);
  
  var targetNode = document.evaluate('(/descendant::TABLE/descendant::TABLE/descendant::FONT[1]|id("simple-header")/descendant::FORM|/descendant::DIV[@class="adminmenu"]|/descendant::DIV[@class="calendar"])', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

  if (!targetNode) {
    return;
  }

  var toggleCtrl = targetNode.appendChild(document.createElement('LABEL')).appendChild(document.createElement('INPUT'));
  
  toggleCtrl.type = 'checkbox';
  toggleCtrl.addEventListener('click', function (e) {
    if (toggleCtrl.checked) {
      var setupShader = function (node, isCaption) {
        if (node) {
          if (isCaption(node)) {
            var paddingLeft, paddingTop, paddingRight, paddingBottom;
            var shaded;
            var toggleMode = function (e) {
              if (e.altKey || e.ctrlKey || e.shiftKey) {
                return;
              }
              
              node.style.border = (shaded = !shaded) ? 'thin outset' : 'thin inset';
              node.style.paddingLeft = (shaded ? paddingLeft : paddingLeft + 1) + 'px';
              node.style.paddingTop = (shaded ? paddingTop : paddingTop + 1) + 'px';
              node.style.paddingRight = (shaded ? paddingRight + 1 : paddingRight) + 'px';
              node.style.paddingBottom = (shaded ? paddingBottom + 1 : paddingBottom) + 'px';
              
              (function (node) {
                if (node && !isCaption(node)) {
                  if (node.style) {
                    node.style.display = shaded ? 'none' : 'block';
                  }
                  
                  arguments.callee(node.nextSibling);
                }
              })(node.nextSibling);
            }
            
            if (node.title = (function (node, text) {
              if (node && !isCaption(node) && text.length < 256) {
                return arguments.callee(node.nextSibling, text += node.textContent.replace(/^\s*(.*?)\s*$/m, '$1'));
              }
              else {
                return text;
              }
            })(node.nextSibling, '')) {
              var style = document.defaultView.getComputedStyle(node, '');

              paddingLeft = parseFloat(style.getPropertyValue('padding-left'));
              paddingTop = parseFloat(style.getPropertyValue('padding-top'));
              paddingRight = parseFloat(style.getPropertyValue('padding-right'));
              paddingBottom = parseFloat(style.getPropertyValue('padding-bottom'));
              
              node.style.cursor = 'pointer';
              node.addEventListener('click', toggleMode, false);

              var links = hyperlinks.evaluate(node, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
              
              for (var i = 0; i < links.snapshotLength; ++i) {
                with (links.snapshotItem(i)) {
                  addEventListener('click', function (e) {
                    e.preventDefault();
                  }, false);
                  addEventListener('dblclick', function (e) {
                    location.assign(href);
                  }, false);
                }
              }
              
              toggleMode({});
            }
          }
          
          arguments.callee(node.nextSibling, isCaption);
        }
      }
      
      for (var i = 0; i < sections.snapshotLength; ++i) {
        setupShader(sections.snapshotItem(i).firstChild, function (node) {
          return node.tagName == 'H3';
        });
      }

      for (var i = 0; i < comments.snapshotLength; ++i) {
        setupShader(comments.snapshotItem(i).firstChild, function (node) {
          return node.className == 'caption';
        });
      }
    }
    else {
      location.reload(false);
    }
  }, false);
  toggleCtrl.parentNode.appendChild(document.createTextNode('\u898b\u51fa\u3057\u30e2\u30fc\u30c9'));
})()

