////
// Plugin - jquery.assistantfeed.js
//
// makes use of the:
//    clearable-text-field - jQuery plugin
//    jcarot - jQuery plugin
//    autosize - jQuery plugin
//    assistantInput - jQuery plugin
(function(){
	var defaultValues = {
		'title' : null,
		'content' : null,
		'comingUpFunction' : null,
		'resetFunction' : null,
		'noAutosizeClass' : '',
		'lateAutosizeAttach' : false	// 'true' is a useful value when the parent is not visible at all (display:none)
	};
	// AssistantFeed - Assign two elements to a feed (e.g. newsfeed, comment, feedback etc.)
	(AssistantFeed = function(element, options) {
		this.elem = jQuery(element);
		this.settings = options;
		this.init();
		this.initHandlers();
	}).prototype = {
		elem : null,
		titleElem : {
			clearableTextField : function() {},
			bind : function() {},
			siblings : function(){return {
				fadeOut : function(){},
				show : function() {}
			}},
			ai_setInitialText : function() {},
			ai_setDefaultText : function() {},
			ai_isDirty : function() { return false; },
			ai_isValid : function() { return false; },
			ai_holdsDefaultText : function() { return ''; },
			val : function() { return ''; }
		},
		contentElem : null,
		settings : new Object(),

		init : function() {
			if (this.settings['title'] != null) {
				this.titleElem = this.settings['title'];
			}
			this.contentElem = this.settings['content'];
		},

		initHandlers : function() {
			var s = this.settings;
			if (typeof s['comingUpFunction'] === "function") {
				this.titleElem.on('focus', s['comingUpFunction']);
			}
			// bind [x] button plugin to title field
			this.titleElem.clearableTextField({
		    	'clearFunction':  typeof s['resetFunction'] === "function" ? s['resetFunction'] : null,
		    	'hideWhenEmpty' : false
		    });

			// If the browser cannot support autosize plugin the height will be static.
			var bCanAutosize = this.canAutosize(this.contentElem.get(0));
			if (!bCanAutosize) {
				// For all browsers that doesn't support "autosize":
				this.contentElem.addClass(s["noAutosizeClass"]);
			}

			if (this.settings['lateAutosizeAttach'] !== true) {
				this.attachAutosize();
			}
		},
		
		attachAutosize : function() {
			// Bind autosize plugin to "textarea"
			// If the autosize plugin is already attached, the autosize() call will return early.
			this.contentElem.autosize();
		},
		
		reset : function() {
			// Set default values in form elements
			this.titleElem.ai_setInitialText();
			this.contentElem.ai_setDefaultText();

			// Force resizing of textarea
			// The "autosize.resize" was introduced in version 1.17.0
			this.contentElem.trigger("autosize.resize");

			// The "text_clear_button" element is defined by jquery.clearableText plugin
			this.titleElem.siblings('.text_clear_button').fadeOut('fast');
		},
		
		showForm : function() {
			this.titleElem.ai_setDefaultText();
			this.contentElem.ai_setDefaultText();
			this.titleElem.siblings('.text_clear_button').show();
			this.attachAutosize();
		},
		
		isDirty : function() {
			return (this.titleElem.ai_isDirty() || this.contentElem.ai_isDirty());
		},

		isValid : function() {
			return (this.titleElem.ai_isValid() || this.contentElem.ai_isValid());
		},

		getValueArray : function() {
			return ({
				'title' : this.titleElem.ai_holdsDefaultText() ? '' : this.titleElem.val(),
				'content' : this.contentElem.ai_holdsDefaultText() ? '' : this.contentElem.val()
			});
		},

		canAutosize : function(element) {
			return (element && (typeof element["oninput"] === "function" || ("onpropertychange" in element)));
		}
	};

	jQuery.fn.assistantFeed = 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('assistantFeed')) {
				d.init();
				return;
			}
			// Store plugin object in this element's data
			element.data('assistantFeed', new AssistantFeed(this, jQuery.extend({}, options)));
		});
	};

	jQuery.fn.af_reset = function() {
		var d;
		if (d = jQuery(this).data('assistantFeed')) {
			d.reset();
		}
	};

	jQuery.fn.af_showForm = function() {
		var d;
		if (d = jQuery(this).data('assistantFeed')) {
			d.showForm();
		}
	};

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

	jQuery.fn.af_isValid = function() {
		var d;
		if (d = jQuery(this).data('assistantFeed')) {
			return d.isValid();
		}
		return false;
	};

	jQuery.fn.af_getValueArray = function() {
		var d;
		if (d = jQuery(this).data('assistantFeed')) {
			return d.getValueArray();
		}
		return {
			'title' : '',
			'content' : ''
		};
	};

})();
