// Bind para os controles do cabeçalho
$(document).ready(function() {
	// Dá foco no body
	$("body").focus();
	
	if ($("#searchSubmitHeader")[0] != undefined) {
		$("#searchSubmitHeader:first").bind("click", function()
		{
			if ($("input#searchTermTextBoxHeader:first").val() == "Procuro por")
				return;
				
			executeSearch(
				$("input#searchTermTextBoxHeader:first").val(),
				$("select#searchLocationSelectHeader option:selected").val(),
				"null",
				1,
				10,
				"Relevancia");
		});
	}
	
	if ($("#searchSubmitBottom")[0] != undefined) {
		$("#searchSubmitBottom:first").bind("click", function()
		{
			if ($("input#searchTermTextBoxBottom:first").val() == "Procuro por")
				return;
				
			executeSearch(
				$("input#searchTermTextBoxBottom:first").val(),
				$("select#searchLocationSelectBottom option:selected").val(),
				"null",
				1,
				10,
				"Relevancia");
		});
	}
	
	if ($("input#searchTermTextBoxBottom:first") != undefined)
	{
		$("input#searchTermTextBoxBottom:first").bind("keypress", function(e)
		{
			if (e.which == 13)
			{
				if ($("input#searchTermTextBoxBottom:first").val() == "Procuro por")
					return;
					
				executeSearch(
					$("input#searchTermTextBoxBottom:first").val(),
					$("select#searchLocationSelectBottom option:selected").val(),
					"null",
					1,
					10,
					"Relevancia");
			}
		});
	}
	
	// Formulário de pesquisa no corpo
	/* COMEÇO: PARA ÁREA DE NÃO RESULTADOS */
	
	if($("input#searchTermTextBox")[0] != undefined) {
		$("input#searchTermTextBox:first").toggleClass("water-mark");
		
		$("input#searchTermTextBox:first").bind("blur", function() {
			if (!$(this).hasClass("water-mark")) {
				if ($(this).val().length <= 0)
					$(this).toggleClass("water-mark");
			}
		});
		
		$("input#searchTermTextBox:first").bind("focus", function() {
			if ($(this).hasClass("water-mark"))
				$(this).toggleClass("water-mark");
		});
	}
	
	if($("select#searchLocationSelect")[0] != undefined) {
		fillSearchLocationSelect("select#searchLocationSelect:first");
	}
	
	if($("input#searchSubmit")[0] != undefined) {
		$("input#searchSubmit").bind("click", function() {
			executeSearch($("input#searchTermTextBox:first").val()
				, $("select#searchLocationSelect option:selected").val()
				, "null"
				, 1
				, 10
				, "Relevancia");
		});
	}
	
	/* FIM: PARA ÁREA DE NÃO RESULTADOS */
});

// Preenche a combo de categorias
var fillSearchLocationSelect = function(element) {
	var params = false;
	var combo = $(element);
	
	combo.find("option").remove(); // Remove todos os itens
	
	var linha = $.jqURL.get("linha");
	
	if (linha == "") 
		combo.append("<option value=''>Todo o site</option>"); // Adiciona o primeiro item ("Todo o site")
	else
		combo.append("<option value='' selected='selected'>Todo o site</option>"); // Adiciona o primeiro item ("Todo o site") selecionado
	
	
	params = { execute : "fillSearchLocationSelect" }
	
	$.ajax({ url : documentPath() + "Default.aspx"
		, type : "POST"
		, data : params
		, dataType : "json"
		, success : function (data, textStatus) {
			for(value in data) {
				if (linha == data[value].Value) {
					combo.append("<option value='" + data[value].Value + "' selected>" + data[value].Text + "</option>");
				}
				else {
					combo.append("<option value='" + data[value].Value + "'>" + data[value].Text + "</option>");
				}
			}

		}
		, error : function (XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}