
var Main_Area_JS = new Class({
	Implements: [Options, Events, Chain],

	initialize: function(options){
		this.setOptions(options);
		this.setHeaderNav();

		if(!$('main')) return;
		this.togglers = $('main').getElement('div#nav').getElements('li');
		this.tabs = $('main').getElements('div.tab');
		if(!this.togglers || !this.tabs) return;

		this.active = 0;
		this.togglers.each(function(el, i){
			el.addEvent('click', function(e){
				e.stop();
				this.switchto(i);
			}.bind(this));
		}.bind(this));
	},

	setHeaderNav: function(){
		if(!$('header_navigation')) return;

		var active_pg = -1;
		var pages = $('header_navigation').getElements('li');
		pages.each(function(pg, i){
			if(pg.hasClass('current_page_item')) active_pg = i;
		}.bind(this));
		if(active_pg >= 0) return;
		if(pages[1] != null){
			if($$('.post') != '') pages[1].addClass('current_page_item');
			else pages[0].addClass('current_page_item');
		}
	},

	switchto: function(i){
		if(i == this.active) return;
		var cords = this.tabs[this.active].getCoordinates();
		this.fxOld = new Fx.Tween(this.tabs[this.active], {duration: 1});
		this.fxNew = new Fx.Tween(this.tabs[i], {duration: 1});

		this.tabs[i].set('opacity', 0);
		this.tabs[i].setStyles({
			'position': 'absolute',
			'top': cords.top,
			'left': cords.left
		});
		this.tabs[i].addClass('active');
		this.fxOld.start('opacity', 0).chain(function(){
			this.tabs[this.active].removeClass('active');
			this.togglers[this.active].removeClass('active');
		}.bind(this));
		this.fxNew.start('opacity', 1).chain(function(){
			this.tabs[i].removeProperty('style');
			this.togglers[i].addClass('active');
			this.active = i;
		}.bind(this));
	}
});

window.addEvent('domready', function(){ new Main_Area_JS(); });