/* Ready [i] */
$(document).ready(function(){
    
	$("#baladacast div.video img").mouseover(function() {		
		$(this).animate({ borderColor: "#8cb12c" }, 300);
	}).mouseout(function(){
		$(this).animate({ borderColor: "#000000" }, 300);
	});
	
	// Faz o texto padr�o dos inputs desaparecerem.
    $("input").focus(function(){
        if(this.value == this.defaultValue && this.className.indexOf("noclear")<0){
            this.value='';
        }
    });
    $("input").blur(function(){
        if(this.value == '' && this.className.indexOf("noclear")<0){
            this.value=this.defaultValue;
        }
    });
	
    // Menu Topo
    $("#topo #menu li").hover(
        function(){
            if($(this).attr("selected")==undefined) {
                $(this).addClass('selected');
            }
        },
        function(){
            if($(this).attr("selected")==undefined) {
                $(this).removeClass('selected');
            }
        }
        );
    $("#topo #menu li").click(
        function(){
            location.href=$(this).attr("href");
        }
        );
	
    // Menu Lateral
    $(".menu-extendivel li").hover(
        function(){
            $(this).addClass('selected');
        },
        function(){
            $(this).removeClass('selected');
        }
        );
	
    $('.menu-extendivel h2').click(function(){
        if($(this).attr("class")=="selected") {
            $(this).removeClass("selected");
            $(this).next().slideUp("fast");
        }
        else {
            $('.menu-extendivel ul').slideUp("fast");
            $('.menu-extendivel h2').each(function(i) {
                $(this).removeClass("selected");
            });
            $(this).addClass("selected");
            $(this).next().slideDown("fast");
        }
    });
	
    $(".menu-parceiros li").hover(
        function(){
            $(this).addClass('selected');
        },
        function(){
            $(this).removeClass('selected');
        }
        );
		
    // Buscar
    $("#buscar #tipo li").click(
        function(){
            location.href=$(this).attr("href");
        }
        );
	
    // Rodape
    $("#rodape a").hover(
        function(){
            $(this).addClass('selected');
        },
        function(){
            $(this).removeClass('selected');
        }
        );
	
    // login
    $('#login').bind('submit', function (e) {
        e.preventDefault();
        
        $.ajax({
            url: '/assyncs/cadastro/login.php',
            type: 'POST',
            data: $('form#login').serialize(),
            beforeSend: function() {
                $('#status').show();
                
                $('.loginErro').remove();
            },
            success: function(e) {
                $('#status').hide();
                
                if (e.match(/(true)/gmi)) {
                    location.href='/';
                } else {
                    var msg = '<div class="loginErro">' + e + '</div>';
                    
                    $('form#login').before(msg);
                }
            },
            error: function() {
                alert('Erro, tente novamente mais tarde.');
            }
        });
    });
    
    // logout
    $('#btLogout').bind('click', function (e) {
        e.preventDefault();
        
        $.ajax({
            url: '/assyncs/cadastro/logout.php',
            type: 'GET',
            beforeSend: function() {
                $('#status').show();
            },
            success: function(e) {
                $('#status').hide();
                    
                if (e.match(/(true)/gmi)) {
                    location.href='/';
                } else {
                    alert('Você não pode efetuar esta operação.')
                }
            },
            error: function() {
                alert('Erro, tente novamente mais tarde.');
            }
        });
    });
    
    // recuperar senha
    $('#btRecuperarSenha').bind('click', function (e) {
        e.preventDefault();
        
        if ($('form#login input[name=usuario]').val().length == 0) {
            var msg = '<div class="loginErro">Preencha seu login.</div>';
                    
            $('form#login').before(msg);
        } else {
            $.ajax({
                url: '/assyncs/cadastro/recuperarsenha.php',
                type: 'POST',
                data: $('form#login').serialize(),
                beforeSend: function() {
                    $('#status').show();

                    $('.loginErro').remove();
                },
                success: function(e) {
                    $('#status').hide();
                    
                    var msg = '';

                    if (e.match(/(true)/gmi)) {
                        //location.href='/';
                        msg  = '<div class="msgRecuperacaoSenha">';
                        msg += 'Em breve você receberá um e-mail com as instruções ';
                        msg += 'de recuperação de senha.';
                        msg += '</div>';
                    } else {
                        msg = '<div class="loginErro">' + e + '</div>';
                    }
                    
                    $('form#login').before(msg);
                },
                error: function() {
                    alert('Erro, tente novamente mais tarde.');
                }
            });
        }
    });
    
    // Inits
    Events.All();
});
/* Ready [f] */

/* Functions [i] */
var Events = {
    All: function ()
    {
        this.ListHover();
        this.AjaxForms();
    },
    ListHover: function ()
    {
        $('.paginas li, #buscar #tipo li').hover(
            function(){
                if ($(this).attr("selected")!="true") {
                    $(this).addClass("selected");
                }
            },function(){
                if ($(this).attr("selected")!="true") {
                    $(this).removeClass("selected");
                }
            }
            );
    },
    AjaxForms: function()
    {
        $("form[type=validate]").each(function(i){
            $(this).validate({
                meta: "validate",
                submitHandler: function(form) {
                    form.submit();
                },
                showErrors: function(errorMap, errorList) {
                    if(errorList.length>0) {
                        Status.set(errorList[0].message,8);
                    }
                }
            });
        });
        $("form[type=target]").each(function(i){
            $(this).validate({
                meta: "validate",
                submitHandler: function(form) {
                    var optionsAjaxSubmit = {
                        target: $(form).attr("target"),
                        beforeSubmit: function(formData, jqForm, options) {
                            Status.set("Carregando...");
                        },
                        success: function(responseText, statusText){
                            Events.All();
                            Status.hide();
                        },
                        error: function(request,error) {},
                        clearForm: null
                    };
                    if ($(form).attr("clear")=="true") {
                        optionsAjaxSubmit.clearForm=true;
                    }
								
                    $(form).ajaxSubmit(optionsAjaxSubmit);
                },
                showErrors: function(errorMap, errorList) {
                    if(errorList.length>0) {
                        Status.set(errorList[0].message,8);
                    }
                }
            });
        });
    }
};

var Status = {
    set: function (msg,timeout) {
        clearTimeout(Status.t);
        $('#status').html(msg);
        $('#status').show();
        if(timeout>0) {
            Status.t = setTimeout(function(){
                $('#status').hide();
            }, (timeout*1000));
        }
    },
    hide: function() {
        $('#status').hide();
    },
    t: null
};
/* Functions [f] */
