if (typeof ihs == "undefined")
	var ihs = {};

if (typeof ihs.logging == "undefined" || ihs.logging == null) {
    ihs.logging = {};
}

ihs.logging.serverHost = null;

ihs.logging.getServerHost = function() {
        if (ihs.logging.serverHost == null || typeof ihs.logging.serverHost == "undefined") {
            var src = ihs.logging.getLoggingScriptTag();
            var matches = src.match(/(https?:\/\/[\w\-\d\.:]+)\/traffic\-logger\/javascript\/logging\.js(\?.*)?$/);
            if (matches && matches[1]) {
                ihs.logging.serverHost = matches[1];
            } else {
                //If the link to this file was not an absolute url, grab the protocol/hostname from the current address
                ihs.logging.serverHost = window.location.href.match(/(https?:\/\/[\w\d\.:]+).*/)[1];
            }
        }
        return ihs.logging.serverHost;
    };
    
ihs.logging.getLoggingScriptTag = function() {
        var scriptList = document.getElementsByTagName('script');
        for (var i = 0; i < scriptList.length; i++) {
            var src = scriptList[i].getAttribute('src');
            if (src && src.match(/logging\.js(\?.*)?/)) {
                return src;
            }
        }
    };

ihs.addScript = function(name) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = ihs.logging.getServerHost()+"/traffic-logger/javascript/"+name+"-10872.js";
    script.setAttribute('id', name+"_script_tag");
    document.getElementsByTagName("head")[0].appendChild(script);
}

var historyWritten = false;
/**
 * Check to see if the current page has been visited (i.e. user clicked the back button)
 * Pages are marked with a hash parameter (#visited=true). Calls window.location.replace()
 * function to keep from entering multiple entries in the browser's history stack
 * Call ihs.util.history.isFirstLoad() to find out if the user has been to this page
 * previously
 */
ihs.history = 
    {
    	isLoaded: false,
    	domLoadTime: -1,
    	pageLoadTime: -1,
    	loadStartTime: -1,
        onLoad: function(e) {
			var trafficIDString = "572999027";
			var trafficID = -1;
			if (trafficIDString.match(/\d/)) {
				trafficID = parseInt(trafficIDString);
			}
			if (trafficID > 0 && (!window.location.hash || window.location.hash.indexOf("trafficLogID") < 0)) {
				var newUrl = window.location.href;
				if (newUrl.indexOf('#') < 0) {
					newUrl += "#trafficLogID="+trafficID;
				} else {
					newUrl += "&trafficLogID="+trafficID;
				}
				window.location.replace(newUrl);
			}
				
        	this.isLoaded = true;
            var visited = (window.location.hash.indexOf('visited') >= 0) ? true : false;
            if (!visited) {
	            this._isFirstLoad = true;
	            var hashloc = window.location.href;
	            if (window.location.hash.length > 2) {
	                hashloc += "&visited=true";
	            } else {
	                hashloc = "#visited=true";
	            }
	            window.location.replace(hashloc);
            }
            /*
             * If we never captured the actual page load time, note it here
             * In reality, it should only make a difference of a few milliseconds
             */
            if (ihs.history.pageLoadTime <= 0) 
                ihs.history.notePageLoad();
        },
        
        _isFirstLoad: false,
        isFirstLoad: function() {
            return this._isFirstLoad;
        },
        noteDomLoad: function() {
        	if (ihs.history.domLoadTime <= 0)
        		ihs.history.domLoadTime = new Date().getTime();
       		return true;
        },
        notePageLoad: function() {
        	if (ihs.history.pageLoadTime <= 0)
	        	ihs.history.pageLoadTime = new Date().getTime();
	        if (ihs.history.domLoadTime <= 0)
	           ihs.history.domLoadTime = ihs.history.pageLoadTime;
       		return true;
        }
    };

    
ihs.loadScript = function() {
	if (ihs.history.isLoaded)
		return true;
	try {
        ihs.history.onLoad();
        ihs.logging.execute();
    } catch (e) {
    	try {
	        if (typeof console != "undefined" && console !== null && console.log !== null) {
	        	console.log("Exception caught executing logging "+e);
	        }
    	} catch(e) {}
    }
}


ihs.startLoadTimer = function(time) {
    if (arguments.length < 1) 
        time = 10000;
	setTimeout(function() {
				if (!ihs.history.isLoaded) {
					ihs.history.notePageLoad();
					ihs.loadScript();
				}
			}, time);
}

var loadStartTime = window.location.href.match(/.*loadStart=(\d+).*/);
if (loadStartTime) {
	ihs.history.loadStartTime = loadStartTime[1];
} else {
    ihs.history.loadStartTime = new Date().getTime();
}
if (window.addEventListener) {
    window.addEventListener('load', ihs.history.notePageLoad, false);
    window.addEventListener('load', ihs.loadScript, false);
    if (document.addEventListener) {
    	document.addEventListener("DOMContentLoaded", ihs.history.noteDomLoad, false);
    	document.addEventListener("DOMContentLoaded", ihs.startLoadTimer, false);
   	}
} else if (window.attachEvent) {
    window.attachEvent('onload', ihs.history.notePageLoad);
    window.attachEvent('onload', ihs.loadScript);
    
}

if (/MSIE/i.test(navigator.userAgent)) {
	var serverHost = ihs.logging.getServerHost();
	document.write("<script id=\"__ie_onload\" defer=\"defer\" src=\""+serverHost+"/traffic-logger/javascript/blank.js\"><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
	  if (this.readyState == "complete") {
	    ihs.history.noteDomLoad();
	  	ihs.startLoadTimer();

	  }
	};
}

if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      clearInterval(_timer);
      ihs.history.noteDomLoad(); // call the onload handler
      ihs.startLoadTimer();
    }
  }, 10);
}

if (!ihs.logging.apiLoaded) {
	if (typeof Prototype == "undefined")
	    ihs.addScript('logging-api-alldeps');
	else
	    ihs.addScript('logging-api');
}
var startTime = new Date().getTime();

    
