$(document).ready(function(){
    function getId(obj){
        var num = obj.id;
        var id = num.split('_');
        return id[1];
    }
    function showDiv(obj){
        var num = obj.id;
        var id = num.split('_');

        if($('div#pd_'+id[1]).css('display') == 'none'){
            $('div#pd_'+id[1]).fadeIn('fast');
        }else{
            $('div#pd_'+id[1]).fadeOut('fast');
        }
    }
    /*Msg add to cart*/
    $('#msg').fadeIn(3000, function(){
        $(this).fadeOut('slow');
    });
    $('#cart').fadeIn('slow');

    /*Mostrar o Carrinho*/
//    $('#cart').mouseenter(function(){
//        $('#dcart').slideDown('fast');
//    }).mouseleave(function(){
//        $('#dcart').slideUp('fast');
//    }).click(function(){
//        document.location = 'carrinho.php';
//    });
    $('#btncom').css('cursor','pointer').click(function(){
        document.location = 'carrinho.php';
    });
    $('img#bb').click(function(){
        document.location = 'nav.php'
    }).css('cursor', 'pointer');
    $('img#endthis').click(function(){
        document.location = 'pedido.php'
    }).css('cursor', 'pointer');

    $('div.grid').css('cursor','pointer').mouseenter(function(){
        $(this).css('background','#dff1ff');
    }).mouseleave(function(){
        $(this).css('background','#FFFFFF');
    }).click(function(){
        var a = getId(this);
        document.location = 'pedido.php?cod='+a;
    });

    $('#address').click(function (){
        $('#ender').fadeOut('slow');
        $('#address').fadeOut('slow', function(){
            $('.sign').fadeIn('slow');
        });
    });

    $('input#voltar').click(function(){
        $('div.sign').fadeOut('slow', function(){
            $('div#ender').fadeIn('slow',function(){
                $('div#address').fadeIn('slow');
            });

        });
    });


    $('#submit').click(function(e){
        e.preventDefault();
		
		/* validações*/
		if ($('#endereco').val()=='') {  
		  alert('O campo endereço não pode ficar vazio.');
		  return false;  
		}
		
		if ($('#cep').val()=='') {
		  alert('O campo CEP não pode ficar vazio.');
		  return false;
		}
		
		if ($('#bairro').val()=='') {
		  alert('O campo bairro não pode ficar vazio.');
		  return false;
		}
		
		if ($('#cidade').val()=='') {
		  alert('O campo cidade não pode ficar vazio.');
		  return false;
		}
		
		if ($('#estado').val()=='') {
		  alert('Selecione o estado.');
		  return false;
		}

        $.ajax({
            type: 'POST',
            url: 'add_address.php',
            data: {
                estado: $('#estado').val(),
                cep:$('#cep').val(),
                cidade:$('#cidade').val(),
                bairro:$('#bairro').val(),
                complemento:$('#complemento').val(),
                endereco:$('#endereco').val(),
                numero:$('#numero').val(),
                tipo:$('#tipo').val()
            },
			
			
            success: function(){
                document.location = 'endereco.php';
            }
        });
    });

    $('#en span').click(function(){
        $('input#nome').css({
            'border':'1px #c2c2c2 solid'
        }).focus().removeAttr('readonly');
        var t = $('#en span').text();
        if(t == 'Concluir'){
            $.ajax({
                type: 'POST',
                url: 'edit_perfil.php',
                data: {
                    nome: $('input#nome').val(),
                    email:$('input#email').val()
                },
                success: function(){
                    $('input#nome').css('border','none').attr('readonly', 'readonly');
                    $('#en span').text('editar');
                }
            });
        }
        
        $('#en span').text('Concluir');
        
    }).css('cursor','pointer');
    $('#ee span').click(function(){
        $('input#email').css({
            'border':'1px #c2c2c2 solid'
        }).focus().removeAttr('readonly');

        var t = $('#ee span').text();
        if(t == 'Concluir'){
            $.ajax({
                type: 'POST',
                url: 'edit_perfil.php',
                data: {
                    nome: $('input#nome').val(),
                    email:$('input#email').val()
                },
                success: function(){
                    $('input#email').css('border','none').attr('readonly', 'readonly');
                    $('#ee span').text('editar');
                }
            });
        }
        $('#ee span').text('Concluir');
    }).css('cursor','pointer');

    $('#pfl_input span').mouseenter(function(){
        $(this).css('text-decoration','underline');
    }).mouseleave(function(){
        $(this).css('text-decoration','none');
    });
    $('div#alt').click(function(){
        var pass = $('input#pass').val();
        var pass_conf = $('input#pass_conf').val();
        if(pass == pass_conf && pass != ''){
            $.ajax({
                type: 'POST',
                url: 'edit_perfil.php',
                data: {
                    senha: $('input#pass').val()
                },
                success: function(e){
                    $('div#pws').fadeOut('fast',function(){
                        $('div#troca').fadeOut('fast', function(){
                            $(this).html(e).fadeIn('fast');
                        });
                    });
                }
            });
        }else{
            alert('Erro ao alterar senha');
        }
    });

    $('div#gal img').css('cursor','pointer').click(function(){
        var i = getId(this);
        $.ajax({
            type: 'POST',
            url: 'file.php',
            data:{
                id: i
            },
            success: function(e){
                location.href = 'http://medeiros.srv.tnx.com.br/' + e;
            }
        });
    });

    //$('div#busca').css({'width':'220','height':'64'});
    //$('input[type=text]#keyword').css({'margin-top':'16','margin-left':'42','border':'none','width':'155','float':'left'});
    $('div#btn').css(/*{'cursor':'pointer','float':'left','height':'23','margin-top':'13','width':'23'}*/).click(function(){
        document.location = 'busca.php?keyword=' + $('input[type=text]#keyword').val();
    });
});


