/*
vote_topic.js: ajax for voting a forum topic

require: tw-sack.js, prototype.lite.js (or: prototype.js), moo.fx.js
*/
var current_url = document.location.search || document.location.href;
var requestFile = '/score/vote';

var ajax = new sack();

var voting_object = 0; 	//forum_topic_id
var add_score = 0; 		//score to be added

function vote_topic(object_id, forum_id, object_user_id, group_id, school_id, score) {
	if(!voting_object) voting_object = object_id;
	if(!add_score) add_score = score;

	ajax.setVar('object_type', 'topic');
	ajax.setVar('object_id', object_id);
	ajax.setVar('forum_id', forum_id);
	ajax.setVar('object_user_id', object_user_id);
	ajax.setVar('group_id', group_id);
	ajax.setVar('school_id', school_id);
	ajax.setVar('score', score);
	ajax.requestFile = requestFile;
	ajax.method = 'GET';
	
	ajax.onLoading = whenVoteLoading;
//	ajax.onLoaded = whenVoteLoading;
//	ajax.onInteractive = whenVoteLoading;
	ajax.onCompletion = whenVoted;
	ajax.onError = whenVoteError;
	ajax.runAJAX();
}

function whenVoteLoading() {
    var eff1 = new fx.Opacity('score_text', {duration: 200 });
    var eff2 = new fx.Opacity('vote_op', {duration: 200 });
    eff1.setOpacity(0.4);
    eff2.setOpacity(0.4);
}

function whenVoteError() {
	var errstr = ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR;
	document.getElementById('vote_hint').innerHTML = errstr;
	voting_object = 0;
	add_score = 0;
}

function whenVoted() {
	var res = ajax.response;
	var msg = '';
	if(res == 'SUCCESS') {
		var obj = document.score_form.score_total;
		obj.value = parseInt(obj.value) + add_score;
		document.getElementById('score_text').innerHTML = '' + obj.value;
		var voted_img = '<img src="http://img.zorpia.com/forums/voted.gif">';
		document.getElementById('vote_op').innerHTML = voted_img;
	}
	else if(res == 'VOTED_BEFORE') {
		msg = ZORPIA_JS_LANG.VOTED_BEFORE;
	}
	else if(res == 'NOT_LOGINED') {
		msg = ZORPIA_JS_LANG.NOT_LOGINED;
		document.location.href = '/login?goto=' + current_url;
	}
	else if(res == 'ERROR_SERVER' || res == 'ERROR_SCORE' || res == 'ERROR_OBJECT') {
		msg = ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR;
	}
	document.getElementById('vote_hint').innerHTML = msg;
	document.getElementById('thumbs_down').style.display = 'none';
    var eff1 = new fx.Opacity('score_text', {duration: 100 });
    var eff2 = new fx.Opacity('vote_op', {duration: 100 });
    eff1.setOpacity(1);
    eff2.setOpacity(1);

	voting_object = 0;
	add_score = 0;
}
