// jQuery plugin by Andy Chapman
// Based on PlaceHeld by [Max Wheeler](max@makenosound.com)
// Adds cross browser placeholders if HTML5 is not enabled already
// text input - puts placeholder text into input
// password input - adds a facade input with placeholder text
// others - not yet
(function($){
  $.placeHeld = function(el, options){
    var base = this;
    base.$el = $(el);
    base.el = el;
    base.$el.data("placeHeld", base);
    base.placeholderText = base.$el.attr("placeholder");
    
    


    if(base.$el.attr("type") == "password") {
    	base.init = function(){
            // Build the options from default and input options
            base.options = $.extend({},$.placeHeld.defaultOptions, options);
            // Bind functions to blur and focus events and trigger blur (on the facade)
            base.createPlace();
            base.$el.bind('blur', base.holdPlace);
            base.$facade.bind('focus', base.releasePlace);
            base.$el.trigger('blur');
            // Bind to form submit for removing placeholders before submission
            base.$el.parents('form').bind('submit', base.clearPlace);
        };
    	// Add an input field with same attributes as password field (except text)
    	base.createPlace = function() {
    		if(base.$el.val() == "") {
    			// Have to create the element with type (IE doesn't let you change it later)
	    		base.$el.after("<input type='text'>");
	    		base.$facade = base.$el.next();
	    		// IMPROVE Copy all the original properties
	    		base.$facade.addClass(base.$el.attr('class'));
	    		base.$facade.width(base.$el.width());
	    		base.$facade.height(base.$el.height());
	    		// Add/change the others
	    		base.$facade.attr('name', base.$el.attr('name') + "_facade");
	    		base.$facade.attr('id', base.$el.attr('id') + "_facade");
	    		base.$facade.addClass(base.options.className);
	    		base.$facade.attr('tabindex', base.$el.attr('tabindex'));
	    		base.$facade.val(base.placeholderText);
	    		base.$facade.css("display","none");	    		    	
    		}
		};

    	// Hide the original, show the facade
    	base.holdPlace = function() {
    		var value = base.$el.val();
    		if(!value)  {
				base.$facade.css("display","");
				base.$el.css("display","none");
			}
		};
    	// Hide the facade, show the original
    	base.releasePlace = function() {
    		base.$facade.css("display","none");
			base.$el.css("display","");
			base.$el.focus();
		};
		// Tidy up
		base.clearPlace = function() {
	      base.$facade.remove();
	      base.$facade = null;
	      base.$el.css("display", "");
	    };
    } else {
    	base.init = function(){
            // Build the options from default and input options
            base.options = $.extend({},$.placeHeld.defaultOptions, options);
            // IE has a tendency to remember the placeholder value and insert it on back so remove it if it does             
  	      	if (base.$el.val() == base.placeholderText) base.$el.val('');
            // Bind functions to blur and focus events and trigger blur
            base.$el.bind('blur', base.holdPlace).bind('focus', base.releasePlace).trigger('blur');
            // Bind to form submit for removing placeholders before submission
            base.$el.parents('form').bind('submit', base.clearPlace);
        };
        // Hold with the default value attribute    	
	    base.holdPlace = function() {
	      var value = base.$el.val();
	      if (!value) base.$el.val(base.placeholderText).addClass(base.options.className);
	    };
	    // Refill with the default value attribute
	    base.releasePlace = function() {
	      var value = base.$el.val();
	      if (value == base.placeholderText) base.$el.val('').removeClass(base.options.className);
	    };
	    // Refill with the default value attribute
	    base.clearPlace = function() {
	      var value = base.$el.val();
	      if (value == base.placeholderText && base.$el.hasClass(base.options.className)) base.$el.val('');
	    };
    }
    base.init();
  };
  
  $.placeHeld.defaultOptions = { className: "placeheld" };
  
  $.fn.forcePlaceHoldersCleared = function() {
	  return this.each(function() {
		 if ($(this).val() == $(this).attr("placeholder")) $(this).val('');
	  });
  };
  
  $.fn.addPlaceHolders = function(options) {

	// Check for placeholder attribute support
	if (!!("placeholder" in $('<input>')[0])) return;
	
    return this.each(function() {
      (new $.placeHeld(this, options));
    });
  };
})(jQuery);
