/*
* Vekz
* http://www.callmekev.com/jquery.autobox.js
* autobox plugin -
* Used on text inputs with default values. Clears the default value on focus.
* Restores the default value on blur if empty or the same
* overlays text inputs for password boxes and swaps them on focus
* use straight CSS for styling the focus of the text input
* 
*/

jQuery.fn.autobox = function(options){  
  var settings = jQuery.extend({defaultClass : 'maskPassInput', filledClass : 'filled'}, options);
  
  return this.each(function (){
    var textInput = jQuery(this);
    var defaultVal = textInput.val();
    textInput.addClass(settings.defaultClass);
    
    if(textInput.attr('type') == 'password'){
      var newInput = jQuery('<input type="text" class="'+settings.defaultClass+'"value="'+textInput.val()+'" />');
      newInput.css({
        'position' : 'absolute',
        'z-index' : 10,
		'padding-left' : 5,
		'color': '#888',
		'border':'none',
        'top' : textInput.position().top+7+'px',
        'left' : textInput.position().left+5+'px',
				'width' : textInput.width()-5+'px'
      });
      
      jQuery(window).resize(function(){
        newInput.css({
          'top' : textInput.position().top+'px',
          'left' : textInput.position().left+'px'
        });
      });
      
      newInput.bind('focus', function(){
        var $this = jQuery(this);
        $this.hide();
        textInput.show();
        textInput.css({'visibility' : 'visible'});
        textInput.focus();
      });
      
      textInput.before(newInput);
    }
    
    textInput.bind('focus', function(){
      var $this = jQuery(this);
      $this.removeClass(settings.defaultClass);
      $this.addClass(settings.filledClass);
      if($this.val() == defaultVal){
        $this.val('');
      }
    });
      
    textInput.bind('blur', function(){
      var $this = jQuery(this);
      if($this.val() == ''){
        $this.val(defaultVal);
        $this.addClass(settings.defaultClass);
        $this.removeClass(settings.filledClass);
        if($this.attr('type') == 'password'){
          newInput.show();
        }
      }else{
        $this.addClass(settings.filledClass);
      }
    });
  });
};
