// ==UserScript==
// @name	  		Link Preview
// @author			Mark Husson <mhusson atte gmail dought com>
// @description		preview links by holding the ctrl key
// @namespace		http://michaelhusson.com/mark/greasemonkey/
// @include		 	*

// ==/UserScript=
var weAreOverThis = "";
var previewWindow = "";
var shadowDiv = "";

var as = document.getElementsByTagName("a");
for(var i=0;i<as.length;i++){
	as[i].setAttribute("onmouseover","weAreOver(this)");
	as[i].setAttribute("onmouseout","weAreOut()");
}

var timer = false;
function keyDown(e){
	var keypressed = e.which;
	if(keypressed == 17){ //ctrl
		if(!timer){
			timer = setTimeout('previewLink()', 1000); //1.5 secs
		}
	}else if(keypressed == 27){ //esc
		if(typeof(previewWindow) != "string"){
			hideEverything();
		}
	}
}
function keyUp(e){
	var keypressed = e.which;
	if(keypressed == 17){
		if(timer){
			clearTimeout(timer); //cancel timeout
			timer = false;
		}
	}
}

hideEverything = function(){
	previewWindow.style.display = "none";
	shadowDiv.style.display = "none";
	xDiv.style.display = "none";
	previewWindow = "";
}

document.onkeydown = keyDown;
document.onkeyup = keyUp;

previewLink = function(){
	if((typeof(weAreOverThis.href) != "undefined") && (weAreOverThis.href.indexOf("javascript:") == -1) && (weAreOverThis.href.charAt(weAreOverThis.href.length-1) != "#")){
		shadowDiv = document.createElement("div");
		shadowDiv.style.width = window.innerWidth+"px";
		shadowDiv.style.height = window.innerHeight+"px";
		shadowDiv.style.backgroundColor = "black";
		shadowDiv.style.display = "block";
		shadowDiv.style.top = "0px";
		shadowDiv.style.left = "0px";
		shadowDiv.style.position = "fixed";
		shadowDiv.style.opacity = ".5";
		
		xDiv = document.createElement("img");
		xDiv.setAttribute("src","http://michaelhusson.com/mark/favicon.ico");
		xDiv.setAttribute("onclick","hideEverything()");
		xDiv.setAttribute("title","Close Preview [esc]");
		xDiv.style.backgroundColor = "black";
		xDiv.style.border = "1px solid black";
		xDiv.style.cursor = "pointer";
		xDiv.style.display = "block";
		xDiv.style.top = "50px";
		xDiv.style.left = "50px";
		xDiv.style.position = "fixed";
		
		previewWindow = document.createElement("iframe");
		previewWindow.setAttribute("src",weAreOverThis.href);
		previewWindow.setAttribute("frameborder","0");
		previewWindow.style.width = window.innerWidth-120+"px";
		previewWindow.style.height = window.innerHeight-100+"px";
		previewWindow.style.borderTop = "1px solid black";
		previewWindow.style.borderRight = "3px double black";
		previewWindow.style.borderBottom = "3px double black";
		previewWindow.style.borderLeft = "1px solid black";
		previewWindow.style.display = "block";
		previewWindow.style.top = "50px";
		previewWindow.style.left = "50px";
		previewWindow.style.position = "fixed";
		previewWindow.onkeypress=keyUp;
		
		document.body.appendChild(shadowDiv);
		document.body.appendChild(previewWindow);
		document.body.appendChild(xDiv);
	}
}

weAreOver = function(elem){
	weAreOverThis = elem;
}
weAreOut = function(){
	weAreOverThis = "";
}
