//<![CDATA[

//  Declare custom variable object 
var vs  = new (function () {

var This = this; // This refers to the current vs object. 
This.sensorDomain;
This.sensorPath;
This.internalDoms;
This.fileTypes;
This.customProps = {};
This.standardProps = {};

// Regular expression we use to escape special characters for REFERRER and URL strings 
This.RE={"%09":/\t/g,"%20":/ /g,"%23":/\#/g,"%26":/\&/g,"%2B":/\+/g,"%3F":/\?/g,"%5C":/\\/g,"%22":/\"/g,"%7F":/\x7F/g,"%A0":/\xA0/g};

This.setSensorDomain = function(domain) {
  This.sensorDomain = domain;
}
  
This.setSensorPath = function(path) {
  This.sensorPath = path;
}

This.setInternalDoms = function(internaldoms) {
  This.internalDoms = internaldoms;
}

This.setFileTypes = function(filetypes) {
  This.fileTypes = filetypes;
}

This.setStandardProperty = function(prop,val) {
  This.standardProps[prop] = val;
}

This.setCustomProperty = function(prop,val) {
  This.customProps[prop] = val;
}

// Set the standard client-side properties
This.setStandardProperty("URL", document.location.pathname);
This.setStandardProperty("REFERRER", document.referrer);
This.setStandardProperty("screenres", screen.availWidth + "x" + screen.availHeight);
This.setStandardProperty("cachedefeat", (new Date()).getTime());
This.setStandardProperty("vs.dns", document.location.host);
This.setStandardProperty("vs.pt", document.title);

// Set the version
This.setStandardProperty("vs.ver", "1.7");

  
// We use this to collect META tag information and pass it to the vs object 
This.META = function(){
	var elems;
	// Because there might be crazy people out there still using IE4 
	if (document.all){
		elems=document.all.tags("meta");
	}
	else if (document.documentElement){
		elems=document.getElementsByTagName("meta");
	}
	if (typeof(elems)!="undefined"){
		for (var i=1;i<=elems.length;i++){
			var meta=elems.item(i-1);
			if (meta.name){
				 if (meta.name.indexOf('vs.')==0){
					This.customProps[meta.name.substring(3)]=meta.content;
				}
			}
		}
	}
}

// Function to call the ESCAPE function and appends the token/value pairs to the output string with the & character 
// The values has to be escaped, since they may contain special characters 
This.A = function(N,V){
	return "&"+N+"="+This.ESCAPE(V, This.RE);
}

// Function to escape strings 
This.ESCAPE = function(S, REL){
	if (typeof(REL)!="undefined"){
		var retStr = new String(S);
		for (R in REL){
			retStr = retStr.replace(REL[R],R);
		}
		return retStr;
	}
	else{
		return escape(S);
	}
}

// This function is called to generate the stat tag call 
This.StatCall = function(){
	 // Clear output variable 
	output = "";

	// Check whether the sensorDomain has a value.  If not, use the current document domain 
	if (This.sensorDomain == undefined) {
		This.sensorDomain = document.location.host;
	}

	// Check whether the sensorPath has a value.  If not, use the hard coded value "/js/stats/zag.js" 
	if (This.sensorPath == undefined) {
		This.sensorPath = "/js/stats/zag.js";
	}

	// If the protocol is secure (HTTPS), then the stat tag call is sent in the same manner 
	// This still depends on properly setting the sensorDomain and sensorPath variables 
	var output="http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+This.sensorDomain+This.sensorPath+"?Log=1";
	
	// Itirate through all the vs object variables 
	// and append it to our parameters 
	for (N in This.standardProps){
		if (This.standardProps[N]) {
			output +=This.A(N,This.standardProps[N]);
		}
	}
	
	// Itirate through all the vs object variables 
	// and append it to our parameters 
	for (N in This.customProps){
		if (This.customProps[N]) {
			output +=This.A(N,This.customProps[N]);
		}
	}

	// Check if there are query parameters from the browser URL and add them if they exist
	if (document.location.search != '') {
		output +="&" + document.location.search.substring(1);
	}	
	
	// Write out the stat tag call 
	var head = document.getElementsByTagName('head').item(0)
	var scriptTag = document.getElementById('loadScript');
//	if(scriptTag) head.removeChild(scriptTag);
	var script = document.createElement('script');
	script.src = output;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script);
}

// This function requires token/value pairs passed to it and calls the vsStatCall function to instrument the call 
// Valid token values start with "vs." followed by the value string 
This.StatEvent = function(){
	// The referring page for all events should be the current page where it is called 
	This.setStandardProperty("REFERRER",document.location);
	if (arguments.length%2==0){
		for (var i=0;i<arguments.length;i+=2){
			This.standardProps[arguments[i]]=arguments[i+1];
		}
		This.setStandardProperty("cachedefeat",(new Date()).getTime());
		This.StatCall();
	}
}

// Function used to determine the current pages domain for use in external link tracking 
This.OnsiteCheck = function(host){
	// Assigns the current domain as the internal domain if the internalDoms variable is not set
	if (This.internalDoms == undefined) {
		// This.internalDoms = document.location.hostname;
		This.internalDoms = This.getDomain(document.location.hostname);
	}

	var aDoms=This.internalDoms.split(',');
	for (var i=0;i<aDoms.length;i++){
		if (host.indexOf(aDoms[i])!=-1){
			return 1;
		}
	}
	return 0;
}

// Set the properties for the element tracked via event handlers 
This.Evt = function(evt,tag){
	var e=evt.target||evt.srcElement;
	while (e.tagName&&(e.tagName!=tag)){
		e=e.parentElement||e.parentNode;
	}
	return e;
}

// Bind tracking functions to event handlers 
This.EventBind = function(event,func){
	if ((typeof(func)=="function")&&document.body){
		// W3C event registration model 
       
		if (document.body.addEventListener){
            document.addEventListener(event, func, false);
		}
		// Microsoft event registration model 
		else if(document.body.attachEvent){
			document.body.attachEvent("on"+event, func);
		}
	}
}

// Call event handler tracking functions 
This.ET = function(){
	This.EventBind("mousedown",function(evt) { This.TrackDownload(evt); });
	This.EventBind("mousedown",function(evt) { This.TrackOffsite(evt); });	
}

// Function to grab domain name from hostname value
This.getDomain = function(host){
	var strhost = host.toLowerCase().split(".");
	var sitedomain = strhost[strhost.length - 2]+"."+strhost[strhost.length - 1];
	return sitedomain;
}

// Function to track downloaded files based on file extension 
This.TrackDownload = function(evt){
	evt=evt||(window.event||"");
	if (evt){
		var e=This.Evt(evt,"A");
		if(e.hostname) {
			sitedomain = This.getDomain(e.hostname);
		} else {
			sitedomain = '';
		}

		// Fix for different way that IE and Firefox handles e.pathname variable
		if (typeof(e.pathname) != 'undefined') {
			var regpath = /^\//;
			if (!e.pathname.match(regpath)) {
				newpathname = "/"+e.pathname;
			} else {
				newpathname = e.pathname;
			}
		} else {
			return 0;
		}

		if (sitedomain&&This.OnsiteCheck(sitedomain)){
			// Check whether the fileTypes variable has been populated and assign default file types when it is not
			if (This.fileTypes == undefined) {
				This.fileTypes="xls,doc,pdf,txt,csv,zip,mp3,mpg,avi,mov,gz,gzip";
			}

			if (This.fileTypes.indexOf(newpathname.substring(newpathname.lastIndexOf(".")+1,newpathname.length))!=-1){
				var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
				var path=newpathname?((newpathname.indexOf("/")!=0)?"/"+newpathname:newpathname):"/";
				This.setStandardProperty("vs.dl", "http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://" + e.hostname + path + e.search);
				This.StatEvent("URL","","vs.os","","httpStatuscode","","vs.pt","");
			}
		}
	}
}

// Function to track offsite links 
This.TrackOffsite = function(evt){
	evt=evt||(window.event||"");
	if (evt){
		var e=This.Evt(evt,"A");
		if(e.hostname) {
			sitedomain = This.getDomain(e.hostname);
		} else {
			sitedomain = '';
		}

		if (sitedomain&&!This.OnsiteCheck(sitedomain)&&e.className != 'internal_lnk'){
			var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
			var path=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
			This.setStandardProperty("vs.os", "http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://" + e.hostname + path + e.search);
			This.StatEvent("URL","","vs.dl","","httpStatuscode","","vs.pt","");
		}
	}
}

});

//]]>