if ( typeof _pageTrackerCount == "undefined" ) {
	var _pageTrackerCount = 0;
	var _d2ka = [];
}

function _pageTracker(eventId, eventName) {
	this.trackerCount = _pageTrackerCount;
	_pageTrackerCount += 1;

	if ( eventName ) {
		this.eventName = eventName;
	} else {
		this.eventName = null;
	}

	this.redirectUrl = null;
	this.onMarkerCallback = null;
	this.parameters = [];
	this.markers = [];
	this.externalMarkers = [];

	_d2ka[ this.trackerCount ] = this;

	this.isDisabled = (window.location.search.indexOf('_tracker=false') == -1) ? false : true;
	this.isDebug = (window.location.search.indexOf('_tracker=debug') == -1) ? false : true;
	if ( this.isDisabled ) {
		var debugEl = document.createElement('div');

		debugEl.style.color = 'white';
		debugEl.style.padding = '6px';
		debugEl.style.backgroundColor = '#2F579B';
		debugEl.innerHTML = 'Tracking code for event ';

		if ( this.eventName != null ) {
			debugEl.innerHTML += this.eventName;
		} else {
			debugEl.innerHTML += eventId;
		}

		debugEl.innerHTML += ' correctly installed</div>';

		if ( document.body.childNodes.length > 0 ) {
			document.body.insertBefore(debugEl, document.body.childNodes[0]);
		} else {
			document.body.appendChild(debugEl);
		}

		return;
	}

	this.tsUrl  = this.isSSL() ? 'https://pix.lfstmedia.com' : 'http://pix.lfstmedia.com';
	this.tsUrl += '/_tracker/' + eventId + '?__ts=' + new Date().getTime();
}

_pageTracker.prototype.setRedirectUrl = function(url) {
	if ( this.isDisabled ) {
		var trackerScript = document.getElementById('_trackerScript');
		trackerScript.parentNode.innerHTML += '<div>Click <a href="' + url +'">here</a> to get redirect to page: ' + url + '</div>';

		return;
	}

	this.redirectUrl = url;
}

_pageTracker.prototype.addParameter = function(pKey, pValue) {
	this.parameters.push({key: pKey, value: pValue});
	this.preprocessParameter(pKey, pValue);
}

_pageTracker.prototype.run = function() {
	if ( this.isDisabled ) { return; }

	if ( this.onMarkerCallback != null ) {
		var mrks = [];

		for ( var i = 0; i < this.externalMarkers.length; i++ ) {
			mrks[ this.externalMarkers[i].key ] = this.externalMarkers[i].value;
		}

		this.onMarkerCallback(mrks);
	}

	if ( this.redirectUrl == null ) {
		return;
	}

	window.location = this.redirectUrl;
}

_pageTracker.prototype.parseStruct = function(struct, isBold) {
	var bold = isBold;
	var result = '<ul>';

	if ( typeof(struct['isCurrent']) != 'undefined' && struct['isCurrent'] == true ) {
		bold = true;
	}

	for (s in struct) {
		switch ( typeof(struct[s]) ) {
			case 'object':
				if ( bold ) {
					result += '<li style="color: yellow;">';
				} else {
					result += '<li>';
				}

				result += String(s) + '</li>';
				result += this.parseStruct(struct[s], bold);
				break;

			case 'function':
			case 'undefined':
				break;

			default:
				if ( bold ) {
					result += '<li style="color: yellow;">';
				} else {
					result += '<li>';
				}

				result += String(s) + ': ' + String(struct[s]) + '</li>';
				break;
		}
	}

	result += '</ul>';
	return result;
}

_pageTracker.prototype.onDebugBtnShowClick = function(e, self, struct) {
	var debugInfoElement = null;
	if ( !e ) { var e = window.event; }

	if ( self.parentNode.childNodes.length <= 1 ) {
		debugInfoElement = document.createElement('div');
		debugInfoElement.style.color = 'white';
		debugInfoElement.style.padding = '6px';
		debugInfoElement.style.fontSize = '11pt';
		debugInfoElement.style.backgroundColor = '#2F579B';
		debugInfoElement.innerHTML = this.parseStruct(struct, false);

		self.parentNode.appendChild(debugInfoElement);
	} else {
		debugInfoElement = self.parentNode.childNodes[1];
	}

	if ( self.value == 'Show' ) {
		self.value = 'Hide';
		debugInfoElement.style.display = '';
	} else {
		self.value = 'Show';
		debugInfoElement.style.display = 'none';
	}
}

_pageTracker.prototype.display = function(struct) {
	var debugBtnContainer = document.createElement('div');
	debugBtnContainer.style.color = 'white';
	debugBtnContainer.style.padding = '6px';
	debugBtnContainer.style.fontSize = '11pt';
	debugBtnContainer.style.backgroundColor = '#2F579B';

	var self = this;
	var debugBtnShow = document.createElement('input');
	debugBtnShow.setAttribute('type', 'button');
	debugBtnShow.value = 'Show';
	debugBtnShow.onclick = function(e) {
		self.onDebugBtnShowClick(e, this, struct);
	}

	debugBtnContainer.appendChild(debugBtnShow);

	if ( document.body.childNodes.length > 0 ) {
		document.body.insertBefore(debugBtnContainer, document.body.childNodes[0]);
	} else {
		document.body.appendChild(debugBtnContainer);
	}

	this.onDebugBtnShowClick({}, debugBtnShow, struct);
}

_pageTracker.prototype.isSSL = function() {
	if ( this.isDisabled ) { return; }
	return (document.location.protocol == 'http:') ? false : true;
}

_pageTracker.prototype.readCookie = function(name) {
	if ( this.isDisabled ) { return; }
	if ( document.cookie.indexOf(name) == -1 ) {
		return null;
	}

	var cookies = document.cookie.split(';');
	for ( var i = 0; i < cookies.length; i++ ) {
		var c = cookies[i];

		// Strip spaces
		while ( c.charAt(0) == ' ' ) {
			c = c.substring(1, c.length);
		}

		if ( c.indexOf(name + '=') == 0 ) {
			return unescape( c.substring(name.length + 1, c.length) );
		}		
	}

	return null;	
}

_pageTracker.prototype.createCookie = function(name, value, days) {
	if ( this.isDisabled ) { return; }

	var expires = '';
	if ( days ) {
		var date = new Date();
		date.setTime( date.getTime() + (days * 24 * 60 * 60 * 1000) );
		var expires = '; expires=' + date.toGMTString();
	}

	document.cookie = name + '=' + value + expires + '; path=/';
}

_pageTracker.prototype.readContent = function(query) {
	if ( this.isDisabled ) { return; }
	var script = document.createElement('script');
	var targetUrl = this.tsUrl;

	targetUrl += '&__pt=_d2ka[' + this.trackerCount + ']';

	var oid = null;
	
	for ( var i = 0; i < this.parameters.length; i++ ) {
		targetUrl += '&' + this.parameters[i].key + '=' + escape(this.parameters[i].value);
		if( this.parameters[i].key == '__oid') {
		   oid = this.parameters[i].value;
		}
	}
	
	if( oid == null ){
	   oid = this.readOid();
	   if(oid!=null) {
	   	targetUrl += '&__oid=' + oid;
	   }
	}

	for ( var j = 0; j < this.markers.length; j++ ) {
		targetUrl += '&marker[' + this.markers[j].key + ']=' + escape(this.markers[j].value);
	}

	if ( query != null ) {
		targetUrl += '&' + query;
	}

	if ( this.isDebug ) {
		targetUrl += '&_tracker=debug';
	}

	script.setAttribute('src', targetUrl);
	script.setAttribute('type', 'text/javascript');

	var head = document.getElementsByTagName('head')[0];
	head.appendChild(script);
}

_pageTracker.prototype.insertCode = function(code) {
	if ( this.isDisabled ) { return; }

	var codeURL = this.isSSL() ? 'https://pix.lfstmedia.com' : 'http://pix.lfstmedia.com';
	codeURL += '/_js/' + code + '?__ts=' + new Date().getTime();

	var iframe = document.createElement('iframe');
	iframe.src = codeURL;
	iframe.style.position = 'absolute';
	iframe.style.left = '0px';
	iframe.style.top = '0px';
	iframe.style.height = '1px';
	iframe.style.width = '1px';
	iframe.style.visibility = 'hidden';

	document.body.appendChild(iframe);	
}

_pageTracker.prototype.insertHtml = function(content) {
	if ( this.isDisabled ) { return; }

	var iframe = document.createElement('iframe');
	iframe.src = 'javascript:;';
	iframe.style.position = 'absolute';
	iframe.style.left = '0px';
	iframe.style.top = '0px';
	iframe.style.height = '1px';
	iframe.style.width = '1px';
	iframe.style.visibility = 'hidden';

	document.body.appendChild(iframe);

	var doc = iframe.document;

	if ( iframe.contentDocument ) {
		doc = iframe.contentDocument;
	} else if ( iframe.contentWindow ) {
		doc = iframe.contentWindow.document;
	}

	doc.open(); doc.write('<html><head></head><body>' + content + '</body></html>'); doc.close();
}

_pageTracker.prototype.addMarker = function(pKey, pValue) {
	this.externalMarkers.push({key: pKey, value: pValue});
}

_pageTracker.prototype.setMarker = function(pKey, pValue) {
	this.markers.push({key: pKey, value: pValue});
}

_pageTracker.prototype.onMarker = function(callback) {
	this.onMarkerCallback = callback;
}

_pageTracker.prototype.preprocessParameter = function(pKey, pValue) {

  if(pKey=='hnm')
    this.parameters.push({key: '__hnm', value: pValue});
  if(pKey=='ver')
    this.parameters.push({key: '__ver', value: pValue});
  if(pKey=='dpp'){
    var dpp_plus = pValue.replace(/ /g,'+');
    this.parameters.push({key: '__dpp', value: dpp_plus});
  }
  if(pKey=='exit-path')
    this.parameters.push({key: '__epath', value: pValue});

}

_pageTracker.prototype.readOid = function() {
    var url = document.getElementsByTagName('base');

    if( url.length==0) {
      url = document.location.href;
    }else{
      url = url[0].getAttribute('href');
    }

    var matched = url.match(/^http[s]?:\/\/[0-9a-zA-Z\._-]+\/([0-9a-zA-Z\_-]+)/);
	if( matched ) {
       return matched[1];
    } else {
       return null;
    }
}

_pageTracker.prototype.track = function() {
	if ( this.isDisabled ) { return; }

	var trs = this.readCookie('trs');

	if ( /^ip[0-9a-zA-Z]{1,6}-[0-9a-zA-Z]{22}$/.test(trs) ) {
		this.readContent('__sid='+trs);
	} else {
		this.readContent(trs);
	}
}
