/*
* 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 = $.extend({defaultClass : 'default'}, options);
  
  return this.each(function (){
    var textInput = $(this);
    var defaultVal = textInput.val();
	
	if(textInput.attr('defaultValue'))
		defaultVal = textInput.attr('defaultValue');
    
    if(textInput.attr('type') == 'password'){
	
	  var tabindexString = "";
	  
	  var tabindex = textInput.attr('tabindex');
	  
	  if(tabindex > 0)
	  	tabindexString = ' tabindex="' + tabindex + '"';
	
      var newInput = $('<input type="text" class="'+settings.defaultClass+'"value="'+textInput.val()+'"'+tabindexString+' />');
      newInput.css({'display' : ''});
      
      newInput.bind('focus', function(){
        var $this = $(this);
        $this.css({'display' : 'none'});
        textInput.css({'display' : ''});
        textInput.focus();
		
		// IE PIE.htc fix
		var inputClasses = textInput.attr('class');
		textInput.removeClass(inputClasses).addClass(inputClasses);
      });
      
	  textInput.css({'display' : 'none'});
      textInput.before(newInput);
    }
    
    textInput.bind('focus', function(){
      var $this = $(this);
      if($this.val() == defaultVal){
        $this.val('');
      }
    });
      
    textInput.bind('blur', function(){
      var $this = $(this);
      if($this.val() == '' || $this.val() == defaultVal){
        $this.val(defaultVal);
        if($this.attr('type') == 'password'){
		  $this.css({'display' : 'none'})
          newInput.css({'display' : ''});
		  
		  // IE6 bug fix
		  newInput.removeClass(settings.defaultClass);
		  newInput.addClass(settings.defaultClass);
        }
      }
    });
  });
};
