/* START POLL FUNCTIONS */


function poll_vote(poll_id)
{
	var error = "";
	$('poll_feedback_div').className= "feedback_div";

	//check if an option is selected
	var poll_opinions = $('poll_vote_form').elements['poll_opinion'];
	var checked_opinion = "";

	for (var i=0; i<poll_opinions.length; i++)
	{
		if( poll_opinions[i].checked )
		{
			checked_opinion = poll_opinions[i];
		}
	}

	if (checked_opinion == "")
	{
		error +=  ERROR_NO_SELECTION +".<br />";
	}

	//show error if any
	if ( !error.empty() )
	{
		$('poll_feedback_div').addClassName('red_border');
		$('poll_feedback_div').update(error);
		if ( $('poll_feedback_div').getStyle('display') == 'none' )
		{
			Effect.BlindDown('poll_feedback_div', { duration: 0.5 });
		}
		return false;
	}
	//hide feedback
	$('poll_feedback_div').style.display = "none";

	//vote ajax
	var url = "/ajax.php";
	var mode = "poll_vote";
	
	var progress_bar_max_width = $('polloptions').offsetWidth-13;

	var pars = 'mode=' + mode + '&poll_id=' + poll_id + '&vote=' + checked_opinion.value + '&progress_bar_max_width=' + progress_bar_max_width;

	//update
	new Ajax.Updater('pollupdater', url,
		{
			method:'post',
			evalScripts: true,
			parameters: pars
		}
	);
}

function get_new_poll(to_id, type, poll_id, direction, timestamp)
{
	var progress_bar_max_width = $('polloptions').offsetWidth-13;
	//alert(progress_bar_max_width);
	
	//vote ajax
	var url = "/ajax.php";
	var mode = "get_new_poll";

	var pars = 'mode=' + mode + '&to_id=' + to_id + '&type=' + type + '&poll_id=' + poll_id + '&direction=' + direction + '&timestamp=' + timestamp + '&progress_bar_max_width=' + progress_bar_max_width;

	//update
	new Ajax.Updater('pollitem', url,
		{
			method:'post',
			evalScripts: true,
			parameters: pars
		}
	);
}

function delete_poll(poll_id)
{
	//vote ajax
	var url = "/ajax.php";
	var mode = "delete_poll";

	var pars = 'mode=' + mode + '&poll_id=' + poll_id;

	//update
	new Ajax.Updater('poll_feedback_div', url,
		{
			method:'post',
			parameters: pars,
			onComplete: function()
			{
				if ( $('poll_feedback_div').getStyle('display') == 'none' )
				{
					Effect.BlindDown('poll_feedback_div', { duration: 0.5 });
				}
			}
		}
	);

}


function adjust_progress_bars()
{
	//diplay the right width for the progress bars
	var highest_percentage = 0;
	var highest_percentage_id = 0;
	for (var i=0; i<result_array.length; i++)
	{
		if (result_array[i][1] > highest_percentage)
		{
			highest_percentage_id = result_array[i][0];
			highest_percentage = result_array[i][1];
		}
	}
	
	//calculate pixels per percent
	var ppp = ($('polloptions').offsetWidth-13) / highest_percentage;
	
	//set width for progress bars
	for (var i=0; i<result_array.length; i++)
	{
		$('progress_bar' + result_array[i][0]).style.width = Math.round(result_array[i][1] * ppp) + "px";
	}
}



/* END POLL FUNCTIONS */




