function limparCampos(form, descMeta)
{
	if(descMeta == undefined || descMeta == '' || descMeta == null) descMeta = 'title';
	$(form).find('input[type="text"], textarea').each(function(){
		if($(this).val() == $(this).attr(descMeta))
		{
			$(this).val('');
		}
	});
}

function preencherCampos(form,descMeta)
{
	if(descMeta == undefined || descMeta == '' || descMeta == null) descMeta = 'title';
	$(form).find('input[type="text"], textarea').each(function(){
		if($(this).val() == '')
		{
			$(this).val($(this).attr(descMeta));
		}
	});
}

function prepararCampos(campos, descMeta)
{
	if(descMeta == undefined || descMeta == '' || descMeta == null) descMeta = 'title';
	var meta = 'desc';
	$(campos).each(function(){
		$(this)
			.attr(meta,$(this).attr(descMeta))
			.val($(this).attr(meta))
			.focus(function(){
				if($(this).val() == $(this).attr(meta)) $(this).val('');
			})
			.blur(function(){
				if($(this).val() == '') $(this).val($(this).attr(meta));
			});
	});
}

$.fn.erro = function() {
	return $(this).html(
		'<span class="ui-state-error ui-corner-all" style="display: inline-block; padding: 3px 10px;">'+
		'<span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>'+
		$(this).text() +
		'</span>');
}

$.fn.aviso = function() {
	return $(this).html(
		'<span class="ui-state-highlight ui-corner-all" style="display: inline-block; padding: 3px 10px;">'+
		'<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>'+
		$(this).text() +
		'</span>');
}
	
function ocultaErrosValidacao()
{
	$.validationEngine.closePrompt('.formError',true);
}

function startShadowbox()
{
	Shadowbox.clearCache(); Shadowbox.init(); Shadowbox.setup();
}

function openShadowbox(content, player, title, width, height)
{
	Shadowbox.open({ content: content, player: player, title: title, width: width, height: height});
}

function validaFormulario(formID)
{
	return $(formID).validationEngine({returnIsValid:true});
}

function dialogo(titulo, conteudo)
{
	$('<div title="'+titulo+'">'+conteudo+'</div>').dialog({modal:true,show: 'drop', hide: 'drop', buttons:{ 'Ok':function(){ $(this).dialog('close'); } }});
}

function confirmar(titulo, pergunta, botoes)
{
	$('<div title="'+titulo+'">'+pergunta+'</div>').dialog({modal:true,show: 'drop', hide: 'drop', buttons: botoes });
}

function load(url,selector)
{
	if(selector == undefined || selector == '') selector = '#painel';
	$(selector).load(url);
}

function insertBBCode(target, code)
{
	var textarea = document.getElementById(target);

	var len = textarea.value.length;
	var start = textarea.selectionStart;
	var end = textarea.selectionEnd;
	var sel = textarea.value.substring(start, end);
	 
	// This is the selected text and alert it
	var replace = '['+code+']' + sel + '[/'+code+']';
	// Here we are replacing the selected text with this one
	textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
}

function insertBBCode2(target, code)
{
	var textarea = document.getElementById(target);

	var len = textarea.value.length;
	var start = textarea.selectionStart;
	var end = textarea.selectionStart;
	 
	// This is the selected text and alert it
	var replace = '['+code+']';
	// Here we are replacing the selected text with this one
	textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
}

var aguarde;
function abreAguarde(texto){ if(texto==undefined)texto='Aguarde um momento';aguarde = $('<p>'+texto+'...</p>').dialog({modal: true,closeOnEscape: false,open: function() { $(".ui-dialog-titlebar-close").hide(); }});}
function fechaAguarde(){aguarde.dialog('close');}
