IWP_Module_Comments = function (uniqueId, captchaImageAction, replyDialogAction, replyDialogTitle, replyDialogChildTitle, settingsDialogAction, settingsDialogTitle) {
	var self = this;

	this.uniqueId = uniqueId;
	this.captchaImageAction = captchaImageAction;
	this.replyDialogTitle = replyDialogTitle;
	this.replyDialogChildTitle = replyDialogChildTitle;
	this.replyDialogAction = replyDialogAction;
	this.settingsDialogAction = settingsDialogAction;
	this.settingsDialogTitle = settingsDialogTitle;

	this.initialised = false;
	this.form = null;
	this.replyDialogNode = null;
	this.draftText = '';

	this.requestNewCaptchaResponse = function (xml, clearInput) {
		if (typeof clearInput == 'undefined') {
			var clearInput = true;
		}
		var html = $('captchaimg', xml).text();
		if (html) {
			$('.captchaImageContainer', self.form).html(html);	//	replace the img html
			if (clearInput) {
				$(':input[name=captcha]', self.form).val('');		//	clear the captcha text input
			}
		}
	};

	this.requestNewCaptchaClick = function () {
		$(this).blur();
		$.post(self.captchaImageAction, self.requestNewCaptchaResponse);
		return false;
	};

	this._replyToCommentClick_rxp = /\#replyto\_(\d+)$/i;
	this.replyToCommentClick = function () {
		var id = false;
		if (self._replyToCommentClick_rxp.test(this.href)) {
			var id = parseInt(RegExp.$1, 10);
		}
		self.showReplyDialog(id);
		return false;
	};

	this._draftWatchTimer = null;
	this.draftWatchTimer = function () {
		var message = $('textarea[name=message]', self.form);
		if (message.length) {
			//	message field found, copy the value to draft for this page
			self.draftText = message.val();
		} else {
			//	message field not found, form must have closed - stop the timer
			self.endDraftWatch();
		}

	};

	this.beginDraftWatch = function () {
		self.endDraftWatch();
		self._draftWatchTimer = setInterval(self.draftWatchTimer, 250);
	};

	this.endDraftWatch = function () {
		try {
			clearInterval(self._draftWatchTimer);
		}
		catch (e) {
			//	do nothing if a browser complains about an invalid interval handle
		}
	};

	this.useDraftText = function () {
		//	if draft text has been saved, insert that into the textarea
		if (self.draftText) {
			$('textarea[name=message]', self.form).val(self.draftText);
		}
	};

	this.showReplyDialog = function (id) {
		var id = id || 0;

		var destinationNode;
		if (id) {
			destinationNode = $('#CommentItemReplyContainer_' + id);
			$('.CancelButton', self.form).show();
			$('.CommentsDialogHeading', self.form).text(self.replyDialogChildTitle);
		} else {
			destinationNode = $('#ContentRootReplyContainer_' + self.uniqueId);
			$('.CancelButton', self.form).hide();
			$('.CommentsDialogHeading', self.form).text(self.replyDialogTitle);
		}

		if (id !== self.replyingTo) {
			//	if the replying-to id has changed, move the form in the dom
			if (typeof tinyMCE != 'undefined') {
				var editor = tinyMCE.get("CommentsForm_"+ self.uniqueId +"_Message");
			} else {
				var editor = false;
			}

			if (editor) {
				tinyMCE.execCommand('mceRemoveControl', false, 'CommentsForm_'+ self.uniqueId +'_Message');
			}

			destinationNode.append(self.replyDialogNode);
			self.toggleReplyFormContainer(true);

			if (editor) {
				tinyMCE.execCommand('mceAddControl', false, 'CommentsForm_'+ self.uniqueId +'_Message');
			}

			self.replyingTo = id;
			$('input[name=replyto]', self.form).val(id);

			if (id) {
				//	using the commentend anchors, scroll to the end of the form

				var scrollTo = $('a[name=commentend_'+ id +']').offset().top - $(window).height() + 16;
				if (scrollTo < 0) {
					scrollTo = 0;
				}

				$.scrollTo(scrollTo, {
					duration: 'slow',
					easing: 'swing'
				});
			}
		}
	};

	this.settingsClick = function () {
		self.showSettingsDialog();
		return false;
	};

	this.showSettingsDialog = function () {
		//	display the thread settings dialog
		
		$.iModal({
			type: 'ajax',
			title: self.settingsDialogTitle,
			url: self.settingsDialogAction,
			width: 500
		});
	};

	this.cancelButtonClick = function () {
		self.showReplyDialog(0);
		return false;
	};

	this.htmlInfoLinkClick = function () {
		$('.CommentsHTMLInfoLink', self.form).hide();
		$('.CommentsHTMLInfo', self.form).fadeIn();
		return false;
	};

	this.toggleReplyFormContainer = function (toggle) {
		if (typeof toggle == 'undefined') {
			var toggle = null;
		}
		var dialog = $('#CommentsDialog_'+ self.uniqueId);
		if (toggle === true || dialog.hasClass('CommentsDialogHidden')) {
			dialog.removeClass('CommentsDialogHidden');
		} else {
			dialog.addClass('CommentsDialogHidden');
		}
		return false;
	};

	//	initialise function split in two to support ajax reply form being loaded separately from reply links

	this.initialiseForm = function (prefix) {
		//	initalise the reply form
		//	also doubles for initialising the settings form using since jquery can handle this ok with it's selectors

		if (typeof prefix == 'undefined') {
			var prefix = 'CommentsForm';
		}

		var form = $('#'+ prefix +'_' + self.uniqueId);
		if (self.form === null) {
			self.form = form;
		}

		if (form.length) {
			form.interspireAjaxForm({
				beforeSubmit: function (data) {
					if (typeof tinyMCE != 'undefined') {
						var editor = tinyMCE.get("CommentsForm_"+ self.uniqueId +"_Message");
						if (editor) {
							//	find the message field in the data and replace it with tinymce content
							for (var i = data.length; i--;) {
								if (data[i].name == 'message') {
									data[i].value = tinyMCE.get("CommentsForm_"+ self.uniqueId +"_Message").getContent();
								}
							}
						}
					}
					return true;
				},
				success: function (xml) {
					if ($('status', xml).text() == 1) {
						$.iModal.close();

						//	display custom messages
						$('customMessage', xml).each(function(){
							alert($(this).text());
						});

						//	reset our form
						form.each(function(){
							this.reset();
						});
					}
					self.requestNewCaptchaResponse(xml, false);
				}
			});

			$('.requestNewCaptchaLink', form).click(self.requestNewCaptchaClick);
			$('.CancelButton', form).click(self.cancelButtonClick);
			$('.CommentsHTMLInfoLink', form).click(self.htmlInfoLinkClick);
			$('#CommentsForm_'+ self.uniqueId +' legend a, #CommentsFormHiddenContainer_'+ self.uniqueId +' a').click(self.toggleReplyFormContainer);

			self.replyDialogNode = $('#CommentsDialog_' + self.uniqueId);
			self.useDraftText();
			self.beginDraftWatch();
		}

		//	check url to see if we should do anything immediately
		if (location.hash && location.hash.length > 2) {
			var hash = location.hash;

			if (/^#replyto_([0-9]*)$/i.test(hash)) {
				var id = parseInt(RegExp.$1, 10);
				self.showReplyDialog(id);
				location.hash = '#';
			}
		}
	};

	this.initialiseLinks = function () {
		//	initalise comment links

		$('#ContentComments_' + self.uniqueId + ' .CommentReplyLink').click(self.replyToCommentClick);
		$('#ContentComments_' + self.uniqueId + ' .CommentSettingsLink').click(self.settingsClick);
	};

	//	keep a static listf form instances
	IWP_Module_Comments.instances[this.uniqueId] = this;
};

IWP_Module_Comments.instances = {};
