
var sistema = {

    addMessage : function(msg, tipo)
    {
        var img = [];
        img[1] = $('#caminho').val() + 'public/images/icones/notification_done.png'; //1 - ok
        img[2] = $('#caminho').val() + 'public/images/icones/notification_error.png'; //2 - error
        img[3] = $('#caminho').val() + 'public/images/icones/user_warning.png'; //3 - aviso

        var unique_id  = $.gritter.add({
            title: 'Luzicity',
            text: msg,
            image: img[tipo],
            sticky: true,
            time: '3000',
            class_name: 'my-sticky-class'
        });

        setTimeout(function() {
            $.gritter.remove(unique_id, {
                fade: true,
                speed: 'slow'
            });
        }, 2000);
    }
}

$(document).ready(function() {

    var caminho = $('#caminho').val();
    var iconCarregando = '<span class="">Carregando. Por favor aguarde...&nbsp;<img src="'+caminho+'public/images/mini2.gif" alt="Icone: carregando" /></span>';

    $('a[rel*=lightbox]').lightBox();

    $("img").lazyload({
        effect : 'fadeIn',
        placeholder : caminho + 'public/images/mini2.gif'
    });

    /*
* para foco em algum campo
if (campoFoco) {
$.scrollTo('#'+campoFoco,500);
$('#'+campoFoco).focus();
}
**/

    //gritter
    /*
var unique_id = $.gritter.add({
    // (string | mandatory) the heading of the notification
    title: 'Em breve, novidades.',
    // (string | mandatory) the text inside the notification
    text: 'Em breve, novidades.',
    // (string | optional) the image to display on the left
    //image: 'http://s3.amazonaws.com/twitter_production/profile_images/132499022/myface_bigger.jpg',
    // (bool | optional) if you want it to fade out on its own or just sit there
    sticky: true,
    // (int | optional) the time you want it to be alive for before fading out
    time: '',
    // (string | optional) the class name you want to apply to that specific message
    class_name: 'my-sticky-class'
});

//You can have it return a unique id, this can be used to manually remove it later using

setTimeout(function() {

    $.gritter.remove(unique_id, {
            fade: true,
            speed: 'slow'
    });

}, 2000);

//$.gritter.removeAll();
*/
    //fim gritter

    //abre o prompt sózinho aqui.
    //$.prompt('Não deixe o peixe fora dagua');
    //preenche a imagem de fundo do usuário quando ele faz login e possuí uma imagem.
    var imagemLoginUsuarioFundo = $(".imagemLoginUsuarioFundo").attr("id");
    if(imagemLoginUsuarioFundo != undefined) {
        if(imagemLoginUsuarioFundo != 'imagemLoginFundo_') {
            $('.imagemLoginUsuarioFundo').css("background-image", "url("+caminho+"imagem.php?imagem="+imagemLoginUsuarioFundo.substr(17)+")");
        }

    }
    //formaulario de login para iniciar sessão no luzicity, por carlos eduardo zend#009595
    $('#formLogin').submit(function() {
        var serializeDados = $('#formLogin').serialize();
        $.ajax({
            type        : 'POST',
            dataType    : 'tpl',
            data        : serializeDados,
            url         : caminho+'cliente/efetuarlogin',
            beforeSend: function() {
                $('#widgetcarregando').removeClass('invisivel');
                $('#layoutCarregando').html(iconCarregando);
            },
            complete: function() {
                $('#widgetcarregando').addClass('invisivel');
                $(iconCarregando).remove();
            },
            success: function(data) {

                if(data != '') {

                    //sistema.addMessage('Bem vindo' + data, 1);

                    $('#loginUsuario').html(data);

                    window.location.href='';

                } else {

                    sistema.addMessage('Usuário ou senha inválidos', 2);

                    $('#emailLogin').css('border','2px solid #ff0000');
                    $('#emailLogin').css('color', '#ff0000');

                    $('#senhaLogin').css('border','2px solid #ff0000');
                    $('#senhaLogin').css('color', '#ff0000');

                    $('#emailLogin').attr('value',          'Email');
                    $('#senhaLogin').attr('value',          'Senha');
                }
            },
            error: function() {

                sistema.addMessage('Usuário ou senha inválidos', 2);

                $('#emailLogin').css('border',  '2px solid #ff0000');
                $('#emailLogin').css('color',   '#ff0000');

                $('#senhaLogin').css('border',  '2px solid #ff0000');
                $('#senhaLogin').css('color',   '#ff0000');

                $('#emailLogin').attr('value',  'Email');
                $('#senhaLogin').attr('value',  'Senha');

                $('#widgetcarregando').removeClass('invisivel');

                $('#layoutCarregandoDireita').show().html('<p class="destaque">Lamento! Ocorreu um erro. Por favor tente novamente.').hide(2000);
                $('#widgetcarregando').addClass('invisivel');
            }
        });
    });

    //ajax para busca.
    $('#formBuscaGeral').submit(function(e) {
        e.preventDefault();
        var serializeDados = $('#formBuscaGeral').serialize();
        var texto = $('#buscaGeral').val();

        if(texto != '' && texto.length > 0 && texto != 'Pesquisa') {
            $.ajax({
                type:       'POST',
                dataType:   'tpl',
                data:       serializeDados,
                returnType: 'JSON',
                url:        caminho + 'produto-pesquisa/pesquisa/',
                beforeSend: function() {

                    $('#widgetcarregando').removeClass('invisivel');
                    $('#layoutCarregando').html(iconCarregando).show();
                },
                complete: function(data) {
                    $('#widgetcarregando').addClass('invisivel');
                    $("#layoutCarregando").hide();
                },
                success: function(data) {
                    $('#layoutConteudoPrincipal').hide();
                    $("#layoutBuscaGeral").html(data);
                },
                error: function(xhr, er) {
                    $('#layoutBuscaGeral').html('<p class="destaque">Lamento! Ocorreu um erro. Por favor tente novamente.')
                }
            });
        } else {
            $('#layoutConteudoPrincipal').show();
            $("#layoutBuscaGeral").html('');
        }
    });

    //campo busca
    var textoBuscaDefault = $('#label_buscaGeral').attr('title');
    //$('#buscaGeral').val(textoBuscaDefault).
    $('#buscaGeral').css('color', '#c0c0c0')
    .focus(function(){
        if ($(this).val() == textoBuscaDefault){
            $(this).css('border', '2px solid #0000ff');
            $(this).css('color', '#000000').val('');
        }
    }).blur(function(){
        if ($(this).val() == '' ){
            $(this).css('border', '');
            $(this).val(textoBuscaDefault).css('color', '#c0c0c0');
        }
    });

    //campo email
    var textoEmailDefault = $('#label_email').attr('title');
    if(textoEmailDefault) {
        $('#emailLogin').val(textoEmailDefault).css('color', '#c0c0c0')
        .focus(function(){
            if ($(this).val() == textoEmailDefault){
                $(this).css('border', '2px solid #0000ff');
                $(this).css('color', '#000000').val('');
            }
        }).blur(function(){
            if ($(this).val() == '' ){
                $(this).css('border', '');
                $(this).val(textoEmailDefault).css('color', '#c0c0c0');
            }
        });
    }

    //campo senha
    var textoSenhaDefault = $('#label_senha').attr('title');
    if(textoSenhaDefault) {
        $('#senhaLogin').val(textoSenhaDefault).css('color', '#c0c0c0')
        .focus(function(){
            if ($(this).val() == textoSenhaDefault){
                $(this).css('border', '2px solid #0000ff');
                $(this).css('color', '#000000').val('');
            }
        }).blur(function(){
            if ($(this).val() == '' ){
                $(this).css('border', '');
                $(this).val(textoSenhaDefault).css('color', '#c0c0c0');
            }
        });
    }

//<![CDATA[
//Facebook
// get the placeholder
var c = $('.fblike');

// Create the Plugin element
var e = document.createElement('fb:like');

// Add your attributes
e.setAttribute('action', 'like');
e.setAttribute('font', 'Verdana');
e.setAttribute('width', '65');
e.setAttribute('href', caminho);
e.setAttribute('show_faces', 'true');
e.setAttribute('layout', 'box_count');

// put the Plugin into the placeholder
c.append(e);
//]]>
});

/*
 * Máscaras que não utilizam o masked input plugin
 */
function mask(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execMask()",1)
}
function execMask(){
    v_obj.value=v_fun(v_obj.value)
}
function number(v){
    return v.replace(/\D/g,"")
}
