function Sitetuners(experiment) {
	var that = this;
 
	// get the query string and return it as an object
	function getQueryString() {
		var query = document.location.search.substring(1, document.location.search.length), 
			pairs = query.split('&'),
			position, index, argname, value,
			queryObject = {};
			
		for(index = 0; index < pairs.length; index++) {
			position = pairs[index].indexOf('=');			// Look for 'name=value'
			if (position == -1) { // If not found, skip
				continue;
			}
			argname = pairs[index].substring(0,position);	// Extract the name
			value = pairs[index].substring(position+1);		// Extract the value
			queryObject[argname] = value;
 		}
		return queryObject;
	}	// end getQueryString	
	
	// get a cookie
	function getCookie(cookieName) {
		var allcookies = document.cookie,
			position = allcookies.indexOf(cookieName),
			start, end;
		if (position != -1) {	// get the cookie
			start = position + cookieName.length + 1;
			end = allcookies.indexOf(';', start);
			if (end === -1) {
				end = allcookies.length;
			}
			return decodeURIComponent(allcookies.substring(start, end));
		}
		return false;	// could not find the cookie
	}
	
	// set a cookie
	function setCookie(cookieName, cookieValue, cookieYears) {
		// the following code sets the expiration date for the sitetuners cookies
		// the default is one year if the third argument is not passed
		var expiration = new Date(), domain = document.domain, position, allcookies;
	
		if (arguments.length == 2) {
			cookieYears = 1;
		}
			
		expiration.setFullYear(expiration.getFullYear() + cookieYears);
	
		// set the domain this script is running on
		if (!domain.match(/^\d+\.\d+\.\d+\.\d+$/)) {
			if (domain.match(/(\..+\..+$)/)) {
				domain = RegExp.$1;
			}
			else {
				domain = '';
			}
		}
	
		if (domain === '') {
			document.cookie = cookieName + '=' + cookieValue + '; path=/; expires=' + 
				expiration.toGMTString();
		} else {
			document.cookie = cookieName + '=' + cookieValue + '; path=/; expires=' + 
				expiration.toGMTString() + '; domain=' + domain;
		}
	
		allcookies = document.cookie;
		position = allcookies.indexOf(cookieName);
		
		if (position != -1) {	// cookie was set
			return true;
		}
		return false;
	}
	
	// can a cookie be set for this visitor
	function ableToSetCookies() {
		if (setCookie('sitetunersTest', 'Test', 1)) {
			setCookie('sitetunersTest', 'Test', -1);
			return true;
		}
		if (that.logging) {
			createScript(that.protocol + '://tuningengine1.sitetuners.com/logSitetunersVisit.php?experiment=' + that.experiment + '&log=cookiesnotallowed&referrer=' + document.referrer);
		}
		that.excludeReason = 'cookies not allowed';
		return false;
	}
	
	// determine if visitor should be excluded
	function excludeThisVisitor() {
		if (!document.getElementById || !document.getElementsByTagName || 
			!ableToSetCookies() || getCookie('sitetuners' + that.experiment + 'Exclude')) {
			that.exclude = true;
			setCookie('sitetuners' + that.experiment + 'Exclude', true, 1);

			if(!document.getElementById || !document.getElementsByTagName) {
				log('javascript');
			}
			else if (getCookie('sitetuners' + that.experiment + 'Exclude')) {
				log('previouslyexcluded');
			}
			
			return true;
		}
  		return false;
	}
	
	function createScript(scriptSrc) {
		document.write('<script src="' + scriptSrc + '"></script>');
	}

	function log(logMsg) {
		if (that.logging) {
			createScript(this.protocol + '://tuningengine1.sitetuners.com/logSitetunersVisit.php?experiment=' + this.experiment + '&log=' + logMsg + '&referrer=' + document.referrer);
		}
	}
	
	this.logging = false;
	this.live = true;
	this.experiment = experiment;
	this.hasBeenCalled = false;
	this.recipe = false;
	this.transaction = false;
	this.protocol = document.location.protocol == 'https:' ? 'https' : 'http';
	this.exclude = excludeThisVisitor();
	this.excludeReason = 'none';
	
	// during instantiation check to see if we need to set a script
	if (!this.exclude) {

		var queryString = getQueryString();		// get the Query String as an object
		if (queryString.sitetunersExperiment && queryString.sitetunersExperiment == this.experiment) {
			if (queryString.sitetunersRecipe) {
				this.recipe = queryString.sitetunersRecipe;
				setCookie('sitetunersRecipe' + this.experiment, this.recipe);
			}
			log('querystring');
		}
		else {	
			// is there a recipe
			this.recipe = getCookie('sitetunersRecipe' + this.experiment);
			if (!this.recipe && this.live) {
				// as long as we are live create the script
				log('newvisit');
				createScript(this.protocol + '://tuningengine1.sitetuners.com/getSitetunersData.php?experiment=' + this.experiment + '&callback=sitetuners' + this.experiment + '.callback&format=jsonp&askingfor=recipetransaction');
			}
			else {
				this.transaction = getCookie('sitetunersTransaction' + this.experiment);
				log('returnvisit');
			}
		}
	}
	
	this.createTransactionImage = function() {
		if (!this.transaction) {
			this.transaction = getCookie('sitetunersTransaction' + this.experiment);
		}
		if (!this.recipe) {
			this.recipe = getCookie('sitetunersRecipe' + this.experiment);
		}
		var image = new Image();
		image.onLoad = function() { emptyFunc(); };
		image.src = this.protocol + '://tuningengine1.sitetuners.com/postSitetunersTransactionRecipe.php?experiment=' + this.experiment + '&transaction=' + this.transaction + '&recipe=' + this.recipe + '&reason=' + this.excludeReason;
	}
	
	// generic handler which does nothing
	this.handler = function() {
	};
	
	this.imageConversion = function(linkValue, goal) {
		var image = new Image();
		image.onLoad = function() { emptyFunc(); };
		image.src = this.protocol + '://tuningengine1.sitetuners.com/postSitetunersConversion.php?experiment=' + this.experiment + '&transaction=' + this.transaction + '&value=' + linkValue + '&conversionType=' + goal + '&rand=' + Math.random();
	}

	this.clickConversion = function(linkValue, goal) {
		imageConvesion(linkValue, goal);
		pause(500);
	}

	function emptyFunc() { return true; }

	function pause(milliseconds) {
		var now = new Date();
		var expire = now.getTime() + milliseconds;
		while(now.getTime() < expire)
			now = new Date();
	}

	this.callback = function(object) {
		if (object.status == 'success' && !this.hasBeenCalled) {
			this.recipe = object.recipe;
			this.transaction = object.transaction;
			setCookie('sitetunersRecipe' + this.experiment, this.recipe);
			setCookie('sitetunersTransaction' + this.experiment, this.transaction);
		}
		else if (object.status == 'failure') {
			this.excludeReason = object.reason;
		}
	};	
}

