
if (!itdr) var itdr = new Object();
if (!itdr.classes) itdr.classes = new Object();
if (!itdr.classes.Comments) itdr.classes.Comments = new function () {
	// private members
	var basePath = "/darkroom/";
	var _allow = false;
	var _regRequired = false;
	var _regAllowed = false;
	var _blogid;
	var _submit = "/darkroom/common/php/comments.php";
	var _comments = "/darkroom/mt/mt-comments.cgi";
	// these are default values from the defualt, lets just use them, even if they are confugsing
	var mtcmtauth = itdr.classes.Cookie.get("mtcmtauth"); // "commenter name"
	var mtcmthome = itdr.classes.Cookie.get("mtcmthome"); // "commenter url"
	var mtcmtmail = itdr.classes.Cookie.get("mtcmtmail"); // "commenter email"
	// these next four, i assume, are cookies set by the comments application, not by javascript
	var commenter_name;
	var commenter_id;
	var commenter_url;
	var commenter_blog_ids;
	// stats hit
	var loader = new itdr.classes.AjaxConnection();
	loader.setVariable("t", "1f3870be274f6c49b3e31a0c6728957f");
	loader.setVariable("a", "create_comment");
	// stats hit
	var counter;
	// private functions
	function init () {
		// if registration is allowed, parse cookie info to see if they're logged in
		if (_regAllowed) {
			var bits = unescape(itdr.classes.Cookie.get("commenter_id")).split(":");
			commenter_name = itdr.classes.Cookie.get("commenter_name");
			commenter_id = bits[0] || "";
			commenter_blog_ids = bits[1] || "";
			commenter_url = itdr.classes.Cookie.get('commenter_url');
		}
		// log the stats hit
		counter = new Image();
		if (!commenter_id) commenter_id = '';
		counter.src = "/stats.php?u=" + escape(document.location.href) + "&r=" + escape(document.referrer) + "&a=" + commenter_id + "&b=1&t=&i=";
		// get all theform locations
		var forms = itdr.func.getElementsByClassName("CommentForm");
		for (var i=0; i<forms.length; ++i) setupForm (forms[i]);
	}
	function setupForm (form) {
		var entry_id = form.id.split("-").pop();
		var disableUser = false;
		var disableForm = false;
		var status = "";
		var user_block = "";
		var action = "";
		if (_allow) {
			if (_regAllowed) {
				if (commenter_name && (commenter_id=="" || commenter_blog_ids.indexOf("'" + _blogid + "'")>-1)) { // user is signed in
					status = "You're Signed In";
					// since we're logged in, disable the inputs
					disableUser = true;
					// figure out the urser uri.  lets's start with the default url given to us
					var user_uri = commenter_url;
					if (commenter_id!="") { // they are logged in with MT naitive, so lets make a link to their account instead
						user_uri = _comments + '?__mode=edit_profile&commenter=' + commenter_id + '&blog_id=' + _blogid;
						if (entry_id) user_uri += '&entry_id=' + entry_id;
						else user_uri += '&static=1';
					}
					// create the user block
					if (user_uri=="") user_block = commenter_name; // no uri, so just use thier name
					else user_block = '<a href="' + user_uri + '">' + commenter_name + '</a>'; // link the name and the uri
					// add an action
					action = '<a href="' + _comments + '?__mode=handle_sign_in&static=1&logout=1&entry_id=' + entry_id + '">Sign Out</a>';
				} else if (commenter_name) {
					disableForm = true;
					status = "Insufficient Permissions";
					action = '<a href="' + _comments + '?__mode=handle_sign_in&static=1&logout=1&entry_id=' + entry_id + '">Sign In/Out</a>';
				} else {
					if (_regRequired) {
						disableForm = true;
						status = "Registration Required";
						action = '<a href="' + _comments + '?__mode=login&entry_id=' + entry_id + '&blog_id=' + _blogid + '&static=1">Sign In</a>';
					} else {
						status = "Commenting Anonymously";
						action = '<a href="' + _comments + '?__mode=login&entry_id=' + entry_id + '&blog_id=' + _blogid + '&static=1">Sign In (Optional)</a>';
					}
				}
			} else {
				status = "Commenting Anonymously";
				action = '<a href="' + _comments + '?__mode=login&entry_id=' + entry_id + '&blog_id=' + _blogid + '&static=1">Sign In (Optional)</a>';
			}
		} else {
			disableForm = true;
			status = "Comments Disabled";
		}
		var greeting = new Array();
		if (status!="") greeting.push(status);
		if (user_block!="") greeting.push(user_block);
		if (action!="") greeting.push(action);
		greeting = greeting.join(" | ");
		var form_string = '\
<form style="position:relative" method="post" action="javascript:itdr.classes.Comments.click(' + entry_id + ');">\
	<input name="entry_id" value="' + entry_id + '" type="hidden">\
	<table>\
		<tr><td colspan="2">\
			<div class="CommenterGreeting">' + greeting + '</div>\
		</td></tr>\
		<tr><td>\
			<div class="CommentFormInput">\
				<label>Your Name:</label>\
				<input ' + (commenter_name!==""||mtcmtauth!="" ? 'class="Valid" ' : "") + (disableUser||disableForm ? 'disabled="disabled" ' : "") + 'name="author" value="' + (commenter_name ? commenter_name : mtcmtauth) + '" onblur="javascript:itdr.classes.Comments.blur(this);" onfocus="javascript:itdr.classes.Comments.focus(this);" />\
			</div>\
		</td><td rowspan="5">\
			<div class="CommentFormTextArea">\
				<label>Your Message:</label>\
				<textarea ' + (disableForm ? 'disabled="disabled" ' : "") + 'name="text" rows="6" cols="50" onblur="javascript:itdr.classes.Comments.blur(this);" onfocus="javascript:itdr.classes.Comments.focus(this);"></textarea>\
			</div>\
		</td></tr>\
		<tr><td>\
			<div class="CommentFormInput">\
				<label>Your Email Address:</label>\
				<input ' + (commenter_url!==""||mtcmtmail!="" ? 'class="Valid" ' : "") + (disableUser||disableForm ? 'disabled="disabled" ' : "") + 'name="email" value="' + (commenter_url ? commenter_url : mtcmtmail) + '" onblur="javascript:itdr.classes.Comments.blur(this);" onfocus="javascript:itdr.classes.Comments.focus(this);" />\
			</div>\
		</td></tr>\
		<tr><td>\
			<div class="CommentFormInput">\
				<label>Your Url:</label>\
				<input ' + (commenter_url!==""||mtcmthome!="" ? 'class="Valid" ' : "") + (disableUser||disableForm ? 'disabled="disabled" ' : "") + 'name="url" value="' + (commenter_url ? commenter_url : mtcmthome) + '" onblur="javascript:itdr.classes.Comments.blur(this);" onfocus="javascript:itdr.classes.Comments.focus(this);" />\
			</div>\
		</td></tr>\
		<tr><td>\
			<div class="SubmitButton">\
				<input value="Submit" name="submit" type="submit">\
			</div>\
			<div class="CommentMemory">\
				<input ' + (disableUser||disableForm ? 'disabled="disabled" ' : "") + (mtcmtauth||mtcmthome||mtcmtmail ? 'checked="checked" ' : "") + 'onclick="javascript:itdr.classes.Comments.toggle(\'' + entry_id + '\');" name="store" type="checkbox" />\
				<label>Remember personal info?</label>\
			</div>\
		</td></tr><tr><td>\
			<div class="ServerResponse">\
				<input name="response" disabled="disabled" value="' + (disableForm ? '' : "") + '" />\
			</div>\
		</td></tr>\
	</table>\
</form>';
		form_string = form_string.split("\t").join("").split("\n").join("").split("\t").join("");
		form.innerHTML = form_string;
	}
	function updateComments (id, newComments) {
		// replace the comments text
		var entry = document.getElementById("entry-" + id);
		var CommentsContainer = itdr.func.getElementsByClassName("CommentsContainer", "div", entry)[0];
		CommentsContainer.innerHTML = newComments;
		// update any numbers showing
		var count = itdr.func.getElementsByClassName("Comment", "div", CommentsContainer).length;
		var CommentCounts = itdr.func.getElementsByClassName("CommentCount", "*", entry);
		for (var i=0; i<CommentCounts.length; ++i) {
			var CommentCount = CommentCounts[i];
			CommentCount.innerHTML = count;
		};
		// also update the comments class in the wrapper
		var CommentsWrapper = itdr.func.getElementsByClassName("CommentsWrapper", "div", entry)[0];
		var classes = CommentsWrapper.className.split(" ");
		classes.pop();
		classes.push("Comment"+count);
		CommentsWrapper.className = classes.join(" ");
	}
	function showResponse (id, response) {
		var form = document.getElementById("comment-form-" + id).getElementsByTagName("form")[0];
		form.response.value = response;
	}
	function killLoader (id) {
		var commenent_form = document.getElementById("comment-form-" + id);
		var form = commenent_form.getElementsByTagName("form")[0];
		var loader = itdr.func.getElementsByClassName("CommentFormLoader", "div", commenent_form)[0];
		form.removeChild(loader);
	}
	function showLoader (id) {
		var commenent_form = document.getElementById("comment-form-" + id);
		var form = commenent_form.getElementsByTagName("form")[0];
		var table = commenent_form.getElementsByTagName("table")[0];
		var loader = document.createElement("div");
		loader.innerHTML = '<div style="background:#fff;width:' + table.offsetWidth + 'px;height:' + table.offsetHeight + 'px;opacity:.5;filter:alpha(opacity=50);"></div><img style="position:absolute;top:' + Math.round((table.offsetHeight-32)/2) + 'px;left:' + Math.round((table.offsetWidth-32)/2) + 'px" src="/darkroom/blog/images/loader.gif" />';
		loader.style.position = "absolute";
		loader.style.top = table.offsetTop + "px";
		loader.style.left = table.offsetLeft + "px";
		loader.style.zIndex = "10000";
		loader.className = "CommentFormLoader";
		form.insertBefore(loader, table);
	}
	function showLastPost (id) {
		itdr.classes.CommentSpace.toggle('entry-'+id, 'CommentsWrapper');
		var entry = document.getElementById("entry-" + id);
		var CommentsWrapper = itdr.func.getElementsByClassName("CommentsWrapper", "div", entry)[0];
		CommentsWrapper.scrollTop = CommentsWrapper.scrollHeight;
	}
	// public methods
	this.setBasePath = function (path) {
		basePath = path;
	}
	this.setBlogID = function (id) {
		_blogid = id;
	}
	this.allow = function (bool) {
		_allow = bool;
	}
	this.registration = function (allow, require) {
		_regAllowed = allow;
		_regRequired = require;
	}
	this.click = function (id) {
		showLoader(id);
		var form = document.getElementById("comment-form-" + id).getElementsByTagName("form")[0];
		loader.setVariable("author", form.author.value);
		loader.setVariable("email", form.email.value);
		loader.setVariable("text", form.text.value);
		loader.setVariable("entry_id", form.entry_id.value);
		loader.setVariable("url", form.url.value);
		loader.sendAndLoad(basePath + "common/php/comments.php", "POST");
		loader.onLoad = function () {
			var xml = this.getXML();
			var status = xml.getElementsByTagName("status")[0].firstChild.nodeValue;
			var response = xml.getElementsByTagName("response")[0].firstChild.nodeValue;
			killLoader(id);
			showResponse(id, response);
			if (status=="true") {
				var comments = xml.getElementsByTagName("comments")[0].firstChild.nodeValue;
				updateComments(id, comments);
				showLastPost(id);
				form.text.value = "";
				form.text.onblur();
			}
		}
	}
	this.blur = function (input) {
		var isValid = input.value!="" && input.value!=undefined && input.value!=null;
		if (isValid) input.className = "Valid";
		else input.className = "";
	}
	this.focus = function (input) {
		input.className = "Focused";
	}
	this.toggle = function (id) {
		var form = document.getElementById("comment-form-" + id).getElementsByTagName("form")[0];
		var newstate = form.store.checked;
		if (newstate==true) {
			itdr.classes.Cookie.set("mtcmtauth", form.author.value, 365);
			itdr.classes.Cookie.set("mtcmtmail", form.email.value, 365);
			itdr.classes.Cookie.set("mtcmthome", form.url.value, 365);
		} else {
			itdr.classes.Cookie.kill("mtcmtauth");
			itdr.classes.Cookie.kill("mtcmtmail");
			itdr.classes.Cookie.kill("mtcmthome");
		}
	}
	// do everything on dom load
	itdr.func.DOMLoad(init);
}

// a little backwards compatibility
if (!thetainteractive) var thetainteractive = itdr;
if (!intothedarkroom) var intothedarkroom = itdr;
