counter = 1;
rightAnswers = 0;
  
$.fn.addQuestion = function(options) {
		
    var defaults = {
        question : 'A question',
        options : ['First answer', 'Second answer', 'Third answer'],
        answer : 2,
        textReplace : false,
        replaceFrom : '*',
        replaceTo : '_______',
        explanation: ''
    };
			
    var options = $.extend(defaults, options);
		
    return this.each(function() {
			
        html = '<div class="q"><div class="question" id="q' +counter+ '"><span>' + counter + "</span> " +options.question+ '</div>'
        + '<div class="mytestmessage" style="display: none;"></div><div class="options">';
								
        $.each(options.options, function(index, value) { 
            html = html + '<input type="radio" value="' +value+ '" name="' +counter+ '"';
            if ((index + 1) == options.answer) {
                html = html + ' class="a" ';
            }
            html = html + ' /> ' +value+ '<br />';
        });

        html  =	html + '</div><div class="explanation" style="color: #666;">' + options.explanation + '</div><div style="clear:both;"></div></div>';
				
        $(this).append(html);

        if (options.textReplace == true) {
            var currentId = $('#q' + counter);
            currentId.html(currentId.html().replace(options.replaceFrom, options.replaceTo));
        }
				
        html = '';
        counter ++;
				
        $(this).find('.explanation').hide();
    });
};
		
$.fn.checkTest = function(options) {
		
    var defaults = {
        container : 'questions',
        replaceTo : '_______',
        noAnswerText : '(no answer)',
        buttonText : 'Submit',
        rightColour : 'green',
        wrongColour : 'red'
    };
			
    var options = $.extend(defaults, options);
		
    return this.each(function() {
			
			
        $(this).append('<div id="stats"><button class="my_submit">' +options.buttonText+ '</button></div>');
			
        $(this).find('button.my_submit').live('click', function() {
				
            $("#questions div.q").each(function(index){
                var answer = $(this).find("input:checked");
					
                var currentId = $(this).find('.question');
					
                if (answer.val() == null) {
                    var answer_val = options.noAnswerText;
                }
                else {
                    var answer_val = answer.val();
                }
					
                currentId.html(currentId.html().replace(options.replaceTo, '<strong>' +answer_val+ '</strong>' ));
					
                if (answer.attr('class') == 'a') {
					
                    rightAnswers++;
						
                    $(this).attr('style', 'color: ' +options.rightColour+ '; background: url(/images/2011/tick.gif) top left no-repeat; padding-left: 28px;');
                    $(this).find("span").attr('style', 'color: ' +options.rightColour+ ';');
					
                }
					
                else {
                    var rightAnswer = $(this).find('.a').val();
                    var rightAnswerText = currentId.html().replace(answer_val, rightAnswer);	
                    $(this).attr('style', 'color: ' +options.wrongColour+ '; background: url(/images/2011/cross.gif) top left no-repeat; padding-left: 28px;');
                    $(this).find("span").attr('style', 'color: ' +options.wrongColour+ ';');
                                                        
                //$(this).find('.mytestmessage').attr('style', 'display: block; color: ' +options.wrongColour+ ';').html('The right answer was: <span style="color: green;">'+ rightAnswerText+ '</span>');
                }
					
                $(this).find('.options').hide();
                $(this).find('.explanation').show();
            });
				
            var totalQuestions = $(".q").size();
            var percentage = Math.round((rightAnswers * 100) / totalQuestions) + "%";
				
            $("#stats").html('Your score: ' +rightAnswers+ ' / ' +totalQuestions+ '<br />' + percentage);
			
        });
    });
};
