// ==UserScript==
// @name           HN Comment Embed
// @namespace      HNE
// @description    Embed HN metadata & comments in any webpage that has been submitted to Hacker News.
// @include	   *
// @exclude        http://news.ycombinator.com*
// @exclude	   http://www.google.com*
// @exclude	   http://maps.google.com*
// @exclude	   http://images.google.com*
// @exclude	   http://video.google.com*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @require        http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js
// ==/UserScript==

if (window.top != window.self)  //don't run on frames or iframes. From http://stackoverflow.com/questions/1535404/how-to-exclude-iframe-in-greasemonkey
    return;

version = 8;

$(document).ready(function() {
  
  domainName = getDomainName();
  if (domainName) {
    searchYC(domainName, 1);
  }
  
});

function getDomainName() {

  specialDomains = ["blogspot", "wordpress", "livejournal", "blogs", "typepad",
                   "weebly", "posterous", "blog-city", "supersized", "dreamhosters",
                   "eurekster", "blogsome", "edogo", "blog", "com"];

  specialSuffixes = ["uk", "jp", "au", "in", "ph", "tr", "za", "my", "nz", "br",
                          "mx", "th", "sg", "id", "pk", "eg", "il", "at", "pl"];

  try {
    hostparts = window.location.hostname.split(".")
  }
  catch(e) { 
    return false; 
  }
  
  if (hostparts.length < 1) {
    return false;
  }
  
  if (hostparts.length == 1) {
    return hostparts[0];
  }
  
  suffix = hostparts[hostparts.length-1];
  domainName = hostparts[hostparts.length-2];
  fullDomain = domainName + "." + suffix;
  
  if ((hostparts.length > 2) && 
      ((jQuery.inArray(suffix, specialSuffixes) > -1) || (jQuery.inArray(domainName, specialDomains) > -1))) {
    fullDomain = hostparts[hostparts.length-3] + "." + fullDomain;
  }
  
  return fullDomain;
}

function searchYC(fullDomain, pageNum) {
  queryURL = "http://json-automatic.searchyc.com/submissions/" + fullDomain + "?page=" + pageNum;
  GM_log("Querying " + fullDomain + ", page " + pageNum + ". URL: " + queryURL);
  $.getJSON(queryURL, function(data) {
  
      if (data.length < 1) {
	GM_log("Entry not found.");
	return false;
      }
      
      var foundItem;
      data.forEach(function(item, i) {
	if (window.location.toString().indexOf(item.url) == 0) { // allows extra query params at end of location
	  foundItem = item;
	}
      });  
      
      if (foundItem) {
	createPanel("<b>" + foundItem.title + "</b> posted on " + foundItem.post_date, "http://news.ycombinator.com/item?id=" + foundItem.id);
	return;
      }
      
      searchYC(fullDomain, pageNum+1);
    });
}

function toggleElement(elemName) {
  element = $(elemName);
  if (element.style.display == 'none') {
    element.style.display = 'block';http://stackoverflow.com/questions/1535404/how-to-exclude-iframe-in-greasemonkey
    GM_log('return');
    return;
  }
  GM_log('no return');
  element.style.display = 'none';
}

function createPanel(panelTitle, HNurl) {
  
  GM_log("Displaying HN Item: " + panelTitle + " || URL: " + HNurl);
  
  if ($(".HNembed").length > 0) return; // avoid situations where multiple results might be triggered.
  
  GM_addStyle(<><![CDATA[
    #HNembed 	{ position: fixed;
                  clear: both; 
                  width: 700px; 
                  height: 100%; 
                  border-left: 2px solid gray; 
                  padding: 0px; 
                  margin: 0px; 
                  text-align: center; 
                  color: #335500; 
                  background-color: #F6F6EF; 
                  z-index: 999;  
                  right: -700px; 
                  top: 0; }
    #HNsite 	{ width: 100%;
		  height: 178px; }
    #HNheader 	{ background-color: #FF6600;
		  color: #222222;
		  font-family: Verdana;
		  font-size: 10pt;
		  cursor: pointer;
		  height: 20px; }
    #HNtab	{ background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAB9CAIAAAA3LDRGAAAACXBIWXMAAA6cAAAOxAF19oSBAAAEoUlEQVRoge2ZfUxVZRjAf+fC/VBASQLjy7IIkGoWosYa4IIYG2b+0WZZyUgn1NZsiba5uZWbbQK6NrcUdSTUFuhW5rRRzfwK5UPTS4Z1nUWBkBdQgpD7ye0PuIPk5b7cw42geP4679lzfud5nvd5n+d9z1Fc6/GJaHyDmQZNMlBs5eDFB40EfUjipzT1qAK19gK0W3jrPH/audRBQY0qUJ8TF1T/jtVJfgK1KzlxQxUoRM/JVk63AaTcx5Iw/rAJ1PyloMwo0o8BB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aEF39ln8CK4TIVT0cTDfLZKXsq9v5pkHv1l8hO0R4kT+FvZSTPw6+B0cVtweSL0ZQBlbhs2Rt9AQLdimJKXNanVo0PlKfYjhbQUq9fU8HWurFY5179JS5hHtmyN+pWFI98zdC1+8F/uEH2xTzmF5epS1yli1pMS73tu0rd8Z1qQDM2NQxcWMtf0FdX6lRbBNiOFuiO75T9MZKC8pSxWDFcpswS+RdBw4I9nmrrwwb5F3/Onc9Lg6h9AAAAAElFTkSuQmCC);
		  background-color: #FF6600;
		  border-color: #F6F6EF; 
		  border-style: solid;
		  border-width: 2px 0px 2px 2px;
		  clear:both;
		  color: #222222;
		  cursor: pointer;
		  font-family: Verdana;
		  font-size: 10pt;
		  font-weight: bold;
		  height: 125px !important; 
		  position: fixed;
		  right: 0;
		  text-align: center;
		  text-indent: -100000px;
		  top: 10%;
		  
		  width: 23px !important; 
		  z-index:999; }
    #HNupdate	{ color: yellow;
		  margin-left: 10px;
		  font-weight: bold; }
    #HNtitle	{ width: 100%; }
  ]]></>.toString());

  HNembed = $("<div />").attr({'id' : 'HNembed'});
  HNsite = $("<iframe />").attr({'id' : 'HNsite', 'src' : HNurl});
  HNtab = $("<div>HackerNews</div>").attr({'id' : 'HNtab'});
  
  panelTitle = ">>> <b>Hacker News</b> >>>";
  HNtitle = $("<span>" + panelTitle + "</span>").attr({'id' : 'HNtitle'});
  HNheader = $("<div />").attr({'id' : 'HNheader'});
  HNupdate = $("<span>Update Script</span>").attr({'id' : 'HNupdate'}).hide().click(function() {
    window.location.href="http://share.shmichael.com/hn_comment_embed.user.js?" + (new Date()).getTime() + ".user.js";
  });

  $(window).resize(fixIframeHeight);

  function fixIframeHeight() {
    HNembed.height($(window).height());
    HNsite.height(HNembed.height()-20);  
  }

  function togglePanel() {
    
    openPanel = HNtab.is(":visible");
    GM_log("Panel Toggle. Is it an openning? " + openPanel);
    
    embedPosition = openPanel ? "0px" : "-700px";
    tabPosition = openPanel ? "-25px" : "0px";
    
    if (openPanel) {
      fixIframeHeight();
      HNtab.animate({right: tabPosition}, 150, "linear", function() {
	HNembed.show();
	HNtab.hide();
	HNembed.animate({right: embedPosition},400,"linear");
      });
    }
    else {
      HNembed.animate({right: embedPosition}, 400, "linear", function() {
	HNtab.show();
	HNembed.hide();
	HNtab.animate({right: tabPosition}, 150, "linear");
      });
    }
  }
  
  HNheader.click(togglePanel);
  HNtab.click(togglePanel);
  
  HNheader.append(HNtitle);
  HNheader.append(HNupdate);
  
  HNembed.append(HNheader);
  HNembed.append(HNsite);
  HNembed.hide();
  
  $('body').append(HNtab);
  $('body').append(HNembed);
  
  checkUpdate(HNupdate);
}

function checkUpdate(HNupdate) {
  GM_log('testing updates');
  GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://share.shmichael.com/hn_comment_embed.version?'+(new Date()).getTime(),
    headers: {
    'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
    'Accept': 'application/atom+xml,application/xml,text/xml',
    'Cache-Control': 'no-cache',
    },
    onload: function(responseDetails) {
      GM_log('updated version ' + responseDetails.responseText);
      if (parseInt(responseDetails.responseText) > version) {
	HNupdate.show();
      }
    }
  });
}
