(function($) { // secure $ jQuery alias
/*******************************************************************************************/
// jquery.placeholder.js - rev 2
// Copyright (c) 2011, Larry (http://larionov.biz)
// Liscensed under the BSD License (BSD-LICENSE.txt)
// http://www.opensource.org/licenses/bsd-license.php
// Created: 2011-11-01
/*******************************************************************************************/

$.Placeholder = function(element, config) {
	var self = this;
	this.element = $(element);
	if (this.element.size() == 0) {
		return;
	}
	this.config = $.extend({}, this.defaultConfig, config || {});
	this.config.placeholder = this.config.placeholder || this.element.data('placeholder') || '';
	this.config.elementType = this.element.attr('type') == 'password'? 'password': 'text';

	this.element.focus(function() {
			self._removePlaceholder();
		})
		.blur(function() {
			self._trySetPlaceholder();
		});
	this.element.parents('form').submit(function() {
		self._removePlaceholder();
	});
	this._trySetPlaceholder();
};
$.Placeholder.prototype.defaultConfig = {
	placeholder: null,
	classPlaceholder: 'placeholder'
};
$.Placeholder.prototype._trySetPlaceholder = function() {
	if (this.element.val() == '') {
		this.element.val(this.config.placeholder);
	}
	if (this.element.val() == this.config.placeholder) {
		this.element.addClass(this.config.classPlaceholder);
		if (this.config.elementType == 'password') {
			this.element.prop('type', 'text');
		}
	}
};
$.Placeholder.prototype._removePlaceholder = function() {
	if (this.config.elementType == 'password') {
		this.element.prop('type', 'password');
	}
	this.element.removeClass(this.config.classPlaceholder);
	if (this.element.val() == this.config.placeholder) {
		this.element.val('');
	}
};

$.fn.Placeholder = function(config) {
	return this.each(function(i, element) {
		new $.Placeholder(element, config);
	});
};

})(jQuery);

