/**
 * @author dan.rades
 * todo: documentare
 */

(function($) {                                         
$.fn.nextQuiz = function(o) {  

	var emptyFunction = function(){};
	 
    o = $.extend({				
		type : 'simple',
		showScore : true,
		size : 5		
    }, o || {});
	
	return this.each(function(i) {
		var _self = $(this);
		_self.type = $('.quizType', _self).val() || 'letter';				
		_self.submitButton = $('.quizSubmitButton').eq(0);
		_self.resetButton = $('.quizResetButton').eq(0);
		
		var visibleQuestions = $('.quizVisibleQuestions', _self).val();			
		var showAllResults = $('.quizShowAllResults',_self).val();
		
		if (typeof visibleQuestions != 'undefined' && visibleQuestions > 0) {
			_self.hasSlides = true;
			_self.slides = $('.quizSlide', _self);
			_self.slides.eq(0).show();
			_self.currentSlideIdx = 0;
			_self.nextButton = $('.quizNextButton',_self).eq(0);
			_self.prevButton = $('.quizPrevButton',_self).eq(0);			
			
		}
		else {
			_self.hasSlides = false;			
		}
		
		_self.nextButton.click(function() {
			var questions = $('.quizQuestion', _self.slides.eq(_self.currentSlideIdx));
			var answers = $('.quizAnswer:checked', questions);

			if (answers.length != questions.length) {
				alert('Trebuie sa selectati un raspuns pentru toate intrebarile!');
				return false;
			}			
			
			_self.currentSlideIdx ++;
			_self.slides.hide();
			_self.slides.eq(_self.currentSlideIdx).show();
			
			if (_self.currentSlideIdx + 1 == _self.slides.length) {
				_self.nextButton.hide();
				_self.submitButton.show();
			}
			_self.prevButton.show();
			
	
		});
		
		_self.prevButton.click(function() {
			_self.currentSlideIdx --;
			_self.slides.hide();
			_self.slides.eq(_self.currentSlideIdx).show();
			
			if (_self.currentSlideIdx == 0) {
				_self.prevButton.hide();
			}
			_self.submitButton.hide();
			_self.nextButton.show();
			
	
		});	
		
		_self.submitButton.click(function() {
			var questions = $('.quizQuestion', _self.slides.eq(_self.currentSlideIdx));
			var answers = $('.quizAnswer:checked', questions);

			if (answers.length != questions.length) {
				alert('Trebuie sa selectati un raspuns pentru toate intrebarile!');
				return false;
			}
			
			$('.quizResult', _self).hide();	
			
			if (_self.type == 'points') {
				var punctaj = 0;
				$('.quizAnswer:checked', _self).each(function() {
					punctaj += parseInt($(this).val());	
				});
				if (o.showScore) {
					$('.quizScore',_self).text('Ai obtinut un scor de ' + punctaj + ' puncte').show();				
				}
				else {
					$('.quizScore',_self).text('Rezultat test:').show();
				}

				
				if (showAllResults == "YES") {
					$('.quizResult', _self).show();
				}
				else {
					$('.quizResult', _self).each(function(){
						var _this = $(this);
						punctaj = parseInt(punctaj);
						var min = parseInt($('.quizMin', _this).val());
						var max = parseInt($('.quizMax', _this).val());

						if (punctaj >= min && punctaj <= max) {
							_this.show();
						}
					});
				}
			}
			else {
				var votes = [];
				var letter = '';
				$('.quizAnswer:checked', _self).each(function() {
					i = parseInt($(this).val()) - 1;	

					if (typeof votes[i] == 'undefined') {
						votes[i] = [];
						votes[i]['count'] = 0;
						votes[i]['letter'] = String.fromCharCode(65 + i);
					}
					votes[i]['count'] ++;					
				});							

				var max = 0;
				letter = '';
				for (i = 0; i < votes.length; i ++) {
					if (typeof votes[i] != 'undefined') {
						if (max < votes[i]['count']) {
							max = votes[i]['count'];
							letter = votes[i]['letter'];
						}
					}
				}
				if (o.showScore) {
					$('.quizScore', _self).text('Ai obtinut o majoritate de ' + letter).show();
				}
				else {
					$('.quizScore', _self).text('Rezultat test:').show();
				}				
				if (showAllResults == "YES") {
					$('.quizResult',_self).show();
				}
				else {
					$('input.quizLetter[value="' + letter + '"]').parent().show();
				}
			}
			
			window.location.hash = 'quizResults';
			_self.prevButton.hide();
			_self.resetButton.show();			
			
		});			

		_self.resetButton.click(function() {
			$('.quizAnswer:checked').attr('checked', false);
			_self.currentSlideIdx = 0;
						
			_self.slides.hide();
			$('.quizResult', _self).hide();			
			$('.quizScore',_self).hide();
						
			_self.slides.eq(0).show();
			
			_self.prevButton.hide();
			_self.submitButton.hide();			
			_self.resetButton.hide();
						
			_self.nextButton.show();
			window.location.hash = 'quizStart';

		});
		
																									
	});
	
};
})(jQuery);