
var feedback_types = new Array('useful', 'funny', 'cool');

function set_element_html(element_id, text) {
	var element = document.getElementById(element_id);	
	element.innerHTML = text;
}

chainOnload(function(){
var preload = new Image();
for (var i = 0; i < feedback_types.length; i++) {
	var button_types = new Array('off', 'on', 'press');
	for (var j = 0; j < button_types.length; j++) {
		preload.src = '/i/new/btn/rate_' + feedback_types[i] + '_' + button_types[j] + '.gif';
	}
}
});

function ReviewFeedbackHandler(rid, previous_feedback, stats) {
	STATE_READY = 0;
	STATE_SUBMITTING = 1;
	STATE_ERROR = 2;

	this.rid = rid;

	this.state = STATE_READY;
	this.setting = previous_feedback;
	this.stats = stats;

	this.set_button_image = function(which, img) {
		i = document.getElementById('review_feedback_' + which + '_image.' + this.rid).src = '/i/new/btn/rate_' + which + '_' + img + '.gif';
	}	

	this.init = function(which) {
		for (var i = 0; i < feedback_types.length; i++) {
			if (this.setting[feedback_types[i]]) {
				this.set_button_image(feedback_types[i], 'press');
			} else {
				this.set_button_image(feedback_types[i], 'off');
			}
		}
	}
	
	function state_change_cb(handler) {
		return (function(req) {
			if (req.readyState == 4) {
				if ((req.status >= 200) && (req.status < 300)) {
					set_element_html('review_feedback_message.' + handler.rid, 'Saved. Thanks!');
					handler.state = STATE_READY;
				} else {
					set_element_html('review_feedback_message.' + handler.rid, 'Error, try again later');
					handler.state = STATE_ERROR;
				}
			}
		});
	}
	
	this.button_click = function(which) {
		if (this.state != STATE_READY) {
			return;
		}

		if (!this.setting[which]) {
			this.set_button_image(which, 'press');
			set_element_html('review_feedback_message.' + this.rid, '<span style="color:gray;">Saving...</span>');
			this.state = STATE_SUBMITTING;
			this.setting[which] = true;
			this.stats[which]++;
			set_element_html('review_feedback_stats.' + this.rid + '.' + which, '(' + this.stats[which] + ')');
			// append the nocache argument to prevent Safari (and other browsers?) from caching request			
			var opt = {
			 method: 'get',			 
			 onComplete:state_change_cb(this)
			};
			(new Ajax.Request('/review_feedback?rid=' + encodeURIComponent(this.rid) + '&amp;fb=' + encodeURIComponent(which) + '&amp;state=on&amp;nocache=' + (new Date()).getTime(), opt));
			
		} else {
			// withdraw feedback
			this.set_button_image(which, 'off');
			set_element_html('review_feedback_message.' + this.rid, '<span style="color:gray;">Saving...</span>');
			this.state = STATE_SUBMITTING;
			this.setting[which] = false;
			this.stats[which]--;
			set_element_html('review_feedback_stats.' + this.rid + '.' + which, this.stats[which] <= 0 ? '' : '(' + this.stats[which] + ')');
			// append the nocache argument to prevent Safari (and other browsers?) from caching request
			var opt = {
			 method: 'get',			 
			 onComplete:state_change_cb(this)
			};
			(new Ajax.Request('/review_feedback?rid=' + encodeURIComponent(this.rid) + '&amp;fb=' + encodeURIComponent(which) + '&amp;state=off&amp;nocache=' + (new Date()).getTime(), opt));
		}
	}

	this.mouseover = function(which) {
		if ((this.state == STATE_READY) && (!this.setting[which])) {
			this.set_button_image(which, 'on');
		}
	}

	this.mouseout = function(which) {
		if ((this.state == STATE_READY) && (!this.setting[which])) {
			this.set_button_image(which, 'off');
		}
	}
}

var review_feedback_handlers = new Array();