var poll = Class.create();

poll.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateImageList and then
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {
		
		if($('surveyRight'))
			$('surveyRight').observe('submit', this.send);
			
		if($('nextTip'))
			$('nextTip').observe('click', this.changeTip);
		
	},
	
	changeTip: function() {
		Effect.toggle('currentTip', 'appear', { 
												duration: 0.3,
												afterFinish: function (obj) { 
													$('currentTip').update('Další tip'); 
													Effect.toggle('currentTip', 'appear', { delay: 0.2, duration: 0.3 });
												}
												});		
	},
	
	send: function(e) {
		
		$('surveySubmit').value = 'hlasuji...';
		$('surveySubmit').disabled = 'disabled';
		
		var url = '/_actions.php';		
		
		new Ajax.Request(url, {
		  method: 'post',
		  parameters: $('surveyRight').serialize(true),
		  onComplete: function(transport) {
		  	/*
		    var notice = $('notice');
		    if (transport.responseText.match())
		      notice.update('Yeah! You are in the Top 10!').setStyle({ background: '#dfd' });
		    else
		      notice.update('Damn! You are beyond #10...').setStyle({ background: '#fdd' });
		    */
		  	myPoll.completed(transport.responseText);
		  }
		});

		Event.stop(e);
	},
	
	reset: function() {
		$('surveySubmit').value = 'hlasovat';
		$('surveySubmit').disabled = '';
	},
	
	completed: function(responseText) {
		/*
		new Effect.Appear('newComment', { duration: 0.2, from: 1, to: 0 });
		
		$('comments').insert(new Template('<div class="comment"><strong class="userName">#{username}</strong> <span class="datum">před minutou</span><p class="text">#{comment}</p></div>')
	    .evaluate({
	      username: myComments.myUsername,
	      comment: myComments.myComment
	    })); 
	    */
		if(responseText.match('ERR_not_checked')){
			alert("Vyberte prosím odpověď!");
			this.reset();
		}
		if(responseText.match(1)){
			this.updateSurvey();
		}
	},
	
	updateSurvey: function() {
		$('surveyRight').update('Děkujeme za váš hlas');
		new Ajax.Updater('surveyRight', '/_actions.php?action=poll_results', {
		  insertion: Insertion.Bottom
		});
	}
}

function initPoll(){ myPoll = new poll();}
Event.observe(window, 'load', initPoll, false);
