var ajaxCount = 0;

$(document).ready(function () {
	
	$.ajaxSetup({ 
		cache: false,
		beforeSend: function(xhr) {
			ajaxCount++;
			if (ajaxCount > 0) { $('#processing').dialog('open'); }
		},
		complete: function() {
			ajaxCount--;
			if (ajaxCount == 0) { $('#processing').dialog('close'); }
		}
	});
	
	//Define as cores do menu.
	$('#menuUl [href=\''+location.search+'\']').addClass('headMenuSelected');
	if (location.search == '') {
		$('#menuUl').children(':first').children(':first').addClass('headMenuSelected');
	}
	
	$('#processing').dialog({
		autoOpen: false,
		width: 200,
		minHeight: 50,
		bgiframe: true,
		modal: true,
		closeButton: false,
		closeOnEscape: false
	});
	
	//Inicializa a div dialog
	$('#dialog').dialog({
		autoOpen: false,
		width: 600,
		bgiframe: true,
		modal: true,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	
	//Inicializa a div dialog_form
	$("#dialog_form").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 230,
		width: 500,
		modal: true,
		buttons: {
			'Ok': function() {
				$('#dialog_form form').submit();
			},
			'Cancelar': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$("#dialog_form").html('');
		}
	});
	
	$(document).bind('keydown keypress', function (event) {
		var tag = event.target.tagName;
		if (event.which === 8 // Backspace has code 8, 'e.keyCode' doesn't work in IE
	        && tag !== 'INPUT' // we don't want to override it for inputs
	        && tag !== 'TEXTAREA') {
				event.stopPropagation();
				event.preventDefault();
		        return false;
		}
	});
	
});

function sleep(seconds) {
	var now = new Date().getTime();
	while (new Date().getTime() - now < seconds*1000);
}

