function Poll()
{
	this.pollId = 0;
	this.optionId = 0;
	this.formId = 'poll_form_id';
	this.resultTargetId = 'poll_container_div_id';
	this.voteButtonId = 'vote_button_item_id';
	this.optionsName = 'poll_vote';
	this.radioGroupId = 'poll_votes_items';
	
	this.errorWarning = 'Er is een probleem opgetreden bij het ophalen van de data.';
	this.ajaxAction = null;
	this.classPrefix = null;
};

Object.extend(Poll.prototype,
{
	init : function(ajaxAction, classPrefix)
	{
	   this.ajaxAction = ajaxAction;
	   this.classPrefix = classPrefix;
	   try { 
			var voteButton = $(this.voteButtonId);
			var nodes = Form.getInputs(myPoll.formId, 'radio', this.classPrefix + '-' + this.optionsName);
			var selectedItem = $A(nodes).find(function(node) { 
				return node.checked; 
			});
			try {
				// now disable these radio buttons:
				nodes.invoke('disable');
				var pollId = $(myPoll.formId).action.replace(/^.*\/poll\/([0-9]+)\/vote\.html$/ig, '$1');
				// set selectedItem id behind the ajaxCall url
				this.ajaxAction += '&' + this.classPrefix + '-voteId=' + selectedItem.value;
				this.vote(pollId, selectedItem.value); 
			} catch (e) {
				 nodes.invoke('enable');
				alert('Er dient wel een keuze gemaakt te zijn!');
			}
			Event.stop(e);
		} catch (error) {
			// prevent error. No poll available.
		}
	},
	
	
	vote : function(pollId, optionId)
	{
		// handling vote
		new Ajax.Request(myPoll.ajaxAction,
		  {
			method:'get',
			onSuccess: function(transport){	
				var response = transport.responseText || myPoll.errorWarning;
			  	var targetId = myPoll.resultTargetId;
			  	while($(targetId).hasChildNodes())
			  		$(targetId).removeChild($(targetId).lastChild);
			  	var myObject = eval('(' + response + ')');
			  	$(targetId).innerHTML = myObject.body;
			  	$('poll_reactions_button').observe('click', function(event) {
					var link = document.createElement('a');
					link.href = '/ajax_server.php?act=poll&sub=uitslag&id=' + pollId + '&output=text';
					link.rel = 'lightbox_ajax|500|500|~href~';
					myLightbox.start(link, 1, 1);
					Event.stop(event); 
				});
			},
			onFailure: function(){ 
				$(this.resultTargetId).innerHTML = myPoll.errorWarning;
			}
		  });
		
	}
});

// activate the poll handling
function initPoll() { 
	myPoll = new Poll()
}
Event.observe(window, 'load', initPoll, false);