//LISTA DE FUNÇÕES ACIONADAS NAS PÁGINAS
$(document).ready(function() {

    $(document).bind("contextmenu",function(e){
        return false;
    });
	
    var zIndexNumber = 1000;
    $('li').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
    
    // initialize scrollable
    $("div.scrollablePromocao").scrollable({ 
        size: 2, 
        items: '#thumbs',   
        hoverClass: 'hover' 
    }); 
    $("div.scrollableImovel").scrollable();

    $(".comparaButton").tooltip({

        // place tooltip on the right edge
        position: ['top', 'left'],

        // a little tweaking of the position
        offset: [0, 16],

        // use a simple show/hide effect
        effect: 'fade',

        // custom opacity setting
        opacity: 1
    });

    // setup ul.tabs to work as tabs for each div directly under div.panes
    $("ul.tabs").tabs("div.panes > div");

    $('li#submenuLink').hover(
        function() {
            $('#submenu', this).css('display', 'block');
        },
        function() {
            $('#submenu', this).css('display', 'none');
        });

    $('#fecharButton').click(function() {
        return false;
    });

    $('#filtrar').change(function() {
        $(this).parent().submit();
    });

    $(".slideswf").each(function() {
        var span = $(this);
        span.flash({
            src: span.attr("title"),
            width: 762,
            height: 248
        });
    });

    $('.slides-nav a').each(function() {
        $(this).click(function() {
            var span = $($(this).attr('href'));
            span.flash({
                src: span.attr("title"),
                width: 762,
                height: 248
            });
            return false;
        });
    });

    $('#cadastroNewsletter').submit(function() {
        var interesses = 0;
        var retorno = false;

        $(this).find('input.check').each(function() {
            if($(this).attr('checked') == true) {
                interesses++;
            }
        });
        if(interesses == 0) {
            alert('Você precisa selecionar pelo menos uma área de interesse!');
            return false;
        }
        
        $(this).find('input.inputInteiro').each(function() {
            if($(this).val() == "" || $(this).val() == 'Nome' || $(this).val() == 'Email'){
                alert('Todos os campos devem ser preenchidos!');
                $(this).focus();
                retorno = false;
            }else{
                retorno = true;
            }
        });

        return retorno;
    });

    $('#formContato').submit(function(){
        var errors = 0;
        var retorno = false;

        $(this).find('input').each(function(){
            if($(this).val().length == 0){
                errors++;
            }
        });

        $(this).find('textarea').each(function(){
            if($(this).val().length == 0){
                errors++;
            }
        });

        if(errors>0){
            alert('Todos os campos precisam ser preenchidos!');
            retorno = false;
        }else{
            retorno = true;
        }

        return retorno;

    });

    $('a.reload').click(function(){
        $.ajax({
            url: '/faleconosco/captcha_image?' + Math.round(Math.random(0)*1000)+1,
            type: GET,
            cache: false,
            success: function(html){
                $('#captcha').html(html);
            }
        });
        return false;
    });
    
    $('#comparacaoBox').dialog({
        autoOpen: false,
        closeOnEscape: true,
        draggable: false,
        resizable: false,
        height: 350,
        width: 802,
        modal: true,
        position: 'center'
    });

    $('a.comparaLink').click(function() {
        var parent = $(this).parent().parent().parent();
        var strData = "";
        var contador = 0;

        parent.find('input').each(function() {
            if($(this).attr('checked') == true) {
                strData += 'data[Imovel][' + contador + ']=' + $(this).val() + '&';
                contador++;
            }
        });

        if(contador > 3) {
            alert('Você deve selecionar no máximo 3 imóveis!');
        }else{
            if(contador < 2) {
                alert('Você deve selecionar pelo menos 2 imóveis!');
            }else{
                $('#comparacaoBox').dialog('open');

                $("#comparacao").html('<img id="loading" src="/img/loading.gif" />');

                $.ajax({
                    url: '/imoveis/comparacao',
                    type: 'POST',
                    data: strData,
                    cache: false,
                    success: function(html) {
                        $('#comparacao').html(html);
                    },
                    error: function() {
                        alert('Erro no processamento!')
                    }
                });

                $('#fecharButton').click(function() {
                    $('#comparacaoBox').dialog('close');
                });
            }
        }

        return false;
    });

    $('a.comparaLinkBusca').click(function() {
        var parent = $(this).parent().parent().parent().parent();
        var strData = "";
        var contador = 0;

        parent.find('input').each(function() {
            if($(this).attr('checked') == true) {
                strData += 'data[Imovel][' + contador + ']=' + $(this).val() + '&';
                contador++;
            }
        });

        if(contador > 3) {
            alert('Você deve selecionar no máximo 3 imóveis!');
        }else{
            if(contador < 2) {
                alert('Você deve selecionar pelo menos 2 imóveis!');
            }else{
                $('#comparacaoBox').dialog('open');

                $("#comparacao").html('<img src="/img/loading.gif" />');

                $.ajax({
                    url: '/imoveis/comparacao',
                    type: 'POST',
                    data: strData,
                    cache: false,
                    success: function(html) {
                        $('#comparacao').html(html);
                    },
                    error: function() {
                        alert('Erro no processamento!')
                    }
                });

                $('#fecharButton').click(function() {
                    $('#comparacaoBox').dialog('close');
                });
            }
        }

        return false;
    });

    $('div.imovelBox').each(function() {
        var div = $(this);

        $.ajax({
            url: '/imoveis/busca_promocao/' + div.attr('title'),
            type: 'GET',
            cache: false,
            success: function(html) {
                div.append(html);
            }
        });
    });

    // add a 'js' class to the body
    $('body').addClass('js');

    // initialise the slideshow when the DOM is ready
    $slideshow.init();
});