
	YAHOO.namespace('Infinite');
	YAHOO.Infinite.CharCounter = function (editorId, element, position, min, max)
	{
		this.insertEl = YAHOO.util.Dom.get(element);
		this.insertPos = position;

		this.maxChars = max;
		this.minChars = min;

		if (this.maxChars)
		{
			this.mode = 'remain';
		}
		else
		{
			this.mode = 'used';
		}

		if (this.insertPos == 'after')
		{
			YAHOO.util.Dom.insertAfter(this.buildCounterHTML(editorId), this.insertEl);
		}
		else
		{
			YAHOO.util.Dom.insertBefore(this.buildCounterHTML(editorId), this.insertEl);
		}
	}

	YAHOO.Infinite.CharCounter.prototype = {
		insertEl: null,
		insertPos: null,

		charCounter: null,
		charCounterInput: null,

		minChars: 0,
		maxChars: null,
		currentChars: 0,

		mode: 'remain',

		buildCounterHTML: function (editorId) {
			this.charCounter = document.createElement("div");
			this.charCounter.id = editorId + "_charCounter";
			this.charCounter.className = "charcounter";

			this.charCounterInput = document.createElement("input");
			this.charCounterInput.type = "text";
			this.charCounterInput.disabled = true;

			this.update(0);

			this.charCounter.appendChild(this.charCounterInput);

			if (this.mode == 'used')
			{
				minCount = '';
				if (this.minChars > 0) { minCount = ' (' + this.minChars + ' min)'; }
				textNode = document.createTextNode(' characters used' + minCount);
			}
			else
			{
				textNode = document.createTextNode(' characters remaining (' + this.maxChars + ' max)');
			}

			this.charCounter.appendChild(textNode);

			return this.charCounter;
		},

		update: function (newChars) {
			this.currentChars = newChars;

			if (this.mode == 'remain')
			{
				this.charCounterInput.value = this.maxChars - newChars;
			}
			else
			{
				this.charCounterInput.value = newChars;
			}
		},

		/* Adapted from http://javascript.internet.com/snippets/remove-html-tags.html */
		stripHTML: function (input){
			return input.replace(/<\/?[^>]+(>|$)/g, "").replace(/&nbsp;/g, "").replace(/&amp;/, '&');
		}
	}

