
WebTracker.prototype._initBuckets = function()
{
	this._bucket={S:0,V:0};
	this.initialised=0;
	var tn = new Date();
	this.VisitorExpiry = new Date();
	this.VisitorExpiry.setDate(tn.getDate()+30); // 30 days
	this.BucketSessionCookie="__wtsb";
	this.BucketVisitorCookie="__wtvb";
	
	var fl = [0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000,0x10000,0x20000,0x40000,0x80000,0x100000,0x200000,0x400000,0x800000,0x1000000,0x2000000,0x4000000,0x8000000,0x10000000,0x20000000,0x40000000,0x80000000];
	var sr={},vr={};
	for(var i=0; i<fl.length; i++)
	{
		var n=i+1;
		sr["S"+n] = {Name:"S"+n,M:fl[i]};
		vr["V"+n] = {Name:"V"+n,M:fl[i]};
	}
	this._fr={
		S:sr, // Session
		V:vr // Visitor
	}
	
	var vc=0, sc=0;
	sc = this._getCookie(this.BucketSessionCookie);
	vc = this._getCookie(this.BucketVisitorCookie);
	if(sc>0)
		this._bucket.S=sc;
	if(vc>0)
		this._bucket.V=vc;
	
	this.initialised=1;
}

WebTracker.prototype._getRegisterByName = function(t,bn)
{
	if(typeof(this._fr) == "undefined")
		return;
		
	for(var n in this._fr[t])
	{
		if(this._fr[t][n].Name==bn)
			return this._fr[t][n].M;
	}
	return 0;
}

WebTracker.prototype._checkBucketState = function(n)
{
	if(typeof(this.initialised)=="undefined" || this.initialised==0)
		this._initBuckets();
}

WebTracker.prototype.AddToBucket = function(t,n)
{
	this._checkBucketState();
	t = t.toUpperCase();
	var f = this._getRegisterByName(t,n);
	if(f>0)
	{
		if(this._bucket[t] ==0)
			this._bucket[t] = f;
		else
			this._bucket[t] = this._bucket[t] | f;
		
		this._store();
	}
}

WebTracker.prototype.RemoveFromBucket = function(t,n)
{
	this._checkBucketState();
	t = t.toUpperCase();
	var f = this._getRegisterByName(t,n);
	if(f>0 && this._bucket[t] & f)
	{
		if(this._bucket[t] ==0)
			return;
		else
			this._bucket[t] = this._bucket[t] ^ f;
	
		this._store();
	}
}

WebTracker.prototype.ChangeBucket = function(t,from,to)
{
	this.RemoveFromBucket(t,from);
	this.AddToBucket(t,to);
}

WebTracker.prototype.InBucket = function(t,n)
{
	this._checkBucketState();
	t = t.toUpperCase();
	var f = this._getRegisterByName(t,n);
	
	return this._bucket[t] & f;
}


WebTracker.prototype.SetBucketName = function(t,n,v)
{
	this._checkBucketState();
	if(typeof(this._fr[t][n]) != "undefined")
		this._fr[t][n].Name = v;
}

WebTracker.prototype._store = function()
{
	this._setCookie(this.BucketSessionCookie,this._bucket['S'],undefined,this._cookieDomain);
	this._setCookie(this.BucketVisitorCookie,this._bucket['V'],this.VisitorExpiry,this._cookieDomain);
}

WebTracker.prototype.GetAllBuckets = function()
{
	this._checkBucketState();
	var b = {
		VisitorBuckets:[],
		SessionBuckets:[]
	}
	for(var i in this._fr.V)
	{
		if(this._bucket['V'] & this._fr.V[i].M)
			b.VisitorBuckets.push(this._fr.V[i].Name);
	}
	for(var i in this._fr.S)
	{
		if(this._bucket['S'] & this._fr.S[i].M)
			b.SessionBuckets.push(this._fr.S[i].Name);
	}
	return b;
}


