
var RFA_TRACKING_HOSTNAME = 'r.refinedads.com';

if (window.location && window.location.protocol && window.location.protocol == 'https:')
{
	var RFA_TRACKING_URL = 'https://' + RFA_TRACKING_HOSTNAME;
}
else
{
	var RFA_TRACKING_URL = 'http://' + RFA_TRACKING_HOSTNAME;
}

// Parser for splitting url and getting url query values
RFA_Url_Parser = function(url){ this.url = url; this.parseUrl(url); }
RFA_Url_Parser.prototype.regexp = /(https?:\/\/)([a-zA-Z0-9_\-\.]+)(:[0-9]+)?\/?(.*)?/
RFA_Url_Parser.prototype.parseUrl = function(url){ result = this.url.match(this.regexp); if (!result){ result = []; } this.scheme = result[1] || ""; this.host = result[2] || ""; this.port = result[3] || ""; this.path = result[4] || ""; return this; }
RFA_Url_Parser.prototype.getQueryValue = function(key){ key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+key+"=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(this.url); if( results == null ){ return ""; } else { return decodeURIComponent(results[1]); }}
RFA_Url_Parser.prototype.getHost = function(){ if (this.host){ return this.host; } return "" }
RFA_Url_Parser.prototype.getPath = function(){ if (this.path){ var testPath = "/" + this.path; if (testPath.indexOf("#") != -1){ testPath = testPath.substr(0, testPath.indexOf("#")); } if (testPath.indexOf("?") != -1){ return testPath.substr(0, testPath.indexOf("?")); } return testPath; } return "/" }
RFA_Url_Parser.prototype.getTLD = function(){ if (!this.host){ return ""; } var hostParts = this.host.split(".").reverse(); var tldParts = [hostParts[0]]; if (hostParts[1]){ tldParts.push(hostParts[1]); } if (tldParts.join(".").length <= 5 && hostParts[2]){ tldParts.push(hostParts[2]); } return tldParts.reverse().join(".");}


RFA_SEO_GENERIC_Tracking = function()
{
	var trackUrl = function(aid, oid, level1, level2, level3)
	{
		document.write('<img src="' + RFA_TRACKING_URL + "/r.rfa?aid=" + aid + "&oid=" + oid + "&c1=" + encodeURIComponent(level1) + "&c2=" + encodeURIComponent(level2) + "&c3=" + encodeURIComponent(level3) + "&rfanoip=1" + "&url=" + encodeURIComponent(RFA_TRACKING_URL) + "%2Fpixel.gif" + '" width="1" height="1">');
	}

	var siteParser = new RFA_Url_Parser(document.location.href);
	
	var referrerUrl = document.referrer;

	// If a referrer URL is set
	if (referrerUrl)
	{
		// Split referrer URL
		var referrerParser = new RFA_Url_Parser(referrerUrl);
		
		// Traffic from same hostname
		if (siteParser.getHost() == referrerParser.getHost())
		{
			return;
		}

		// Check if paid traffic and not in referrer whitelist 
		if (RFA_IGNORE_CGI_PARAMETER && RFA_IGNORE_CGI_PARAMETER.length > 0)
		{
			for (var i = 0; i < RFA_IGNORE_CGI_PARAMETER.length; i++)
			{
				if (siteParser.getQueryValue(RFA_IGNORE_CGI_PARAMETER[i]) != "")
				{
					// Paid traffic
					return;
				}
			}
		}
		
		// Traffic from list of ignored hostnames
		if (RFA_IGNORE_REFERRER_HOSTNAMES && RFA_IGNORE_REFERRER_HOSTNAMES.length > 0 && RFA_IGNORE_REFERRER_HOSTNAMES.indexOf(referrerParser.getHost()) != -1)
		{
			return;
		}
		
		var hostName = referrerParser.getHost();
		var hostParts = hostName.split(".").reverse();
		
		// Hostname to short, contains no .
		if (hostParts.length < 2) { return; }
		
		var searchEngines = ['Google', 'Yahoo', 'Bing'];
		
		for (var i = 0; i < searchEngines.length; i++)
		{
			// Check if hostname contains the search engine name on second last part (or 3rd if co.uk)
			if (hostParts[1] == searchEngines[i].toLowerCase() || (hostParts.length > 2 && hostParts[2] == searchEngines[i] && hostParts[0] == "uk" && hostParts[1] == "co"))
			{
				// Track the referrer host and query
				trackUrl(RFA_SEO_AID, RFA_OID, searchEngines[i], hostName, referrerParser.getQueryValue("q"));
				return;
			}
		}
		
		// Listed search engines were not found
		
		// Structure for non-searchengine referrers
		trackUrl(RFA_GENERIC_AID, RFA_OID, referrerParser.getTLD(), referrerParser.getHost(), referrerParser.getPath());
		return;
	}
};

RFA_SEO_GENERIC_Tracking();
