var ABTestList = new Array();

var ABTest = function() {
	this.testid = null;
	this.element = null;
	this.selected_test = null;
	this.test_units = [];
	this.num_clicks;

	this.init = function( args ) {
		this.testid = args.testid;
		this.element = args.element;
		this.test_units = args.test_units;
		this.selected_test = this.test_units[ Math.floor(Math.random() * this.test_units.length) ];
		this.num_clicks = 0;
		this.register_events( this.selected_test );
		
		ABTestList.push(this);
	};

	this.clicked = function( url ){
		Tracker.gClick( '/_ga/click/abtest/' + this.testid + '/' + url );		
	};

	this.register_events = function(){
		var elm = $( this.element );
		if( elm ){
			elm.addClassName(this.selected_test.className);
			Event.observe(elm,'mousedown',function(event){				
				var el = Event.element(event);
				if(el.tagName == 'IMG') {
					el = el.parentNode;
				}
				if(el.tagName == 'A' || el.tagName == 'INPUT' || el.tagName == 'BUTTON') {
					if(this.num_clicks != '5+' && this.num_clicks < 4) 
						this.num_clicks += 1;
					else 
						this.num_clicks = '5+';
					var abtrack_id = el.getAttribute('rel:abtrack_id');
					if( abtrack_id == null ) abtrack_id = el.tagName;
					this.clicked( this.selected_test.analytics_url + '/click/' + abtrack_id + '/' + this.num_clicks);
				}
			}.bind( this ));
		}
	};

	this.register_impression = function(){
		Tracker.gClick( '/_ga/click/abtest/' + this.testid + '/' + this.selected_test.analytics_url + '/impression' );		
	};
};
