// ==UserScript==
// @name          redbubblehover
// @namespace     rog
// @description   Displays more detailed hovers for images and some links redbubble.com
// @include       http://*redbubble.com*
// ==/UserScript==

// Original facebook script by: Justin Rosenthal (justin.rosenthal at gmail)
// Original modification by znerp.
// Further modification by znerp.
// Adapted to redbubble.com by rog
// Update 04JAN08: Fixed the bugs that Xavier reported, "writing" now works, and the hover disappears when it should

function sqr(x) { return (x*x) }
eventThingX = 0;
eventThingY = 0;
var globalTimer;
var newDiv = document.createElement('div');
var inner = newDiv.appendChild(document.createElement('div'));
newDiv.setAttribute('id', 'rbdiv');
newDiv.setAttribute('style', 'padding:3px;position:fixed;display:none;z-index:100;top:20px;left:20px;background-color:#3B5998;');
document.body.appendChild(newDiv);
newDiv.addEventListener(
  'mouseover',
  function(event) {
    this.style.display = "inline";
  },
  true);
newDiv.addEventListener(
  'mouseout',
  function(event) {
    window.clearTimeout(globalTimer);
    this.style.display = "none";
  },
  true);
newDiv.addEventListener(
  'mousemove',
  function(e) {
    if (sqr(eventThingX - e.pageX) + sqr(eventThingY - e.pageY) > 1337) {
      window.clearTimeout(globalTimer);
      this.style.display = "none";
    }
  },
  true);
  
  
// Images, hover for all art and clothing that isn't already [x]large size
var allImages = document.evaluate('//img', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i=allImages.snapshotLength-1; i>=0; i--){
  var thisImage = allImages.snapshotItem(i);
  var src = thisImage.src;
  if ((src.indexOf('view:main') != -1 && src.indexOf('size:large') == -1 && src.indexOf('size:xlarge') == -1 )){
  thisImage.addEventListener(
    'mouseover',
    function(event) {
      title = this.getAttribute('title');
      fullsize=this.src.replace(/\/cropped/,"").replace(/size:[a-z]+/,"size:large");
      thisImage = this;
      window.clearTimeout(globalTimer);
      globalTimer = window.setTimeout(
        function (thisImage) {return function(result) {
          inner.setAttribute('style', 'background:url("http://www.redbubble.com/images/favicon.png");min-height:'+100+'px;min-width:'+100+'px;');
          if (title) inner.innerHTML = "<a href="+thisImage.parentNode.href+"><img title = '"+title +"' style = 'max-height: "+ (parseInt(window.innerHeight) - 40) +"px; margin-bottom: -2px; cursor: crosshair;' src='" + fullsize + "'></a></div>";
          else inner.innerHTML = "<a href="+thisImage.parentNode.href+"><img style = 'max-height: "+ (parseInt(window.innerHeight) - 40) +"px; margin-bottom: -2px; cursor: crosshair;' src='" + fullsize + "'></a></div>";
          newDiv.style.display = "inline";
        }}(thisImage),500
      );
    },
    true);
  thisImage.addEventListener(
    'mouseout',
    function(event) {
      window.clearTimeout(globalTimer);
      newDiv.style.display = "none";
    },
    true);
  thisImage.addEventListener(
    'mousemove',
    function(e) {
      eventThingX = e.pageX;
      eventThingY = e.pageY;
    },
    true);
  }
}

// Links, hover image for art links, ajax fetch journal entries, artist profiles
var allLinks = document.evaluate('//a', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i=allLinks.snapshotLength-1; i>=0; i--){
  var thisLink = allLinks.snapshotItem(i);
  var href = thisLink.href;
  if ((/people\/.*\/art\/[0-9]+/).test(href)){
// ART
  thisLink.addEventListener(
    'mouseover',
    function(event) {
      title = this.getAttribute('title');
      fullsize=this.href.replace(/http:\/\/www\./,"http://images-" + Math.floor(Math.random()*4) + ".").replace(/\/people\/.*\/art\//,"/img/art/size:large/view:main/").replace(/#.*$/,"").replace(/$/,".jpg");
      thisLink = this;
      window.clearTimeout(globalTimer);
      globalTimer = window.setTimeout(
        function (thisLink) {return function(result) {
          inner.setAttribute('style', 'background:url("http://www.redbubble.com/images/favicon.png");min-height:'+100+'px;min-width:'+100+'px;');
          if (title) inner.innerHTML = "<a href="+thisLink.parentNode.href+"><img title = '"+title +"' style = 'max-height: "+ (parseInt(window.innerHeight) - 40) +"px; margin-bottom: -2px; cursor: crosshair;' src='" + fullsize + "'></a></div>";
          else inner.innerHTML = "<a href="+thisLink.parentNode.href+"><img style = 'max-height: "+ (parseInt(window.innerHeight) - 40) +"px; margin-bottom: -2px; cursor: crosshair;' src='" + fullsize + "'></a></div>";
          newDiv.style.display = "inline";
        }}(thisLink),500
      );
    },
    true);
  thisLink.addEventListener(
    'mouseout',
    function(event) {
      window.clearTimeout(globalTimer);
      newDiv.style.display = "none";
    },
    true);
  thisLink.addEventListener(
    'mousemove',
    function(e) {
      eventThingX = e.pageX;
      eventThingY = e.pageY;
    },
    true);
  }
  else if ((/people\/.*\/journal\/[0-9]+/).test(href)){
// JOURNAL
  thisLink.addEventListener(
    'mouseover',
    function(event) {
      thisLink = this;
      window.clearTimeout(globalTimer);
      globalTimer = window.setTimeout(
        function (thisLink) {return function(result) {
          inner.setAttribute('style', 'background:white;min-height:'+100+'px;min-width:'+100+'px;');
	  inner.innerHTML = "loading...";
	  newDiv.style.display = "inline";
          GM_xmlhttpRequest({
		  method: 'GET',
		  url: thisLink.href,
		  onload: function(responseDetails) {
			  // convert string to XML object
//			  var xmlobject = (new DOMParser()).parseFromString(responseDetails.responseText, "application/xml");
//			  inner.innerHTML = xmlobject.getElementById('thought').innerHTML;
		          inner.innerHTML = responseDetails.responseText.match(/<div id="thought">[^\\]+<\/div>\n          <\/div>/m);
		  }
	  });
        }}(thisLink),500
      );
    },
    true);
  thisLink.addEventListener(
    'mouseout',
    function(event) {
      window.clearTimeout(globalTimer);
      newDiv.style.display = "none";
    },
    true);
  thisLink.addEventListener(
    'mousemove',
    function(e) {
      eventThingX = e.pageX;
      eventThingY = e.pageY;
    },
    true);
  }
  else if ((/people\/.*\/writing\/[0-9]+/).test(href)){
// WRITING
  thisLink.addEventListener(
    'mouseover',
    function(event) {
      thisLink = this;
      window.clearTimeout(globalTimer);
      globalTimer = window.setTimeout(
        function (thisLink) {return function(result) {
          inner.setAttribute('style', 'background:white;min-height:'+100+'px;min-width:'+100+'px;');
	  inner.innerHTML = "loading...";
	  newDiv.style.display = "inline";
          GM_xmlhttpRequest({
		  method: 'GET',
		  url: thisLink.href,
		  onload: function(responseDetails) {
			  // convert string to XML object
//			  var xmlobject = (new DOMParser()).parseFromString(responseDetails.responseText, "application/xml");
//			  inner.innerHTML = xmlobject.getElementById('work').innerHTML;
		          inner.innerHTML = responseDetails.responseText.match(/<div class="written-work-begin"><\/div>[^\\]+<div class="written-work-end"><\/div>/m);
		  }
	  });
        }}(thisLink),500
      );
    },
    true);
  thisLink.addEventListener(
    'mouseout',
    function(event) {
      window.clearTimeout(globalTimer);
      newDiv.style.display = "none";
    },
    true);
  thisLink.addEventListener(
    'mousemove',
    function(e) {
      eventThingX = e.pageX;
      eventThingY = e.pageY;
    },
    true);
  }
/* Not quite working yet
  else if ((/people\/[^\/]+$/).test(href)){
// PERSON
  thisLink.addEventListener(
    'mouseover',
    function(event) {
      title = this.getAttribute('title');
      thisLink = this;
      window.clearTimeout(globalTimer);
      globalTimer = window.setTimeout(
        function (thisLink) {return function(result) {
          inner.setAttribute('style', 'background:white;min-height:'+100+'px;min-width:'+100+'px;');
          GM_xmlhttpRequest({
		  method: 'GET',
		  url: thisLink.href,
		  onload: function(responseDetails) {
			  // convert string to XML object
			  var xmlobject = (new DOMParser()).parseFromString(responseDetails.responseText, "application/xml");
			  bio = xmlobject.getElementById('details').parentNode.getElementsByTagName('p');
			  biotext="";
			  for (var i = 0; i < bio.length; i++) { 
			      biotext += "<p>" + bio[i].innerHTML + "</p>"; 
			  }
			  inner.innerHTML = biotext;
			  newDiv.style.display = "inline";

		  }
	  });
        }}(thisLink),500
      );
    },
    true);
  thisLink.addEventListener(
    'mouseout',
    function(event) {
      window.clearTimeout(globalTimer);
      newDiv.style.display = "none";
    },
    true);
  thisLink.addEventListener(
    'mousemove',
    function(e) {
      eventThingX = e.pageX;
      eventThingY = e.pageY;
    },
    true);
  }
*/
}


