/**
 * @author steve.pole
 * To use:
 * AlchemyTracking.registerLanding();
 * AlchemyTracking.registerConversion([optional] value, [optional] quantity);
 */
if (!window.AlchemyTracking) {
	AlchemyTracking = {};
}

AlchemyTracking.send = function(ad_id, cmp_id, land_time, value, type, u_id) {
	var pix, url;
	if (ad_id && cmp_id) {
		pix = new Image();
		
		if (!u_id) {
			u_id = AlchemyTracking.refreshUUID();
		}
		
		// ie wants .gif, so rewrite pixel.gif using htaccess
		// no http or https so browser will use protocol of current page
		// see http://www.faqs.org/rfcs/rfc1808.html
		url = "//tracking.alchemysocial.com/pixel.gif";
		url += "?alc_adid=" + ad_id + "&alc_cmpid=" + cmp_id + "&alc_uid=" + u_id;
		if (land_time) {
			url += "&alc_land=" + land_time;
		}
		if (value) {
			url += "&val=" + value;
		}
		if (type) {
			url += "&type=" + type;
		}
		pix.src = url;
	}
};
AlchemyTracking.sendPixel = function(value, type) {
	var pix, url;
	pix = new Image();
	
	// ie wants .gif, so rewrite pixel.gif using htaccess
	// no http or https so browser will use protocol of current page
	// see http://www.faqs.org/rfcs/rfc1808.html
	url = "//tracking.alchemysocial.com/pixel.gif?";
	if (value) {
		url += "val=" + value;
	}
	if (type) {
		url += "&type=" + type;
	}
	pix.src = url;
};
AlchemyTracking.setCookie = function(c_name, value, expiredays) {
	var exdate=new Date();
	var path = "/";
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
		((path) ? ";path=" + path : "" ) ;
};
AlchemyTracking.getCookie = function(c_name) {
	var c_start, c_end;
	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 "";
};
AlchemyTracking.qs = function(name) {
	url = window.location.search.substring(1);
	nva = url.split("&");
	for (i=0;i<nva.length;i++) {
		nv = nva[i].split("=");
		if (nv[0] == name) {
			return nv[1];
		}
	}
};
AlchemyTracking.createUUID = function() {
	// http://www.ietf.org/rfc/rfc4122.txt
	var s = [];
	var hexDigits = "0123456789ABCDEF";
	for (var i = 0; i < 32; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[12] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
	// bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);  

	var uuid = s.join("");
	return uuid;
};
AlchemyTracking.registerLanding = function() {
	var ad_id, u_id, cmp_id, land_time, date;
	
	date = new Date;
	ad_id = AlchemyTracking.qs("alc_adid");
	cmp_id = AlchemyTracking.qs("alc_cmpid");
	land_time = parseInt(date.getTime() / 1000);
	
	if (ad_id && cmp_id) {
		AlchemyTracking.setCookie("alc_adid", ad_id, 30);
		AlchemyTracking.setCookie("alc_cmpid", cmp_id, 30);
		AlchemyTracking.setCookie("alc_land", land_time, 30);
		
		u_id = AlchemyTracking.refreshUUID();
	}
};
AlchemyTracking.refreshUUID = function() {
	var u_id = AlchemyTracking.getCookie("alc_uid");
	if (!u_id) {
		u_id = AlchemyTracking.createUUID();
	}
	AlchemyTracking.setCookie("alc_uid", u_id, 365);
	return u_id;
};
AlchemyTracking.registerConversion = function(value, type, u_id) {
	var ad_id, cmp_id, land_time;
	ad_id = AlchemyTracking.getCookie("alc_adid");
	cmp_id = AlchemyTracking.getCookie("alc_cmpid");
	land_time = AlchemyTracking.getCookie("alc_land");
	
	AlchemyTracking.send(ad_id, cmp_id, land_time, value, type, u_id);
};
