/* Placeholder for inputs */

(function( $ ){
  
  $(document).ready(function(){
    $("input[placeholder], textarea[placeholder]").not("[prettysearch=yes]").each(function(){
      new Placeholder(this);
    })
  })
  
  this.Placeholder = function(){
  	return this.construct__.apply(this, arguments);
  }
  
  Placeholder.prototype.construct__ = function( node ){
    this.domNode = $( node );

    this.placeholderText = this.domNode.attr("placeholder");
    this.domNode.removeAttr("placeholder");

    this.build_();
    this.bindEvents_();
    
    if( this.domNode.val() != '' )
      this.status( false );
  };
  
  Placeholder.prototype.build_ = function(){
    this.placeholderNode = $("<span>").addClass("placeholder-node");
    this.placeholderNode.css({
      position: "relative"
    })
    this.domNode.wrap( this.placeholderNode );

    this.textNode = $("<span>").addClass("placeholder");
    this.textNode
      .text( this.placeholderText )
      .css({ position: "absolute", zIndex: 1, left: 5, top: 0 })
    this.domNode.after( this.textNode );
  };

  Placeholder.prototype.bindEvents_ = function(){
    var self = this;

    this.domNode
      .focus(function(){
        self.status( false );
      })
      .blur(function(){
        if( self.domNode.val() == '' )
          self.status( true );
      })

    this.textNode
      .mouseup(function(){
        if( !self.domNode.is(":disabled") )
          self.domNode.focus();
      });
  };
  
  Placeholder.prototype.status = function( bool ){
    if( bool )
      this.textNode.show()
    else
      this.textNode.hide()
  };
    
})( jQuery );

/* Placeholder for inputs (end) */
