// JavaScript Document

var URL_SITE;

$().ready(function() {

    URL_SITE = Zend().baseUrl() + '/' + Zend().module() + '/';

    var menus = $('#header-content #menu-topo li');
    for(var i=0; i < menus.length; i++) {
        $(menus[i]).bind('mouseenter', function() {
            $(this).addClass('over');
        });
		
        $(menus[i]).bind('mouseleave', function() {
            $(this).removeClass('over');
        });
    }
	
    var menus = $('#menu-site li');
    for(var i=0; i < menus.length; i++) {
        $(menus[i]).bind('mouseenter', function() {
            $(this).addClass('over');
            $(this).find('.submenu:first').show();
        });
		
        $(menus[i]).bind('mouseleave', function() {
            $(this).removeClass('over');
            $(this).find('.submenu').hide();
        });
    }
	
});

function updateOrcamento(item) {
    var id = item.name.split('_');
    var qtd = parseInt(item.value);

    $('#finalizar_orcamento a').hide();
    $('#finalizar_orcamento span').show();
	
    $.ajax({
        url: URL_SITE + 'orcamento/update/id/' + id[1],
        data: ({
            'qtd':qtd
        }),
        type:'POST',
        success:function(data) {
            $('#finalizar_orcamento a').show();
            $('#finalizar_orcamento span').hide();
            item.value = data;
        }
    });
}

function clearForm(ele) {

    $('#' + ele).find(':input').each(function() {   	
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                if($(this).attr('valor') == $(this).val()) {
                    $(this).val('');
                }
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
    formClear = true;
}

function confirmContato() {
	
    $('#msgform').hide();
	
    $('#msgform').html('Contato Realizado, em breve entraremos em contato!');
    $('#msgform').fadeIn();
	
    $('#form_contato').find(':input').each(function() {
        $(this).val('');
    });
}


function initForm(form) {
    $('#' + form).find(':input').each(function() {
        $(this).attr('valor', $(this).val());
    });
	
    validaFormulario(form);
}

function validaFormulario(form) {
    var elements;
	
    $('#' + form).validate( {
        rules : {
            nome : {
                required:true
            },
            email : {
                email : true,
                required : true
            },
            telefone: {
                required:true
            },
            cidade: {
                required:true
            },
            uf: {
                required:true
            }
        },
        messages : {
            nome:'Preencha seu nome',
            email:'Preencha seu e-mail corretamente',
            telefone:"Preencha seu telefone",
            cidade:"Preencha sua cidade",
            uf:"Preencha UF"
        }
    });
}

function submitOrcamento() {
    clearForm('form_orcamento');
    $('#form_orcamento').submit();
}

function submitContato() {
    clearForm('form_contato');
    $('#form_contato').submit();
}
function setMaskFone(obj) {
    if(!obj.maskFone) {
        obj.maskFone = true;
        $(obj).mask('(99) 9999-9999');
    }
}

function Tritoq_Download(id, file)
{
    var url	  = Zend().baseUrl() + '/' + Zend().module() + '/' + Zend().controller() + '/download';
	
    if(file && id) {
        var TFrame;
        if($('#Tritoq_Frame_Download').length <= 0) {
            TFrame = $(document.createElement('iframe'));
            TFrame.attr('name','TritoqFrameDownload');
            TFrame.attr('id',  'Tritoq_Frame_Download');
            TFrame.attr('style','height:0px; width:0px; border:0; display:none;');
        } else {
            TFrame = $('#Tritoq_Frame_Download');
        }
		
        // Formulário
        var TFile		= $(document.createElement('input'));
        TFile.attr('type', 'hidden').attr('name', 'Tritoq_File').attr('value' ,file);
		
        var TIdFile		= $(document.createElement('input'));
        TIdFile.attr('type', 'hidden').attr('name', 'Tritoq_IdFile').attr('value' ,id);
		
        var TForm;
        if($('#Tritoq_Form_Download').length <= 0) {
            TForm = $(document.createElement('form'));
            TForm.attr('action', url).attr('method', 'POST').attr('target', 'TritoqFrameDownload').attr('id', 'Tritoq_Form_Download');
        } else {
            $('#Tritoq_Form_Download input').remove();
            TForm = $('#Tritoq_Form_Download');
        }
		
        // Apends
        $(TForm).append(TFile);
        $(TForm).append(TIdFile);
        $('body').append(TFrame);
        $('body').append(TForm);
		
        // Submit Form
        $(TForm).submit();
    }
}

function initNewsletter() {
    $('#form_newsletter').validate({
        rules : {
            nNome : {
                required:true
            },
            nEmail : {
                email : true,
                required : true
            }
        },
        messages : {
            nNome:'Preencha seu nome',
            nEmail:'Preencha seu e-mail corretamente'
        },
        submitHandler:function() {
            $.post(Zend().currentPath()+"/news",{
                nome: $('#nNome').val(),
                email:$('#nEmail').val()
            }, function (data)

            {
                    /*
				* 1 - Sucesso
				* 2 - E-mail ja existe
				*/
                    if($.trim(data) == '1') {
                        $('#nNome').val('Nome');
                        $('#nEmail').val('E-mail');
                        $('#status_news').html('<span style="color:green;">E-mail cadastrado com sucesso</span>');
                    } else {
                        $('#status_news').html('Esse e-mail já está cadastrado');
                    }
                    $('#status_news').show();
                });
			
        }
    })
}

function insertNews (form) {
    if($('#nNome').val()  == 'Nome')   {
        $('#nNome').val('');
    }
    if($('#nEmail').val() == 'E-mail') {
        $('#nEmail').val('');
    }
}

function loadRepresentantes(id) {

    $.ajax({
        url: URL_SITE + 'representantes/list',
        data: ({
            'id':id
        }),
        type:'POST',
        success:function(data) {
            $('#conteudo_representante').html(data);
        }
    });
}

function slideTo(id) {
    var classe = $('#' + id).attr('class');
    if(!classe) {
        $('#' + id).slideDown();
        $('#' + id).attr('class','show');
    } else {
        $('#' + id).hide();
        $('#' + id).removeAttr('class');
    }
}

function keyUpRe(event) {
    if(event.keyCode == '13') {
        buscaRepresentante();
    }
}

function buscaRepresentante() {
    var b = $('#busca_repre').val();
    var id = $('#id_estado').val();
	
    if(b == '') {
        $('#busca_repre').focus();
    } else {
        $.ajax({
            url: URL_SITE + 'representantes/list',
            data: ({
                'id':id,
                'search':b
            }),
            type:'POST',
            success:function(data) {
                $('#conteudo_representante').html(data);
            }
        });
    }
}

function submitLogin() {    
    clearForm('form_login');    
    $('#form_login').submit();
}

function initFormLogin()
{
    $('#form_login').find(':input').each(function() {
        $(this).attr('valor', $(this).val());
    });

    $('#area-restrita #login, #area-restrita #senha').bind('focus', function() { if($(this).val() == $(this).attr('valor')) $(this).val(''); })
    $('#area-restrita #login, #area-restrita #senha').bind('blur', function() { if($(this).val() == '') $(this).val( $(this).attr('valor') ); })

    if($('#area-restrita #form_login').length > 0)
    {
        $('#form_login').validate({
            rules:{
                login:'required',
                senha:'required'
            },
            messages:{
                login:'Digite seu login',
                senha:'Digite sua senha'
            }
        });
    }
}
