var AndoTrack;
if (!this.AndoTrack) {
	// AndoTrack singleton and namespace
	function AndoTrackSingleton() {

		document.write(unescape("%3Cimg%20name%3D'andotrackingpixel'%20style%3D'display%3Anone%3B'%20src%3D''%20width%3D0%20height%3D0%20%2F%3E"));

		var andoPingInterval = "60000";
		var andoclearIt = false;
		var andohost = 'lt.andomedia.com';
		var andopingTimer = null;
		var andopausePing = false;
		var andolastPing = 0;
		/* Threshold is in milliseconds */
		var andopauseThreshold = 60*3*1000; // 3 minutes
		var andocurrentsid = 0;
		var andocurrentextra = "";
		var debugInfo = "";
		var avid = "";

		var _stationId;
		var _cookie;
		var _vid;
		var _ft;
		var _tt;
		var _lt;
		var _sn;

		    
		var setCookie = function() {
			loadVisitorVariables();
		}
		var createCookie = function(name, value, days) {
			var expires = "";
			if (days) {
				var date = new Date();
				date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
				expires = "; expires="+date.toUTCString();
			}
			else { expires = "" }
			var c = name + "=" + value + expires + "; path=/";
			document.cookie = c;
		}

		var readCookie = function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for (var i = 0; i < ca.length; i++) {
				var c = ca[i];
				debugInfo += formatDate() + " Cookie : " + c + "\r\n";
				while (c.charAt(0) == ' ') c = c.substring(1, c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
			}
			return null;
		}

                var loadVisitorVariables = function() {
                        _cookie = readCookie("avid");
                        if (_cookie == null)
                            _cookie = ".";

                        var cArray = _cookie.split(".")
                        var d = new Date();
                        if (cArray.length == 6) {
                                _stationId = cArray[0];
                                _vid = cArray[1];
                                _ft = cArray[2];
                                if (_ft > 9999999999) {
                                        _ft = Math.floor(_ft / 1000);
                                }
                                _lt = cArray[4];
                                if (_lt > 9999999999) {
                                        _lt = Math.floor(_lt / 1000);
                                }
                                _tt = Math.floor(d.getTime() / 1000);
                                _sn = cArray[5];
                                _sn = parseInt(_sn) + 1;
                        }
                        else {
                                _stationId = andocurrentsid;
                                _vid = Math.floor(Math.random() * 9999999);
                                _ft = Math.floor(d.getTime() / 1000);
                                _lt = Math.floor(d.getTime() / 1000);
                                _tt = Math.floor(d.getTime() / 1000);
                                _sn = 1;
                        }

                        var cookieValue = _stationId + "." + _vid + "." + _ft + "." + _lt + "." + Math.floor(d.getTime() / 1000) + "." + _sn;
                        avid = cookieValue;
                        createCookie("avid", cookieValue, 720);
                }

		var formatDate = function() {
			cur = new Date();
			sec = "";
			min = "";
			if (cur.getSeconds() < 10) {
				sec = "0" + cur.getSeconds();
			}
			else {
				sec = cur.getSeconds();
			}
			if (cur.getMinutes() < 10) {
				min = "0" + cur.getMinutes();
			}
			else {
				min = cur.getMinutes();
			}
			return cur.getMonth() + "-" + cur.getDate() + "-" + cur.getFullYear() + " " + cur.getHours() + ":" + cur.getMinutes() + ":" + sec;
		}

		this.AndoStartTracking = function(sid, extra)
		{
			debugInfo = "";
			myRand=parseInt(Math.random()*99999999);
			clearInterval(andopingTimer);

			andocurrentsid = sid;
			setCookie();
			document["andotrackingpixel"].src="";
			url = "http://" + andohost + "/lt?sid=" + sid + "&avid=" + escape(avid) + "&cb=" + myRand;
			if (extra) {
				if (extra != "") {
					url = url + "&" + extra;
					andocurrentextra = extra;
				}
			}
			document["andotrackingpixel"].src=url;
			andolastPing = new Date().getTime();
			andopingTimer = setInterval("AndoTrack.AndoPing()", andoPingInterval);
			andoclearIt = true;
			debugInfo += formatDate() + " New Listener Started : " + url;
		}
		this.AndoPauseTracking = function()
		{
			andopausePing = true;
			debugInfo = formatDate() + " Tracking Paused";
		}
		this.AndoResumeTracking = function()
		{
			andopausePing = false;
			debugInfo = formatDate() + " Tracking Resumed";
		}
		this.AndoEndTracking = function()
		{
			clearInterval(andopingTimer);
			andopingTimer = null;
			debugInfo = formatDate() + " Tracking Ended";
		}
		this.AndoGetDebug = function()
		{
			return debugInfo;
		}

		this.AndoPing = function()
		{
			if (!andopausePing) {
				clearInterval(andopingTimer);
				currentTime = new Date().getTime();
				if ((currentTime - andolastPing) > andopauseThreshold) {
					/* We need to start tracking a new session */
					AndoStartTracking(andocurrentsid, andocurrentextra);	
				}
				else {
					myRand=parseInt(Math.random()*99999999);
					document["andotrackingpixel"].src="";
					url = "http://" + andohost + "/lt?cu=1&cb=" + myRand;
					document["andotrackingpixel"].src= url;
					andolastPing = new Date().getTime();
					andopingTimer = setInterval("AndoTrack.AndoPing()", andoPingInterval);
					debugInfo = formatDate() + " Ping: " + url;
				}
			}
		}
		this.AndoEvent = function(eventName, eventValue)
		{
			if (andopingTimer) {
				/* We are currently pinging */
				myRand=parseInt(Math.random()*99999999);
				document["andotrackingpixel"].src="";
				url = "http://" + andohost + "/lt?cu=1&a=i&in=" + escape(eventName) + "&iv=" + escape(eventValue) + "&cb=" + myRand;
				document["andotrackingpixel"].src= url;
				debugInfo = formatDate() + " Event: " + url;
			}
		}
	}
	AndoTrack = new AndoTrackSingleton();
}
