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

	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+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;
		this.ajaxRequest.openGet('index.php?action=ThankDelete&output=xml&userID='+userID+'&postID='+postID+SID_ARG_2ND, this.handleResponse);
	}
	
	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 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).innerHTML = post.childNodes[j].firstChild.nodeValue;
            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.hasClassName('hidden')) {
                div.hide();
                div.removeClassName('hidden');
                div.innerHTML = post.childNodes[j].firstChild.nodeValue;
                div.blindDown();
              }
              else {
                div.innerHTML = post.childNodes[j].firstChild.nodeValue;
              }

              var userDiv = document.getElementById('thankUser-'+postID);
              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;
          }
          
          var userThanksCredits = document.getElementsByName('userThanks'+userID);
          for (var j=0; j < userThanksCredits.length; j++) {
            var element = $(userThanksCredits[j].nextSibling);
            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);
          element.fade({ duration: 0.8 });
        }
			}
			thankOmat.ajaxRequest = null;
      thankOmat.postID = 0;
		}
	}
}

var thankOmat = new ThankOmat();
