/*
Author: Creative Markup
Author URI: http://creativemarkup.com.br
Description: Montagem dinamica do formulario, AJAX da resposta e validação do formulario
Version: 1.0
*/

$( function()
{
	// Montar as perguntas
	$( "ul.perguntas > li" ).each( function( i )
	{
		var $this = $( this ),
			html = "",
			pergunta, tipo, letras;
		
		if( !$this.is( ".cruzada" ) )
		{
			pergunta = $this.text();
			tipo = $this.attr( "rel" );
			
			html += '<strong>' + (i+1)  + '.</strong><input type="hidden" name="pergunta[' + i + '][tipo]" value="' + tipo + '"><ul><li><input type="radio" name="pergunta[' + i + '][opcao]" id="pergunta' + i + '1" value="1" /><label for="pergunta' + i + '1">1</label></li><li><input type="radio" name="pergunta[' + i + '][opcao]" id="pergunta' + i + '3" value="3" /><label for="pergunta' + i + '3">3</label></li><li><input type="radio" name="pergunta[' + i + '][opcao]" class="{required:true}" id="pergunta' + i + '5" value="5"/><label for="pergunta' + i + '5">5</label></li></ul><h3> ' + pergunta + '</h3>';
		}
		else
		{
			html += '<h3><strong>' + (i+1) + '.</strong> </h3><input type="hidden" name="pergunta[' + i + '][opcao]" value="' + $this.attr( "id" ) + '"><ul>';
			letras = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" ];
			
			$( "ul > li", $this ).each( function( j )
			{
				pergunta = $( this ).text(),
				tipo = $( this ).attr( "rel" );
				
				html +=	'<li><strong>' + letras[ j ] + '</strong><input type="radio" name="pergunta[' + i + '][tipo]" id="pergunta' + j + '1' + i + '" value="' + tipo + '" /><label for="pergunta' + j + '1' + i + '">' + pergunta + '</label></li>';
			});
					
			html += '</ul>';
		}
		
		$this.empty().append( html );
	});
	
	var validateForm = function( elem )
		{
			var count = 0;
			
			$( "ul.perguntas > li", elem ).each( function()
			{
				var $this = $( this );
				
				if( !$( "input:checked", $this ).length )
				{
					$this.addClass( "error" );
					
					count ++
				}
				else
				{
					$this.removeClass( "error" );
				}
			});
			
			if( count )
				return false;
				
			return true;
		};
	
	$( "button.bt_proxima, button.bt_anterior" ).click( function()
	{
		var $this = $( this ),
			parent = $this.parent();
			
		window.scrollTo( 0, 0 );
		
		if( $this.is( ".bt_proxima" ) && !validateForm( parent ) )
			return false;

		parent
			.hide()
			.removeClass( "ativo" );

		parent[ $this.is( ".bt_proxima" ) ? "next" : "prev" ]()
			.show()
			.addClass( "ativo" );
	});
	
	$( "form" ).submit( function()
	{
		$this = $( this );
		
		validateForm( $this.parent() )
			&& $this.ajaxSubmit( function( response )
			{
				var response = $( response ).appendTo( "body" ),
					dicas = $( "div.dicas" );
				
				$this.add( dicas ).hide();
				
				$( ".bt_refazer", response ).click( function()
				{
					$this.add( dicas ).show();
					response.hide().remove();
					
					$this
						.find( ".ativo" ).hide().removeClass( "ativo" ).end()
						.find( ".etapa1" ).show().addClass( "ativo" );
						
					window.scrollTo( 0, 0 );
						
					return false;
				});
			})
			
		return false;
	});
});
