function ThankOmat() {
	this.ajaxRequest = null;
	this.postID = 0;
	this.containers = new Object();

	this.showHideThankUser = function(postID) {
		document.getElementById('thankStatsLink-'+postID).blur();
		var element = $('thankUser-'+postID);
		if (element == null) {
			return;
		}
		
		if (element.visible()) {
			element.blindUp({
				duration: Math.sqrt(element.getHeight())/40
			});
		}
		else {
			element.blindDown({
				duration: Math.sqrt(element.getHeight())/40
			});
		}
	}

	this.thankPost = function(postID) {
		if (this.ajaxRequest != null) return;
		this.ajaxRequest = new AjaxRequest();
		this.postID = postID;
		if (this.ajaxRequest.openGet('index.php?action=Thank&output=xml&postID='+postID+'&t='+SECURITY_TOKEN+SID_ARG_2ND, this.handleResponse)) {
			var imgs = document.getElementsByName('thankImg'+postID);
			for (var i = 0; i < imgs.length; i++) {
				imgs[i].src = imgs[i].src.replace(/thankS\.png/, 'thankLoadS.gif');
				imgs[i].src = imgs[i].src.replace(/thankM\.png/, 'thankLoadM.gif');
			}
		}
	}

	this.deleteThank = function(postID, userID) {
		if (this.ajaxRequest != null) return;
		this.ajaxRequest = new AjaxRequest();
		this.postID = postID;
		if (this.ajaxRequest.openGet('index.php?action=ThankDelete&output=xml&userID='+userID+'&postID='+postID+'&t='+SECURITY_TOKEN+SID_ARG_2ND, this.handleResponse)) {
			var imgs = document.getElementsByName('thankImg'+postID);
			for (var i = 0; i < imgs.length; i++) {
				imgs[i].src = imgs[i].src.replace(/thankLoadS\.gif/, 'thankS.png');
				imgs[i].src = imgs[i].src.replace(/thankLoadM\.gif/, 'thankM.png');
			}
		}
	}
	
	this.handleResponse = function() {
		if (thankOmat.ajaxRequest.xmlHttpRequest.readyState == 4) {
			if (thankOmat.ajaxRequest.xmlHttpRequest.status != 200) {
				// throw an exception
				alert('ajax request http error '+thankOmat.ajaxRequest.xmlHttpRequest.status);
			}
			else if (thankOmat.ajaxRequest.xmlHttpRequest.responseText.indexOf('<?xml') == -1) {
				alert(thankOmat.ajaxRequest.xmlHttpRequest.responseText);
			}
			else {
				var xml = thankOmat.ajaxRequest.xmlHttpRequest.responseXML;
        
				// posts
				var redoImageResize = false;
				var posts = xml.getElementsByTagName("post");
				for (var i = 0; i < posts.length; i++) {
					var post = posts[i];
					var postID = 0;
					for (var j = 0; j < post.attributes.length; j++) {
						if (post.attributes[j].name == 'postID')
							postID = post.attributes[j].value;
					}
					if (postID == 0) continue;
					for (var j = 0; j < post.childNodes.length; j++) {
						if (post.childNodes[j].nodeName == "postText" && document.getElementById('postText'+postID)) {
							document.getElementById('postText'+postID).innerHTML = post.childNodes[j].firstChild.nodeValue;
							redoImageResize = true;
						} else if (post.childNodes[j].nodeName == "postThankStats") {
							var userDiv = document.getElementById('thankUser-'+postID);
							var oldClassName = '';
							if (userDiv) oldClassName = userDiv.className;

							var div = $('thankStats'+postID);
							if (post.childNodes[j].firstChild.nodeValue == '')
								div.blindUp();
							else if (!div.visible()) {
								div.innerHTML = post.childNodes[j].firstChild.nodeValue;
								div.blindDown();
							}
							else {
								div.innerHTML = post.childNodes[j].firstChild.nodeValue;
							}

							if (userDiv) userDiv.className = oldClassName;
						}
					}
				}
        
				// thank stats
				var userThanks = xml.getElementsByTagName("userThanks");
				for (var i = 0; i < userThanks.length; i++) {
					var userID = 0;
					for (var j = 0; j < userThanks[i].attributes.length; j++) {
						if (userThanks[i].attributes[j].name == 'userID')
							userID = userThanks[i].attributes[j].value;
					}

					if (thankOmat.containers[userID]) {
						for (var j = 0; j < thankOmat.containers[userID].length; j++) {
							var element = $(thankOmat.containers[userID][j]);
							
							if (userThanks[i].firstChild.nodeValue == '')
								element.blindUp();
							else if (element.innerHTML == '' || !element.visible()) {
								element.hide();
								element.innerHTML = userThanks[i].firstChild.nodeValue;
								element.blindDown();
							}
							else
								element.innerHTML = userThanks[i].firstChild.nodeValue;
						}
					}
				}
			}

			// Hide Button
			var thankPostButtons = document.getElementsByName("thankPostButton"+thankOmat.postID);
			for (var i=0; i < thankPostButtons.length; i++) {
				var element = $(thankPostButtons[i].parentNode);

				if (element.visible()) {
					element.fade({
						duration: 0.8
					});
				}
				else {
					element.appear({
						duration: 0.8
					});
				}
			}
			thankOmat.ajaxRequest = null;
			thankOmat.postID = 0;
			if (redoImageResize && ImageResizer != undefined) {
				new ImageResizer();
			}
		}
	}

	this.addCreditContainer = function(userID, containerID) {
		if (!this.containers[userID]) {
			this.containers[userID] = new Array();
		}
		this.containers[userID][this.containers[userID].length] = containerID;
	}
}

var thankOmat = new ThankOmat();
