oRcsf.RegisterObject('Module_Poll_Poll');
var RCSF_Plugin_Module_Poll_Poll =
{
	// This object is setup like this, where action_id is the action in the database
	// _moOptions : {
	//		 i_action_id : optionObject
	//		,i_action_id : optionObject
	// }
	_moOptions : {}
	
	/**
	 *
	 */
	,OnDomLoaded : function()
	{
		if(this._moOptions.b_show_result == true)
		{
			
		}
		else
		{
			this.AssignFormObservers();
		}
	}
	
	// TODO : ONLY ASSIGN IF BUTTON IS PRESENT--DONT SHOW ON RESULT
	,AssignFormObservers : function()
	{
		// Assign form handlers
		$$('.poll_submit input').each(function(oSubmit){
			$(oSubmit).observe('click', function(){
				var i_node_id = $(oSubmit).getAttribute('rel');
				this.SubmitPoll(i_node_id);
			}.bind(this))
		}.bind(this));
	}
	
	,SetPollOptions : function(iNodeId,oOptions)
	{
		this._moOptions[iNodeId] = oOptions;
	}
	
	,SubmitPoll : function(iNodeId)
	{
		var a_data = this._moOptions[iNodeId];
		var o_form = $('poll_' + a_data.i_poll_id + '_form');

		
		if(a_data.i_max_answers > 1)
			a_data.a_answers 	= this.GetPollValueFromCheckbox(o_form,'poll_' + a_data.i_poll_id + '_form_question_id_' + a_data.i_question_id);
		else
			a_data.a_answers 	= this.GetPollValueFromRadio(o_form,'poll_' + a_data.i_poll_id + '_form_question_id_' + a_data.i_question_id);
		
		this.moModule.moCore.RequestServer(
				a_data
			,{
			 	 s_mode			: 'data'
				,s_module		: 'poll'
				,s_controller	: 'poll'
				,s_action		: 'submitpoll'
				,o_scope		: this
				,s_callback		: 'OnPollSubmitted'
			}
		);
	}
	
	,OnPollSubmitted : function(oData) 
	{
		var options = this._moOptions[oData.a_result.a_options.i_node_id];
		if(oData.s_status == 'error')
		{
			$('poll_' + this._moOptions[oData.a_result.a_options.i_node_id].i_poll_id + '_container').update(oData.s_message);
		}
		else
		{
			$('poll_' + this._moOptions[oData.a_result.a_options.i_node_id].i_poll_id + '_container').update(oData.s_html);
		}
	}
	/**
	* 	sContainerId
	*	bUseAnimation
	*/
	,ShowResultBars : function(sContainerId,iNodeId,iNrVotes,bUseAnimation)
	{
		$$('#' + sContainerId + ' .poll_answer_result_bar_inner').each(function(oBar){
			var percent_width 	= oBar.up('div').getWidth() / 100;
			var o_perc_field 	= oBar.up().next('.poll_answer_result_perc');

			var i_total = oBar.getAttribute('rel') / iNrVotes * 100;
			if(bUseAnimation == 1)
			{
				$(oBar).morph('width:'+percent_width * i_total + 'px',{
					 duration 		: 2
					,afterUpdate 	: function(oEl){ 
						o_perc_field.update(Math.round(oBar.getWidth() / percent_width) + '%') 
					}.bind(this)
				});
			}
			else
			{
				$(oBar).style.width = percent_width * i_total + 'px';	
				o_perc_field.update(Math.round(i_total) + '%') 
			}
		}.bind(this));
	}
	
	
	/*
	* @param {form Object} or {form id} el
	* @param {radio group name} radioGroup
	*/
	,GetPollValueFromRadio : function(oForm, sGroupName) 
	{
		var checked = oForm.getInputs('radio', sGroupName).find(
   			function(re) 
   			{
   				return re.checked;
    		}
	   );
	   return checked ? [$F(checked)] : [];
	}
    
    /*
	* @param {form Object} or {form id} el
	* @param {radio group name} radioGroup
	*/
	,GetPollValueFromCheckbox : function(oForm, sGroupName) 
	{
		var a_answers = [];
		var checked = oForm.getInputs('checkbox',sGroupName).find(
   			function(re) 
   			{
   				if(re.checked)
	   				a_answers.push($F(re));
    		}
	    );
	   return a_answers;
    }
}