var current = null;
var tabs = null;
var buttons = null; 
var none_selected = true;

var selected_color = '#E8E8E8';
var hover_color = '#efefef';
var default_color = '#F5F5F5';
var border_color = '#747577';

var selected_style = {'background': selected_color, 'border': '1px solid '+border_color, 'border-bottom': '0'};
var hover_style = {'background': hover_color, 'border': '1px solid '+border_color, 'border-bottom': '0'};
var not_selected_style = {'background': default_color, 'border': '1px solid '+border_color, 'border-bottom': '0'};

var min_height = 0;

window.addEvent('load',function(){

	tabs = $$('div[class^="bar"]');
	buttons = $$('#buttons li');

	current = tabs[0];
	looptabs(true);

	buttons.each(function(element, index){
		element.addEvent('click', function(){
			if(current != tabs[index]) current = tabs[index];
			looptabs(false, buttons);
			var fx = new Fx.Slide(tabs[index]);
			fx.show();
			element.set('styles', selected_style);
			none_selected = false;
			
			// A little fix so it doesn't resize the div if the content is smaller.
			$('tabs').set('styles', {'height': min_height});
			
			//window.scrollTo(0, tabs[index].getPosition().y);
		}).addEvent('mouseover', function(){
			if(current != tabs[index] || none_selected)
			{
				element.set('styles', hover_style);
			}		
		}).addEvent('mouseleave', function(){
			if(current != tabs[index] || none_selected)
			{
				element.set('styles', not_selected_style);
			}	
		});
	});

	function looptabs(first, clicked)
	{
		var max_h = 0;
		var height = 0;
		tabs.each(function(element, index){
			var fx = new Fx.Slide(element);

			if(current != tabs[index] && !first)
			{
				fx.hide();
				if(clicked != undefined) buttons[index].set('styles', not_selected_style);								
			}
			
			else if(first)
			{
				
				var elem_h = element.getStyle('height').toInt();
				if(elem_h > max_h) max_h = elem_h;

				if(!element.match('.main_pressure'))
				{
					fx.hide();
				}
				
				else
				{
					buttons[index].set('styles', selected_style);
					current = tabs[index];
					none_selected = false;
				}
			}
		});
		
		if(first)
		{
			min_height = $('tabs').getStyle('height').toInt();
		}
	}
});
