/* requires jQuery 1.4+ */
var requiredMessage = 'Please choose an answer above.';


jq(document).ready(function() {

	jq('#poll-submit-js').show();
	jq('#poll-submit').hide();

	if (jq('.portletVotePortlet').length)
		jq('#poll-submit-js').html('<img src="/img/submit-small.png" alt="Submit" />');		
	else if (jq('.home-portlet').length)
		jq('#poll-submit-js').html('<img src="/img/home-trivia-submit.png" alt="Submit" />');		
	else
		jq('#poll-submit-js').html('<img src="/img/button-submit.png" alt="Submit" />');
	
	// question is visible
	if(jq('#form-qpv-question').length > 0) {
		
		jq('#poll-submit-js').click(pollSubmit);
		
	}
		
});

pollSubmit = function() {

	var answer_id = jq('input:radio[@name=answer_id]:checked').val();

	if (answer_id == undefined) {
		if (jq('#poll-submit-js').length == 0)
			return false;
			
		if (jq('#qcvote-error-message').length == 0)
			jq('<p id="qcvote-error-message">' + requiredMessage + '</p>').insertBefore('#poll-submit-js');
		jq('#qcvote-error-message').show().delay(3000).fadeOut();
		return false;
	}

	jq('#form-qpv-question').wrap('<div class="qpv-results" />');
	
	if (jq('.portletVotePortlet').length)
		jq('#poll-submit-js').html('<img src="/img/next-small.png" alt="Next" />').attr('id', 'poll-next-js');
	else if (jq('.home-portlet').length)
		jq('#poll-submit-js').html('<img src="/img/home-trivia-next.png" alt="Next" />').attr('id', 'poll-next-js');		
	else
		jq('#poll-submit-js').html('<img src="/img/button-next.png" alt="Next" />').attr('id', 'poll-next-js');
	
	jq('#poll-next-js').click(nextQuestion);

	jq.ajax({
		url: 'vote_process?question_id=' + jq('#qpv-question-id').val() + '&answer_id=' + answer_id + '&ajax=1',
		returnType: 'json',
		success: function(data) {

			jq('#form-qpv-question ul').remove();
			
			var jsonData = eval("(" + data + ")");

			var answerTotal = jsonData.length;
			if (answerTotal > 0) {

				var table = '<table cellspacing="0" id="qcVoteResultsTable">';


				for (i = 0; i < answerTotal; i++) {
		
					table += '<tr>';
					table += '	<td class="qpv-answer-label">' + jsonData[i].name + '</td>'; 
					table += '	<td class="qpv-graph">';
					
					if (jsonData[i].popularity > 0) {
						table += '<div '; 
						
						if (jsonData[i].correct == 1)
							table += 'class="qpv-correct" ';
							
						table += 'style="width: ' + (jsonData[i].popularity * 100) + '%;'
						
						if (jsonData[i].popularity < .25 && jq('.portletVotePortlet').length)
							table += ' font-size: 8px; ';
						else if  (jsonData[i].popularity < .35 && jq('.portletVotePortlet').length)
							table += ' font-size: 10px; ';
						else if  (jsonData[i].popularity < .20 && !(jq('.portletVotePortlet').length))
							table += ' font-size: 10px; ';

						
						table += '">';
						
						table +=  Math.round(jsonData[i].popularity * 100) + '%';
						
						table += '</div>';
					}
					
					table += '	</td>';
					table += '</tr>';

				} // end for
				
				table += '</table>';
	
				table += '<span>Total Votes (' + jsonData[0].total + ')</span>'
	
				jq('#form-qpv-question h3').after(table);

			} // end if	
		} // end success function
	}); // end ajax call
	
}

nextQuestion = function(){
	var d = new Date();
	
	jq.ajax({
		url: 'ajax_random_question?nocache=' + d.getTime(),
		returnType: 'json',
		success: function(data){
			jq('#form-qpv-question').unwrap();
			jq('#form-qpv-question table').next().remove(); // remove vote count
			jq('#form-qpv-question table').remove();
			jq('#poll-next-js').remove();

			var jsonData = eval("(" + data + ")");

			jq('#form-qpv-question h3').html(jsonData[0].name);
			jq('#qpv-question-id').val(jsonData[0].id);

			var answerTotal = jsonData.length;
			if (answerTotal > 0) {
			
				var answers = '<ul';
				
				if (answerTotal > 4) answers += ' class="qcvote-answers-5plus"'
				 
				answers += '>';
				
				for (i = 1; i < answerTotal; i++) {
				
					answers += '<li>';
					answers += '<input type="radio" name="answer_id" value="' + jsonData[i].id + '" id="answer-' + jsonData[i].id + '" /> ';
					answers += '<label for="answer-' + jsonData[i].id + '">' + jsonData[i].name + '</label>';
					answers += '</li>';
					
				} // end for
				answers += '</ul>';
				
				jq('#form-qpv-question h3').first().after(answers);

				if (jq('.portletVotePortlet').length)
					jq('#form-qpv-question .formControls').append('<a id="poll-submit-js" class="poll-submit"><img src="/img/submit-small.png" alt="Submit" /></a>');
				else if (jq('.home-portlet').length)
					jq('#form-qpv-question .formControls').append('<a id="poll-submit-js" class="poll-submit"><img src="/img/home-trivia-submit.png" alt="Submit" /></a>');
				else
					jq('#form-qpv-question .formControls').append('<a id="poll-submit-js" class="poll-submit"><img src="/img/button-submit.png" alt="Submit" /></a>');
				
				jq('#poll-submit-js').click(pollSubmit);
			}		
		}
	});	
	
}
