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

	this.showHideThankUser = function(postID) {
    var userDiv = document.getElementById('thankUser-'+postID);
    if (userDiv) {
      if (userDiv.className == 'hidden') {
        userDiv.className = '';
      } else {
        userDiv.className = 'hidden';
      }
    }
	}

	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;
              div = document.getElementById('thankStats'+postID);
              div.innerHTML = post.childNodes[j].firstChild.nodeValue;
              if (post.childNodes[j].firstChild.nodeValue == '')
                div.className += ' hidden';
              else
                div.className = div.className.replace(/hidden/, '').replace(/ hidden/, '');
              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++) {
            userThanksCredits[j].nextSibling.innerHTML = userThanks[i].firstChild.nodeValue;
          }
        }
			}
			
  		// Hide Button
  		var thankPostButtons = document.getElementsByName("thankPostButton"+thankOmat.postID);
      for (var i=0; i < thankPostButtons.length; i++) {
        thankPostButtons[i].parentNode.className += ' hidden';
      }
			thankOmat.ajaxRequest = null;
      thankOmat.postID = 0;
		}
	}
}

var thankOmat = new ThankOmat();
