var vft_visitId;
var vft_userId = userId;
var vft_httpReferer = httpReferer;

// Determine base
vft_base = "//www.visitorfishtank.com/worker/?controller=tracker";

// Determine resolution
var vft_resolution=screen.width + 'x' + screen.height;

// Determine language
var vft_language;
if (navigator.userLanguage) // Explorer
	vft_language = navigator.userLanguage;
else if (navigator.language) // FF
	vft_language = navigator.language;
else
	vft_language = "en";

// Sense actions
addListener(window, 'load', pageLoad);
addListener(window, 'unload', poll);

// Add polling every 20 seconds
setInterval("poll()", 20000);

// Display image
if (document.getElementById('visitorfishtank_stats') != null) {
	document.getElementById('visitorfishtank_stats').getElementsByTagName('img')[0].src=vft_base + '&userId=' + vft_userId + '&action=displayImage';
} else {
	if (vft_userId >= 39) {
		// show an error since they should be using the id based code
		document.writeln('Your visitorfishtank stat code has an error');
	} else {
		document.writeln('<a href="http://www.visitorfishtank.com"><img src="' + vft_base + '&userId=' + vft_userId + '&action=displayImage' + '" border="0" alt="Free Website Statistics"></a>');
	}
}


////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////
function pageLoad(){
	
	vft_visitId=getCookie('vft_visitId');	
	if(vft_visitId.length==0){
		vft_visitId=generateKey();
		setCookie('vft_visitId', vft_visitId, 30);
	}
	
	getUrl(vft_base + '&userId=' + vft_userId + '&httpReferer=' + vft_httpReferer + '&resolution=' + vft_resolution + '&language=' + vft_language + '&action=pageLoad&visitId=' + vft_visitId);
	
}

function poll(){
	getUrl(vft_base + '&userId=' + vft_userId + '&action=poll&visitId=' + vft_visitId);
}

function addListener(element, type, expression, bubbling){

	bubbling = bubbling || false;
	
	if(window.addEventListener) { // Standard
	
		element.addEventListener(type, expression, bubbling);
		return true;
	
	}
	else if(window.attachEvent) { // IE
	
		element.attachEvent('on' + type, expression);	
		return true;
	
	} else return false;

}

function getUrl(url){
	image=new Image(10,10);
	image.src=url + '&rnd=' + Math.floor(Math.random()*100000+1); // random number between 1 and 100,000 
}

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.toUTCString())+
	";domain=." + getDomain() + ";";
}

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));
	    }
	  }
	return "";
}

function generateKey() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 15;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function getDomain(){
	var re=/.*\.(.*\..*)$/;
	if(re.exec(window.parent.location.host)==null){
		return window.parent.location.host;
	}
	else{
		return re.exec(window.parent.location.host)[1];
	}
}