var IMG_SUBMIT = '/images/submit.gif';
var IMG_RESULTS = '/images/results.gif';
var KOEF = 15;
var INCREMENT_URL = '/incrementChoiceServlet';
var POLLS_LIST_URL = '/pollsListServlet';

var Poll = Class.create();
Poll.prototype = {
	initialize : function(json, container) {
		this.info = {id : 0, description : '', showResults : false, choices : []};
		Object.extend(this.info, json);
		this.headerBlock = Builder.node('div', {className : 'pollHeader'});
        this.footerBlock = Builder.node('div', {className : 'pollFooter'});
        this.borderBlock = Builder.node('div', {className : 'borderBlock'});
        this.choicesContainer = Builder.node('div', {className : 'choicesContainer'});
		this.mainBlock = Builder.node('div', {className : 'pollMain'}, [
			this.headerBlock,
            Builder.node('div', {className : 'back_dark'} ,[
                Builder.node('div', {className : 'infoPoll'}, this.info.description),
			    this.choicesContainer,
                Builder.node('div', {className : 'footerContainer'} ,[
                    this.borderBlock,
                    this.footerBlock
                ])
            ])
        ]);
		container.appendChild(this.mainBlock);
		this.show();
	},
	getVotes : function() {
		this.votes = 0;
		$A(this.info.choices).each(function(c) {
			this.votes += c.voices;
		}.bindAsEventListener(this));
		return this.votes;
	},
	show : function() {
		if (this.info.showResults) {
			this.headerBlock.innerHTML = 'Poll Results';
			this.footerBlock.innerHTML = 'Total votes : ' + this.getVotes();
		} else {
			this.headerBlock.innerHTML = 'Quick Poll';
			var bs = Builder.node('img', {src : IMG_SUBMIT, alt : 'submit'});
			var br = Builder.node('img', {src : IMG_RESULTS, alt : 'results'});
			Event.observe(bs, 'click', function() {
				if (this.selected) {
                    this.info.showResults = true;
                    new Ajax.Request((INCREMENT_URL + '?choiceId=' + this.selected.id + '&pollId=' + this.info.id), {
						method : 'get',
						onSuccess : function(t) {
                            if (t.responseText == 'true') {
								this.selected.voices++;
							}

                            this.show();
						}.bindAsEventListener(this),
						onFailure : function(t) {
							this.show();
						}.bindAsEventListener(this) 
					});
				}
			}.bindAsEventListener(this));
			Event.observe(br, 'click', function() {
                this.info.showResults = true;
                this.show();
			}.bindAsEventListener(this));
			this.footerBlock.innerHTML = '';
			this.footerBlock.appendChild(bs);
			this.footerBlock.appendChild(br);
		}
		this.showChoices();
	},
	showChoices : function() {
		this.choicesContainer.innerHTML = '';
		$A(this.info.choices).each(function(c) {
			var b;
			if (this.info.showResults) {
				var rateBlock = Builder.node('div');
				var rate;
				if (this.votes > 0)
					rate = Math.round(c.voices / this.votes * KOEF);
				else
					rate = 0;
				if (rate == 0 && c.voices > 0) {
					rate = 1;
				}
                for (var i = 1; i <= rate; i++) {
					rateBlock.appendChild(Builder.node('div', {className : 'rate'}, ' '));
				}
				for (var i = rate; i < KOEF; i++) {
					rateBlock.appendChild(Builder.node('div', {className : 'unrate'}, ' '));
				}
				rateBlock.appendChild(Builder.node('div', {className : 'voices'}, c.voices.toString()));
				rateBlock.appendChild(Builder.node('br', {clear : 'all'}));
				b = Builder.node('div', {className : 'results'}, [c.name, rateBlock]);
			} else {
				c.radio = Builder.node('input', {type : 'radio'});
				Event.observe(c.radio, 'click', function() {
					$A(this.info.choices).each(function(c) {
						c.radio.checked = false;
					}.bindAsEventListener(this));
					c.radio.checked = true;
					this.selected = c;
				}.bindAsEventListener(this));
				b = Builder.node('div', {className : 'choice'}, [c.radio, c.name]);
			}
			this.choicesContainer.appendChild(b);
		}.bindAsEventListener(this));
	}
}

Event.observe(window, 'load', function() {
	new Ajax.Request(POLLS_LIST_URL, {
		method : 'get',
		onSuccess : function(t) {
			var json = eval(t.responseText);
			$A(json).each(function(p) {
				new Poll(p, $('quickPollsContainer'));
			});
		}
	});
});

