var Poll = {

	formId 			: 'form#poll_form_id',
	resultTargetId 	: 'div#poll_container_div_id',	
	voteButtonId 	: 'button#vote_button_item_id',
	errorWarning 	:  'Er is een probleem opgetreden bij het ophalen van de data.',


	init : function() {
		try { 
			var myObj = this;
			jQuery(this.voteButtonId).click(function() {
	            try {
	            	var selectedValue = jQuery(myObj.formId + ' input:radio:checked').val();
	            	if (parseInt(selectedValue) >= 0)  {
		            	var pollId = jQuery(myObj.formId + ' input[type=hidden][name=pollId]').val();
		            	var questionId= jQuery(myObj.formId + ' input[type=hidden][name=questionId]').val();
		            	myObj.vote(pollId, questionId, selectedValue, 'vote');
	            	} else {
	            		alert('Er dient wel een keuze gemaakt te zijn.');
	            		return false;
	            	}
	            } catch (e) {
	            	jQuery(myObj.formId + ' input[type=radio]').attr('checked', false);
	            	alert('Er dient wel een keuze gemaakt te zijn.');
	            	return false;
	            }
			});
		} catch (error) {
			// prevent error. No poll available.
		}
	},
	
	
	vote : function(pollId, questionId, optionId, action) {
		// handling vote
		jQuery.ajax({	
			type: 'GET',
			cache: false,
			url:'/poll?action=' + action + '&pollId=' + pollId + '&questionId=' + questionId + '&answerId=' + optionId,
			error : function(httpRequest, textStatus, errorThrown) {
				jQuery(Poll.resultTargetId).html("Er is een fout opgetreden bij het verwerken van uw keuze.");
				return false;
			},
			success : function(response) {
		      	jQuery(Poll.resultTargetId).html(response); 
		      	jQuery('button#poll_reactions_button').click(function() {
                    jQuery('span#poll_reactions_button_span').fancybox({
        				'autoScale'			: false,
        				'width'				: 530,
                		'height'			: 530,
        				'transitionIn'		: 'none',
        				'transitionOut'		: 'none',
        				'type'				: 'iframe',
        				'href' 				: '/'+ pollId+ '?iframe=true&ot=erdee.LightboxPageLayout.ot'
        			});
                   	// activate the fancybox window
                    jQuery('span#poll_reactions_button_span').trigger('click');
                    return false;
                });
		    }
		});
		return false;
	}
}
