
	/**
	 * dTblur - the jquery plugin
	 *
	 * this text should tell you how the plugin works. too bad it doesn't.
	 */
	(function( $ ){
			
		//
		// Default settings
		var defaultSettings = {
			'attr' :			'title', 		// what attribute holds the replacement text
			'type' :			'pending',		// pending (clear on keydown) or replace (clear on focus)
			'classname' :		'pending' 		// what class should be applied during 'pending' state
		};
		
		//
		// Methods
		var methods = {
		
			//
			// Initialize
			init : function( options ) {
			
				return this.each(function() {
				
					var $this = $(this);
					
					//
					// Apply settings
					var settings = defaultSettings;
					if ( options ) settings = $.extend( {}, defaultSettings, options );
					$this.data( 'dTblur', settings );
					
					//
					// Passwords
					if( typeof( $(this).attr(settings.attr) ) == 'string' && $(this).attr('type') == 'password' ) {
						
						//
						// Create text element
						$this.after('<input type="text" name="passwordText" title="' + $this.attr('title') + '" />');
						var $next = $this.next('input');
						
						//
						// Apply focus handler
						$next.bind( 'focus.dTblur', function() { methods.focus.call($next); } );
						
						//
						// Apply blur handler
						$next.bind( 'blur.dTblur', function() { methods.blur.call($next); } );
						
						//
						// Call blur handler
						$next.data( 'dTblur', settings ).blur();
						
						//
						// Show / hide password element
						$this.hide();
						$next.bind( 'keydown.dTblur', function(e) {
								$next.hide().val('').blur();
								$this.show().focus();
							});
						$this.bind( 'keyup.dTblur', function(e) {
								if( !$this.val().length ) {
									$this.hide();
									$next.show();
								}
							});
					
					//
					// Text fields
					} else if( typeof( $(this).attr(settings.attr) ) == 'string' ) {
					
						//
						// Apply focus handler
						$this.bind( 'focus.dTblur', function() { methods.focus.call($this); } );
						
						//
						// Apply blur handler
						$this.bind( 'blur.dTblur', function() { methods.blur.call($this); } );
						
						//
						// Apply pending handlers
						if( settings.type=='pending' ) {
						
							//
							// Apply keydown handler
							$this.bind( 'keydown.dTblur', function(e) { methods.keydown.call($this,e); } );
							
							//
							// Apply keyup handler
							$this.bind( 'keyup.dTblur', function(e) { methods.keyup.call($this,e); } );
						
						}
						
						//
						// Call blur handler
						methods.blur.call($this);
						
					}
					
					//
					// Format phone numbers on blur
					if( $this.attr('name') == 'phone' || $this.attr('name') == 'billing_phone' || $this.attr('name') == 'shipping_phone' ) {
						$this.bind( 'blur.dTblur', function() { methods.phone.call($this); } );
					}
					
				});
				
			},
			
			//
			// Focus method
			focus : function(e) {
			
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTblur');
				
					if( settings != undefined ) {
					
						if( settings.type=='replace' ) {
						
							//
							// Clear text if in default state
							if( $this.attr(settings.attr) != undefined && $this.attr(settings.attr).length && $this.val() == $this.attr(settings.attr) ) {
								$this.val('');
							}
							
						
						} else if( settings.type=='pending' ) {
						
							//
							// Add pending class if in default state
							if( $this.attr(settings.attr) != undefined && $this.attr(settings.attr).length && $this.val() == $this.attr(settings.attr) ) {
								$this.addClass(settings.classname);
							}
						
						}
					
					}
				
				});
				
			},
			
			//
			// Blur method
			blur : function(e) {
			
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTblur');
					
					if( settings != undefined ) {
				
						//
						// Restore text if empty
						if( !$this.val().length && $this.attr(settings.attr) != undefined && $this.attr(settings.attr).length ) {
							$this.val($(this).attr(settings.attr));
						}
					
						if( settings.type=='pending' ) {
							
							//
							// Return to default state
							$this.removeClass(settings.classname);
						
						}
					
					}
					
				});
			
			},
			
			//
			// Keydown method
			keydown : function(e) {
				
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTblur');
				
					if( settings != undefined ) {
				
						if( $this.attr(settings.attr) != undefined && $this.attr(settings.attr).length && $this.val()==$this.attr(settings.attr) ) {
							$this.removeClass(settings.classname);
							$this.val('');
						}
					
					}
					
				});
			
			},
			
			//
			// Keyup method
			keyup : function(e) {
				
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTblur');
				
					if( settings != undefined ) {
				
						if( !$this.val().length && $this.attr(settings.attr) != undefined && $this.attr(settings.attr).length ) {
							$this.addClass(settings.classname);
							$this.val($(this).attr(settings.attr));
						}
					
					}
					
				});
			
			},
			
			//
			// Clear method
			clear : function() {
			
				return this.each(function() {
					var $this = $(this);
					methods.focus.call($this);
					methods.keydown.call($this);
				});
			
			},
			
			//
			// Restore method
			restore : function() {
			
				return this.each(function() {
					var $this = $(this);
					methods.keyup.call($this);
					methods.blur.call($this);
				});
			
			},
			
			/**
			 * FORMAT PHONE
			 *
			 * This function strips formatting from a bound field and reformats
			 * it according to the following format:
			 * (NPA) NXX-0000
			 * (614) 538-0095
			 */
			phone : function() {
			
				return this.each(function() {
			
					var $this = $(this);
			
					var phone = $this.val();
					var newphone;
					phone = phone.replace(/[^0-9]+/g, ''); // get rid of everything that is not a number
				
					switch(phone.length) {
						case 7: 
							newphone = 'xxx-' + phone.substring(0,3) + '-' + phone.substring(3); break;
						case 10: 
							newphone = '(' + phone.substring(0,3) + ') ' + phone.substring(3,6) + '-' + phone.substring(6); break;
						case 11:  // an extre digit, must be the country code, right?
							newphone = '(' + phone.substring(1,4) + ') ' + phone.substring(4,7) + '-' + phone.substring(7); break;
						default:  // if not, blank it out and replace it with a format example.
							newphone = (phone.length > 1) ? 'xxx-xxx-xxxx' : ''; break;
					}
				
					$this.val(newphone);
					
				});
			
			}
					
		}
	  
		$.fn.dTblur = function( method ) {
			
			//
			// Method call
			if ( methods[method] ) {
				return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
			
			//
			// Default init call
			} else if ( typeof method === 'object' || ! method ) {
				return methods.init.apply( this, arguments );
			
			//
			// Method doesn't exist
			} else {
				$.error( 'Method ' +  method + ' does not exist on jQuery.dTblur' );
			}
		
		};
	  
	})( jQuery );
	
