
    var loginOpened 	= false;
    var etapaNumber 	= 1;
    var integrantes 	= 1;
    var form			= 0;
    var formFinalizar 	= false;

    $(function()
    {
        loginScreen();
        loginValidacao();
        formulariosEtapas();
        $(".content .required-email").blur(function()
        {
            verificaEmail();
        });

		if( $(".form.etapa3").length != 0 ) 
				etapaNumber = 3;
    });


    /* LOGIN
                -------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    function loginScreen()
    {
        $(".header .loginCall a").attr("href", "javascript:;");

        $(".header .loginCall a").click(function()
        {
            loginOpened = true;
            $(".login").slideDown(2000, "easeOutBounce");
        });

        $(".login .fechar a").click(function()
        {
            loginOpened = false;

            $(".login").slideUp(2000, "easeOutBounce");
        });
    }

    function loginValidacao()
    {
        $(".login .txt").each(function()
        {
            if( $(this).attr("value") != "")
                $(this).prev("label").css("visibility", "hidden");
        })

        $(".login .txt").bind("focus", function()
        {
            $(this).prev("label").css("visibility", "hidden");
        });

        $(".login .txt").bind("blur", function()
        {
            if( $(this).attr("value") == "")
                $(this).prev("label").css("visibility", "visible");
        });

    }


    /* FORMULÁRIOS
                -------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    function formulariosEtapas()
    {

        //		----------------------- Botao Finalizar ----------------------------------

        $(".content .finalizarSubmit, .etapa3 .btEnviar").click(function()
        {
            validation();
            return formFinalizar;
        });


        if( $(".content .msgSucessoCadastro").length != 0 )
        {
            loginOpened = true;
            $(".login").slideDown(2000, "easeOutBounce");

            $('html, body').animate(
            {
                scrollTop: 0
            }, 1000);
        }
        /* -- */


        $(".content .navigation li.tipo").addClass("ativo");

        if( $(".contentEstudante").length == 1 )
            $(".content .navigation li.dados").addClass("ativo");


        $(".content .proximaEtapa a ").click(function()
        {
            nextSteps();
        });


        $(".content .etapa1 .agencia a").click(function()
        {
            nextSteps();
        });


        $(".integrantes .btFecharIntegrantes a").each(function()
        {
            $(this).click(function(){
                $(this).parent("span").parent("div").slideUp("slow", function()

                {
                        if( integrantes >= 1 )
                            integrantes = integrantes - 2;
                    });
            });
        });


        $(".content .form .btEnviar").click(function()
        {
            var valido = false;

            validationTxtField();

            if( $(".content .etapa"+etapaNumber+" .error").length == 0 )
                valido = true;

            return valido;
        });

        $(".content .adicionarIntegrantes a").click(function()
        {
            $(this).parent("p").parent(".form").children(".integrantes:eq("+integrantes+")").slideDown(1500);

            if(integrantes <= 3)
                ++integrantes;
        });


        $(".etapaTV .checkGroup li .checkField").each(function()
        {
            var eq = 0;

            $(this).click(function()
            {	
                if( $(this).attr("name") == "tv_categoria_mercado")
                {
                    eq = 0;
                }

                else if( $(this).attr("name") == "tv_categoria_varejo")
                    eq = 1;

                else
                    eq = 2;

                if( $(".etapaTV .separaUpload:eq("+eq+")").is(":hidden") )
                {
                    $(".etapaTV .separaUpload:eq("+eq+")").slideDown(1000);
					$(".etapaTV .separaUpload:eq("+eq+") .txtField").addClass("required-entry");
					
                    $('html, body').animate(
                    {
                        scrollTop: $(".etapaTV .separaUpload:eq("+eq+")").offset().top
                    }, 1000);
                }

                else
				{
                    $(".etapaTV .separaUpload:eq("+eq+")").slideUp(1000);
					$(".etapaTV .separaUpload:eq("+eq+") .txtField").removeClass("required-entry");
            	}

				})
        });

        $(".etapa3 .meiosMidia input").each(function()
        {
            var inputName  = 0;
            var timerCheck = 0;
            var abasAtivas = 0;
            var checked	   = 0;

            $(this).click( function()
            {
                inputName = $(this).attr("name");
                timerCheck = 0;

                checked = 0;

                $(".checkGroup:first li input").each(function()
                {
                    if( $(this).is(":checked") == true )
                        checked = checked + 1;
                });

                if( 0 < checked )
                    $(".btEnviar").fadeIn("slow");

                else
                    $(".btEnviar").fadeOut("slow");


                if( $(".etapa3 ." + inputName).is(":hidden"))
                {
                    abasAtivas = abasAtivas + 1;

                    $(".etapa3 ." + inputName).slideDown("slow");
// Fuck					$(".etapa3 ." + inputName + ".").

                    timerCheck = window.setTimeout(function()
                    {
                        $('html, body').animate(
                        {
                            scrollTop: $("." + inputName).offset().top
                        }, 1000);

                    }, 1000);

                    $(".content ."+inputName+" .txtField").addClass("required-entry");

                    $(".content .separaUpload").each(function()
                    {
                        $(this).children("p").children(".required-entry").removeClass("required-entry");

                    });
                }

                else
                {
                    abasAtivas = abasAtivas - 1;

                    $(".etapa3 ." + inputName).slideUp(1000);

                    $(".content ."+inputName+" .txtField").each(function()
                    {
                        $(this).removeClass("required-entry");
                    });
                }

                if( abasAtivas >= 1 )
                    $(".btEnviar").fadeIn("slow");

            //				else
            //					$(".btEnviar").fadeOut("slow");

            });
        });

    }

    function nextSteps()
    {
        $(".content .form.etapa"+etapaNumber).slideUp(1000, function()
        {
            $(".content .form.etapa"+etapaNumber).slideDown(1000);

            $(".content .navigation li.ativo").removeClass("ativo");

            for(var i = 0; i <= (etapaNumber - 1); i++)
                $(".content .navigation li:eq("+(i)+")").addClass("ativo");
        });

        etapaNumber = etapaNumber + 1;
    }


    function prevSteps()
    {
        $(".content .form.etapa"+etapaNumber).slideUp(1000, function()
        {
            $(".content .form.etapa"+etapaNumber).slideDown(1000);

            $(".content .navigation li.ativo").removeClass("ativo");

            for(var i = 0; i <= (etapaNumber - 1); i++)
                $(".content .navigation li:eq("+(i)+")").addClass("ativo");
        });

        etapaNumber = etapaNumber + 1;
    }

    function validation()
    {				
        validationTxtField();

        if( $(".content .etapa"+etapaNumber+" .error").length == 0 )
        {
            formFinalizar = true;
        }

        else
        {
            $('html, body').animate(
            {
                scrollTop: ($(".content .etapa"+etapaNumber+" .error:first").offset().top) - 50
            }, 1000);
        }
    }

    function validationTxtField()
    {
        var label 		= 0;
        var labelLenght = 0;

        $(".content .etapa"+etapaNumber+" .required-entry").each(function()
        {
            if( $(this).attr("value") == "")
            {
                label = $(this).prev("label").text();
                labelLenght = $(this).prev("label").text().length;
                label = label.substr(0, labelLenght -2);

                $(this).addClass("error");
                $(this).parent("p").children("span").remove();
                $(this).parent("p").append("<span>O campo "+ label +" precisa ser preenchido.</span>");
            }
        });
        $(".content .etapa"+etapaNumber+" .error").focus(function()
        {
            $(this).removeClass("error");
            $(this).parent("p").children("span").fadeOut("slow", function()
            {
                $(this).remove()
            });
        });
    }

    function verificaEmail()
    {
        var email = $("#email").attr('value');
        jQuery.post("/cadastro/ajax.buscaemail", {
            'email':email
        },function(data)
        {
            if( data > 0 )
            {
                $("#email").addClass("error");
                $("#email").children(".txtField").addClass("error");
                $("#email").children("span").remove();
                $("#email").parent("p").append("<span id='msgEmail'>Seu e-mail já se encontra cadastrado em nosso banco. Por favor, faça o login e utilize o link Mídia para enviar um novo material.</span>");
            }
            else
            {
                $("#email").removeClass("required-email");
                $("#email").removeClass("error");
                $("#msgEmail").remove();
            }
        });
    }


    function modalFormValidation()
    {
        var camposErrados 	 = 0;
        var returnValidation = false;


        $(".modal form .txt").each(function()
        {
            $(this).focus(function()
            {
                if( $(this).attr("value") == "Site:" )
                    $(this).attr("value", "http://");

                else if( $(this).attr("value") == $(this).prev("label").text() )
                    $(this).attr("value", "");


                if( $(this).attr("id") == "telefone" )
                    $('#telefone').setMask("phone",'Telefone*:');
            });

            $(this).blur(function()
            {
                if( ($(this).attr("value") ==  "") || ($(this).attr("value") ==  "http://"))
                    $(this).attr("value", $(this).prev("label").text());
            });
        });

        $(".modal form .enviar").click(function()
        {
            camposErrados = 0;

            $(".modal form .txt").each(function()
            {
                if( $(this).attr("value") == $(this).prev("label").text() )
                {
                    $(this).addClass("error");
                    $(".modal form .errorMessage").text("Todos os campos marcados com * são de preenchimento obrigatório.").show(1000);

                    camposErrados = camposErrados + 1;
                }

                else
                    camposErrados = camposErrados - 1;
            });

            $(".modal form .txt.error").click(function()
            {
                $(this).removeClass("error");
                $(".modal form .errorMessage").hide(1000);
            });


            if( camposErrados <= 0 )
                enviaContato();

            return false;
        });
    }