// JavaScript Document

$(document).ready(function () {
	
	// EasyToolTip
	try {
		$("img, a").easyTooltip();
	} catch (e) {
		// ...
	}

	// Auto Clear

	var t = jQuery("input.autoclear");
	t.each(function () {
		var a, o = jQuery(this);
		a = o.attr("title");
		if (a) o.val(a);
	});
	t.focus(function () {
		var a, o = jQuery(this);
		a = o.attr("title");
		if (a && a == o.val())
			o.val("");
	});
	t.blur(function () {
		var a, o = jQuery(this);
		a = o.attr("title");
		if (a && o.val() == "")
			o.val(a);
	});

	// Texto padrão da busca

	var buscaampla = {
		element: null,
		text: "QUE IMÓVEL VOCÊ PROCURA?",
		focus: function () {
			if (this.value == buscaampla.text)
				this.value = "";
		},
		blur: function () {
			if (this.value == "")
				this.value = buscaampla.text;
		}
	}

	buscaampla.element = document.getElementById("buscaampla");
	if (buscaampla.element)
	{
		buscaampla.element.onfocus = buscaampla.focus;
		buscaampla.element.onblur = buscaampla.blur;
		if (buscaampla.element.value == "")
			buscaampla.element.value = buscaampla.text;
	}

	try {
		jQuery(document.form_busca).submit(function (e) {
			if (buscaampla.element
				&& buscaampla.element.value != ""
				&& buscaampla.element.value != buscaampla.text)
				return true;
			e.preventDefault();
			return false;
		});
	} catch (e) {
		// oops...
	}

	// Autocomplete Busca Ampla
	jQuery.getJSON('/apps/site/index.php/palavras', function(data) {

		jQuery("#buscaampla").autocomplete(data, {
			width: 356,
			max: 10,
			highlight: false,
			multiple: true,
			multipleSeparator: " ",
			scroll: true,
			scrollHeight: 300,
			selectFirst: false
		});

		jQuery("#clear").click(function() {
			jQuery(":input").unautocomplete();
		});

	});

	// Script que exibe submenus da navegação principal
    $("ul.topnav li a").click(function() { //When trigger is clicked...
        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
        $(this).parent().hover(function() {}, function(){
            $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        });
        //Following events are applied to the trigger (Hover events for the trigger)
        }).hover(function() {
            $(this).addClass("subhover"); //On hover over, add class "subhover"
        }, function(){  //On Hover Out
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });


	// Box Login Área do Corretor
	$(".signin").click(function(e) {
		e.preventDefault();
		$("fieldset#signin_menu").toggle();
		$(".signin").toggleClass("menu-open");
	});
	$("fieldset#signin_menu").mouseup(function() {
		return false
	});
	$(document).mouseup(function(e) {
		if($(e.target).parent("a.signin").length==0) {
			$(".signin").removeClass("menu-open");
			$("fieldset#signin_menu").hide();
		}
	});

});

// Navigation

function nav (url) {
	document.location.href = url;
}

function busca (texto)
{
	alert("Você quer pesquisar: " + texto);
}
