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';
	
	// activate the init function
	this.init();
	this.errorWarning = 'Er is een probleem opgetreden bij het ophalen van de data.';
};

Object.extend(Poll.prototype,
{
	init : function()
	{
		try { 
			var voteButton = $(this.voteButtonId);
			voteButton.observe('click', function(e) {
			 var nodes = Form.getInputs(myPoll.formId, 'radio', 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');
                    myPoll.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('/ajax_server.php?act=poll&sub=poll_vote&id=' + pollId + '&voteId=' + optionId + '&output=text',
		  {
		    method:'get',
		    onSuccess: function(transport){	
		    	var response = transport.responseText || myPoll.errorWarning;
		      	var targetId = myPoll.resultTargetId;
		      	while($(targetId).hasChildNodes())
		      		$(targetId).removeChild($(targetId).lastChild);
		      	$(targetId).innerHTML = response;
		      	$('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);