/**
* jQuery smartArea plugin.
* Based on the version found here: http://www.ajaxbestiary.com/2007/11/14/smart-area-a-lightweight-resizing-text-area-plugin-for-jquery/
* NOTE: This plugin is tailored to work with AYTM website and will not work well with any other.
*/

jQuery.fn.smartArea = function(initRows, maxRows) {
	var initRowsCopy = initRows || 1;
	var maxRowsCopy = maxRows || 9;
	return this.each(function() {
		if (!jQuery(this).is("textarea")) {
			return false;
		}
		jQuery(this).click(function() {
			jQuery.SA.resizeArea(this, initRowsCopy, maxRowsCopy);
		}).keyup(function() {
			jQuery.SA.resizeArea(this, initRowsCopy, maxRowsCopy);
		});
		jQuery.SA.resizeArea(this, initRowsCopy, maxRowsCopy);
		return this;
	});
};

jQuery.SA = {
	measureBlock: null,
	measureBlockFor: null,
	measureBlockFontSize: null,
	measureBlockLineHeight: null,
	lastRows: 0,
	resizeArea: function(t, r, m) {
		if (t.value.length == 0) {
			t.rows = r > 1 ? r : 1;
		}
		else {
			jqt = $(t);			
			if (jQuery.SA.measureBlockFor != t) {
				// create div and recalculate params
				jQuery.SA.measureBlockFontSize = parseFloat(jqt.css("font-size"));
				jQuery.SA.measureBlockLineHeight = jQuery.SA.measureBlockFontSize;
				measureBlockFontFamily = jqt.css("font-family");
				measureBlockFontWeight = jqt.css("font-weight");	
				measureBlockWidth = jqt.width();
				if (jQuery.SA.measureBlock === null) {
					jQuery.SA.measureBlock = $("<textarea id='smart-area-measure-block' style='position:absolute;left:-9999px;top:0px;overflow-y:scroll;display:block;text-align:left;height:0px;width:" + measureBlockWidth + "px !important;text-align:left;font-size:" + jQuery.SA.measureBlockFontSize + "px !important;line-height:" + jQuery.SA.measureBlockLineHeight + "px !important;font-family:" + measureBlockFontFamily + ";font-weight:" + measureBlockFontWeight + " !important;'></textarea>");
					jQuery.SA.measureBlock.appendTo('body');
				} else {
					jQuery.SA.measureBlock.attr('style',"position:absolute;left:-9999px;top:0px;overflow-y:scroll;display:block;text-align:left;height:0px;width:" + measureBlockWidth + "px !important;text-align:left;font-size:" + jQuery.SA.measureBlockFontSize + "px !important;line-height:" + jQuery.SA.measureBlockLineHeight + "px !important;font-family:" + measureBlockFontFamily + ";font-weight:" + measureBlockFontWeight + " !important");
				}
				jQuery.SA.measureBlockFor = t;
				jQuery.SA.lastRows = 0;
			}
			jQuery.SA.measureBlock.val(t.value).scrollTop(10000);
			rows = 0;
			if (navigator.userAgent.indexOf('Firefox') != -1)
				rows = Math.ceil(Math.max(jQuery.SA.measureBlock.scrollTop(), r * jQuery.SA.measureBlockLineHeight) / jQuery.SA.measureBlockFontSize);
			else
				rows = Math.round(Math.max(jQuery.SA.measureBlock.scrollTop(), r * jQuery.SA.measureBlockLineHeight) / jQuery.SA.measureBlockFontSize);
			if (rows <= m) rows = (r > rows) ? r : rows;
			else rows = m;
			if (rows != jQuery.SA.lastRows) {
				t.rows = rows;
				jqt.css('overflow-y', (rows <= m) ? 'hidden' : 'scroll');
			}
			jQuery.SA.lastRows = rows;
		}
	}
};