// $Revision: 13563 $
// $Date: 2007-05-25 16:02:51 -0400 (Fri, 25 May 2007) $

////////////////////////////////////////////////////////////////////////
// Analytics class
////////////////////////////////////////////////////////////////////////

/**
 * Main Analytics class.
 * The Analytics is available as a global variable analytics
 * 
 * @author     Daniel Sevcik <daniel.sevcik@dev.webdevelopers.cz>
 * @version    $Revision: 13563 $
 * @access     public
 */
var Analytics=function() {
		this.currentValues={};		
		
		this.constructor=function() {
				notice('Analytics (revision '+'$Revision: 13563 $'.replace(/[^0-9]/g, '')+')');
				var thisObj=this;

				this._initReferer();
				this._initEntryPage();
				
				if (window.attachEvent) {
						window.attachEvent("onunload", function() {thisObj.onUnload();});
				} else if (document.addEventListener) {
						window.addEventListener("unload", function() {thisObj.onUnload();}, true);
				}
		}

		/**
		 * Sets the entry page if not known yet.
		 *
		 * @access private
		 * @return void
		 */
		this._initEntryPage=function() {
				var referer=document.referrer || document.referer;
				var defaultVal;
				
				if (this._isURLInSite(referer)) {
						defaultVal=referer;
				} else {
						defaultVal=document.location.href;
				}
				this._initValue('entryPage', defaultVal, false);				
		}
		
		/**
		 * Sets the referer if not known yet.
     * Note: Always update the referrer for every external link (david said)
		 *
		 * @access private
		 * @return void
		 */
		this._initReferer=function() {
				// Set the current referer only if it is not originating from current website.
				var currentReferer=document.referrer || document.referer;
				var cookieReferer=getAnalyticsCookie('originalReferer');

				if (!currentReferer && !cookieReferer) { // First page no referrer - suppose blocked
						this._initValue('originalReferer', 'Analytics:Blocked', false);
				} else if (currentReferer && !cookieReferer) {
						if (this._isURLInSite(currentReferer)) { 
								// If referrer is in-site there is always value set by previous page unless Analytics is not installed.
								// Also may happen if the user had Javascript OFF on the previous page...
								this._initValue('originalReferer', 'Analytics:Unhandled:'+currentReferer, false);
						} else {
								this._initValue('originalReferer', currentReferer, false);								
						}
				} else if (currentReferer && cookieReferer) {
						if (cookieReferer == 'Analytics:Blocked') { // Not blocked if we have current
								this._initValue('originalReferer', 'Analytics:Typed-in', true);
						} else {
								notice('Unchanged originalReferer='+cookieReferer);
						}
				} else { //if (!currentReferer && cookieReferer)
						notice('Unchanged originalReferer='+cookieReferer);
				}
		}

		this._isURLInSite=function(url) {
				return url && this.extractHost(url) == document.location.host;				
		}

		/**
		 * Sets the referer if not known yet.
     * Note: Always update the referrer for every external link (agreed)
		 *
		 * @access private
		 * @param string name of the value
		 * @param string defaultVal default value
		 * @param bool override if defaultVal is non-empty always override the current value with the defaultVal 
		 * @return void
		 */
		this._initValue=function(name, defaultVal, override) {
				// Set the default
				var val=false;
				if (typeof defaultVal !== 'string' || !defaultVal.length) {
						val=getAnalyticsCookie(name);
						warning('Cannot get the default value for '+name+'!\nOld cookie value: '+val+'\nDefault: '+defaultVal);
				} else if (override || !getAnalyticsCookie(name)) {
						val=defaultVal;
						debug('Setting (forced) the '+name+'='+val+'...\nOld cookie value: '+getAnalyticsCookie(name)+'\nDefault: '+defaultVal);
						setAnalyticsCookie(name, val);
				} else {
						val=getAnalyticsCookie(name);						
						debug('Old cookie value '+name+'='+val+'\nDefault: '+defaultVal);
				}

				this.currentValues[name]=val;
		}

		this.onUnload=function() {
				notice('Re-saving all the values..');
				for(var name in this.currentValues) {
						if (this.currentValues[name]) {
								setAnalyticsCookie(name, this.currentValues[name]);
						}
				}
		}

		/**
		 * Extract the host name from the URL.
		 *
		 * @access public
		 * @return string
		 */
		this.extractHost=function(url) {
				var parts=(url+'').match(/:\/\/([^:/]+)/);
				return parts && parts[1] && parts[1].toLowerCase();
		}

		// Call constructor
		this.constructor();
}

////////////////////////////////////////////////////////////////////////
// Function definitions
////////////////////////////////////////////////////////////////////////
		function notice(msg) {return debug(msg, 0);}
function warning(msg) {return debug(msg, 1);}
function error(msg) {return debug(msg, 2);}

/**
 * Print the debugging msg to the javascript console (FireBug) if available.
 *
 * @access public
 * @param string msg
 * @param int severity 0: notice (default), 1: warning, 2: error
 * @return string msg
 */
function debug(msg, severity) {
		
		if (!window.console || !window.console.log) {  // No FireBug installed
				// For other browsers...
				if (document.location.search.match(/\?debug/) || (document.referer || document.referrer).match(/debug/)) {
						alert(msg);
				}
				return;
		}
		msg='SpirePRO: '+msg;

		switch (severity) {
		case 2: window.console.err(msg); break
		case 1: window.console.warn(msg); break
		default: window.console.log(msg);				
		}
}

/**
 * 
 *
 * @access Public
 * @param string name
 * @param string value
 * @param string expiration in seconds relative to the current time
 * @param string path
 * @param string domain
 * @param bool secure
 * @return void
 */
function setAnalyticsCookie(name, value) {
		name='analytics_'+name;
		var expiration=3600 * 24 * 30;
		var path='/';
		var domain=document.location.host.replace(/^www.?\./, '');
		var secure=false;

		var expirationDate;
		if (expiration) {
				expirationDate=new Date();
				expirationDate.setTime (expirationDate.getTime() + expiration);
		} 
		document.cookie = name + "=" + escape (value) + ((expirationDate) ? "; expires=" + expirationDate.toGMTString() : "") + (path ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/**
 * 
 *
 * @access private
 * @param int 
 * @param 
 * @param 
 * @return void
 */
function getAnalyticsCookie(name) {
		name='analytics_'+name;		
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
				var j = i + alen;
				if (document.cookie.substring(i, j) == arg)
						return _getCookieVal (j);
				i = document.cookie.indexOf(" ", i) + 1;
				if (i == 0) break;
		}
		return null;
}

/**
 * 
 *
 * @access private
 * @param int 
 * @param 
 * @param 
 * @return void
 */
function _getCookieVal(offset) {
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
				endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
}

/**
 * 
 *
 * @access private
 * @param int 
 * @param 
 * @param 
 * @return void
 */
function deleteAnalyticsCookie(name) { 
		var path='/';
		var domain=document.location.host.replace(/^www.?\./, '');
		if (getAnalyticsCookie(name)) {
				document.cookie='analytics_' + name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
}

////////////////////////////////////////////////////////////////////////
// INITIALIZATION
////////////////////////////////////////////////////////////////////////

new Analytics();
