$(function() {

	  $('.password').each(function() {

		  // Normally I use object detection, however, in this case since I need to 
		  // detect Konqueror and Safari which don't have unique objects,
		  // I will use the user agent string to detect them. Only use this type of 
		  // detection as a last resort.
		  // I'm doing this because example 2 crashes Konqueror 3.4 and Safari 1.0

		  var ua=navigator.userAgent.toLowerCase();
		  if(!((ua.indexOf('konqueror')!=-1) && /khtml\/3\.[0-4]/.test(ua)) && 
		    !(((ua.indexOf('safari')!=-1) && !window.print))) {

		      // Set the third value to the text you want to appear in the field.
		      changeInputType(this,'text','password',false,true);
		  }
	  });

	/*** AJAX DEFAULT CONFIGURATION**/
	$.ajaxSetup({
		error: function(jqXHR, textStatus, errorThrown) {
			// tratamento de erro padrão
		}
	});

	// aqui to fazendo um bind de um elemento html com as solicitações
	// ajax, por exemplo, é como se esse elemento fosse uma div com um gif carregando
	// e eu desse '$(this).show()' nele
	$('#loadingMask').ajaxSend(function(e, jqxhr, settings) {

		if (settings.url.indexOf("/activities/last/") == -1 && 
			settings.url.indexOf("/visit/register") == -1 &&
			settings.url.indexOf("catalog/items/last") == -1) {

			$(this).center();
			$(this).fadeIn('fast');
	  }
	});

	// aqui to fazendo um bind de um elemento html com as solicitações
	// ajax, por exemplo, é como se esse elemento fosse uma div com um gif carregando
	// e eu desse '$(this).hide()' nele
	$('#loadingMask').ajaxComplete(function(e, jqXHR, settings) {
		// é executado quando a solicitação ajax é completada
		$(this).fadeOut('slow');
	});
	/*** AJAX DEFAULT CONFIGURATION**/
});

jQuery.fn.center = function () {

    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
};

//Example 2 (JS part 1)
function changeInputType(

  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var isMSIE=/*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
  if(!isMSIE){
    var newElm=document.createElement('input');
    newElm.type=iType;
  } else {
    var newElm=document.createElement('span');
    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
    newElm=newElm.firstChild;
  }
  var props=['name','id','className','size','tabIndex','accessKey'];
  for(var i=0,l=props.length;i<l;i++){
    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
  }
  newElm.onfocus=function(){return function(){
    if(this.hasFocus) return;
    var newElm=changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) newElm.hasFocus=true;
  };}();
  newElm.onblur=function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,'text',iValue,false,true);
    }
  };}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  // some browsers need the value set before the element is added to the page
  // while others need it set after
  if(!blankValue) newElm.value=iValue;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!isMSIE && !blankValue) newElm.value=iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm=newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }

  return newElm;
}

function clearDefault(el, defaultValue) {

	if (defaultValue == el.value) {

		el.value = '';
		$(el).css('color', '');
	}
}

function clearDefaultPassword(el, defaultValue) {

	$(el).attr('type', 'password');
	clearDefault(el, defaultValue);
}

function restoreDefault(el, defaultValue, color) {

	if($(el).val() == '') {

		$(el).val(defaultValue);
		$(el).css('color', color);
		return true;
	}

	return false;
}

function restoreDefaultPassword(el, defaultValue, color) {

	if(el.value == '') {

		$(el).attr('type', 'text');
	}
	restoreDefault(el, defaultValue, color);
}

jQuery.fn.onlyNumbers = function() {

	    return this.each(function() {

	        $(this).keydown(function(e) {

	            var key = e.charCode || e.keyCode || 0;

	            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
	            return (
	                key == 8 || 
	                key == 9 ||
	                key == 46 ||
	                (key >= 37 && key <= 40) ||
	                (key >= 48 && key <= 57) ||
	                (key >= 96 && key <= 105));
	        });
	    });
};

function errorsToList(errors) {

	var list = '<ul>';
	list += errorsToLiList(errors);
	list += '</ul>';

	return list;
}

function errorsToLiList(errors) {

	var list = '';
	for(var i = 0; i < errors.length; i++) {

		list += '<li>' + errors[i] + '</li>';
	}

	return list;
}

function errorsInLine(errors) {

	var list = '';
	for(var i = 0; i < errors.length; i++) {

		if(list != '') {
		
			list += '\n';
		}

		list += '' + errors[i] + '';
	}
	list += '';

	return list;
}
