////
// Plugin - jquery.assistantinput.js
//
(function(){
	var defaultValues = {
		'defaultText' : '',
		'initialText' : '',
		'classNameInitial' : '',
		'className' : '',
		'validationFunction' : null
	};
	// Constructor
	(AssistantInput = function(element, options) {
		this.elem = jQuery(element);
		this.elemAsDOM = element;
		this.settings = options;
		this.init();
	}).prototype = {
		elem : null,
		elemAsDOM:null,
		settings : new Object(),

		init : function() {
			var s = this.settings;
			this.setText(s['initialText'], s['classNameInitial'], s['className']);
			this.elem.on('keyup change paste cut', s['validationFunction']);

			this.elem.on('keydown paste', jQuery.proxy(function() {
				var s = this.settings;
				if (this.holdsDefaultText()) {
					this.setText("", s['className'], s['classNameInitial']);
				}
			}, this));

			this.elem.on('keyup', jQuery.proxy(function() {
				var s = this.settings;
				if(this.isEmpty()) {
					this.setText(s['defaultText'], s['classNameInitial'], s['className']);
					this.collapseTo(0, 0);
				}
			}, this));

			this.elem.on('blur', jQuery.proxy(function() {
				if (this.isEmpty()) {
					var s = this.settings;
					this.setText(s['initialText'], s['classNameInitial'], s['className']);
				}
			}, this));

			this.elem.on('select focus mouseup contextmenu', jQuery.proxy(function() {
				if (this.holdsDefaultText()) {
					this.collapseTo(0, 0);
				}
			}, this));

			this.elem.on('paste', jQuery.proxy(function(e) {
				// setTimeout... in IE the content is available AFTER the paste event!
				setTimeout(jQuery.proxy(function(){
					// Force resizing of textarea
					// The "autosize.resize" was introduced in version 1.17.0
					this.elem.trigger("autosize.resize");
				}, this), 15);
			}, this));
		},

		setText : function (text, addClass, removeClass) {
			this.elem.val(text);
			if(addClass) {
				if(!this.elem.hasClass(addClass)){
					this.elem.addClass(addClass);
				}
			}
			if(removeClass) {
				if(this.elem.hasClass(removeClass)){
					this.elem.removeClass(removeClass);
				}
			}
		},
		
		setInitialText : function() {
			var s = this.settings;
			this.setText(s['initialText'], s['classNameInitial'], s['className']);
		},

		setDefaultText : function() {
			var s = this.settings;
			this.setText(s['defaultText'], s['classNameInitial'], s['className']);
		},

		holdsDefaultText : function() {
			var val = this.elem.val();
			return (
				val == this.settings['defaultText'] ||
				val == this.settings['initialText']
			);
		},
		
		isEmpty : function() {
			return (this.elem.val().trim() === "");
		},
		
		collapseTo : function(left, right) {
			
			if (this.hasSelectionSupport()){
				 if(this.hasCaretPositionChanged(left,right)){
					 this.elem.caret(left, right);
				 }
			}
			else{
				 this.elem.caret(left, right);
			}			
		},
		
		hasSelectionSupport : function(){
			return this.elemAsDOM.selectionStart || this.elemAsDOM.selectionStart == '0';
		},
		
		hasCaretPositionChanged : function(left,right){
			return this.elemAsDOM.selectionStart!==left || this.elemAsDOM.selectionEnd !==right;
		},
		
		
		isValid : function() {
			return (
				!this.holdsDefaultText() && 
				!this.isEmpty()
			);
		},
		
		isDirty : function() {
			return !this.holdsDefaultText();
		}
	};

	jQuery.fn.assistantInput = function(options) {
		// Merge options with default values
		options = jQuery.extend({}, defaultValues, options || {});

		return this.each(function(index, element) {
			var element = jQuery(element), d;
			// Return early if this element already has a plugin instance
			if (d = element.data('assistantInput')) {
				d.init();
				return;
			}
			// Store plugin object in this element's data
			element.data('assistantInput', new AssistantInput(this, jQuery.extend({}, options)));
		});
	};
	
	jQuery.fn.ai_setInitialText = function() {
		var d;
		if (d = jQuery(this).data('assistantInput')) {
			d.setInitialText();
		}
	};

	jQuery.fn.ai_setDefaultText = function() {
		var d;
		if (d = jQuery(this).data('assistantInput')) {
			d.setDefaultText();
		}
	};

	jQuery.fn.ai_holdsDefaultText = function() {
		var d;
		if (d = jQuery(this).data('assistantInput')) {
			return d.holdsDefaultText();
		}
		return false;
	};

	jQuery.fn.ai_isValid = function() {
		var d;
		if (d = jQuery(this).data('assistantInput')) {
			return d.isValid();
		}
		return true;
	};

	jQuery.fn.ai_isDirty = function() {
		var d;
		if (d = jQuery(this).data('assistantInput')) {
			return d.isDirty();
		}
		return false;
	};
})();
