var Tabs = function( $target, index )
{
	var li, a, i=0, o=this;
	this.$src = $($target.cloneNode(true));
	this.$tgt = $(DOM.create('div'));
	this.$tgt.before( $target );
	if(!index) index = 0;

	DOM.rem( $target );

	this.dts = this.$src.$t('dt');
	this.dds = this.$src.$t('dd');

	// making menu
	this.$menu = this.$tgt.create( 'ul' , { id: 'tabsMenu' });

	this.dts.forEach(
		function( el ) {
				li = o.$menu.create('li');
				li.inside( o.$menu );
			if (el.id != "") {
				a = DOM.create('a');
				a.style.cursor = 'pointer';
				a.innerHTML = el.innerHTML;
				DOM.inside( a , li );
				a.pastId = el.id;

				a.clickedIndex = i;

				Evt.add( a , 'click', function(evt){ Evt.cancel(evt); o.changeTab(this) });
			}
			i++;
		}
	);

	this.$menu.inside( this.$tgt );
	this.$titles = this.$menu.$t('li');

	// making body
	this.$body = this.$tgt.create('ul', { id: 'tabsBody' });

	i = 0;
	this.$contents = [];
	this.dds.forEach(
		function( el ) {
		//	if (o.dts[i].id != "") {
				li = o.$body.create('li');
				li.innerHTML = '<h3>' + o.dts[i].innerHTML + '</h3>' + el.innerHTML;

				li.inside( o.$body );

				o.$contents[o.$contents.length] = li;
		//	}
			i++;
		}
	);

	this.$body.inside( this.$tgt );

	this.changeTab( index );
}

Tabs.prototype.changeTab = function( index ) {

	if(typeof index!= 'number' )
	{
		location.hash = index.pastId;

		index = index.clickedIndex
	}
	this.$titles.forEach( function( el ){ removeClass(el,'actual') } );
	addClass( this.$titles[index], 'actual' );

	this.$contents.forEach( function( el ){
		el.style.display = 'none';
		el.style.visibility = 'hidden';
	} );
	this.$contents[index].style.display = 'block';
	this.$contents[index].style.visibility = 'visible';

}

bodyLoad.add(
	function(){
		var h = location.hash , index=0;
		if(h) {
			switch( h.substring(1) )
			{
				case 'juvenil': index=1; break;
				case 'juniores': index=2; break;
			}
		}
		window.scrollTo(0,0);
		new Tabs( $('dados') , index );

	}
);