$(function(){


	//eraseCookie('comparar');

	habitare_add_tag();
	habitare_mais_imoveis();
	habitare_mais_imoveis_tag();
    habitare_mais_imoveis_personalizacao();
	habitare_mais_noticias();
	habitare_mais_blog();
	habitare_add_comparacao();
	habitare_remove_comparacao();
	habitare_form_agende_visita();
	//CAROUSEL HOME
	habitare_carousel_home();
	//$(window).resize( habitare_carousel_home());

	habitare_add_escolhas();
	habitare_remove_escolhas();

	area_do_cliente_visite_a_obra();
	habitare_mais_videos();
	//Mascara genérica dos forms.
	$('.tel' ,'form').mask('(99)9999-9999');
	$('.date','form').mask('99/99/9999');
	$('.cpf' ,'form').mask('999.999.999-99');
	$('.cep' ,'form').mask('99.999-999');



	$("#search_query").focus(function(){
		if(this.value == "Encontre seu imóvel"){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = "Encontre seu imóvel";
		}
	});

	//ZOOM IMOVEL
	if( $("#foto-imovel a").length ){
	    /*
		$("#foto-imovel a").jqzoom({
	    	zoomWidth: 374,
	        zoomHeight: 500,
	        xOffset: 32,
	        title: false,
	        lens:false
	    });
		*/
        $(".cloud-zoom").CloudZoom({
        	zoomWidth: '374',
	        zoomHeight: '500',
	        position: 'right',
	        adjustX: 32
        });
	}



	//ACCORDION
	$('.accordion dd').addClass('hide'); //para todo o site

	$('#filtros .accordion dd').removeClass('hide'); //abre todos os filtros
	$('#filtros .accordion dd').addClass('show'); //só para os filtros
	$('#filtros .accordion dt').addClass('ativo'); //ativa todos os filtros

	$('#result .accordion dd').removeClass('hide'); //abre todos os filtros
	$('#result .accordion dd').addClass('show'); //só para os filtros
	$('#result .accordion dt').addClass('ativo'); //ativa todos os filtros

	$('.accordion dt').click(function(){

		var $el = $(this),
			$dd = $(this).next('dd'),
			$visible = $('.accordion dt:visible'),
			$ativo = $el.siblings('.ativo'),
			type = $el.parent().attr('rel');

		if(!$el.hasClass('ativo') ){
			if(type != "hideshow") {
				$visible.removeClass('ativo').next().slideUp('fast');
			}
			$el.addClass('ativo').next().slideDown('fast');

		}else{
			$el.removeClass('ativo').next().slideUp('fast');
		}

	}).filter(':first').click();

	$('#filtros .accordion dt').filter(':first').click();
	$('#result .accordion dt').filter(':first').click();

	//FILTROS
	$('#filtros input:checkbox').click(function(){
		if($(this).is(':checked')){
			$(this).parent().addClass('checked');
		} else {
			$(this).parent().removeClass('checked');
		}
	});

	//IMPRESSAO E COMPARTILHAMENTO
	if($("#menu-conteudo").length){
		$("#m-compartilhar a").click(function() {
			$("#share-this").toggleClass('hide');
			return false;
		});
	}

	//ATENDIMENTO ONLINE
	/*
	if($("#u-atendimento-online").length){
			$("#u-atendimento-online > span").click(function() {
				$(this).next('ul').toggleClass('hide');
				return false;
			});
		}
	*/

	/*if($("#canal-atendimento").length){
		$("#canal-atendimento a").click(function() {
			$(this).next('.overlay').animate({
				opacity: 1
			}).css('display', 'block');
			return false;
		});
		$("#canal-atendimento  .btn-close").click(function() {
			$(this).parent().animate({
				opacity: 0
			}, function(){
				$(this).css('display', 'none');
			});
		});
	}*/


	//ALERTA PARA VISITAS
	if($("#box-alerta").length > 0 ){
		$("#lista-dicas li:first").css({
			width: '255px'
		}).addClass('ativo');

		$("#lista-dicas li").hover(function(){
			var $el = $(this);
			$el.siblings().stop().animate({
				width: '34px'
			}).removeClass('ativo');

			$el.stop().animate({
				width: '255px'
			}).addClass('ativo');
		});
	}


	//LiGHTBOX
	var prettyMarkup = '<div class="pp_pic_holder"> \
						<a class="pp_close" href="#">Close</a> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">next</a> \
											<a class="pp_previous" href="#">previous</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details clearfix"> \
											<p class="pp_description"></p> \
											<div class="pp_nav" style="display:block"> \
												<a href="#" class="pp_expand" title="Expandir a imagem">Expand</a> \
												<a href="#" class="pp_arrow_previous">Previous</a> \
												<p class="currentTextHolder">0/0</p> \
												<a href="#" class="pp_arrow_next">Next</a> \
											</div> \
										</div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>';

	if($(".single").length > 0) {
        $(".hentry .size-thumbnail").parent().prettyPhoto({
        	allow_resize: false,
        	slideshow:5000,
			autoplay_slideshow: false,
        	markup: prettyMarkup
      });
    }
	$("a[rel=prettyPhotoLive]").live("click",function() {
		$.prettyPhoto.open($(this).attr("href"),"","");
		return false;
	});
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed:'slow',
		allow_resize: true,
		slideshow:5000,
		autoplay_slideshow: false,
		show_title: false,
		markup: prettyMarkup,
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no" scrolling="yes"></iframe>'
	});


	//TABS
	$(".t-nav li:eq(0)").addClass('ativo');
	$(".t-content .tab").not(':eq(0)').addClass('hide');
	$(".t-nav li a").click(function(){
		var $this = $(this),
		$href = this.hash,
		$tab = $($href),
		$currentTab = $(".t-content .tab:visible");

		$(".t-nav li").removeClass('ativo');
		$this.parent().addClass('ativo');

		$currentTab.addClass('hide');
		$tab.removeClass('hide');

		return false;
	});

	habitare_vagas_disponiveis_click();

/*	//TWITTER
	$("#twitt").getTwitter({
		userName: "habitarebh",
		numTweets: 1,
		loaderText: "Siga a @habitarebh no twitter!",
		showHeading: false,
		showProfileLink: false,
		showTimestamp: false
	});
	*/

       // Retirada de premios

       $('.premios li div input').click(function(){

            $("#premioForm").show();

            $('#rp_premio').val( $(this).parent().find('h3').text() + ' - ' + $(this).parent().find('p').text() );

            $('html, body').animate({
                scrollTop: $("#premioForm").offset().top
            }, 500);

       });

       $('.premios li div img[title]').tooltip();

	   //Fechar o aviso
        $(".fechar-alert").click(function(){

            createCookie('popup','closed',30);

            $(this).parent().slideUp();

            return false;

        });

});

function log() {
	if (window.console && window.console.log)
		window.console.log('[habitare] ' + Array.prototype.join.call(arguments,' '));
};


function habitare_carousel_home()
{

	var $flip = $("#flip");
	var screen_width = 	$(document).width();
	$flip.css('width',screen_width);
	var visible_itens = screen_width / 316 ;
	 $flip.jCarouselLite({
	        btnNext: "#s-next",
	        btnPrev: "#s-prev",
	        visible: visible_itens
	 	});
}

function habitare_add_escolhas()
{
	$btn_escolhas = $('#btn-adicionar-escolhas');
	var cookie = null;
	$btn_escolhas.click(function(){
		var imovel_id = this.hash.replace('#','');

		if(imovel_id.length){
			cookie = concatCookie( imovel_id, 'minhas_escolhas' , '||', 999 );
			createCookie('minhas_escolhas',cookie,30);
			//Muda o status do botão.
			$btn_escolhas.attr('href','#');
			$btn_escolhas.toggleClass('ativo');
		}
		return false;
	});
}

function habitare_remove_escolhas()
{
	$('.bt-remover','#imoveis').live('click',function(){
		var imovel_id = this.hash.replace('#','');

		if(imovel_id){
			var cookie = readCookie('minhas_escolhas');

			var counter_posts = $('.hproduct','#imoveis').length;

			if(cookie){
				cookie = cookie.replace(imovel_id,'');
				cookie = clearCookie(cookie,'||');

				createCookie('minhas_escolhas',cookie,30);
				$('#imovel-'+imovel_id).fadeOut('slow',function(){
					$('#imovel-'+imovel_id).remove();
					$('#counter_posts').val(counter_posts-1);

				});
			}
		}

		if(readCookie('minhas_escolhas') == '' ){
			eraseCookie('minhas_escolhas');
		}

		return false;
	});
}


function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}

//eraseCookie('comparar');
function concatCookie( value, cookie , concat , limit )
{
	var temp = readCookie(cookie);
	temp = clearCookie(temp,concat);
	var in_cookie = false;
	var key = 0;

	if( temp  ){
		//O cookie já existe.

		//Verificar se o VALUE não está no cookie.
		var lista_id_imoveis = temp.split(concat);
		for(key in lista_id_imoveis){
			if(value == lista_id_imoveis[key]){
				in_cookie = true;
			}
		}

		if(!in_cookie && lista_id_imoveis.length < limit){
			temp = temp+concat+value;
		}

		var lista_imoveis = temp;
	}else{
		var lista_imoveis = value;
	}
	return lista_imoveis;
}

function clearCookie(cookie,concat)
{
	var result = '';
	if(cookie){
		var list = cookie.split(concat);
		for(i in list){
			if(typeof( list[i] )=="undefined"){
				//log(list[i]+'== undefined');
			}else if(list[i] == null ){
				//log(list[i]+'== null');
			}else if(list[i] == ''){
				//log(list[i]+'== empty');
			}else{
				result = result+list[i]+concat;
			}
		}

		//retira o concat do final da string;
		var to = result.length - (concat.length);
		result = result.substring(0,to);
	}
	return result;
}

function habitare_add_comparacao()
{
	$btn_comparacao = $('#btn-adicionar-comparacao');
	var imoveis_adicionados = $('li','#div-comparar').length;

	$btn_comparacao.click(function(){
		var imovel_id = this.hash.replace('#','');

		//Muda o status do botão.
		$btn_comparacao.attr('href','#');

		if(imovel_id.length){

			$btn_comparacao.toggleClass('ativo');

			comparar = concatCookie( imovel_id, 'comparar', '||', 5 );
			createCookie('comparar',comparar,30);

			var top =  ( $('#box-comparar').position().top + 150);

			$('html').animate({scrollTop: top }, 1000, function(){

				var $divComparar = $('#div-comparar');
				var $imovel = $('#comparar-imovel-'+imovel_id);

				//Se o imovel já estiver na lista, apenas dá um destaque.
				if( $imovel.length ){
					$imovel.addClass('current');

				}else{
					//Insere o imovel à lista;
					imoveis_adicionados  = imoveis_adicionados+1;
					var imovel_title     = $('#imovel_title').val();
					var imovel_permalink = $('#imovel_permalink').val();

					//Se na lista tiver menos que 5
					if(imoveis_adicionados <= 5){

						var html = '<li id="comparar-imovel-'+imovel_id+'" class="current"><a title="'+imovel_title+'" href="'+imovel_permalink+'">'+imovel_title+'</a></li>';

						$divComparar.children('ul').append(html);
					}else if(imoveis_adicionados==5){
						$('#txt-adicionar-outros').remove();
					}
					$('#div-comparar-empty').remove();

				}

				//Adiciona o botao de ir para pagina de comparação.
				if( imoveis_adicionados > 1 && $('#btn-comparar').length == 0 ){
					$('#div-comparar').append( '<a href="/imoveis" id="txt-adicionar-outros">adicionar outros</a><a id="btn-comparar" class="btn" href="/comparar/" title="Clique aqui e compare os imoveis selecionados.">Comparar</a>');
					$('#txt-adicionar-comparacao').remove();

				}else if( imoveis_adicionados == 1 && $('#txt-adicionar-comparacao').length == 0 ){
					//Texto que informa o mínimo de imoveis a serem comparados.
					$('#div-comparar').append('<span id="txt-adicionar-comparacao">Adicione pelo menos mais um <a href="/imoveis/">imóvel</a> para poder compará-los.</span>');

				}else if( imoveis_adicionados >= 5 ){
					//Texto que informa o limite maximo de imoveis a serem comparados.
					$('#txt-adicionar-comparacao').remove();
					$('#btn-comparar').before('<span id="txt-adicionar-comparacao">Você pode comparar no máximo 5 imóveis.</span>');
					$('#txt-adicionar-outros').remove();

				}


			});//End Animate
		}
		return false;
	});//End Click
}

function habitare_remove_comparacao()
{
	$('.remove_comparacao','#lista-imoveis').click(function(){
		var imovel_id = this.hash.replace('#','');
		if(imovel_id){
			var cookie = readCookie('comparar');
			if(cookie){

				//var list_cookies = cookie.split('||');


				cookie = cookie.replace(imovel_id,'');
				cookie = clearCookie(cookie,'||');
				createCookie('comparar',cookie,30);
				$('#imovel-'+imovel_id).fadeOut();
				$('#imovel-'+imovel_id).remove();

				var counter = $('.imoveis','#itens').length;

				var html_adicione_mais = '<li class="imoveis lista-item mais-item"><p>Adicione mais um <a href="/imoveis">imóvel</a> para poder compará-los.</p></li>';

				if( counter == 1 ){
					$('#itens').append(html_adicione_mais);
				}else if( counter == 0){
					$('#itens').append(html_adicione_mais);
				}

				habitare_comparar_resize();
			}
		}

		if(readCookie('comparar') == '' ){
			eraseCookie('comparar');
		}

		return false;
	})
}
//createCookie('minhas_escolhas','94||96||98||101||106||133||131||129||109',30);

function habitare_add_tag()
{
	$('#addTag').submit(function(){
		var tag = $('#tag').val(),
			post_id = $('#post_id').val();
		if(tag && post_id){
			$.ajax({
				type: 'POST',
				url: AJAX,
				data: 'action=habitare_tags&post_id='+post_id+'&tags='+tag,
				success: function( response ){
					var response = eval( "(" + response + ")" );
					if(response.status == 'success'){
						$('#tag_response').text('Você adicionou '+response.total+ ' tag(s) ao imóvel');
						$('#tag').val('');
					}
				}
			});
		}
	});
}


/*
 * Mais Imoveis
 * @desc Paginação ajax dos imoveis cadastrados.
 *
 */
function habitare_mais_imoveis()
{
	var $btn = $('#btn-mais-empreendimentos a');
	if( $btn.length )
	{
		$btn.click(function(){
			var paged = this.hash.replace('#','');
			$btn.attr('href', '#');
			if(paged){
				var $counter_posts = $('#counter_posts');
				var pre_posts = $('#pre_posts').val();
				var site_page = $('#site_page').val();
				var posts_per_page = $('#posts_per_page').val();
				var $loading = $(this).siblings('.loading');

				$loading.removeClass('hide');
				$.ajax({
					type: 'POST',
					url: AJAX,
					data: 'action=habitare_mais_imoveis&paged='+paged+'&pre_posts='+pre_posts+'&counter_posts='+$counter_posts.val()+'&site_page='+site_page+'&posts_per_page='+posts_per_page,
					success: function( response ){
						var response = eval( "(" + response + ")" );
						if(response.status == 'success'){
							$('#imoveis').append(response.html);
							$counter_posts.val(response.counter_posts);
							if( response.paged == '0' )
								$btn.remove();
							else
								$btn.attr('href', '#'+response.paged);
						}
						$loading.addClass('hide');
					}
				});
			}//END if(paged);
		return false;

		});


	}
}


/*
 * Mais Imoveis com Tag
 * @desc Paginação ajax dos imoveis cadastrados com navegação pelas tags.
 *
 */
function habitare_mais_imoveis_tag()
{
	var $btn = $('#btn-mais-empreendimentos-tag a');
	if( $btn.length )
	{
		$btn.click(function(){
			var paged = this.hash.replace('#','');
			$btn.attr('href', '#');
			if(paged){
				var $counter_posts = $('#counter_posts');
				var site_page = $('#site_page').val();
				var tag_slug = $('#tag_slug').val();
				var posts_per_page = $('#posts_per_page').val();
				var $loading = $(this).siblings('.loading');

				$loading.removeClass('hide');
				$.ajax({
					type: 'POST',
					url: AJAX,
					data: 'action=habitare_mais_imoveis_tag&paged='+paged+'&counter_posts='+$counter_posts.val()+'&site_page='+site_page+'&posts_per_page='+posts_per_page+'&tag_slug='+tag_slug,
					success: function( response ){
						var response = eval( "(" + response + ")" );
						if(response.status == 'success'){
							$('#imoveis').append(response.html);
							$counter_posts.val(response.counter_posts);
							if( response.paged == '0' )
								$btn.remove();
							else
								$btn.attr('href', '#'+response.paged);
						}
						$loading.addClass('hide');
					}
				});
			}//END if(paged);
		return false;

		});


	}
}


/*
 * Mais Notícias
 * @desc Paginação ajax das notícias.
 *
 */
function habitare_mais_noticias()
{
	var $btn = $('#btn-mais-noticias a');

	if( $btn.length )
	{
		$btn.click(function(){

			var paged = this.hash.replace('#','');

            $btn = $(this);

            $btn.attr('href','#')

            if(paged){

				var $term_slug 	   = $('#term_slug');

				var $loading = $(this).siblings('.loading');

				if($term_slug.val()){

                    var $counter_posts = $('#counter_posts');

					$loading.removeClass('hide');
					$.ajax({
						type: 'POST',
						url: AJAX,
						data: 'action=habitare_mais_noticias&paged='+paged+'&counter_posts='+$counter_posts.val()+'&term_slug='+$term_slug.val(),
						success: function( response ){
							var response = eval( "(" + response + ")" );
							if(response.status == 'success'){
								$('#noticias').append(response.html);
								$counter_posts.val(response.counter_posts);
								if( response.paged == '0' )
									$btn.remove();
								else
									$btn.attr('href', '#'+response.paged);
							}
							$loading.addClass('hide');
						}
					});

				}else{

                    var $ano = $( 'input.ano' , $(this).parent() ).val(),
                        $per_page = $('#per_page').val(),
                        $counter_posts = $('#counter_posts_'+$ano);

					$loading.removeClass('hide');
					$.ajax({
						type: 'POST',
						url: AJAX,
						data: 'action=habitare_mais_noticias&paged='+paged+'&counter_posts='+$counter_posts.val()+'&ano='+$ano+'&per_page='+$per_page,
						success: function( response ){
							var response = eval( "(" + response + ")" );
							if(response.status == 'success'){

                                $('div.ano_' + $ano ).append(response.html);

								$counter_posts.val( response.counter_posts );

								if( response.paged == '0' )
									$btn.remove();
								else
									$btn.attr('href', '#'+response.paged);
							}
							$loading.addClass('hide');
						}
					});
			}
		}//END if(paged);
		return false;
		});
	}
}


/*
 * Mais blog
 * @desc Paginação ajax das notícias do blog.
 *
 */
function habitare_mais_blog()
{
	var $btn = $('#btn-mais-blog a');
	if( $btn.length )
	{
		$btn.click(function(){
			var paged = this.hash.replace('#','');
			$btn.attr('href','#');
			if(paged){
				var $counter_posts = $('#counter_posts');
				var $categoria_id 	   = $('#categoria_id');
				var $loading = $(this).siblings('.loading');

				if($categoria_id.val()){
					$loading.removeClass('hide');
					$.ajax({
						type: 'POST',
						url: AJAX,
						data: 'action=habitare_mais_blog&paged='+paged+'&counter_posts='+$counter_posts.val()+'&categoria_id='+$categoria_id.val(),
						success: function( response ){
							var response = eval( "(" + response + ")" );
							if(response.status == 'success'){
								$('#noticias').append(response.html);
								$counter_posts.val(response.counter_posts);
								if( response.paged == '0' )
									$btn.remove();
								else
									$btn.attr('href', '#'+response.paged);
							}
							$loading.addClass('hide');
						}
					});
				}
				else{
					$loading.removeClass('hide');
					$.ajax({
						type: 'POST',
						url: AJAX,
						data: 'action=habitare_mais_blog&paged='+paged+'&counter_posts='+$counter_posts.val(),
						success: function( response ){
							var response = eval( "(" + response + ")" );
							if(response.status == 'success'){
								$('#noticias').append(response.html);
								$counter_posts.val(response.counter_posts);
								if( response.paged == '0' )
									$btn.remove();
								else
									$btn.attr('href', '#'+response.paged);
							}
							$loading.addClass('hide');
						}
					});
				}
		}//END if(paged);
		return false;
		});
	}
}

function habitare_form_agende_visita()
{
	$('#a_data').change(function(){
		var data = $('#a_data :selected').text();
		var imovel_id = $('#a_imovel').val();
		$.ajax({
			type: 'POST',
			url: AJAX,
			data: 'action=habitare_form_agende_horarios&data='+data+'&imovel_id='+imovel_id,
			success: function( response ){
				var response = eval( "(" + response + ")" );
				if(response.status == 'success'){
					$('#a_hora').html(response.html);
				}
			}
		});

	})
}

function habitare_vagas_disponiveis_click()
{
	$('.link-vaga','#vagas').click(function(){

		var va_id = this.hash.replace('#','');
		$(".t-nav li").toggleClass('ativo');

		$('#tab-1').toggleClass('hide');
		$('#tab-2').toggleClass('hide');
		$('#t_nome').focus();
		$('#t_vaga').val(va_id);

		return false;
	});
}

function area_do_cliente_visite_a_obra()
{
	$("#f_loja",'#area-cliente-visite-a-obra').change(function() {
	    if ($(this).val()) {
	       window.location.href = $(this).val();
	    }
	});
}


/*
 * Create Cookie
 * @desc Cria Cookies
 *
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/*
 * Read Cookie
 * @desc Lê Cookies
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
/*
 * Erase Cookie
 * @desc Exclui Cookies
 */
function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*
 * Mais Videos
 * @desc Paginação ajax dos videos do Youtube.
 *
 */
function habitare_mais_videos()
{
	var $btn = $('#btn-ajax-load a');

	if( $btn.length )
	{
		$btn.click(function(){
			var paged = this.hash.replace('#','');
			$btn.attr('href', '#');
			if(paged){

				var $counter_posts = $('#counter_posts');
				var site_page = $('#site_page').val();
				var posts_per_page = $('#posts_per_page').val();
				var $loading = $(this).siblings('.loading');

				$loading.removeClass('hide');
				$.ajax({
					type: 'POST',
					url: AJAX,
					data: 'action=habitare_mais_videos&paged='+paged+'&site_page='+site_page+'&posts_per_page='+posts_per_page+'&counter_posts='+$counter_posts.val(),
					success: function( response ){
						var response = eval( "(" + response + ")" );
						if(response.status == 'success'){
							$('#videos').append(response.html);
							$counter_posts.val(response.counter_posts);
							if( response.paged == '0' )
								$btn.remove();
							else
								$btn.attr('href', '#'+response.paged);
						}
						$loading.addClass('hide');
					}
				});
			}//END if(paged);
		return false;

		});


	}
}

/*
 * Mais Imoveis
 * @desc Paginação ajax dos imoveis cadastrados.
 *
 */
function habitare_mais_imoveis_personalizacao()
{
	var $btn = $('#btn-mais-empreendimentos-pers a');
	if( $btn.length )
	{
		$btn.click(function(){
			var paged = this.hash.replace('#','');
			$btn.attr('href', '#');
			if(paged){
				var $counter_posts = $('#counter_posts');
				var site_page = $('#site_page').val();
				var posts_per_page = $('#posts_per_page').val();
				var $loading = $(this).siblings('.loading');

				$loading.removeClass('hide');
				$.ajax({
					type: 'POST',
					url: AJAX,
					data: 'action=habitare_mais_imoveis_personalizacao&paged='+paged+'&counter_posts='+$counter_posts.val()+'&site_page='+site_page+'&posts_per_page='+posts_per_page,
					success: function( response ){

						var response = eval( "(" + response + ")" );
						if(response.status == 'success'){
							$('#imoveis').append(response.html);
							$counter_posts.val(response.counter_posts);
							if( response.paged == '0' )
								$btn.remove();
							else
								$btn.attr('href', '#'+response.paged);
						}
						$loading.addClass('hide');
					}
				});
			}//END if(paged);
		return false;

		});


	}
}

