$(function(){
	
	
	////////////////////////////////////////////////////////////////////////////////////////
    // Formulario Newsletter
    $('#bt-new').click(function(event){
    
        email = $('#new-email').val();
        nome = $('#new-nome').val();
        
        error = false;
        
        $('.error').hide();
        
        
        if (nome == '' || nome == 'seu nome') {
            $('#error-nome').html('Nome inválido').fadeIn();
            error = true;
        }
        
        
        
        if (email == '' || email == 'seu email' || !validaEmail(email)) {
            $('#error-email').html('E-mail inválido').fadeIn();
            error = true;
        }
        
        
        if (error == false) {
        
            $('#formulario-newsletter fieldset').append('<div id="carregandoNew"><img src="http://www.idealmoveis.com.br/theme/img/carregando.gif" alt="Carregando" /></div>');
            
            
            $.ajax({
                type: 'post',
                url: 'http://www.megastarh.com.br/ajax.php',
                data: {
                    'acao': 'neswletter',
                    'nome': nome,
                    'email': email
                },
                dataType: 'json',
                success: function(valor){
                
                    // cadastro corretamente
                    if (valor.error == true) {
                        $('#new-email').val('seu email');
                        $('#new-nome').val('seu nome');
                        $('#error-geral').html(valor.msg).fadeIn();
                        // Mostra mensagem cadastro Ok
                        
                        $('#carregandoNew').fadeOut().remove();
                        
                    }
                    else {
                    
                        // Mostra Errors
                        $('#error-geral').html(valor.msg).fadeIn();
                        $('#carregandoNew').fadeOut().remove();
                    }
                }
            });
            
        }
       
        event.preventDefault(event);
        return false;
    });
    
    
    

	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////
    // Formulario solicite
    $('#sol-bt').click(function(event){
    
        email = $('#sol-email').val();
        nome = $('#sol-nome').val();
        telefone = $('#sol-telefone').val();
        cidade = $('#sol-cidade').val();
        mensagem = $('#sol-mensagem').val();
        
        error = false;
        errorList = [];
        
        if (nome == '' || nome == 'insira o seu nome') {
            errorList.push('Nome inválido');
            error = true;
        }
        
        if (email == '' || email == 'insira seu e-mail' || !validaEmail(email)) {
            errorList.push('E-mail inválido');
            error = true;
            
        }
        
        if (telefone == '' || telefone == 'seu telefone') {
            errorList.push('Telefone inválido');
            error = true;
        }
        
        if (cidade == '' || cidade == 'sua cidade') {
            errorList.push('Cidade inválido');
            error = true;
        }
        if (mensagem == '' || mensagem == 'digite sua mensagem') {
            errorList.push('Mensagem inválida');
            error = true;
        }
        
        
        $('#listErrors').slideUp().end();
        
        
        if (error == false) {
            $('#formulario-solicite fieldset').append('<div class="center" id="carregandoNew"><img src="/theme/img/carregando.gif" alt="Carregando" /></div>');
            
            
            $.ajax({
                type: 'post',
                url: 'http://www.megastarh.com.br/ajax.php',
                data: {
                    'acao': 'solicite',
                    'nome': nome,
                    'email': email,
                    'telefone': telefone,
                    'cidade': cidade,
                    'mensagem': mensagem
                },
                dataType: 'json',
                success: function(valor){
                
                
                    // cadastro corretamente
                    if (valor.error == true) {
												
				        email = $('#sol-email').val('insira seu e-mail');
				        nome = $('#sol-nome').val('insira o seu nome');
				        telefone = $('#sol-telefone').val('seu telefone');
				        cidade = $('#sol-cidade').val('sua cidade');
				        mensagem = $('#sol-mensagem').val('digite sua mensagem');
                    
                        $('#fomurlario-solicite .form-con').fadeOut();
                        $('#listErrors').html('<ul></ul>');
                        $('#listErrors ul').html('<li>' + valor.msg + '</li>');
                        $('#listErrors').slideDown();
                        $('#carregandoNew').fadeOut().remove();
                        
                    }
                    else {
                        $('#listErrors').html('<ul></ul>');
                        $('#listErrors ul').html('<li>' + valor.msg + '</li>');
                        $('#listErrors').slideDown();
                        $('#carregandoNew').fadeOut().remove();
                    }
                }
            });
            
        }
        else {
            errors = '';
            for (i = 0; i < errorList.length; i++) {
                errors += '<li>' + errorList[i] + '</li>';
            }
            
            $('#listErrors').html('<ul></ul>');
            $('#listErrors ul').html(errors);
            
            errors = '';
            errorList.length = 0;
            
            $('#listErrors').slideDown();
            
        }
        
        event.preventDefault(event);
        return false;
    });
    

	////////////////////////////////////////////////////////////////////////////////////////
    // Limpa os campos
    $('#sol-nome,#sol-cidade,#sol-email,#sol-telefone,#sol-mensagem').focus(function(event){
        valorAntigo = $(this).attr('value');
        
        if (valorAntigo == 'insira o seu nome' ||
			valorAntigo == 'sua cidade' ||
			valorAntigo == 'insira seu e-mail' ||
			valorAntigo == 'seu telefone' ||
			valorAntigo == 'digite sua mensagem'  ) {
            $(this).attr('value', '');
        }
        
    }).blur(function(event){
        if ($(this).attr('value') == '') {
            $(this).attr('value', valorAntigo);
        }
        
    });



    //////////////////////////////////////////////////////////////////
    // Slide Index Destaque   
    if ($('#player-imgs li').size() > 1) {
        $('#pl-img-1').fadeIn('slow');
        
        
        
        $('body').data('atual', 1);
        
        
        tempoSlideIndex = setInterval('slideIndex();', 5000);
        
        cont = 0;
        totalLink = $('#player-imgs li').size();
        // Acresento os botoes de navegacao
        $('#player-imgs li').each(function(){
            cont++;
            elemento = $(this);
            
            // Crio o link de navegação
            $('#player-index .botoes').prepend('<a class="bt-destaque " onclick="slideIndex(' + cont + ')" id="bt-' + cont + '">0' + cont + '</a>');
            
            totalLink = totalLink - 1;
        });
        
        
        $('#bt-1').addClass('destaque');
		$('#pl-lin-1').addClass('destaque-link');
        
    }
    else {
        $('#pl-img-1').fadeIn('slow');
        //$('#item-1').fadeIn('slow');
        $('#bt-1').addClass('destaque');
    }
    
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
    // Formulario Contato
    $('#enviar-con').click(function(event){
    
        email = $('#con-email').val();
        nome = $('#con-nome').val();
        telefone = $('#con-telefone').val();
        assunto = $('#con-assunto').val();
        mensagem = $('#con-mensagem').val();
        
        error = false;
        errorList = [];
        
        if (nome == '' || nome == 'Insira seu nome completo') {
            errorList.push('Nome inválido, por favor corrija o nome informado');
            error = true;
        }
        
        if (email == '' || email == 'Insira seu e-mail verdadeiro' || !validaEmail(email)) {
            errorList.push('E-mail inválido, por favor corrija o e-mail informado');
            error = true;
            
        }
        
        if (telefone == '') {
            errorList.push('Telefone inválido, por favor corrija o telefone informado');
            error = true;
        }
        
        if (assunto == '') {
            errorList.push('Assunto inválido, por favor corrija o assunto informado');
            error = true;
        }
        if (mensagem == '') {
            errorList.push('Mensagem inválida, por favor corrija a mensagem informada');
            error = true;
        }
        
        
        $('#listErrors').slideUp().end();
        
        
        if (error == false) {
            $('#formulario-contato fieldset').append('<div class="center" id="carregandoNew"><img src="/theme/img/carregando.gif" alt="Carregando" /></div>');
            
            
            $.ajax({
                type: 'post',
                url: 'http://www.megastarh.com.br/ajax.php',
                data: {
                    'acao': 'contato',
                    'nome': nome,
                    'email': email,
                    'telefone': telefone,
                    'assunto': assunto,
                    'mensagem': mensagem
                },
                dataType: 'json',
                success: function(valor){
                
                
                    // cadastro corretamente
                    if (valor.error == true) {
                    
                        $('.form-con').fadeOut();
                        $('#listErrors').html('<ul></ul>');
                        $('#listErrors ul').html('<li>' + valor.msg + '</li>');
                        $('#listErrors').slideDown();
                        $('#carregandoNew').fadeOut().remove();
                        
                    }
                    else {
                        $('#listErrors').html('<ul></ul>');
                        $('#listErrors ul').html('<li>' + valor.msg + '</li>');
                        $('#listErrors').slideDown();
                        $('#carregandoNew').fadeOut().remove();
                    }
                }
            });
            
        }
        else {
            errors = '';
            for (i = 0; i < errorList.length; i++) {
                errors += '<li>' + errorList[i] + '</li>';
            }
            
            $('#listErrors').html('<ul></ul>');
            $('#listErrors ul').html(errors);
            
            errors = '';
            errorList.length = 0;
            
            $('#listErrors').slideDown();
            
        }
        
        event.preventDefault(event);
        return false;
    });
    
    
    
    
    // Mostra os proximos meses da agenda
    $('#bt-avancar-agenda').live('click', function(event){
    
        $('#listagem-agenda').slideUp();
        $('#agenda').html('<img src="/theme/img/carregando.gif" class="carregando" alt="carregando" />');
        
        $.ajax({
            type: 'post',
            url: 'http://www.megastarh.com.br/ajax.php',
            async: false,
            data: {
                'acao': 'avancar-mes'
            },
            dataType: 'json',
            success: function(resp){
            
                $('#listagem-agenda').html(resp.agenda).slideDown();
                
                $('#agenda').html(resp.calendario);
                
            }
        });
        
        event.preventDefault(event);
        return false;
    });
    
    // Mostra os meses anteriores da agenda
    $('#bt-voltar-agenda').live('click', function(event){
    
        $('#listagem-agenda').slideUp();
        $('#agenda').html('<img src="/theme/img/carregando.gif" class="carregando" alt="carregando" />');
        
        $.ajax({
            type: 'post',
            url: 'http://www.megastarh.com.br/ajax.php',
            async: false,
            data: {
                'acao': 'voltar-mes'
            },
            dataType: 'json',
            success: function(resp){
            
                $('#listagem-agenda').html(resp.agenda).slideDown();
                
                $('#agenda').html(resp.calendario);
                
            }
        });
        
        event.preventDefault(event);
        return false;
    })
    
    
    
    //////////////////////////////////////////////////////////////////////////////
    // Hover dia agenda 	
    $('.agenda_dias1').mouseover(function(){
        $(this).css({
            'background-color': '#c40000'
        });
    }).mouseout(function(){
        $(this).css({
            'background-color': '#263040'
        });
    });
    
    //////////////////////////////////////////////////////////////////////////////
    // Color Box 	
    $('a[rel="popup"]').colorbox({
        slideshow: true
    });
    
    ////////////////////////////////////////////////////////////////////////////
    // Links Externos 
    $('.externo').attr('target', '_blanck');
	
	   ////////////////////////////////////////////////////////////////////////////
    // Diminui a letra
    $('.letra-menor').click(function(){
        tamanhoFonte = $('#descricao').css('font-size');
        tamanhoLinha = $('#descricao').css('line-height');
        tamanhoFonte = parseInt(tamanhoFonte);
        tamanhoLinha = parseInt(tamanhoLinha);
        
        
        if (tamanhoFonte > 10) {
            $('#descricao').css({
                'font-size': tamanhoFonte - 2 + 'px',
                'line-height': tamanhoLinha - 2 + 'px'
            });
        }
        
    });
    
    ////////////////////////////////////////////////////////////////////////////
    // Aumenta a letra
    $('.letra-maior').click(function(){
        tamanhoFonte = $('#descricao').css('font-size');
        tamanhoLinha = $('#descricao').css('line-height');
        tamanhoFonte = parseInt(tamanhoFonte);
        tamanhoLinha = parseInt(tamanhoLinha);
        
        if (tamanhoFonte < 20) {
            $('#descricao').css({
                'font-size': tamanhoFonte + 2 + 'px',
                'line-height': tamanhoLinha + 2 + 'px'
            });
            
        }
        
    });

    
});

function validaEmail(mail){
    var er = RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if (er.test(mail) == false) {
        return false;
    }
    
    return true;
}

function slideIndex($acao){

    atual = $('body').data('atual');
    
    if ($acao == undefined) {
        proximo = atual + 1;
        
        
        if (proximo > $('#player-imgs li').size()) {
            proximo = 1;
        }
        
        $('#pl-img-' + proximo).fadeIn('slow');
		$('#pl-img-' + atual).fadeOut('slow');
		
		
		
        $('#player-links li').removeClass('destaque-link');
        $('#pl-lin-' + proximo).addClass('destaque-link');
        
        
        $('.bt-destaque').removeClass('destaque');
        $('#pl-img-' + proximo).addClass('destaque');
        
        
        $('#bt-' + proximo).addClass('destaque');
        $('body').data('atual', proximo);
        
    }
    else {
        clearInterval(tempoSlideIndex);
        proximo = $acao;
        
        if (atual != proximo) {
            if (proximo > $('#player-imgs li').size()) {
                proximo = 1;
            }
            
            $('#pl-img-' + proximo).fadeIn('slow');
            $('#pl-img-' + atual).fadeOut('slow');
            
            
            $('#player-links li').removeClass('destaque-link');
            $('#pl-lin-' + proximo).addClass('destaque-link');
            
            
            
            $('.bt-destaque').removeClass('destaque');
            $('#pl-img-' + proximo).addClass('destaque');
            $('#bt-' + proximo).addClass('destaque');
            
            $('body').data('atual', proximo);
            
        }
        tempoSlideIndex = setInterval('slideIndex();', 5000);
    }
}

