
function switchForms( evt ) {
	window.location = this.href;
	Evt.cancel(evt);
	_switchFormsByCaller( this );

}

function _switchFormsByCaller( caller , dontFocus )
{
	$t( 'form' , $('CadastroIndique') ).forEach(
		function( el ) {
			el.style.display = 'none';
		}
	);

	$(caller.toShow).style.display = 'block';

	if( !dontFocus )
		$(caller.toShow + '_nome').focus();

	var at = caller;

	$t( 'a' , $('opcoes') ).forEach(
		function( el ){
			el.parentNode.className = ( el.parentNode === at.parentNode ) ? 'atual' : '';
		}
	);
}

// funcoes para que as categorias fiquem com efeitos
function toogleClass( el , cn )
{
	if( classExists( el , cn ) )
		removeClass( el , cn );
	else
		addClass( el , cn);
}

function activateRadio()
{
	var tgt = this.parentNode , atual = this , radio;
	$t( 'label' , tgt ).forEach(
		function( el )
		{
			if( atual === el )
			{
				if(isMSIE) $t('input',el)[0].checked=true;
				addClass( el , 'atual' );
			}
			else
				removeClass( el , 'atual' );
		}
	);
}

// enderecos das lojas
function toogleEnderecoLojas( tgt )
{
	$t( 'li' , tgt.parentNode ).forEach(
		function( el ) {
			$t( 'div' , el )[1].style.display = ( tgt === el ) ? 'block' : 'none';
		}
	);

	tgt.style.display = 'block';
}

bodyLoad.add(
	function()
	{
		// criando as abas para a indicacao e o cadastro
		var tgt = $('CadastroIndique');
		var _forms = $t( 'form' , tgt );
		if( _forms.length )
		{
			var opcoes = DOM.create( 'ul' , { id: 'opcoes' } ) , atual , l;
			opcoes.className = 'opcoes';
			$t( 'form' , tgt ).forEach(
				function( el )
				{
					atual = DOM.create( 'a' , { href: '#opcoes' , title: 'Veja o formulário.' } );
					l = $t( 'legend' , el )[0];
					atual.innerHTML = l.innerHTML;
					DOM.rem(l);
					atual.toShow = el.getAttribute('id');
					Evt.add( atual , 'click' , switchForms );

					DOM.inside( atual , DOM.createAndAttach( 'li' , opcoes ) );

				}
			)

			DOM.before( opcoes , $t( 'form' , tgt )[0] );

			var message = $( 'message' );
			var toShow = 0;
			if( message )
			{
				var m2 = message.cloneNode( true );
				DOM.rem( message );
				DOM.before( m2 , opcoes );
				toShow = classExists( m2 , 'indique' ) ? 1 : 0;
				location.href = '#message';
			}
			_switchFormsByCaller( $t( 'a' , opcoes )[toShow] , true );
		}


		// criando os efeitos das abas na busca
		var tgt = $t( 'div' , $('formBusca') )[0];
		$t( 'label' , tgt ).forEach(
			function( el )
			{
				Evt.add( el , 'mouseover' , function() { toogleClass( this , 'hover' ); } );
				Evt.add( el , 'mouseout' , function() { toogleClass( this , 'hover' ); } );
				Evt.add( el , 'click' , activateRadio );
			}
		)

		var r = $('rodape').$t('a[rel=ampliar]');
		for( var i=0 ; i<r.length ; i++ ) {
			new lightBox( r[i] );
		}


		// enderecos das lojas
		$t( 'li' , $('enderecosLojas')).forEach(
			function( el )
			{
				$t( 'h3' , el )[0].style.cursor = 'pointer';
				Evt.add( el , 'click' , function(){ toogleEnderecoLojas( el ); } );
			}
		);

		toogleEnderecoLojas( $('loja1') );

	}
);