function zExtCook(val) {if ((ec = document.cookie.indexOf(";", val)) == -1) {ec = document.cookie.length;}return unescape(document.cookie.substring(val,ec));}

function ZTrack(objs) {
	this.host = objs["h"];
	this.id = objs["i"];
	this.pid = objs["p"];
	this.cid = objs["c"];
	this.uuid = null;
	this.cname = "zift_ua";
	this.legacy = "zift-user";
	this.loadTime = new Date().getTime();
	this.uuid_holder = null;
	
    this.init = function(){
    	this.uuid_holder = this.getRandomId();
    	this.assignLegacyValues();
    	this.bindOnload();
    	this.bindLinks();
    	this.bindUnload();
    };
    
    this.buildBasicReference = function() {
    	return 'id=' + this.id + '&clid=' + this.cid + '&u=' + this.uuid + '&ekey=' + this.getEmailKey() + "&p=" + this.pid + "&uuid_holder=" + this.uuid_holder;
    };
    
    
    /* view events */
    this.bindOnload = function() {
    	this.triggerViewEvent();
    };
        
    this.triggerViewEvent = function() {
    	var url = this.host + 'trk/v?' + this.buildBasicReference() + '&fr=true' + "&refurl=" + escape(this.getReferer());
    	this.fireViewEvent(url);
    };
    
    this.fireViewEvent = function(url) {
    	var sc=document.createElement('script');
    	sc.setAttribute("type","text/javascript");
    	sc.setAttribute("src", url);
    	sc.onload = sc.onreadystatechange = zBind(this.processViewLoad, this, sc);
    	document.getElementsByTagName("head")[0].appendChild(sc);    	
    };
    
    this.processViewLoad = function(sc) {
    	try {
    		this.uuid = eval(this.uuid_holder).uuid;
    		document.getElementsByTagName("head")[0].removeChild(sc);
    	} catch (e) {};
    };
    
    
    /* link events */
    this.bindLinks = function() {
    	var d = document.getElementById("pw_" + this.id);
    	if (d) {
	    	var links = zS("a", d);
	    	var i = 0;
	    	for (i = 0; i < links.length; i++ ) {
	    		var link = links[i];
	    		var href = link.href;
	    		if (href.match("#$") == "#") continue;
	    		if (link.getAttribute('ztm') == "ignore") continue;
	    		link.onclick = this.chain(link.onclick, zBind(this.triggerClickEvent, this, link, href));    		
	    	}
    	}
    };
    
    this.chain = function(o, n) {
		if (o && typeof o == 'function') {
			return function(e) {
				n.call(this, arguments);
				return o.call(this, arguments);				
			};
		} else {
			return function(e) {
				return n.call(this, arguments);
			};
		}
	}

    this.triggerClickEvent = function(elem, href, e) {
    	var url = this.host + 'trk/c?' + this.buildBasicReference() + '&url=' + escape(href) + (elem.title? '&title=' + escape(elem.title) : '');
    	this.fireIMGEvent(url);    	    	
    	return true;
    };
    
    this.fireIMGEvent = function(url) {
    	var random = Math.random();
    	var i = document.createElement("img");
    	i.src = url + "&random=" + random;
    	i.style.width = "1px";
    	i.style.height = "1px";
    	i.style.border = "none";
    	i.onload = zBind(this.recordIMGEvent, this, i);
    	document.body.appendChild(i);
    };
    
    this.recordIMGEvent = function(e) {
    	document.body.removeChild(e);
    };
       
    
    /* unload events */
    this.bindUnload = function() {
    	zAddOnUnload(zBind(this.triggerExitEvent, this));    	
    };
    
    this.triggerExitEvent = function() {
    	var duration = (new Date().getTime() - this.loadTime) / 1000;
    	var url = this.host + 'trk/v?' + this.buildBasicReference() + '&fr=true' + "&duration=" + duration;
    	this.fireIMGEvent(url);
    };
    
    
    /* helpers */
    this.getReferer = function() {
    	if (document.referrer) {
    		return document.referrer;
    	}
    	return "";
    };

    this.getEmailKey = function() {
		var ekey = "";
		try {
			if (zPanel && zPanel != null)
				ekey = zPanel.getEmailKey();
			if (ekey == "") {
				var ek = this.getQueryStringValue("CakeUUID");
				if (ek != null && ek != "" && ek.indexOf("[mailing_id]") == -1)
					ekey = ek;
			}			
		} catch (exception) {
		}
		return ekey;    	
    };
    
    this.getQueryStringValue = function(param) {
    	var q=document.location.search.substring(1);
    	if (q.length > 0){
    		var params=q.split('&');
    		for (var i=0 ; i<params.length ; i++){
                var pos = params[i].indexOf('=');
                var name = params[i].substring(0, pos);
                var value = params[i].substring(pos + 1);
                if (name == param) {
                	return value;
                	break;
                }
    		}
    	}
    	return null
    };
    
    this.preventDefault = function(e) {
    	if (e) e.preventDefault? e.preventDefault() : e.returnValue = false;
    }; 
    
    this.assignLegacyValues = function() {
    	var nameEQ = this.legacy + "=";
    	var ca = document.cookie.split(';');
    	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) {
    			this.uuid = c.substring(nameEQ.length,c.length);
    			break;
    		}
    	}    	
    };
    
    this.getRandomId = function() {
    	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    	var string_length = 8;
    	var randomstring = '';
    	for (var i=0; i<string_length; i++) {
    		var rnum = Math.floor(Math.random() * chars.length);
    		randomstring += chars.substring(rnum,rnum+1);
    	}
    	return "zt_" + randomstring;
    };
    
    
    this.init();
}


