// ==UserScript==
// @name          Google Reader UI Tweek
// @description	 Google Reader Fix Scripts
// @author        Shawn
// @include       http://www.google.com/reader/view/*
// @include       https://www.google.com/reader/view/*
// ==/UserScript==
(function(){var A='@namespace url(http://www.w3.org/1999/xhtml);body{font-family:Lucida Grande,Helvetica,Arial,sans-serif;background:#fff}#nav{width:220px;position:absolute;top:-26px}#lhn-add-subscription{margin:3px 0 2px 10px}#chrome{margin-left:220px}#gbar,#logo-container,#viewer-footer{display:none}#search{top:0;left:180px}#main{top:24px}#search-input{height:16px;margin:3px 4px 0 0;padding:0}#search-restrict-button,#search-submit{margin:1px 0 0}.lhn-section-footer{background:#f8f8f8;border-top:1px solid #ccc}.scroll-tree{font-size:12px;line-height:2}#sub-tree{width:220px}.scroll-tree .icon{top:4px}#current-entry .card{border-color:#ccc}#sub-tree ul ul li a{padding-left:19px}#viewer-top-controls{padding:1px 3px}#chrome-title{font-size:14px;line-height:1}#chrome-header{padding:2px 11px}';if(typeof GM_addStyle!="undefined"){GM_addStyle(A)}else{if(typeof addStyle!="undefined"){addStyle(A)}else{var C=document.getElementsByTagName("head");if(C.length>0){var B=document.createElement("style");B.type="text/css";B.appendChild(document.createTextNode(A));C[0].appendChild(B)}}}})();
var g_totaltext;function $(A){return document.getElementById(A)}function calcUnread(){var D=new Array();var J=document.evaluate("//li[contains(@class, 'folder')]//li[contains(@class, 'folder')]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);var K=0;var A=false;for(var F=0;F<J.snapshotLength;F++){var B=document.evaluate(".//li[contains(@class, 'unread')]/a/span/span[contains(@class, 'unread-count')]",J.snapshotItem(F),null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);var L=0;var G=false;for(var E=0;E<B.snapshotLength;E++){var C=B.snapshotItem(E).innerHTML.match(/\((\d*)\+?\)/);if(C){var I=parseInt(C[1],10);L+=I;if(B.snapshotItem(E).innerHTML.match(/\(100\+\)/)){G=true;A=true}if(D.indexOf(B.snapshotItem(E).parentNode.parentNode.href)<0){K+=I;D[D.length]=B.snapshotItem(E).parentNode.parentNode.href}}}if(L>0){var H=document.evaluate(".//a/span/span[contains(@class, 'unread-count')]",J.snapshotItem(F),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;if(H){H.innerHTML="&nbsp;("+L+(G?"+":"")+")"}}}if(K>0){g_totaltext=K+(A?"+":"");$("reading-list-unread-count").innerHTML=" ("+g_totaltext+")"}}function modifySubtree(){if($("reading-list-unread-count").textContent.match(/1000\+/)){calcUnread()}}if($("reading-list-selector")){$("reading-list-unread-count").addEventListener("DOMSubtreeModified",modifySubtree,false);window.setTimeout(modifySubtree,5000);window.setInterval(function(){calcUnread();if(g_totaltext){document.title=document.title.replace(/1000\+/,g_totaltext).replace("Google Reader","GReader")}},3000)};unsafeWindow.document.watch("title",function(C,A,B){if(matches=B.match(/Google Reader \((\d+\+?)\)/)){B="("+matches[1]+") Google Reader"}return(B)});var GReaderUnreadCount={isChrome:false,totaltext:"",init:function(){var A=true;if(navigator.userAgent.match(/Chrome/)){A=document.location.href.match(/https?:\/\/www\.google\.com(\.[a-z]+)?\/reader\/view/)?true:false;GReaderUnreadCount.isChrome=true}if(A&&document.body){GReaderUnreadCount.waitForReady()}},waitForReady:function(){if(document.getElementById("reading-list-unread-count")){document.getElementById("reading-list-unread-count").addEventListener("DOMSubtreeModified",GReaderUnreadCount.modifySubtree,false);window.setTimeout(GReaderUnreadCount.modifySubtree,5000);window.setInterval(function(){GReaderUnreadCount.calcUnread();if(GReaderUnreadCount.totaltext){document.title=document.title.replace(/1000\+/,GReaderUnreadCount.totaltext).replace("Google Reader","GReader")}},3000)}else{setTimeout(GReaderUnreadCount.waitForReady,500)}},modifySubtree:function(){if(document.getElementById("reading-list-unread-count").textContent.match(/1000\+/)){GReaderUnreadCount.calcUnread()}},findItemUnread:function(A,E){var F=false;var D=0;var B=false;var C=E.innerHTML.match(/\((\d*)\+?\)/);if(C){D=parseInt(C[1],10);if(E.innerHTML.match(/\(1000\+\)/)){F=true}if(A.indexOf(E.parentNode.parentNode.href)<0){A[A.length]=E.parentNode.parentNode.href}else{B=true}}return{count:D,hasplus:F,alreadyCounted:B}},calcUnread:function(){var C=new Array();var H=document.evaluate("//li[contains(@class, 'folder')]//li[contains(@class, 'folder')]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);var I=0;var A=false;for(var E=0;E<H.snapshotLength;E++){var B=document.evaluate(".//li[contains(@class, 'unread')]/a/span/span[contains(@class, 'unread-count')]",H.snapshotItem(E),null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);var J=0;var F=false;for(var D=0;D<B.snapshotLength;D++){var K=GReaderUnreadCount.findItemUnread(C,B.snapshotItem(D));if(K.hasplus){A=true;F=true}J+=K.count;if(!K.alreadyCounted){I+=K.count}}if(J>0){var G=document.evaluate(".//a/span/span[contains(@class, 'unread-count')]",H.snapshotItem(E),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;if(G){G.innerHTML="&nbsp;("+J+(F?"+":"")+")"}}}var B=document.evaluate("//ul[@id='sub-tree']/li/ul/li[not(contains(@class, 'folder')) and contains(@class, 'unread')]/a/span/span[contains(@class, 'unread-count')]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);for(var D=0;D<B.snapshotLength;D++){var K=GReaderUnreadCount.findItemUnread(C,B.snapshotItem(D));if(K.hasplus){A=true}if(!K.alreadyCounted){I+=K.count}}if(I>0){GReaderUnreadCount.totaltext=I+(A?"+":"");document.getElementById("reading-list-unread-count").innerHTML=" ("+GReaderUnreadCount.totaltext+")"}}};GReaderUnreadCount.init();function setRuleStyle(A,B){var E=document.styleSheets;var F;var D;for(i=0;i<E.length;i++){F=E[i].cssRules||E[i].rules;for(var C=0;C<F.length;C++){if(F[C].selectorText&&F[C].selectorText.indexOf(A)!=-1){D=F[C].style.cssText=B;break}}}}setRuleStyle(".entry .entry-body, .entry .entry-title","max-width: 100%;");var Favicons={UNFIXED_ICON_XPATH:'//span[contains(@class,"icon sub-icon")][not(@label)]',EXPORT_URL:"/reader/subscriptions/export",FAVICON_URL:["http://","/favicon.ico"],PARSE_DOMAIN:/:\/\/([\w\.]+)/,domains:{},cacheIcons:{},loadDomains:function(){var A=new XMLHttpRequest();A.open("get",this.EXPORT_URL,true);A.onload=function(){Favicons.setDomains(A.responseXML)};A.send("")},parseDomain:function(B){var A=this.PARSE_DOMAIN.exec(B);return A&&A[1]},setDomains:function(E){var C,B=0,A=E.getElementsByTagName("outline");while(C=A[B++]){if(!C.hasAttribute("htmlUrl")){continue}var D=C.getAttribute("title");if(D.length>24){D=D.substr(0,21)+"..."}this.domains[D]=this.parseDomain(C.getAttribute("htmlUrl"))||this.parseDomain(C.getAttribute("xmlUrl"))}setInterval(function(){Favicons.fixAllIcons()},2000)},fixAllIcons:function(){var D,C=0,B;var E=document.evaluate(this.UNFIXED_ICON_XPATH,document,null,6,null);while(D=E.snapshotItem(C++)){B=D.nextSibling.firstChild.textContent;var F=this.FAVICON_URL.join(this.domains[B]);var A=document.createElement("img");D.appendChild(A);D.setAttribute("label",B);A.setAttribute("style","display:none");A.src=F;A.addEventListener("error",function(G){G.target.parentNode.removeChild(G.target)},false);A.addEventListener("load",function(H){var G=["background: url(",");"];H.target.parentNode.setAttribute("style",G.join(H.target.src));H.target.parentNode.removeChild(H.target)},false)}}};Favicons.loadDomains();