/*
vote_story.js: ajax for voting on a story or stories

Require: tw-sack.js, prototype.lite.js (or: prototype.js), moo.fx.js

*/

var requestFile = '/score/vote';
var object_type = 'story';

var story_voted_image = IMG_DOMAIN + '/forums/voted.gif';

/* story vote handler -- Ajax Class */
function StoryVote(story_id, score) {
	/* init sack ajax */
	var ajax = new sack();
	ajax.requestFile = requestFile;
	ajax.method= 'GET';
	ajax.setVar('object_type', 'story');
	ajax.setVar('object_id', story_id);
	ajax.setVar('score', score);

	this.ajax = ajax;
	this.ajax.voteObj = this;

	this.story_id = story_id;
	this.add_score = score;
	this.score_text = document.getElementById('score_text_' + story_id);
	this.vote_op = document.getElementById('vote_op_' + story_id);
	this.hint_area = document.getElementById('story_hint_' + story_id);
	this.story_bad = document.getElementById('story_bad_' + story_id);
	
	/* event */
	this.whenVoting = function() {
        var eff1 = new fx.Opacity(this.score_text, {duration: 200 });
        var eff2 = new fx.Opacity(this.vote_op, {duration: 200 });
        eff1.setOpacity(0.4);
        eff2.setOpacity(0.4);
	};
	
	/* event: ajax callback */
	this.whenVoted = function() {
		var voteObj = this.voteObj;
		var res = this.response;
		var msg = '';

		if(res == 'NOT_LOGINED') {
			msg = ZORPIA_JS_LANG.NOT_LOGINED;
			voteObj.hint_area.innerHTML = msg;
			document.location.href = '/login?goto=' + 
				escape(document.location.href);
			return;
		}
		
		if(res == 'SUCCESS') {
			var old_score = parseInt(voteObj.score_text.innerHTML);
			voteObj.score_text.innerHTML = old_score + voteObj.add_score;
		}
		else if(res == 'VOTED_BEFORE') {
			msg = ZORPIA_JS_LANG.VOTED_BEFORE;
		}
		else if(res == 'ERROR_SERVER' || res == 'ERROR_SCORE' 
			|| res == 'ERROR_OBJECT') {
			msg = ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR;
		}

		if(msg) {
			voteObj.hint_area.innerHTML = msg;
			voteObj.hint_area.style.display = 'block';
			setTimeout('hide_hint('+story_id+')', 4000);
		}
		voteObj.vote_op.innerHTML = '<img src="' + story_voted_image + '">';
		if(voteObj.story_bad) voteObj.story_bad.style.display = 'none';

        var eff1 = new fx.Opacity(voteObj.score_text, {duration: 100 });
        var eff2 = new fx.Opacity(voteObj.vote_op, {duration: 100 });
        eff1.setOpacity(1);
        eff2.setOpacity(1);

	};

	/* evnet: ajax error callback */
	this.whenVoteError = function() {
		var hint_area = this.voteObj.hint_area;
		var errmsg = ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR;
		hint_area.innerHTML = errmsg;
		hint_area.style.display = 'block';
	};

	this.ajax.onCompletion = this.whenVoted;
	this.ajax.onError = this.whenVoteError;
}

/* main function */
function vote(clickedObj, story_id, score) {
	if(!(story_id && score)) return;

	/* avoid reduplicate voting action */
	clickedObj.onclick = function() {};

	var myVote = new StoryVote(story_id, score);
	myVote.whenVoting();
	myVote.ajax.runAJAX();;
}

function hide_hint(story_id) {
	var hint_area = document.getElementById('story_hint_' + story_id);
	hint_area.style.display='none';
}
