window.addEvent('domready', function() {

	$('busca_vendas').addEvent('change', function() {

		var values = [
			'20.000,00',
			'40.000,00',
			'60.000,00',
			'80.000,00',
			'100.000,00',
			'150.000,00',
			'200.000,00',
			'250.000,00',
			'300.000,00',
			'400.000,00',
			'500.000,00'
		]

		vi = $('valor_inicial');
		vi.empty();
		vi.adopt(new Element('option', {value: '0,00'}).setHTML('0,00'));
		values.each(function(v) {
			vi.adopt(new Element('option', {value: String.replace(v, '.', '').replace(',', '')}).setHTML(v));
		});

		vf = $('valor_final');
		vf.empty();
		values.each(function(v) {
			vf.adopt(new Element('option', {value: String.replace(v, '.', '').replace(',', '')}).setHTML(v));
		});
		vf.adopt(new Element('option', {selected: true, value: ''}).setHTML('Sem limite'));

		$('detalhe_cidade').empty();
		$('detalhe_cidade').adopt(new Element('option', {value: ''}).setHTML('Carregando...'));
		$('detalhe_cidade').setProperty('disabled', true);

		$('detalhe_tipo').empty();
		$('detalhe_tipo').adopt(new Element('option', {value: ''}).setHTML('Carregando...'));
		$('detalhe_tipo').setProperty('disabled', true);

		if ($defined($('bairro'))) {
			$('bairro').empty();
			$('bairro').adopt(new Element('option', {value: ''}).setHTML('Carregando...'));
			$('bairro').setProperty('disabled', true);
		}

		new Ajax('/imovel/cidade', {
			method: 'post',
			data: 'db=venben',
			onComplete: function (req) {
				var cidades = Json.evaluate(req);
				$('detalhe_cidade').setProperty('disabled', false);
				$('detalhe_cidade').empty();

				$('detalhe_cidade').adopt(new Element('option', {value: ''}).setHTML('Todas...'));
				cidades.each(function(cidade) {
					$('detalhe_cidade').adopt(new Element('option', {value: cidade.IMOVEIS.Id}).setHTML(cidade.IMOVEIS.Cidade));
				});
				$('detalhe_cidade').fireEvent('change');

			}

		}).request();
	});

	$('busca_locacao').addEvent('change', function() {
		var values = [
			'200,00',
			'300,00',
			'400,00',
			'500,00',
			'600,00',
			'700,00',
			'800,00',
			'900,00',
			'1000,00',
			'2000,00',
			'3000,00',
			'4000,00'
		]

		vi = $('valor_inicial')
		vi.empty();
		vi.adopt(new Element('option', {value: '0,00'}).setHTML('0,00'));
		values.each(function(v) {
			vi.adopt(new Element('option', {value: String.replace(v, ',', '')}).setHTML(v));
		});

		vf = $('valor_final');
		vf.empty();
		values.each(function(v) {
			vf.adopt(new Element('option', {value: String.replace(v, ',', '')}).setHTML(v));
		});
		vf.adopt(new Element('option', {selected: true, value: ''}).setHTML('Sem limite'));

		$('detalhe_cidade').empty();
		$('detalhe_cidade').adopt(new Element('option', {value: ''}).setHTML('Carregando...'));
		$('detalhe_cidade').setProperty('disabled', true);

		$('detalhe_tipo').empty();
		$('detalhe_tipo').adopt(new Element('option', {value: ''}).setHTML('Carregando...'));
		$('detalhe_tipo').setProperty('disabled', true);

		if ($defined($('bairro'))) {
			$('bairro').empty();
			$('bairro').adopt(new Element('option', {value: ''}).setHTML('Carregando...'));
			$('bairro').setProperty('disabled', true);
		}

		new Ajax('/imovel/cidade', {
			method: 'post',
			data: 'db=visben',
			onComplete: function (req) {
				var cidades = Json.evaluate(req);
				$('detalhe_cidade').setProperty('disabled', false);
				$('detalhe_cidade').empty();

				$('detalhe_cidade').adopt(new Element('option', {value: ''}).setHTML('Todas...'));
				cidades.each(function(cidade) {
					$('detalhe_cidade').adopt(new Element('option', {value: cidade.IMOVEIS.IDCIDADE}).setHTML(cidade.IMOVEIS.NOMECIDADE));
				});
				$('detalhe_cidade').fireEvent('change');
			}

		}).request();
	});

	// Efeitos de mousehover
	$$('div.icones dl dt a').each(function(el) {
		el.addEvent('mouseenter', function(e) {
			var fxFade = new Fx.Style(el, 'opacity', {duration:200});
			fxFade.start(1, 0.7);
		});
		el.addEvent('mouseleave', function(e) {
			var fxFade = new Fx.Style(el, 'opacity', {duration:200});
			fxFade.start(0.7, 1);
		});
	});

	// Navegacao do scroller (imoveis)
	window.scrollImovelTo = function(direction) {
		var ul = $E('#thumbs div.scroll ul');
		var pos = ul.getStyle('margin-left').toInt();
		if ((direction == 'left') && (pos <= -20)) {
			ul.setStyle('margin-left', pos + 20);
		} else if ((direction == 'right') && (pos >= -420)) {
			ul.setStyle('margin-left', pos - 20);
		}
	};

	var nav_left = $E('#thumbs div.nav_left');
	var nav_right = $E('#thumbs div.nav_right');

	if ($defined(nav_left)) {
		nav_left.addEvent('mouseenter', function(e) {
			window.scrollImovel = scrollImovelTo.periodical(50, window, 'left');
		});
		nav_left.addEvent('mouseleave', function(e) {
			$clear(window.scrollImovel);
		});
	}

	if ($defined(nav_right)) {
		nav_right.addEvent('mouseenter', function(e) {
			window.scrollImovel = scrollImovelTo.periodical(50, window, 'right');
		});
		nav_right.addEvent('mouseleave', function(e) {
			$clear(window.scrollImovel);
		});
	}

	// Accordion
	window.boxFx = false;
	$$('div.multi h4').each(function(el) {

		var fixed = el.getNext();
		var sub = fixed.getNext();

		if (fixed.hasClass('selected')) fixed.setStyle('width', 355);

		el.addEvent('click', function(e) {
			if (window.boxFx) return;
			var sel = $E('div.multi div.selected');
			if (sel == el.getNext()) return;
			var selChange = new Fx.Style(sel, 'width', {
				duration: 500,
				onComplete: function(sel) {
					sel.removeClass('selected');
					window.boxFx = false;
				}
			});
			selChange.start(355, 0);

			var clickChange = new Fx.Style(el.getNext(), 'width', {
				duration: 500,
				onStart: function(cel) {
					cel.setStyle('width', 0).addClass('selected');
					window.boxFx = true;
				}
			});
			clickChange.start(0, 355);

		}, this);
	});

	var tipImoveis = new Tips($$('#thumbs ul li a'), {
		showDelay: 400,
		hideDelay: 400
	});

	/* Ajax */
	$('detalhe_cidade').addEvent('change', function() {
		var db = $('busca_vendas').getProperty('checked') ? 'venben' : 'visben';

		new Ajax('/imovel/tipo', {
			method: 'post',
			data: 'cidade=' + $('detalhe_cidade').getProperty('value') + '&db=' + db,
			onComplete: function (req) {
				var tipos = Json.evaluate(req);
				$('detalhe_tipo').setProperty('disabled', false);
				$('detalhe_tipo').empty();

				$('detalhe_tipo').adopt(new Element('option', {value: ''}).setHTML('Qualquer tipo...'));
				tipos.each(function(tipo) {
					$('detalhe_tipo').adopt(new Element('option', {value: tipo.IMOVEIS.Id_Tipo}).setHTML(tipo.IMOVEIS.Tipo));
				});
			}

		}).request();

		if ($defined($('bairro'))) {
			new Ajax('/imovel/bairro', {
				method: 'post',
				data: 'cidade=' + $('detalhe_cidade').getProperty('value') + '&db=' + db,
				onComplete: function (req) {
					var bairros = Json.evaluate(req);
					$('bairro').empty();
					$('bairro').setProperty('disabled', false);

					$('bairro').adopt(new Element('option', {value: ''}).setHTML('Qualquer'));
					bairros.each(function(bairro) {
						$('bairro').adopt(new Element('option', {value: bairro.IMOVEIS.id}).setHTML(bairro.IMOVEIS.nome));
					});
				}

			}).request();
		}

	});

	if ($defined($('detalhe_tipo')) && $defined($('bairro'))) {

		$('bairro').addEvent('change', function() {
			var db = $('busca_vendas').getProperty('checked') ? 'venben' : 'visben';

			new Ajax('/imovel/tipo', {
				method: 'post',
				data: 'cidade=' + $('detalhe_cidade').getProperty('value') + '&db=' + db,
				onComplete: function (req) {
					var tipos = Json.evaluate(req);
					$('detalhe_tipo').empty();
					$('detalhe_tipo').setProperty('disabled', false);

					$('detalhe_tipo').adopt(new Element('option', {value: ''}).setHTML('Qualquer tipo...'));
					tipos.each(function(tipo) {
						$('detalhe_tipo').adopt(new Element('option', {value: tipo.IMOVEIS.Id_Tipo}).setHTML(tipo.IMOVEIS.Tipo));
					});
				}

			}).request();

		});
	}


});