/**
 * @author Luiz Tanure
 */

$(document).ready(function(){

	var hh = $('html').height();

	//remove borda do flash
	theObjects = document.getElementsByTagName("object");
	for (var i = 0; i < theObjects.length; i++) {
		theObjects[i].outerHTML = theObjects[i].outerHTML;
	}

	//abre dialog de vídeo
	$('img.bt_video').click(function(){
		$("div.video").dialog({
			height		: 515,
			width		: 535,
			modal		: true,
			resizable	: false,
			title		: 'Vídeo',
			close		: function(event, ui){
				$(this).dialog( 'destroy' );
			},
			buttons: {"Fechar": function(){ $(this).dialog( 'destroy' ); } }
		});
		return false;
	})

	$('#link_contato_vendas').click(function(){
		var ddialog = $("#contato_vendas").dialog({
			height		: 300,
			width		: 600,
			modal		: true,
			resizable	: false,
			title		: 'Contato de venda',
			close		: function(event, ui){
				$(this).dialog( 'destroy' );
			},
			buttons: {
				"Enviar": function(){
				$("#formVendas").validate();
					var formulario = $('#formVendas');
					if( !formulario.validate().form()) return false;
					var secao = $('#formVendas').attr('name');
					var dados = formulario.serialize();
					$.ajax({
						type: "post",
						url: BASE+'salvar.php',
						data: 'secao='+secao+'&'+dados,
						success: function(msg){
							$(ddialog).dialog( 'destroy' );
							alert(msg)
							return false;
						}
					});
					return false;
				},
				"Cancelar": function(){ $(this).dialog( 'destroy' ); }
			}
		});
		$('#vendasImovel').selectbox();
		return false;
	})

	/* GERAL */
	$('.lightbox').lightBox();
	$('.emp_lightbox').lightBox();

	$('#listaImoveisVenda').selectbox();
	$('#listaImoveisVenda').change(function(){
		var opcao = $("#listaImoveisVenda option:selected").val();
		window.location = BASE+'imovel/'+opcao;
	})


	$('label.checkbox').click(function(){
		var elem = $(this).attr('for');
		$(this).toggleClass('ativo')
	});

	$('label.opcao_enquete').click(function(){
		$('.selecionado').removeClass('selecionado');
		var elem = $(this).attr('for');
		$(this).toggleClass('selecionado')
	});

	$('#enqueteVotar').click(function(){
		var opcao = $("input[@enquete=]:checked").val();

		if(opcao == undefined )
			alert('Selecione uma opção');
		else{
			$.ajax({
				type: "POST",
				url: 'salvar.php',
				data: $('#enquete').serialize(),
				success: function(msg){
					alert(msg)
					return false;
				}
			});
		}

		return false;

	})

	var FORMVALIDATE = $("#form_cadastro").validate();

	var contCadImovel = 0;
	$('#btcadastroCliente').click(function(){
		FORMVALIDATE.resetForm();
		$('.cadApart').show();
		$('#lcadImovel').show();
		$('.cadBloco').show();
		$('#cadApart').addClass('required');
		$('#cadImovel').addClass('required');
		$('#tipo').val('P');

		$("#cadastro").dialog({
			height		: 400,
			width		: 400,
			modal		: true,
			resizable	: false,
			title		: 'Cadastro de Cliente',
			open: function(event, ui) {
				if(contCadImovel == 0){
					$('#cadImovel').selectbox();
					$('.jquery-selectbox').show();
					contCadImovel++;
				} else{
					$('.jquery-selectbox').show();
				}

				},
			close		: function(event, ui){
				$("#cadastro").dialog( 'destroy' );
			},
			buttons: { "Enviar": function(){ SalvaCadastro() }, "Cancelar": function(){ $("#cadastro").dialog( 'destroy' ); } }
		});
		return false;
	})



	$('#btcadastroNaoCliente').click(function(){
		FORMVALIDATE.resetForm();
		$('.cadApart').hide().val('');
		$('.cadImovel').hide().val('');
		$('.cadBloco').hide();
		$('.jquery-selectbox').hide();
		$('#cadApart').removeClass('required');
		$('#cadImovel').removeClass('required');
		$('#cadImovel').hide();
		$('#tipo').val('N');
		$("#cadastro").dialog({
			height		: 400,
			width		: 400,
			modal		: true,
			resizable	: false,
			title		: 'Cadastro',
			close		: function(event, ui){
				$("#cadastro").dialog( 'destroy' );
			},
			buttons: { "Enviar": function(){ SalvaCadastro() }, "Cancelar": function(){ $("#cadastro").dialog( 'destroy' ); } }
		});
		return false;
	})


	$('#btcadastroLanccamento').click(function(){

		FORMVALIDATE.resetForm();
		$('#salvarCadastroAntecipa').hide();
		$("#cadastro_antecipe").dialog({
			height		: 400,
			width		: 400,
			modal		: true,
			resizable	: false,
			title		: 'Cadastro de Lançamento',
			close		: function(event, ui){
				$("#cadastro_antecipe").dialog( 'destroy' );
			},
			buttons: { "Enviar": function(){ SalvaCadastroLancamento() }, "Cancelar": function(){ $("#cadastro_antecipe").dialog( 'destroy' ); } }
		});

		return false;
	})




	$("#form_cadastro").validate();
	/**
	 *
	 * @access public
	 * @return void
	 **/
	function SalvaCadastro(){
		var formulario = $("#form_cadastro");
		if( !formulario.validate().form()) return false;
		var dados = formulario.serialize();
		$.ajax({
			type: "POST",
			url: 'salvar.php',
			data: formulario.serialize(),
			success: function(msg){
				$("#cadastro").dialog( 'destroy' );
				//$("#cadastro").remove();
				alert(msg)
				return false;
			}
		});
		return false;
	}
	function SalvaCadastroLancamento(){
		var formulario = $("#form_atecipe");
		if( !formulario.validate().form()) return false;
		var dados = formulario.serialize();
		$.ajax({
			type: "POST",
			url: 'salvar.php',
			data: formulario.serialize(),
			success: function(msg){
				$("#cadastro").dialog( 'destroy' );
				//$("#cadastro").remove();
				alert(msg)
				return false;
			}
		});
		return false;
	}

	/**
	 * alert
	 *
	 * sobrescreve o alert padrão por um dialog
	 *
	 * @access public
	 * @param string
	 * @return void
	 **/
	function alert(msg){
		var divDialog = '<div id="dialog" class="oculto">'+msg+'</div>';
		$('body').append(divDialog);
		$("#dialog").dialog({
			modal		: true,
			title		: 'Mensagem',
			resizable	: false,
			close		: function(event, ui){
				$("#dialog").dialog( 'destroy' );
				$("#dialog").remove();
			},
			buttons: { "Ok": function(){ $(this).dialog("close"); }}
		});
	}


	/* funções da pagina inicial*/
	var MARGEM_EMPREENDIMENTO = 0;
	var NR_ITENS_EMPREENDIMENTO = $('#inicio_lista_empreendimentos').children('li').size();
	/*exibe empreendimentos do tipo selecionado*/
	$('#empreendimento_menu li').click(function(){
		//remove marcação de menu ativo
		$('#empreendimento_menu li.ativo').removeClass('ativo');
		//menu atual aitvo
		$(this).addClass('ativo');
		//pega o id/classe
		var classe = $(this).attr('id');
		//slide na lista
		MARGEM_EMPREENDIMENTO = 0;
		$('#inicio_lista_empreendimentos').animate({
			marginLeft	: MARGEM_EMPREENDIMENTO
		},500);

		//exibe todos se menu for 'todos'
		if(classe == 'empreendimento_todos'){
			//exibe os items
			$('#inicio_lista_empreendimentos li').animate({width:280},300).animate({opacity:1},200,function(){
				NR_ITENS_EMPREENDIMENTO = $('#inicio_lista_empreendimentos').children('li:visible').size();
			});
			return false;
		}
		//oculta os itens
		$('#inicio_lista_empreendimentos li').not('.'+classe).animate({opacity	: 0},200).animate({width:0},300, function(){
			$(this).css('display', 'none');
			//exibe os itens
			$('#inicio_lista_empreendimentos li.'+classe).css('display', 'block').animate({width:280},300).animate({opacity	: 1},200, function(){
				NR_ITENS_EMPREENDIMENTO = $('#inicio_lista_empreendimentos').children('li:visible').size();
			})
		})
	})

	$('#empreendimento_proximo').click(function(){
		if(MARGEM_EMPREENDIMENTO <= (NR_ITENS_EMPREENDIMENTO-1)*(-280)) return false;
		MARGEM_EMPREENDIMENTO = MARGEM_EMPREENDIMENTO-280;
		$('#inicio_lista_empreendimentos').animate({
			marginLeft	: MARGEM_EMPREENDIMENTO
		},500);
	});
	$('#empreendimento_anterior').click(function(){
		if(MARGEM_EMPREENDIMENTO == 0) return false;
		MARGEM_EMPREENDIMENTO = MARGEM_EMPREENDIMENTO+280;
		$('#inicio_lista_empreendimentos').animate({
			marginLeft	: MARGEM_EMPREENDIMENTO
		},500);
	});

	/* contato */

	$('#contato_menu li a').click(function(){
		//alterna destaque no menu
		$('#contato_menu li.ativo').removeClass('ativo');
		$(this).parent('li') .addClass('ativo');
		//alterna conteudo
		$('#conteudo_aba li.ativo').fadeOut(500, function(){
			$(this).removeClass('ativo');
			var aba = $('#contato_menu li.ativo a').attr('href');

			$(aba).fadeIn(300, function(){
				$(aba).children('form').children('fieldset').children('div').children('select').selectbox();
				//$('#vendasImovel').selectbox();
				//$('#sacImovel').selectbox();
				//$('#trabalheArea').selectbox();
			}).addClass('ativo') ;
			//troca titulo
			$('#conteudo_contato_titulo').removeAttr('class').addClass(aba.replace('#',''))
		})
		return false;
	})



	$('.telefone').mask("(99) 9999-9999");


	/* valida formulario*/
	$(".formulario").validate();
	$('.enviar').click(function(){

		var formulario = $(this).parent('fieldset').parent('form');
		if( !formulario.validate().form()) return false;
		var secao = $(this).parent('fieldset').parent('form').attr('name');
		var dados = formulario.serialize();
		$.ajax({
			type: "post",
			url: 'salvar.php',
			data: 'secao='+secao+'&'+dados,
			success: function(msg){
				alert(msg)
				clear_form_elements(formulario);
				return false;
			}
		});
		return false;

	});






	/* Portfolio*/

	$('#institucional_menu li a').click(function(){
		//alterna destaque no menu
		$('#institucional_menu li.ativo').removeClass('ativo');
		$(this).parent('li') .addClass('ativo');
		//alterna conteudo
		$('#conteudo_aba li.ativo').fadeOut(500, function(){
			$(this).removeClass('ativo');
			var aba = $('#institucional_menu li.ativo a').attr('href');

			$(aba).fadeIn().addClass('ativo') ;
			//troca titulo
			$('#conteudo_institucional_titulo').removeAttr('class').addClass(aba.replace('#',''))
		})
		return false;
	})
	/* imovel*/

	$('#imovel_menu li a').click(function(){
		//alterna destaque no menu
		$('#imovel_menu li.ativo').removeClass('ativo');
		$(this).parent('li') .addClass('ativo');
		//alterna conteudo
		$('#conteudo_aba li.ativo').fadeOut(500, function(){
			$(this).removeClass('ativo');
			var aba = $('#imovel_menu li.ativo a').attr('href');

			$(aba).fadeIn().addClass('ativo') ;
			//troca titulo
			$('#conteudo_imovel_titulo').removeAttr('class').addClass(aba.replace('#',''))
		})
		return false;
	})

	$('#imovel_lista_imagens li a').lightBox();
	var MARGEM_IMOVEL = 0;
	var NR_ITENS_IMOVEl = $('#imovel_lista_imagens').children('li').size();
	$('#imovel_proximo').click(function(){
		if(MARGEM_IMOVEL <= (NR_ITENS_IMOVEl-2)*(-188)) return false;
		MARGEM_IMOVEL = MARGEM_IMOVEL-188;
		$('#imovel_lista_imagens').animate({
			marginLeft	: MARGEM_IMOVEL
		},500);
	});
	$('#imovel_anterior').click(function(){
		if(MARGEM_IMOVEL == 0) return false;
		MARGEM_IMOVEL = MARGEM_IMOVEL+188;
		$('#imovel_lista_imagens').animate({
			marginLeft	: MARGEM_IMOVEL
		},500);
	});

	$('#area_lista_imagens li a').lightBox();
	var MARGEM_area = 0;
	var NR_ITENS_area = $('#area_lista_imagens').children('li').size();
	$('#area_proximo').click(function(){
		if(MARGEM_area <= (NR_ITENS_area-2)*(-188)) return false;
		MARGEM_area = MARGEM_area-188;
		$('#area_lista_imagens').animate({
			marginLeft	: MARGEM_area
		},500);
	});
	$('#area_anterior').click(function(){
		if(MARGEM_area == 0) return false;
		MARGEM_area = MARGEM_area+188;
		$('#area_lista_imagens').animate({
			marginLeft	: MARGEM_area
		},500);
	});

	/* Portfolio*/

	$('#portfolio_imperator a').lightBox();
	$('#portfolio_item_benessere a').lightBox();
	$('#portfolio_item_chateau_de_villandry a').lightBox();
	$('#portfolio_item_la_forme a').lightBox();

	var MARGEM_PORTFOLIO = 0;
	var PAGINA_PORTFOLIO = 1;
	$('#portfolio_anterior').click(function(){
		if(PAGINA_PORTFOLIO == 1) return false;
		if(PAGINA_PORTFOLIO-1 == 1)
			$('#portfolio_anterior').fadeOut();
		PAGINA_PORTFOLIO--;
		$('#portfolio_proxima').fadeIn();
		MARGEM_PORTFOLIO = MARGEM_PORTFOLIO + 600;
		$('#lista_portfolio').animate({
			marginLeft: MARGEM_PORTFOLIO
		})
	})
	$('#portfolio_proxima').click(function(){
		if(PAGINA_PORTFOLIO == $('.portfolio_pagina').size())return false;
		if(PAGINA_PORTFOLIO+1 == $('.portfolio_pagina').size())
			$('#portfolio_proxima').fadeOut();
		PAGINA_PORTFOLIO++;
		$('#portfolio_anterior').fadeIn();
		MARGEM_PORTFOLIO = MARGEM_PORTFOLIO - 600;
		$('#lista_portfolio').animate({
			marginLeft: MARGEM_PORTFOLIO
		})
	})
	/* Portfolio fim*/
	/* Acompanhe*/
	$('#tabela_acompanhe tr td.percent').each(function(){
		var percent = parseInt($(this).text().replace('%',''));
		var hp = percent+'%';
		var tp = (-percent)+'%';
		$(this).append('<span title="'+percent+'%" />');
		$(this).children('span').animate({
			height		: hp,
			marginTop	: tp
		},1000, function(){
			if($.browser.msie && parseInt(jQuery.browser.version.substr(0,3)) < 7){

				if(percent == 0){
					$(this).hide();
				} else if(percent <= 10){
					$(this).height(percent-30);
					$(this).css('margin-top', -percent-5)
				} else {
					$(this).height(percent-10);
				}

			}
		})

	})
	/* Acompanhe fim*/





});

function clear_form_elements(ele) {

    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}