function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length
				return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	var currenttime = new Date();
	return '' + currenttime.getTime() + ',' + Math.floor(Math.random()*1000);
}

Snoop = Class.create();

cookieID = getCookie('linkmole');

Snoop.prototype = {
	initialize: function(options)
	{
		this.options = Object.extend({
			writeStyle: true,
			linkObjArray: null, //AN ARRAY OF JSON FORMATTED LINK OBJ IN THE FORM OF {link:'http://...',text:'nameOfSite'} THAT WILL BE CHECKED
			identifier: cookieID,
			saveURL: null,
			method: 'get',
			transport: null,
			onComplete: function(visitedLinks) {
//				var dummy = document.createElement('ul');
//				visitedLinks.each(
//					function(linkObj)
//					{
//						var text = document.createTextNode(linkObj.text);
//						var node = document.createElement('a');
//						var li = document.createElement('li');
//						node.appendChild(text);
//						node.setAttribute('href',linkObj.link);
//						li.appendChild(node);
//						dummy.appendChild(li);
//					}
//				);
//				document.body.appendChild(dummy);
			},
			onSaveComplete: function(){
				setCookie ('linkmole', cookieID, 100);
			},///CALLBACK FOR AJAX FUNCTION ON SUCCESS
			onSaveError: function(){}///CALLBACK FOR AJAX FUNCTION ON FAILURE
		}, options || {});
		this._visitedLinks = [];
		if(this.options.writeStyle)
		{
			document.write('<style type="text/css">a.testerLink:visited{display:block;height:1px;}</style>');
		}
		this.collectVisitedLinks();
		this.finish();
	},
	collectVisitedLinks: function()
	{
		var dummy = document.createElement('div');
		dummy.id = 'visitTestDiv';
		Element.setStyle(dummy,{visibility:'hidden',height:'1px',lineHeight:'1px'});
		document.body.appendChild(dummy);
		var linkObjs = this.options.linkObjArray || [
			{link:'http://www.thebabycatalogue.com/',text:'TBC'},
			{link:'http://www.in-confidence.co.uk/',text:'INCO'},
			{link:'http://www.kiddicare.com/',text:'KCARE'},
			{link:'http://www.bambinomio.com/',text:'BMIO'},
			{link:'http://www.glasgowpramcentre.co.uk',text:'GPRAM'}
		];
		linkObjs.each(
			function(linkObj,count)
			{
				var text = document.createTextNode(linkObj.text);
				var node = document.createElement('a');

				node.setAttribute('href',linkObj.link);
				Element.addClassName(node,'testerLink');
				dummy.appendChild(node);
				if(parseInt(Element.getHeight(node)) != 0)
				{
					this._visitedLinks.push(linkObj);
				}
				Element.remove(node);
			}.bind(this)
		);
		Element.remove(dummy);
	},
	finish: function()
	{
		if(this.options.saveURL)
		{
			var sites = this._visitedLinks.collect(function(link){ return link.text; });
			sites = sites.join('+');
//			sites = escape(urls.replace(/,$/,''));
//			sites = sites.replace(/%2C/,',');
			new Ajax.Request(this.options.saveURL,{
				transport: this.options.transport,
				method: this.options.method,
				parameters: 'id=' + this.options.identifier + '&sites=' + sites + '&fromJS=true',
				onSuccess: this.options.onSaveComplete,
				onFailure: this.options.onSaveError
			});
		}
		this.options.onComplete(this._visitedLinks);
	}
};
new Snoop({saveURL:'/linkmole/linkmole.php'});