//TL Dynamic Response v1.4 Standalone
//last modified 04/07/2009 
//parnaudov@technologyleaders.com

var top = new Date();

function DynRes(){
	var that=this;
	// begin: user modifiable
	this.enabled=true;
	this.fpc="C2B";

	// Round trip response time buckets (in seconds) defined here.
	this.rtt = [ 
	['Fast',    '0', '1'],
        ['Average', '1', '4'],   
        ['Slow',    '4', '60'] ];   

	// Page loading time buckets (in seconds) defined here.\
	this.plt = [ 
	['Fast',    '0', '0.5'],
 	['Average', '0.5', '4'],   
	['Slow',    '4', '60'] ];   

	// end: user modifiable
	this.DT={};
	this.images=[];
}
DynRes.prototype.drGetCookie=function(){
	var nameEQ = this.fpc + "=";
	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) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
DynRes.prototype.createDeltaCookie=function(href) {
	var time = new Date();
	if (href) var dc = this.fpc+"="+href.replace(/http\:\/\//g,"")+"|"+time.getTime()+";path=/";
	if (dc) document.cookie = dc;
	//alert(dc);
}
DynRes.prototype.deleteDeltaCookie=function() {
	if (_dr.drGetCookie(this.fpc) ) {
    		var d = new Date();
    		document.cookie = this.fpc+"=;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/";
	}
	//var c = _dr.drGetCookie(this.fpc);
	//if (c) alert (c);
}
DynRes.prototype.cleanDeltaCookie=function() { 
	var c = this.drGetCookie();
	if (typeof(c)=='undefined') return;

	if (c && c.indexOf("|")!=-1)
	    var cts = c.split("|");
	else if (c)
	    this.deleteDeltaCookie();
	else 
	    return;

	if (cts[0] && cts[1]) {
	   if(window.location.href.indexOf(cts[0])==-1)
		this.deleteDeltaCookie();
	}
}
DynRes.prototype.trackClick=function(evt) {
 evt = evt||(window.event||"");
   if (evt) {
      var e = _dr.drEvt(evt, "A");
	if ( typeof(e) != 'undefined' && e) {
	if (e.href && e.href.indexOf(window.location.hostname)==-1) {
		//alert ("Detected site exit from link. Aborting");
		this.deleteDeltaCookie(); 
	}
	else if (e.href) {
		//alert("creating C2B cookie");
		this.createDeltaCookie(e.href);
	}
	} // end if e
   } // end if evt
}
DynRes.prototype.drBind=function(event,func){
	if ((typeof(func)=="function")&&document.body){
		if (document.body.addEventListener){
			document.body.addEventListener(event, func.wtbind(this), true);
		}
		else if(document.body.attachEvent){
			document.body.attachEvent("on"+event, func.wtbind(this));
		}
	}
}
DynRes.prototype.drEvt=function(evt,tag){
	var e=evt.target||evt.srcElement;
	while (e.tagName&&(e.tagName!=tag)){
		e=e.parentElement||e.parentNode;
	}
	return e;
}
DynRes.prototype.checkDelta=function() {
		if (top) {
		  var bottom = new Date();
		    this.DT.plt = roundN((bottom-top) / 1000, 2); 
			for (t=0;t<this.plt.length;t++) { 
				if (this.plt[t][1] <= this.DT.plt && this.DT.plt <= this.plt[t][2]) {
					this.DT.plg = this.plt[t][0]+" ("+this.plt[t][1]+"-"+this.plt[t][2]+" sec.)";					
					break;
				}
				if (t==this.plt.length-1 && this.DT.plt > this.plt[t][2]) { 
					this.DT.plg = "Slow beyond Range ("+this.plt[t][1]+"+ sec.)";
					break;
				}
			}
		top="";
		}

		var c = this.drGetCookie();
		if (typeof(c)=='undefined') return;
		    this.deleteDeltaCookie();

		if (c && c.indexOf("|")!=-1)
		    var cts = c.split("|");
		else if (c) //corrupt cookie?
	    	    this.deleteDeltaCookie();
		else 
	    	    return;

	if (cts[0] && cts[1] && window.location.href.indexOf(cts[0])!=-1) {
		var now = new Date();
		var delta = now - cts[1];
		    _dr.DT.rtt = roundN(delta / 1000, 2);
			for (t=0;t<this.rtt.length;t++) { 
				if (this.rtt[t][1] <= _dr.DT.rtt && this.DT.rtt <= this.rtt[t][2]) {
					this.DT.rtg = this.rtt[t][0]+" ("+this.rtt[t][1]+"-"+this.rtt[t][2]+" sec.)";					
					break;
				}
				if (t==this.rtt.length-1 && this.DT.rtt > this.rtt[t][2]) { 
					this.DT.rtg = "Slow beyond Range ("+this.rtt[t][1]+"+ sec.)";
					break;
				}
			} //end click-to-bottom eval

		    this.deleteDeltaCookie();
	   	}
	   	else
		    this.deleteDeltaCookie();
}
DynRes.prototype.drInsert=function(A, B, c) {
	for (f in A) c?B[c+f]=A[f]:B[f]=A[f];
}
Function.prototype.fnbind = function(obj){
	var method=this;
	var temp=function(){
		return method.apply(obj,arguments);
	};
	return temp;
}
function roundN(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
	return result;
}
//tracks page abandonment & potential site return via cookie
DynRes.prototype.drTrackLeave=function() { 
	//var href = window.location.href;
	//if (href) { 
	//var dc = APL+"="+href.replace(/http\:\/\//g,"")+"|"+time.getTime()+";path=/";
	//document.cookie = dc;
	//}
}
// after data collection, set up event listeners for click-to-load tracking
DynRes.prototype.drBeginListen=function() {
	var e=(navigator.appVersion.indexOf("MSIE")!=-1)?"click":"mousedown";
	this.drBind(e,this.trackClick);
}
//Main Data Collection wrapper for WT dcsCollect and GA trackPageview
DynRes.prototype.drCollect=function(){ 
    if (this.enabled){
	  this.checkDelta();
	    if (this.DT) {
	     if(typeof _tag != 'undefined' && _tag.WT) { //new WebTrends tag inject & collect
		this.drInsert(this.DT, _tag.WT, "z_");
		_tag.dcsCollect();
	     }
	     else if (typeof dcsTag != 'undefined' && DCSext) { //old WebTrends tag inject & collect
		this.drInsert(this.DT, DCSext);		
	     }
	   } //delta object exists, inserted
	  this.drBeginListen();
    }
}
//init delta object
var _dr=new DynRes();

//early check and clean to avoid on-site navigate away-return
_dr.cleanDeltaCookie();

//event listeners for page abandonment tracking
if (typeof window.attachEvent != "undefined") {
//    window.attachEvent("onbeforeunload", _dr.drCollect);
    window.attachEvent("onbeforeunload", _dr.drTrackLeave)
} 
else if (typeof window.addEventListener != "undefined") {
//        window.addEventListener("load", _dr.drCollect, false);
        window.addEventListener("unload", _dr.drTrackLeave, false);
}