// JavaScript Document

var ajaxCommentRaters = new Array();

var ajaxCommentRatingsEl = "";

function ajaxCommentRatingsSubmit(comment, rating, outputEl, article, post, file, beta) {
	var urlToPhp = "/assets/scripts/comment_ratings.php?commentid=";
	
	var url = urlToPhp + comment + "&rating=" + rating + "&article=" + article + "&post=" +post + "&file=" + file + "&beta=" + beta;
		
	ajaxCommentRatingsEl = outputEl;
		
	ajaxCommentRatings.xmlHttp.onreadystatechange = function (){};
	ajaxCommentRatings.xmlHttp.abort();
	ajaxCommentRatings.xmlHttp.open("GET", url, true);
	ajaxCommentRatings.xmlHttp.onreadystatechange = ajaxCommentRatingsResponse;
	ajaxCommentRatings.xmlHttp.send(null);
}

function ajaxCommentRatingsResponse() {
	if(ajaxCommentRatings.xmlHttp.readyState == 4 && ajaxCommentRatings.xmlHttp.responseText.length > 0){
		ajaxCommentRatingsEl.innerHTML = ajaxCommentRatings.xmlHttp.responseText;
	}	
}

var ajaxCommentRatings = {
	article : 0,
	post : 0,
	file : 0,
	beta : 0,
	comment : 0,
	outputEl : "",
	formEl: "",
	xmlHttp : null,
	active : false,

	init : function(comment, article, post, file, beta) {
		this.comment = comment;
		this.article = article;
		this.post = post;
		this.file = file;
		this.beta = beta;
		
		this.outputEl = document.getElementById("rate_" + this.comment);
		this.formEl = document.getElementById("formrate_" + this.comment);
		
		this.xmlHttp = this.createXmlHttp();
		
		ajaxCommentRaters[this.comment] = this;
		
		if (this.xmlHttp) {
			var inputs = this.formEl.getElementsByTagName("input");
			var total = inputs.length;
			
			//prevent form being submitted
			this.formEl.onsubmit = function () {
				return false;
			};
			
			for (var i = 0; i < total; i++) {
				var cName = inputs[i].className;
				
				//inputs[i].submitRating = this.submitRating;
				inputs[i].comment = this.comment;
				inputs[i].article = this.article;
				inputs[i].post = this.post;
				inputs[i].file = this.file;
				inputs[i].beta = this.beta;
				inputs[i].outputEl = this.outputEl;
				
				if (cName.indexOf("rateup") >= 0) {
					inputs[i].onclick = inputs[i].onkeydown = function() {
						ajaxCommentRatingsSubmit(this.comment, +1, this.outputEl, this.article, this.post, this.file, this.beta);
						return false; // prevent form submitted	
					}
				} else if (cName.indexOf("ratedown") >= 0) {
					inputs[i].onclick = inputs[i].onkeydown = function() {
						ajaxCommentRatingsSubmit(this.comment, +1, this.outputEl, this.article, this.post, this.file, this.beta);
						return false; // prevent form submitted	
					}
				}
			}
		}
	},
	
	/* thanks Robert Nyman */
	createXmlHttp : function (){
		this.xmlHttp = null;
		if(typeof XMLHttpRequest != "undefined"){
			this.xmlHttp = new XMLHttpRequest();
		}
		else if(typeof window.ActiveXObject != "undefined"){
			try {
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			}
			catch(e){
				try {
					this.xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
				}
				catch(e){
					try {
						this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){
						this.xmlHttp = null;
					}
				}
			}
		}
		return this.xmlHttp;
	},

	submitRating : function(rating) {
		
	},
	
	closeSession : function (){
		delete ajaxCommentRatings;
		ajaxCommentRatings = null;
	},
	
	addEvent : function (elm, evt, func){
		if(elm){
			if(elm.addEventListener){
				elm.addEventListener(evt, func, false);
			}
			else if(window.attachEvent){
				elm.attachEvent(("on" + evt), func)
			}
		}
	}
}

ajaxCommentRatings.addEvent(window, "unload", function(){ajaxCommentRatings.closeSession();});