/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function vitoriaImoveis(){

    var vi = this;

    $('#logomarca').click(function(){
        window.location = 'index.php';
    });

    var opcoes_lightbox = {
            diminuir : 0.75,
            overlayBgColor: '#FFF',
            overlayOpacity: 0.6,
            imageLoading: 'imagens/lightbox-ico-loading.gif',
            imageBtnClose: 'imagens/lightbox-btn-close.gif',
            imageBtnPrev: 'imagens/lightbox-btn-prev.gif',
            imageBtnNext: 'imagens/lightbox-btn-next.gif',
            imageBlank: 'imagens/lightbox-blank.gif',
            containerResizeSpeed: 350,
            txtImage: 'Imagem',
            txtOf: 'de'
        };

    this.COMPRA = 'C';
    this.ALUGA = 'A';
    this.LANCAMENTO = 'L';

    this.textoReferencia = function(){
        $('#ref').focus(function(){
            $(this).val('');
        });
    }

    this.situacaoChange = function(){
        $('#situacao').change(function(){
            $.ajax({
                type:'GET',
                url:'ajax.php',
                data:{
                    'op' : 'precos',
                    'situacao' : $('#situacao').val()
                },
                success: function(dados){
                    $('#preco').html(dados);
                }
            })
        })
    }

    this.cidadeChange = function(cidade, bairro, operacao){
        $('#'+cidade).change(function(){
            $.ajax({
                type:'GET',
                url:'ajax.php',
                data:{
                    'op' : operacao,
                    'cidade' : $('#'+cidade).val()
                },
                success: function(dados){
                    $('#'+bairro).html(dados);
                }
            })
        })
    }

    this.initPesquisa = function(){
        vi.textoReferencia();
        vi.situacaoChange();
        vi.cidadeChange('cidade', 'bairro', 'bairros');

        $('#aPesquisa').click(function(){
            $(this).parent('form').submit();
        });
    }

    this.initPesquisaImoveis = function(){
        vi.cidadeChange('cidadeSiim', 'bairroSiim', 'bairros_siim');

        $('#aPesquisaSiim').click(function(){
            $(this).parents('form').submit();
        });
    }

    this.initImoveis = function(){

        vi.initPesquisaImoveis();

        $('#paginacao a').each(function(){
            $(this).click(function(index, element){
                $('#pesCompleta').append('<input type="hidden" name="p" value="'+$(this).html()+'"').submit();
            })
        });

        $('#conteudo div.imovel > img').click(function(){
            if($('#situacao').val() == vi.LANCAMENTO){
                window.location = 'lancamento.php?ref='+$(this).siblings('input').val();
            }
            else{
                window.location = 'imovel.php?ref='+$(this).siblings('input').val();
            }
        });
    }

    this.initImovel = function(){

        vi.initPesquisaImoveis();

        var acertarPrincipal = function(fotos, index){
            $('div.principal input').val(index);
            $('div.principal a')
                .attr('href', fotos[1])
                .lightBox(opcoes_lightbox);
            $('div.principal a img')
                .attr('src', fotos[0])
                .hide()
                .load(function(){
                    $(this).css('margin-top',($('div.principal').height()-$(this).height())/2).show();
                });
        }

        var mover = function(esquerda){
            var index = $('.principal input').val();

            var lis = $('.carouselImoveis ul li');

            esquerda ? --index : ++index;

            if(index >= 0 && index < lis.size()){
                var li = lis.get(index);
                var fotos = $(li).children().children('input').val().split('|');
                acertarPrincipal(fotos, index);
            }
        }

        $('.botEsq').click(function(){
            mover(true);
        });
        $('.botDir').click(function(){
            mover(false);
        });

        $('div.principal a').lightBox(opcoes_lightbox);

        $.ajax({
            type:'GET',
            url:'ajax.php',
            data:{
                'op' : 'carousel',
                'ref' : $('#id_imovel').val()
            },
            success: function(dados){

                $('body').append('<div style="display:none" id="batchLoadImagens">'+dados+'</div>');

                var imagensCarregadas = function(){
                    $('.carouselImoveis').html($('#batchLoadImagens').html());

                    $('body').remove('#batchLoadImagens');

                    $('.carousel a').click(function(){
                        var fotos = $(this).siblings('input').val().split('|');
                        acertarPrincipal(fotos, $('.carousel li').index($(this).parents('li')));
                    });
                    $('.carouselImoveis').show();

                    if($(".carouselImoveis ul li").length){
                        $(".carouselImoveis").jMyCarousel({
                            visible: '414px',
                            speed : 300,
                            circular : false,
                            btnPrev: $('#hoverEsq'),
                            btnNext: $('#hoverDir')
                        });
                    }
                }

                var imagemCarregada = function(atual, total){
                    $('.carregandoImagens').html('Aguarde, carregando foto '+atual+' de '+total);
                }

                $('#batchLoadImagens img').batchImageLoad({
                        loadingCompleteCallback: imagensCarregadas,
                        imageLoadedCallback: imagemCarregada
                });
            }
        });

        $.ajax({
            type:'GET',
            url:'ajax.php',
            data:{
                'op' : 'imoveis_semelhantes',
                'ref' : $('#id_imovel').val()
            },
            success: function(dados){
                $('.imoveis').html(dados);
            }
        });
    }

    this.admin = function(){
        $('#aFixFotos').click(function(){
            vi.fixFotos();
        });

        $('#aLogout').click(function(){
            vi.logout();
        })
    }

    this.fixFotos = function(){
        $('#aFixFotos').siblings('img').show();
        $.ajax({
            type:'GET',
            url:'ajax.php',
            data:{
                'op' : 'fix_fotos'
            },
            complete: function(){
                $('#aFixFotos').siblings('img').hide();
            }
        });
    }

    this.logout = function(){
        $.ajax({
            type:'GET',
            url:'ajax.php',
            data:{
                'op' : 'logout'
            },
            success: function(dados){
                window.location = 'index.php';
            }
        });
    }

    this.initLancamento = function(){
        $('div.fotos a').lightBox(opcoes_lightbox);
        $('div.fotos div').eq(0).show();

        $('div.navGaleria a').eq(1).click(function(){
            var visivel = $('div.fotos div:visible');
            if(visivel.next().length){
                visivel.hide().next().show();
                $(this).siblings('span').html(visivel.index(visivel)+2);
            }
        });

        $('div.navGaleria a').eq(0).click(function(){
            var visivel = $('div.fotos div:visible');
            if(visivel.prev().length){
                visivel.hide().prev().show();
                $(this).siblings('span').html(visivel.index(visivel)+1);
            }
        });

        $.ajax({
            type:'GET',
            url:'ajax.php',
            data:{
                'op' : 'lancamentos_semelhantes',
                'ref' : $('#id_lancamento').val()
            },
            success: function(dados){
                $('.imoveis').html(dados);
            }
        });
    }
}
