var sideTabs = Class.create();
sideTabs.prototype = {
	pages : {
		"overview"   : 0,
		"options"    : 1,
		"production" : 2,
		"start"      : 3
	},
	initialize: function(togglers, tabs, active){
		this.togglers = togglers || [];
		this.tabs = tabs || [];
		this.targetRegExp = /#(.+)$/;
		var target = this.targetRegExp.exec(window.location);
		if(target && target[1]) {
			this.active = this.pages[target[1]];
		} else {
			this.active = active || 0;
		}
		if (!this.active) {
			this.active = this.pages["overview"];
		}
		this.setup();
	},
	setup: function(){
		var active = this.active;
		this.togglers[active].addClassName('active');
		this.tabs[active].addClassName('active');		
		this.togglers.each(function(el,i){
			el.onclick = function(){
				this.show(el, i);
				GoogleAnalytics.trackEvent('Print Services', 'Tab', el.down('a').innerHTML);
				return false;
			}.bind(this);
		}.bind(this));
	},
	show: function(el,i){
		var active = this.active;
		this.togglers[active].removeClassName('active');
		this.tabs[active].removeClassName('active');
		this.active = i;
		this.togglers[i].addClassName('active');
		this.tabs[i].addClassName('active');
		Position.prepare();
		if(Position.deltaY > 400) {
			window.scrollTo(0,0);
		}
		return false;
	}
};

/*--------------------------------------------------------------------------*/

var acceptable = Class.create();
acceptable.prototype = {
	initialize: function(container){
		this.togglers = $(container).getElementsBySelector('li') || {};
		this.setup();
	},
	setup: function(){
		$A(this.togglers).each(function(el,i){
			el.onclick = function(){
				el.toggleClassName('active');
				return false;
			}
		});
	}
};

/*--------------------------------------------------------------------------*/

Event.onDOMReady(function(){
	var Tabs = new sideTabs($('aCol').getElementsBySelector('li'), $('mainContent').getElementsBySelector('div.tab'), 0);
	new acceptable('acceptable');
	$$('#promoButtons a').each(function(el) {
		el.onclick = function() {
			var matches = el.href.match(/#(.+)$/);
			Tabs.show(el, Tabs.pages[matches[1]]);
			return false;
		}
	});
	$$('a.getStarted').each(function(el){
		el.onclick = function(){
			Tabs.show(el,3);
			return false;
		}
	});
});
