var ENgine_tracker = 
{
	scriptName : "ENgine_tracker.js",
	pixelLocation : "",
	customerId : "",
	uniqueId : "",
	subscriberData : "",
	data : new Array(),
	inited : false,
	totalSet : false,
	isHttps : false,
	
	_getPixelLocation : function(subDomain)
	{
		if(typeof subDomain == 'undefined' || subDomain=='')
		{
			var base = "";
			nl = document.getElementsByTagName('base');
			for (i=0; i<nl.length; i++) 
			{
				if (v = nl[i].href) {
					if (/^https?:\/\/[^\/]+$/.test(v))
						v += '/';
	
					base = v ? v.match(/.*\//)[0] : ''; // Get only directory
				}
			}
			function getBase(n, check) 
			{
				var baseURL = "";
				if (n.src && n.src.indexOf(check)!=-1)
				{
					baseURL = n.src.substring(0, n.src.lastIndexOf('/')+1);
	
					// If path to script is relative and a base href was found add that one infront
					if (base && baseURL.indexOf('://') == -1)
						baseURL = base + baseURL;
	
					return baseURL;
				}
	
				return null;
			};
			baseFound = false;
			nl = document.getElementsByTagName('script');
			for (i=0; i<nl.length; i++) 
			{
				if (bu = getBase(nl[i], this.scriptName))
				{
					this.pixelLocation = bu;
					baseFound = true;
					break;
				}
			}

			// Check head
			if(!baseFound)
			{
				n = document.getElementsByTagName('head')[0];
				if (n) {
					nl = n.getElementsByTagName('script');
					for (i=0; i<nl.length; i++) {
						if (bu = getBase(nl[i], this.scriptName))
						{
							this.pixelLocation = bu;
							break;
						}
					}
				}
			}
		}else
		{
			this.pixelLocation = subDomain + "mail/";
		}
	
		this.pixelLocation += "lead.php";
		this.isHttps = this.pixelLocation.toLowerCase().indexOf('https://')==0;
	},
	
	_init : function ()
	{		 
		if(typeof ENgine_tracker_initialized != 'undefined')
			ENgine_tracker_initialized();
	},
	
	_setLeadData : function (cid, type, uid, total)
	{
		this.customerId	 = cid;
		this.uniqueId		 = uid;
		this.data				 = new Array();
		this.data['type'] = type;
		if(typeof total != 'undefined')
		{
			this.totalSet = true;
			this.data['total'] = total;
		}else
		{
			this.totalSet		 = false;
			this.data['total'] = 0;
		}
	},
	
	_addRow : function (product, category, price, quantity)
	{
		if(typeof this.data['rows'] == 'undefined') this.data['rows'] = new Array();
		if(typeof price		== 'undefined') price = '0';
		if(typeof quantity == 'undefined') quantity = '0';
		if((''+price).indexOf(',')!==-1) price = (''+price).replace(/,/g, '.');

		price		= parseFloat(price);
		quantity = parseFloat(quantity);

		var tmp = new Array();
		tmp['product']	= product;
		tmp['category'] = category;
		tmp['price']		= price;
		tmp['quantity'] = quantity;
		
		if(!this.totalSet)
			this.data['total'] += price * quantity;
		
		this.data['rows'][this.data['rows'].length] = tmp;
	},
	
	_process : function(subDomain)
	{
		this._getPixelLocation(subDomain);
		
		url = this.pixelLocation + '?cid=' + this.customerId + '&uid=' + this.uniqueId;
		if(this.data['type'])	url += '&type='	+ this._encode(this.data['type']);
		if(this.data['total']) url += '&total=' + this._encode(this.data['total']);

		if(this.isHttps && this.subscriberData != '') 
		{
			url += '&aid=' + this._encode(this.subscriberData);
		}else if((cook = this._readCookie())!='')
		{
			url += '&aid=' + cook;
		}

		if(typeof this.data['rows'] != 'undefined')
		{
			for(var i=0;i<this.data['rows'].length;i++)
			{
				if(typeof this.data['rows'][i]['product']	!= 'undefined') url += '&product['+i+']='	+ this._encode(this.data['rows'][i]['product']);
				if(typeof this.data['rows'][i]['category'] != 'undefined') url += '&category['+i+']=' + this._encode(this.data['rows'][i]['category']);
				if(typeof this.data['rows'][i]['price']		!= 'undefined') url += '&price['+i+']='		+ this._encode(this.data['rows'][i]['price']);
				if(typeof this.data['rows'][i]['quantity'] != 'undefined') url += '&quantity['+i+']=' + this._encode(this.data['rows'][i]['quantity']);
			}
		}
		document.write('<img src="'+url+'" width="1" height="1" alt="" />');
	},
	
	_setSubscriberData : function(str)
	{
		this.subscriberData = str;
	},
	
	_encode : function(str)
	{
		return encodeURIComponent(str);
	},
	
	_readCookie : function() 
	{
		var ids = this.customerId.split("__");
		if(typeof ids[0] != 'undefined')
		{
			var name = 'e-ngine_tracking_' + ids[0]; 
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			var cookVal = "";
			for(var i=0;i < ca.length;i++) 
			{
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0)
				{
					cookVal = c.substring(nameEQ.length,c.length);
					break;
				}
			}
			var tmpCookVal = cookVal.split('%257D%257D%257B%257B'); // }}{{ urlencoded
			var returnCookVal = '';
			ids.shift(); 
			var mlid = ids.join('__');
			for(var i=0;i<tmpCookVal.length;i++)
			{
				cookVal = tmpCookVal[i].split('__');
				if((typeof cookVal[0] != 'undefined') && ((cookVal[0] == mlid) || (mlid=='' && returnCookVal == '')))
				{
					returnCookVal = tmpCookVal[i];
					// do not break, maybe correct value isnt first one
				}
			}
			return returnCookVal;
		}
		return '';
	}
	
}
ENgine_tracker._init();