$(document).ready(function() {
	$('a').filter(function() {
		return this.href.match(/relatorio\/\d\/*/);
    }).attr('target', '_blank');
	
	$('a').filter(function() {
		return this.href.match(/report\/\d\/*/);
    }).attr('target', '_blank');

	$('*:last-child').addClass('last-child');
	$('*:first-child').addClass('first-child');

	$('input[hint]').bind('focus blur', function() {
		if ($(this).val() == $(this).attr('hint')) {
			$(this).val('');
		} else if ($(this).val() == '') {
			$(this).val($(this).attr('hint'));
		/*} else {
			$(this).select();*/
		}
	}).each(function(item) {
		$(this).val($(this).attr('hint'));
	});

	$('#frmLogin').submit(function() {
		$('#send-login').css('display', 'none');
		$('#check-login').css('display', 'inline');
		$.ajax({
			url: $(this).attr('action'),
			cache: false,
			type: 'POST',
			dataType: 'json',
			data: {
				login: $('#txtLogin').val(),
				password: $('#txtPassword').val()
			},
			beforeSend: function(){
				block(msgLogin);
			},
			success: function(retorno) {
				$('#check-login').css('display', 'none');
				$('#send-login').css('display', 'inline');
				if (retorno.success) {
					window.location.href = $('#login-redirect').val() || window.location.href;
				} else {
					unblock();
					window.alert(retorno.message);
				}
				
			}
        });
		return false;
	});

	$('#lost-password').click(function() {
		if ($('#txtLogin').val() && $('#txtLogin').val() != $('#txtLogin').attr('hint')) {
			$('#check-login').css('display', 'block');
			$.ajax({
				url: $(this).attr('href'),
				cache: false,
				type: 'POST',
				dataType: 'json',
				data: {
					login: $('#txtLogin').val()
				},
				success: function(retorno) {
					$('#check-login').css('display', 'none');
					window.alert(retorno.message);
				}
	        });
		} else {
			window.alert('Informe o login');
			$('#txtLogin').focus();
		}
		return false;
	});
});

function block(mensagem)
{
	$.blockUI({
		message: '<h3>'+mensagem+'</h3>',
        css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            opacity: .5, 
            color: '#fff' 
    	}
	});
}

function unblock()
{
	$.unblockUI();
}
