$(function(){


	/////////////////////////////////////////////////////////////////////////
    // Link Banner
	$('#img-corner').click(function () {
		id = $('.player-menu.hover').attr('id');
		id = id.replace('player-menu-', '');
		
		window.location = $('#player-img-' + id + ' .titulo-img').attr('href');
		
	}); 
    /////////////////////////////////////////////////////////////////////////
    // Mostra Formulario 
    $('.mostrar').live('click', function(event){
    
        $('#box-contato fieldset').show();
        $('#cadastro-ok').hide();
        
        event.preventDefault();
        return false;
    })
    
	//desabilita o botao direito do mouse
    $(document).bind("contextmenu",function(e){
		return false;
	});
    
    /////////////////////////////////////////////////////////////////////////
    // Formulario de contato
    $('#formulario-contato').pForm({
        action: "http://www.casul.com.br/ajax.php",
        start: function(){
            $('#box-contato fieldset').hide();
            $('#formulario-contato').append('<p class="img-carregando"><span>carregando</span><img  src="http://www.casul.com.br/theme/img/carregando-form.gif"/></p>');
            
        },
        success: function(valor){
        
            $('.img-carregando').remove();
            
            // Cadastro Ok 
            if (valor.error == true) {
                $('#cadastro-ok').show();
                $('#box-contato fieldset').hide();
                $('#con-assunto').val('');
                $('#con-nome').val('');
                $('#con-telefone').val('');
                $('#con-email').val('');
                $('#con-mensagem').val('');
                
            }
            else {
                if (valor.msg == '') {
                    alert('Ocorreu algum erro inesperado, Por favor tente de novo ou entre em contato');
                }
                else {
                    alert(valor.msg);
                }
            }
        }
        
    });
    
    
    ////////////////////////////////////////////////////////////////////////////
    // Mascaras do campos    
    $("#con-telefone").mask("(99)9999-9999");
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
    // Google maps Pagina Contato 
    $('#maps').html('<iframe width="700" height="356" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=Parapu%C3%A3+-+SP,+Brasil&amp;sll=37.0625,-95.677068&amp;sspn=28.058077,79.013672&amp;ie=UTF8&amp;hq=&amp;hnear=Parapu%C3%A3+-+S%C3%A3o+Paulo,+Brasil&amp;ll=-21.779507,-50.790997&amp;spn=0.028375,0.059996&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
    
    // Efeito de hover dos itens do player
    $('#player-opcoes li').mouseover(function(){
        $(this).addClass('hover-mouse');
    }).mouseout(function(){
        $(this).removeClass('hover-mouse');
    })
    
    // Verifica a quantidade de itens do player se for maior que 1 executa o player
    if ($('#player-opcoes li').size() > 1) {
        playerIndex(1);
        
        // Botao Anterior
        $('#player-anterior').click(playerAnterior);
        
        // Botao Proxima
        $('#player-proxima').click(playerProxima);
        
        // Click Nos item do player
        $('#player-opcoes li').click(playerItem)
        
    }
    else {
        // Caso so tenha um item no player
        $('#player-menu-1').addClass('hover');
        
        $('#player-anterior ,#player-proxima').click(function(event){
            event.preventDefault(event);
            return false;
        })
    }
    
    
    
    
    
    
    
    
    /////////////////////////////////////////////////////////////////////////
    // Troca a cidade da previsão do tempo
    
    /////////////////////////////////////////////////////////////////////////
    // Hover da cidade atual
    $('.nome-cidade').mouseover(function(event){
    
        $('#op-cidades').slideDown().css('opacity', 0.85);
        
        event.preventDefault(event);
        return false;
        
    });
    
    
    // Impede que a tela de opções suma antes do user clike na sua opção
    $('#op-cidades').mouseover(function(event){
        event.preventDefault(event);
        return false;
    });
    
    
    // Some com a tela de opções
    $('body').mouseover(function(event){
        $('#op-cidades').slideUp();
    });
    
    // Efeito de hover das opções de cidades
    $('#op-cidades li').mouseover(function(event){
    
        $(this).css({
            'background-color': '#066301',
            'color': '#FFFFFF'
        });
        
        event.preventDefault(event);
        return false;
        
    }).mouseout(function(event){
    
        $(this).css({
            'background-color': '#FFFFFF',
            'color': '#033713'
        });
        
        event.preventDefault(event);
        return false;
        
    });
    
    
    $('#op-cidades li').click(function(event){
    
        // Esconder as opcoes
        $('#op-cidades').slideUp();
        
        // Pego os valores necessarios
        var nomeCidade = $(this).children('.cidade').text();
        var idCidade = $(this).children('.valor-cidade').text();
        
        // Atualizo os nomes das cidades
        $('#box-meterologia .nome-cidade span').text(nomeCidade);
        
        // escondo os dados atuais da previsao
        $('#box-meterologia .imagem').animate({
            "marginLeft": "-=68px"
        }, "fast", function(){
            $('#box-meterologia .imagem').children('img').attr('src', 'http://www.casul.com.br/theme/img/carregando.gif').attr('width', '35').css('padding-top', '5px');
            
            $('#box-meterologia .imagem').animate({
                "marginLeft": "+=68px"
            }, "fast");
            
        });
        
        $('#box-meterologia .temperaturas').animate({
            "marginRight": "-=100px"
        }, "fast");
        
        // Faço a requisicao dos dados necessarios
        $.ajax({
            header: {
                "Content-Type": "text/html;  charset=ISO-8859-1"
            },
            type: "post",
            data: "act=tempo&cidade=" + idCidade,
            url: "http://www.casul.com.br/ajax.php",
            dataType: "json",
            success: function(valor){
            
                // Troco os valores
                $('.temperaturas .max').text(valor.tempMax + 'ºC');
                $('.temperaturas .min').text(valor.tempMin + 'ºC');
                
                
                
                // Animo as mudanca
                $('#box-meterologia .imagem').animate({
                    "marginLeft": "-=68px"
                }, "fast", function(){
                    $('#box-meterologia .imagem').children('img').attr({
                        'width': '45',
                        'src': valor.urlImg,
                        'title': valor.titleTempo
                    }).css('padding-top', '0px');
                    
                    
                    $('#box-meterologia .imagem').animate({
                        "marginLeft": "+=68px"
                    }, "fast");
                    
                });
                
                $('#box-meterologia .temperaturas').animate({
                    "marginRight": "+=100px"
                }, "slow");
                
            }
        });
        
        
        
        event.preventDefault(event);
        return false;
    });
    
    
    
    
    
    
    
    
    
    
    /////////////////////////////////////////////////////////////////////////
    // Formulario de contato
    $('#form-newsletter').pForm({
        action: "http://www.casul.com.br/ajax.php",
        start: function(){
            return true;
        },
        success: function(valor){
        
            // Cadastro Ok 
            if (valor.error == true) {
            
                alert(valor.msg);
                $('#new-email').val('Insira seu e-mail');
                $('#new-nome').val('Insira seu nome');
                
            }
            else {
                if (valor.msg == '') {
                    alert('Ocorreu algum erro inesperado, Por favor tente de novo ou entre em contato');
                }
                else {
                    alert(valor.msg);
                }
            }
        }
        
    });
    
    ////////////////////////////////////////////////////////////////////////////////////////
    // Limpa os campos
    // Campos do formulario de contato
    $('#login, #senha, #new-nome, #new-email, #nota-cpf').focus(function(event){
        valorAntigo = $(this).attr('value');
        
        if (valorAntigo == "Insira o seu login" || valorAntigo == "senha" || valorAntigo == 'Insira seu nome' || valorAntigo == 'Insira seu e-mail' || valorAntigo == 'Insira a chave de acesso') {
            $(this).attr('value', '');
        }
        
        
    }).blur(function(event){
        if ($(this).attr('value') == '') {
            $(this).attr('value', valorAntigo);
        }
        
    });
    
    ////////////////////////////////////////////////////////////////////////////
    // Hover das fotos da galeria
    $('#fotos li').mouseover(function(){
        $(this).css('background-color', '#066301').children().css('opacity', 0.65);
    }).mouseout(function(){
        $(this).css('background-color', '#FFFFFF').children().css('opacity', 1);
    });
    
    
    
    ////////////////////////////////////////////////////////////////////////////
    // Diminui a letra
    $('.letra-menor').live('click', function(){
        tamanhoFonte = $('#descricao').css('font-size');
        tamanhoFonte = parseInt(tamanhoFonte);
        lineH = parseInt($('#descricao').css('line-height'));
        
        
        
        if (tamanhoFonte > 10) {
            $('#descricao').css('font-size', tamanhoFonte - 2 + 'px').css('line-height', lineH - 2 + 'px');
        }
        
    });
    
    ////////////////////////////////////////////////////////////////////////////
    // Aumenta a letra
    $('.letra-maior').live('click', function(){
        tamanhoFonte = $('#descricao').css('font-size');
        tamanhoFonte = parseInt(tamanhoFonte);
        lineH = parseInt($('#descricao').css('line-height'));
        
        if (tamanhoFonte < 20) {
            $('#descricao').css('font-size', tamanhoFonte + 2 + 'px').css('line-height', lineH + 2 + 'px');
            ;
            
        }
        
    });
    
    
    
    
    //////////////////////////////////////////////////////////////////////////////////////////
    // Color Box	
    $('a[rel="popup"]').colorbox({
        slideshow: true
    });
    
    
    // Links Externos 
    $('.externo').attr('target', '_blanck');
    
});



/**
 * Player da Index
 */
function playerIndex(item){

    // Reinalizo o item	
    if (item > parseInt($('#player-opcoes li').size() - 2)) {
        item = 1;
    }
    
    if (item != 1) {
        // Esconder todas as imagens e mostro a proxima
        $('#player-img').animate({
            "marginLeft": "-=712px"
        }, "slow");
        $('.player-menu').removeClass('hover');
        $('#player-menu-' + item).addClass('hover');
    }
    else {
        $('#player-img').animate({
            "marginLeft": "0px"
        }, "slow");
        $('.player-menu').removeClass('hover');
        $('#player-menu-' + item).addClass('hover');
    }
    
    item++;
    
    idAtual = $('.lis-total .hover').attr('id');
    
    idAtual = idAtual.replace('menu', 'img');
    
    $('#titulo-banner').text($('#' + idAtual + ' .titulo-img').attr('title'));
    
    
    tempoPlayerIndex = setTimeout('playerIndex (' + item + ');', 8000);
}

/**
 * Botao Anterior
 */
function playerAnterior(){
    atual = $('.player-menu.hover').attr('id');
    atual = atual.replace('player-menu-', '');
    
    if (atual > 1) {
        atual = atual - 1;
        
        clearInterval(tempoPlayerIndex);
        $('#player-img').animate({
            "marginLeft": "+=712px"
        }, "slow");
        $('.player-menu').removeClass('hover');
        $('#player-menu-' + atual).addClass('hover');
        
        atual++;
        tempoPlayerIndex = setTimeout('playerIndex (' + atual + ');', 8000);
    }
    
    
    
    idAtual = $('.lis-total .hover').attr('id');
    
    idAtual = idAtual.replace('menu', 'img');
    
    $('#titulo-banner').text($('#' + idAtual + ' .titulo-img').attr('title'));
    
    
    return false;
    
    
}

/**
 * Botao Proxima
 */
function playerProxima(){
    atual = $('.player-menu.hover').attr('id');
    atual = atual.replace('player-menu-', '');
    
    if (atual < parseInt($('#player-opcoes li').size() - 2)) {
        atual = parseInt(atual);
        atual++;
        
        clearInterval(tempoPlayerIndex);
        $('#player-img').animate({
            "marginLeft": "-=712px"
        }, "slow");
        $('.player-menu').removeClass('hover');
        $('#player-menu-' + atual).addClass('hover');
        
        atual++;
        tempoPlayerIndex = setTimeout('playerIndex (' + atual + ');', 8000);
    }
    
    idAtual = $('.lis-total .hover').attr('id');
    
    idAtual = idAtual.replace('menu', 'img');
    
    $('#titulo-banner').text($('#' + idAtual + ' .titulo-img').attr('title'));
    
    
    return false;
}

/**
 * PLayer Item
 */
function playerItem(){

    //Paro o player
    clearInterval(tempoPlayerIndex);
    
    //Elemento Atual
    atual = $('.player-menu.hover').attr('id');
    atual = atual.replace('player-menu-', '');
    
    //Elemento do Click
    click = $(this).attr('id');
    click = click.replace('player-menu-', '');
    
    // Coloco o item como Hover
    $('.player-menu').removeClass('hover');
    $('#player-menu-' + click).addClass('hover');
    
    // Caso click maior que o atual vai pra frente se naum para traz
    if (click > atual) {
    
        diferenca = click - atual;
        mover = diferenca * 712;
        $('#player-img').animate({
            "marginLeft": "-=" + mover + "px"
        }, "slow");
        
        click++;
        
        tempoPlayerIndex = setTimeout('playerIndex (' + click + ');', 8000);
    }
    else {
    
        diferenca = atual - click;
        mover = diferenca * 712;
        $('#player-img').animate({
            "marginLeft": "+=" + mover + "px"
        }, "slow");
        
        click++;
        tempoPlayerIndex = setTimeout('playerIndex (' + click + ');', 8000);
    }
    
    
    idAtual = $('.lis-total .hover').attr('id');
    
    idAtual = idAtual.replace('menu', 'img');
    
    $('#titulo-banner').text($('#' + idAtual + ' .titulo-img').attr('title'));
    
}


