

$(function () {
	
	$('.texto-busqueda').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	$('#showAvanzada').click(function(e) {
		e.preventDefault();
		$('#busqueda-fechas').toggle();
	});
	

	
	$('#cercar').click(function(e) {
		e.preventDefault();
		cercar();
	});
	
	 $('#texto-busqueda').keyup(function(event){
		    if (event.keyCode == 13) {
		    	cercar();
		    }
	 });
	
	/*$("form").submit(function() {
        if ($("input:first").val()) {
          $("#formOutput").html("Validated...").show();
          return true;
        }
        $("#formOutput").text("Not valid!").show().fadeOut(1000);
        return false;
   });*/


	$('#inici').datepicker({
		changeMonth: true,
		changeYear: true,
		yearRange: '-19:+0',
		dateFormat: 'dd/mm/yy',
	    firstDay: 1,
		isRTL: false
	});

	$('#fi').datepicker({
		changeMonth: true,
		changeYear: true,
		yearRange: '-19:+0',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false
	});
	
});

function openMessage(tituloMessage,msgMessage){
	createDivMessage(tituloMessage,msgMessage);
	$("#dialog-message").dialog({
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
}

function createDivMessage(tituloMessage,msgMessage){ 
	$('#contenido').append('<div id="dialog-message" title="'+tituloMessage+'"><p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>'+msgMessage+'.</p></div>');
}


function cercar(){
	
	var text = jQuery.trim($('#texto-busqueda').val());
	if (text == textoDefecto) {
		openMessage(traMensaje,traTextoVacio);
		return;
	}
	
	//Validamos que el texto de las fechas sea correcto
	if($('#inici').val() == traFechaInicio){
		$('#inici').val('');
	}
	if($('#fi').val() == traFechaFin){
		$('#fi').val('');
	}
	document.buscadorGeneralForm.submit();
}



