/* -*- C++ -*- */

/* Jazzio Network Services: Visitor statistics collector
 * @(#) $Id$
 */

/* Copyright 2005, 2006, 2007, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

if(typeof com == "undefined") var com = {};
if(typeof com.jazzio == "undefined") com.jazzio = {};
com.jazzio.Stat = new Object();
com.jazzio.Collector = function()
{
	this._version = 115;
	this.root = '';
	this.imgPath = '/collector.gif';
	this.account = 0;
	this.adgroup = 0;
	this.advert = 0;
	this.member = 0;
	this.entrance = 0;
	this.returning = 0;
	this.pageid = 0;
	this.refhost = '';
	this._clientid = '';
	this._cexpires = 'Sun, 18 Jan 2038 00:00:00 GMT';
	this._cpath = '/';
	this._cdomain = '';
	this._csecure = false;
	this.sid = '';
	this.location = document.location;
	com.jazzio.Stat.ss = '';
	this._stat();
}
com.jazzio.Collector.prototype._debug = function(s)
{
	if(typeof Shine != 'undefined') Shine.debug(s);
}
com.jazzio.Collector.prototype._stat = function()
{
	if(self.screen)
		{
			com.jazzio.Stat.ss = self.screen.width + 'x' + self.screen.height + 'x' + self.screen.colorDepth;
		}
	if(this._getCookie('__jcrv'))
		{
			this.returning = 1;
		}
	this._clientid = this._getCookie('__jcid');
	if(this._clientid)
		{
			this.returning = 1;
			this.entrance = 0;
		}
	else
		{
			this._genCID();
			this.entrance = 1;
		}
	this._setCookie('__jcid', this._clientid, true);
	this._setCookie('__jcrv', 1, false);
}
com.jazzio.Collector.prototype._getCookie = function(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	var end;
	if (begin == -1) 
		{
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} 
	else
		{
			begin += 2;
		}
	end = document.cookie.indexOf(";", begin);
	if (end == -1)
		{
			end = dc.length;
		}
	return unescape(dc.substring(begin + prefix.length, end));
}
com.jazzio.Collector.prototype._setCookie = function(name, value, session)
{
	var expires = '';
	if(!session) expires = this._cexpires;
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((this._cpath) ? "; path=" + this._cpath : "") +
	((this._cdomain) ? "; domain=" + this._cdomain : "") +
	((this._csecure) ? "; secure" : "");
}
com.jazzio.Collector.prototype._genCID = function()
{
	var dt, x = '';
	
	dt = new Date;
	if(document.domain)
		{
			x = document.domain;
		}
	this._clientid = Math.round(Math.random()*2147483647) + ':' + dt.getTime() + ':' + x;
}
com.jazzio.Collector.prototype._imageLoaded = function()
{
	return;
}
com.jazzio.Collector.prototype._e = function(s)
{
	return encodeURIComponent(s);
}
com.jazzio.Collector.prototype._canon = function(x)
{
	var i;
	
	if((i = x.indexOf('://')) < 0) return '';
	x = x.substring(i + 3, x.length);
	if((i = x.indexOf('/')) > -1) x = x.substring(0, i);
	if(x.indexOf('www.') == 0) x = x.substring(4, x.length);	
	x = x.toLowerCase();
	return x;
}
com.jazzio.Collector.prototype.collect = function(accId, sid)
{
	var img, params;
	
	if(typeof accId != 'undefined' && accId && accId > 0)
	{
		this.account = accId;
    }
	if(typeof sid != 'undefined' && sid)
	{
		this.sid = sid;
	}
	if(this.refhost == '' && document.referrer)
    {
		this.refhost = document.referrer;
	}
	this.refhost = this._canon(this.refhost);
	this._dom = this._canon(document.domain);
	if(this._dom.length && this._dom == this.refhost)
	{
		this.refhost = '';
		this.entrance = 0;
	}
	params = '';
	params += 'a=' + this._e(this.account) + '&';
	params += 'c=' + this._e(this._clientid) + '&';
	params += 'l=' + this._e(this.location.pathname) + '&';
	params += 'ag=' + this._e(this.adgroup) + '&';
	params += 'ai=' + this._e(this.advert) + '&';
	params += 've=' + this._e(this.entrance) + '&';	
	params += 'vm=' + this._e(this.member) + '&';
	params += 'vr=' + this._e(this.returning) + '&';
	params += 'pi=' + this._e(this.pageid) + '&';
	params += 'r=' + this._e(this.refhost) + '&';
	params += 'ss=' + this._e(com.jazzio.Stat.ss) + '&';
	params += 'pl=' + this._e(this.location.hostname) + '&';
	params += 'jv=' + this._e(this._version) + '&';
	params += 'sid=' + this._e(this.sid);
	img = new Image(1, 1);
	img.src = this.root + this.imgPath + '?' + params;
	img.onload = this._imageLoaded;
}

com.jazzio.tracker = new com.jazzio.Collector();
