/* When adding a case all text [inside brackets] are hidden for the user until
 * he/she clicks on view solution. This function replaces the brackets from db with
 * the correct div to conceal it.
 * A function to replace all [ in the text
 */
(function($) {
    $.fn.replaceBrackets = function() {
        var regexp1 = /\[/gi;
        var regexp2 = /\]/gi;
        
             var output = $(this).html().replace(regexp1,"<div class=\"solution\" style=\"display: none;\">")
             var output = output.replace(regexp2,"</div>")
            
        $(this).html(output);
        return true;
    }
})(jQuery);

$(document).ready(function(){
	//Hide all text within brackets, i.e. the solution
	$('div.trial-text').replaceBrackets();
	
	/* Start the stopwatch using epiclock plugin
	 */             
    $('#stopwatch') 
           .epiclock({
           		mode: EC_STOPWATCH,
           		format: 'i{m} s{s}'
           })      // We define the clock...   
           .clocks(EC_RUN); // Then start the manager. 
    
    //If the user clicks on the button with id solution, kill the stopwatch.
    $('#solution').click(function() {
		$('#stopwatch').css('display','block');
    	$('#stopwatch').clocks(EC_KILL);
    	$('div.solution').slideDown('slow');
	_ptq.push(["view",{'points':100}]);
    });
    
    //Options for ajaxForm
    var options = {
    		target:		'#trial-comment-form',
			beforeSubmit: showLoading,
			success:	commentAdd
	}

	function showLoading(formData, jqForm, options) {
		$('#loading-div').show();	
		return true;
	}

    function commentAdd(html) {
    	$('#trial-comment-form').html(html);
    	$('#CommentAddForm').ajaxForm(options);
	$('#loading-div').hide();
    }
    
	$('#CommentAddForm').ajaxForm(options);
});
