jQuery.fn.centrar = function()
{
    ancho = this.width();
    ancho_contenedor = this.parent().width();
    style = (this.attr('style') != null) ? this.attr('style') + ';' : '';
    style = style + 'margin-left:' + (ancho_contenedor-ancho)/2 + 'px';
    this.attr('style',style);
}

jQuery.fn.estiloInformacion = function()
{
    mensaje = this.attr('innerHTML');
    this.empty();
    this.addClass('ui-corner-all');
    p = this.createPrepend('p');
    $(p).attr('innerHTML',mensaje);
}

jQuery.fn.estiloAdvertencia = function()
{
    mensaje = this.attr('innerHTML');
    this.empty();
    this.addClass('ui-state-highlight ui-corner-all');
    p = this.createPrepend('p');
    $(p).attr('innerHTML',mensaje);
    $(p).createPrepend('span',{ className:'ui-icon ui-icon-info izquierda',style:'margin-right: 0.3em; margin-left: 0.3em;' });
}

jQuery.fn.estiloError = function()
{
    mensaje = this.attr('innerHTML');
    this.empty();
    this.addClass('ui-state-error ui-corner-all');
    p = this.createPrepend('p');
    $(p).attr('innerHTML',mensaje);
    $(p).createPrepend('span',{className:'ui-icon ui-icon-alert izquierda',style:'margin-right: 0.3em; margin-left: 0.3em;'});
}

jQuery.fn.passwordStrength = function( options )
{
    return this.each(function(){
      var that = this;
      that.opts = {};
      that.opts = $.extend({}, $.fn.passwordStrength.defaults, options);

      that.div = $(this).next();
      that.defaultClass = that.div.attr('class');

      that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;

       v = $(this)
      .keyup(function(){
          if( typeof el == "undefined" )
            this.el = $(this);
          var s = getPasswordStrength (this.value);
          var p = this.percents;
          var t = Math.floor( s / p );

          if( 100 <= s )
            t = this.opts.classes.length - 1;

          this.div.removeAttr('class')
                  .addClass( this.defaultClass )
                  .addClass( this.opts.classes[ t ] );

      })
      .next()
      .click(function(){
              $(this).prev().val( randomPassword() ).trigger('keyup');
              return false;
      });
    });
}

    
function getPasswordStrength(H)
{
  var D=Math.min((H.length),5);
  var F=H.replace(/[0-9]/g,"");
  var G=Math.min((H.length-F.length),3);
  var A=H.replace(/\W/g,"");
  var C=Math.min((H.length-A.length),3);
  var B=H.replace(/[A-Z]/g,"");
  var I=Math.min((H.length-B.length),3);
  var E=((D*10)-20)+(G*10)+(C*15)+(I*10);
  return Math.min(Math.max(0,E),E);
}

jQuery(function($) 
{
  if(jQuery().datepicker)
  {
    $.datepicker.regional['es'] =
    {
      clearText: 'Borra',
      clearStatus: 'Borra fecha actual',
      closeText: 'Cerrar',
      closeStatus: 'Cerrar sin guardar',
      prevStatus: 'Mostrar mes anterior',
      prevBigStatus: 'Mostrar año anterior',
      nextStatus: 'Mostrar mes siguiente',
      nextBigStatus: 'Mostrar año siguiente',
      currentText: 'Hoy',
      currentStatus: 'Mostrar mes actual',
      monthNames:  ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
      monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
      monthStatus: 'Seleccionar otro mes',
      yearStatus: 'Seleccionar otro año',
      weekHeader: 'Sm',
      weekStatus: 'Semana del año',
      dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
      dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb'],
      dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
      dayStatus: 'Set DD as first week day',
      dateStatus: 'Select D, M d',
      dateFormat: 'dd/mm/yy',
      firstDay: 1,
      initStatus: 'Seleccionar fecha',
      isRTL: false,
      time24h:true,
      stepMinutes:10,
      stepHours:1,
      duration:'',
      showTime: false,
      constraintInput:false
    };
    $.datepicker.setDefaults($.datepicker.regional['es']);
  }
  
  if(jQuery().timepicker)
  {
    $.timepicker.regional['es'] = {
          timeOnlyTitle: 'Hora',
          timeText: 'Tiempo',
          hourText: 'Horas',
          minuteText: 'Minutos',
          secondText: 'Segundos',
          currentText: 'Actual',
          closeText: 'Cerrar',
          ampm: false
    };
    $.timepicker.setDefaults($.timepicker.regional['es']);
  }
  
  $.fn.passwordStrength.defaults = {
	classes : Array('is0','is10','is20','is30','is40','is50','is60','is70','is80','is90','is100'),
	cache : {}
  }

});
