/************

THIS top half is all about doing the overlay

It currently uses cookies to begin a session of overlaying but this may be a problem on some browsers which disallow 3rd party cookies.

************/

function beginOverlay(siteID) {
	thisHref = document.location.href;
	findQueryString = thisHref.indexOf("?overlay="); // overlay variable always 35 characters long
	if (findQueryString > -1) {//overlay in progress, set cookie with timeout
		getQueryString = thisHref.split("?")[1];
		overlaycookie = getQueryString.substr(8, 35);
		createCookie("overlay", overlaycookie, 0.5);//set cookie for overlay so we don't have to keep passing URL variable across
	}
	
	if (document.cookie.indexOf("overlay") > -1) {
		//load relevant CSS
		var headID = document.getElementsByTagName("head")[0];         
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = 'http://www.snitchstats.com/styles/overlay.css?rand='+Math.random();
		headID.appendChild(cssNode);
		pagehref = document.location.href;
		jsNode = document.createElement('script'); 
		jsNode.setAttribute('type', 'text/javascript');
		jsNode.src = 'http://www.snitchstats.com/includes/ajax-data-loader.cfm?type=link-stats&srcHref='+pagehref+'&siteID='+siteID+'&domain='+document.domain;
		headID.appendChild(jsNode);
		return false;
	} else {
		return true;
	}
}
function createCookie(name,value,hours) {//generic function to set a cookie for x hours
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+((1+hours)*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}
function URLEncode(input) {
	output = escape(input);
	output = output.replace(/&/,"&amp;");
	return output;
}

function addVisit(siteID,vRoot) {
	/*
		siteID		=	siteID!
		vRoot		=	sometimes useful for sending data elsewhere
		
		example:
		
		<script src="http://snitch/stats.js" type="text/javascript"></script>
		<script type="text/javascript">
		addVisit(35,'http://snitch/');
		<!---updateView(35,<cfoutput>#request.snitchDetails.pageID#, #request.snitchDetails.visitID#,'http://snitch/','resolution,'</cfoutput>);--->
		</script>
	*/
						
	logStat = beginOverlay(siteID);
	if (logStat == true) {
		details = "pagetitle,referer,pagehref,resolution,"; // no longer used
		
		if (vRoot == null) {
			vRoot = "http://www.snitchstats.com/";
		}
		var thisDoc = document;
		var pagehref = thisDoc.location;
		var referer = thisDoc.referrer;
		var pagetitle = thisDoc.title;
		var imgPath = vRoot + "stats-js-loader.cfm?addVisit=1";
		var createImg = new Image(1,1);
		
		if (pagehref.hash) thisLoc=pagehref.href.substring(pagehref.href.indexOf('#'));
		if (self.screen) resolution = screen.width+"x"+screen.height; else resolution = '';
		
		fullImgPath = imgPath;
		
		if (details.indexOf("pagetitle,") > -1) {
			fullImgPath = fullImgPath + '&pagetitle=' + URLEncode(pagetitle);
		}
		if (details.indexOf("referer,") > -1) {
			fullImgPath = fullImgPath + '&referer=' + URLEncode(referer);
		}
		if (details.indexOf("pagehref,") > -1) {
			fullImgPath = fullImgPath + '&pagehref=' + URLEncode(pagehref);
		}		
		if (details.indexOf("resolution,") > -1) {
			fullImgPath = fullImgPath + '&resolution=' + resolution;
		}
		fullImgPath = fullImgPath + '&siteID=' + siteID;
		
		createImg.src=fullImgPath;
	}
}
function updateView(siteID, pageID, visitID, vRoot, details) {
	/*
	
	example:
	
	<script src="http://snitch/stats.js" type="text/javascript"></script>
	<script type="text/javascript">
	updateView(35,<cfoutput>#request.snitchDetails.pageID#, #request.snitchDetails.visitID#,'http://snitch/','resolution,'</cfoutput>);
	</script>
	
	*/
	logStat = beginOverlay(siteID);
	if (logStat == true) {
		if (vRoot == null) {
			vRoot = "http://www.snitchstats.com/";
		}
		
		var imgPath = vRoot + "stats.cfm?updateView=1";
		var createImg = new Image(1,1);
		
		if (self.screen) resolution = screen.width+"x"+screen.height; else resolution = '';
		
		fullImgPath = imgPath;
		
		var thisDoc = document;
		var pagetitle = thisDoc.title;
		
		fullImgPath = fullImgPath + '&pagetitle=';
		if (details.indexOf("pagetitle,") > -1) {
			fullImgPath = fullImgPath + URLEncode(pagetitle);
		}
		
		fullImgPath = fullImgPath + '&resolution=';
		if (details.indexOf("resolution,") > -1) {
			fullImgPath = fullImgPath + resolution;
		}
		
		fullImgPath = fullImgPath + '&pageID=' + pageID;
		fullImgPath = fullImgPath + '&visitID=' + visitID;
		fullImgPath = fullImgPath + '&siteID=' + siteID;	
			
		createImg.src=fullImgPath;
	}
}