function abrir(URL) {

  var width = 400;
  var height = 430;
  var left = 99;
  var top = 99;

  window.open(URL,'janela', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}
function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function addFlash(file, width, height, id, wmode, version,flashvars)
{
    var params = {
        'wmode':wmode,
        'allowscriptaccess': "always"
    };
    var attributes = {'id':id};
    swfobject.embedSWF(file, id, width, height, version,"site/js/expressInstall.swf", flashvars, params, attributes);
}

function OpenChat() {
	   var width = 450;
	   var height = 520;
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;
	   window.open(SITE + '/atd/view/?action=AppUsr/login','atendimento', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

function initialize() {
    var latlng = new google.maps.LatLng(-27.631307,-52.272318);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapa"), myOptions);

    var marker = new google.maps.Marker({
      position: latlng,
      map: map,
      title: 'Imóveis do Sul',
      icon: IMAGENS+'/marker.png'
    });   
}   

function setupNivoslider()
{
    $('.nivo').nivoSlider({
        effect: 'fade',
        animSpeed: 500,
        pauseTime: 5000,
        startSlide: 0,
        directionNav: false,
        controlNav: true,
        keyboardNav: true,
        pauseOnHover: true,
        manualAdvance: false
    });
}

function setupNivoslider2()
{
    $('.nivo2').nivoSlider({
        effect: 'fade',
        animSpeed: 500,
        pauseTime: 5000,
        startSlide: 0,
        directionNav: false,
        controlNav: true,
        keyboardNav: true,
        pauseOnHover: true,
        manualAdvance: false
    });
}

$(function(){

    $("form.jqtransform").jqTransform();

    $('a.atendimento, div.atendimento-online a').click(function(){
        OpenChat();
        return false;
    })
    
  //  $('#slider .viewport ul').not('#lista_imoveis').css('display','none');
    
   /* $('ul.navega-tipo a').click(function(){
      var aba = $(this).attr('title');
      $('#slider .viewport ul').fadeOut('fast',function(){
        $('#'+aba).fadeIn();
      });
      return false;
    })*/
    
    setupNivoslider();
    setupNivoslider2();

    $('#slider, #thumbs-imovel').tinycarousel({display: 3});
    $('#slider2, #thumbs-imovel').tinycarousel({display: 3});

    $('.localize-mapa, .favoritos, .compartilhe, .rss, .redes-sociais a').tipsy({gravity: 's', fade: true});
    
    $('#fone').mask("9999-9999",{placeholder:" "});
    $('#ddd').mask("99",{placeholder:" "});
    
   /* $('.menu li').hover(function(){
        $(this).children('ul').stop().animate({'height':'show','opacity':1},100);
    }, function(){
        $(this).children('ul').fadeOut('fast');
    })*/
      
    
    /* AQUIII LISTA JAVA
    $('ul.navega-tipo a').click(function(){
        $('ul.navega-tipo a').removeClass();
        $(this).addClass('escolhido');
        $.get('lista_imoveis',{id:$(this).attr('href')}, function(retorno){
          $('#lista_imoveis').html(retorno);
          $('.nivo img').fadeIn();
        })
        var idescolhido = $(this).attr('id');
        return false;
    }) 
    */
    
    $('#thumbs-imovel li a').click(function(){
      var enderecoImagem = $(this).find('img').attr('src');
      $('#img-grande').attr('src',enderecoImagem);
    })
   
/*----------------------------LIMPA CAMPOS---------------------*/
  
 $(' input, textarea').focus(function(){
    if ($(this).val()===$(this).attr('placeholder'))
    {
     $(this).val('');
    }
   });    
  
  $('input, textarea').blur(function(){
    if ($(this).val()==='')
    {
     $(this).val($(this).attr('placeholder'));
    }
   }); 
   
  
/*----------------------------ENVIO DE CONTATO---------------------*/
  $('#form_contato').submit(function(){

    var nome = $('#nome').val();
    var email = $('#email').val();
   
   /*-------------------------SE VALOR PADRÃƒO OU E-MAIL INVÁLIDO--------------*/

    var email_regexp = new RegExp(/^(("[\w\-\s]+")|([\w\-]+(?:\.[\w\-]+)*)|("[\w\-\s]+")([\w\-]+(?:\.[\w\-]+)*))(@((?:[\w\-]+\.)*\w[\w\-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

    function valid_email(email) {
        return email_regexp.test(email);
    }  
  
   if (nome ==='Nome' || email ==='E-mail' || !valid_email(email) )
   {
     $('#msg-div').empty().append('<p>Todos os campos são obrigatórios.</p>').fadeIn().delay(2000).fadeOut();
     return false;
   }
   
  /*-------------------------FAZ O ENVIO------------------------*/
    if (nome!=='' && email!=='' )
    {
      $('#loading').fadeIn();
      $('#form_contato').fadeTo('slow',0.5);
      $.post('envia_contato',$('#form_contato').serialize(), function(retorno){
          if (retorno === 'true'){
              $('#loading').fadeOut('normal',function(){
              $('#form_contato').fadeTo('slow',1);
              $('#msg-div').empty().append('<p><strong>Contato enviado!</strong></p>').fadeIn().delay(5000).fadeOut();
              $('#form_contato :input').not('input[type=submit]').each(function(){
                var padrao = $(this).attr('placeholder');
                $(this).val(padrao);
              });
            });
        } else {
          $('#msg-div').empty().append('<p>Houve um problema no envio do contato. Por favor tente mais tarde.</p>').fadeIn().delay(2000).fadeOut();
        }
      });
    }
    return false;
  })
/*----------------------------ENVIO DE CONTATO---------------------*/
  
  
$('#ligamos').submit(function(){
    var nome_lig = $('#ligamos input[name=nome]').val();
    var ddd_lig = $('#ddd').val();
    var num_lig = $('#fone').val();
    if (nome_lig!=='' && nome_lig!=='Nome' && ddd_lig!=='' && ddd_lig!=='DDD' && num_lig!=='' && num_lig!=='Telefone'){
      $('#ligamos').fadeTo('fast', 0.5);
       $.post('enviar_telefone',$('#ligamos').serialize(), function(retorno){
         if (retorno === 'true'){
           $('div.mensagem').fadeIn().delay(3000).fadeOut();
           $('#ligamos').fadeTo('fast', 1);
         } else {
           $('div.mensagem').html('<p>Problema no envio. Por favor tente mais tarde.</p>').fadeIn().delay(3000).fadeOut();;
           $('#ligamos').fadeTo('fast', 1);
         }
       })
    } else {
      alert('Preencha todos os campos.');
    }
    return false;
})

$('#newsletter').submit(function(){
       var nome_lig = $('#nome').val();
       if (nome_lig!=='' && nome_lig!=='Nome'){
      $('#newsletter').fadeTo('fast', 0.5);
         $.post('enviar_newsletter',$('#newsletter').serialize(), function(retorno){
         if (retorno === 'true'){
           $('div.mensagems').fadeIn().delay(3000).fadeOut();
           $('#newsletter').fadeTo('fast', 1);
         }else if (retorno === 'existe'){
           $('div.mensagems').html('<p>E-mail já cadastrado.</p>').fadeIn().delay(3000).fadeOut();;
           $('#newsletter').fadeTo('fast', 1);
         }else if (retorno === 'email'){
           $('div.mensagems').html('<p>E-mail inválido.</p>').fadeIn().delay(3000).fadeOut();;
           $('#newsletter').fadeTo('fast', 1);
         }else if (retorno === 'insert'){
           $('div.mensagems').html('<p>Erro ao inserir e-mail.</p>').fadeIn().delay(3000).fadeOut();;
           $('#newsletter').fadeTo('fast', 1);
         } else {
           $('div.mensagems').html('<p>Problema no envio. Por favor tente mais tarde.</p>').fadeIn().delay(3000).fadeOut();;
           $('#newsletter').fadeTo('fast', 1);
         }
       })
    } else {
      alert('Preencha todos os campos.');
    }
    return false;
})

$('#divulgue_imovel').submit(function(){
    var nome = $('#divulgue_imovel input[name=nome]').val();
    var tel = $('#telefone').val();
    var end = $('#endereco').val();
    if (nome!=='' && nome!=='Nome' && tel!=='' && tel!=='Telefone' && end!=='' && end!=='Endereço'){
      $('#divulgue_imovel').fadeTo('fast', 0.5);
       $.post('envia_divulgue',$('#divulgue_imovel').serialize(), function(retorno){
         if (retorno === 'true'){
         } else {
         }
       })
    } else {
      alert('Preencha todos os campos.');
    }
    return false;
})
  
if($.browser.msie){
 
}
  
  
})//finaliza function

// IMICIA FUNÇÃO DE MAPAS
function loadGmapScript()
{
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=showGmap';
    document.body.appendChild(script);
}

function gmap(lat, lng, id)
{
    _lat = lat;
    _lng = lng;
    _aid = id;

    $(document.body).addClass('hidden-scrollbar');
    $("#gmap-info").text('');
    $('#gmap').fadeIn();

    if (typeof google == 'undefined' || typeof google.maps == 'undefined') {
        return loadGmapScript();
    }

    showGmap();
}

function showGmap()
{
    var lat = _lat;
    var lng = _lng;

    var myLatlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true
    };
    
    $('#gmap-inner').css('visibility', 'hidden');
    
    var map = new google.maps.Map(document.getElementById("gmap-inner"), myOptions);

    google.maps.event.addListener(map, 'tilesloaded', function() {
        $('#gmap-inner').css('visibility', 'visible');
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        icon: 'site/images/marker.png'
    });
}

function closeGmap()
{
    $('#gmap-inner').html('');
    $('#gmap').fadeOut();
    $(document.body).removeClass('hidden-scrollbar');
}
