/* 
 * css history knocker (determine what sites your visitors have been to)
 *
 * originally by Jeremiah Grossman
 * http://jeremiahgrossman.blogspot.com/2006/08/i-know-where-youve-been.html
 *
 * ported to additional browsers by Samy Kamkar
 *
 * compatible with ie6, ie7, ie8, ff1.5, ff2, ff3, opera, safari, chrome, flock
 *
 * - code@samy.pl
 */

var no_color = 0;
var no_style = _getRGB("http://samy-was-here-this-should-never-be-visited.com", -1);
if (no_style == -1)
	no_color = _getRGB("http://samy-was-here-"+Math.floor(Math.random()*9999999)+"rand.com");

function hasVisited(url)
{
	return	_testURL("http://" + url, no_color) ||
		_testURL("https://" + url, no_color) ||
		_testURL("http://www." + url, no_color) ||
		_testURL("https://www." + url, no_color);
}
// oops...backwards compat
function hasVisisted(url) { return hasVisited(url); }

/* if test_color, return -1 if we can't set a style */
function _getRGB(u, test_color)
{
	var i = Math.floor(Math.random()*9999999);

	/* create the new anchor tag with the appropriate URL information */
	var link = document.createElement("a");
	link.id = "idpbx" + i;
	link.href = u;
	link.innerHTML = u;

	/* create a custom style tag for the specific link. Set the CSS visited selector to a known value */
	var cssText = '#idpbx' + i + ':visited{display:none;color:#FF0000}';
	/* Methods for IE6, IE7, FF, Opera, and Safari */
	try {
		var style = document.createElement('style');
		if (style.styleSheet)
			style.styleSheet.innerHTML = cssText;
		else if (style.innerHTML)
			style.innerHTML = cssText;
		else
		{
			var cssT = document.createTextNode(cssText);
			style.appendChild(cssT);
		}
		document.body.appendChild(style);
	} catch (e) {
		if (test_color)
			return -1;	
	}
	
	/* quickly add and remove the link from the DOM with enough time to save the visible computed color. */
	var color;
	document.body.appendChild(link);
	if(document.defaultView)
		color = document.defaultView.getComputedStyle(link, null).getPropertyValue('color');
	else
		color = link.currentStyle['color'];

	/* remove it now */
	if (style.innerHTML)
		document.body.removeChild(style);
	document.body.removeChild(link);

	return color;
}

function _testURL(url, no_color)
{
	var color = _getRGB(url);

	/* check to see if the link has been visited if the computed color is red */
	if (color == "rgb(255, 0, 0)" || color == "#ff0000")
		return 1;

	/* if our style trick didn't work, just compare default style colors */
	else if (no_color && color != no_color)
		return 1;

	/* not found */
	return 0;
}

