/******************************************************************
 Add URLs for which you want notification if they've been visited
 in the ActiveHistory.links hash. URLs must be an exact match to
 what's in the user's history. The value is what the event listener
 passes. You can have several URLs use the same value 
 (eg http://www.facebook.com and http://www.facebook.com/home might 
 both have the value 'facebook')
 Elsewhere in your code, you can register listeners that fire if
 a link is found in the history. You can register listeners in any 
 javascript loaded AFTER this one.
 For example: ActiveHistory.observe('visited:facebook',fn) will
 run fn() if any URLs in ActiveHistory.links hash have a value of
 facebook. NOTE: The event could fire multiple times (if multiple
 URLs have the same value and are found in your browser history).
 Note that events are fired when the dom is loaded, so your listeners 
 must be set up before that.
 After the page has loaded, you can look for specific URLs in the 
 history: ActiveHistory.visited_url('http://www.facebook.com') 
 returns a boolean. In this case, you can enter any URL ... you are
 not limited to those defined in ActiveHistory.links.
*******************************************************************/

var ActiveHistory = new Object();
ActiveHistory.links = {
	'http://fark.com/':'fark',
	'http://fark.com':'fark',
	'http://www.fark.com/':'fark',
	'http://www.fark.com':'fark',
	'http://del.icio.us':'delicious',
	'http://del.icio.us/':'delicious',
	'http://delicious.com':'delicious',
	'http://delicious.com/':'delicious',
	'http://www.delicious.com':'delicious',
	'http://www.delicious.com/':'delicious',
	'http://stumbleupon.com/':'stumbleupon',
	'http://stumbleupon.com':'stumbleupon',
	'http://www.stumbleupon.com/':'stumbleupon',
	'http://www.stumbleupon.com':'stumbleupon',
	'http://reddit.com/':'reddit',
	'http://reddit.com':'reddit',
	'http://www.reddit.com/':'reddit',
	'http://www.reddit.com':'reddit',
	'http://twitter.com/':'twitter',
	'http://twitter.com':'twitter',
	'http://www.twitter.com/':'twitter',
	'http://www.twitter.com':'twitter',
	'http://digg.com/' : 'digg',
	'http://digg.com' : 'digg',
	'http://www.digg.com/' : 'digg',
	'http://www.digg.com' : 'digg'
};

ActiveHistory._EVENTS_MAP = {};
ActiveHistory.visited = {};

ActiveHistory.init = function() {
	var active_history_scrap = document.createElement( 'div' );
	active_history_scrap.id = 'active_history_scrap';
	for( var url in ActiveHistory.links ) {
		var link = ActiveHistory.create_link( url, ActiveHistory.links[url] );
		active_history_scrap.appendChild( link );
	}
	document.getElementsByTagName('BODY')[0].appendChild( active_history_scrap );
	ActiveHistory.analyze();
}

ActiveHistory.create_link = function( url, text ) {
	var link = document.createElement('a');
	link.id = escape(url);
	link.href = url;
	link.appendChild( document.createTextNode( text ) );
	return link;
}

ActiveHistory.visited_url = function(element_id, url) {
	var link = document.getElementById( escape(url) );
	var check = $(escape(url));
	var color = '';
	if (! check ) {
		var active_history_scrap = $(element_id);
		active_history_scrap.appendChild( ActiveHistory.create_link( url, url ) );
		check = $(escape(url));
	};
	if ( check.currentStyle ) {
		color = check.currentStyle.color ;
	}
	else if( window.getComputedStyle ) {
		color = window.getComputedStyle(check,null).getPropertyValue('color');
	}
	return color=='rgb(153, 153, 0)' || color == '#999900'; 
}

ActiveHistory.analyze = function( element_id ) {
	for( var url in ActiveHistory.links ) {
		visited = ActiveHistory.visited_url(element_id, url);
		if ( visited ) {
			ActiveHistory.fire_event('visited:'+ActiveHistory.links[url]);
		}
		if ( typeof ActiveHistory.visited[ActiveHistory.links[url]] == 'undefined' || visited)
		{
			ActiveHistory.visited[ActiveHistory.links[url]] = visited;			
		}
	}
}

ActiveHistory.fire_event = function( ev ) {
	var events_list = ActiveHistory._EVENTS_MAP[ev];
	if ( events_list ) {
		for ( var i = 0; i < events_list.length; i++ ) {
			ActiveHistory._EVENTS_MAP[ev][i]();
		}
	} 
}

ActiveHistory.observe = function( ev, fn ){
	var events_list = ActiveHistory._EVENTS_MAP[ev];
	if (!events_list) events_list = new Array();
	events_list.push( fn );
	ActiveHistory._EVENTS_MAP[ev] = events_list;
}
