

/**** JQuery *******/
$(document).ready(function($) {


	jQuery('body').on('click','.next-tab', function(){
	    var next = jQuery('.nav-tabs > .active').next('li');
	    if(next.length)
	    {
	        next.find('a').trigger('click');

	        question_id = parseInt($(this).attr('id'));
	        var answer_option = $("input[name='answer_"+question_id+"']:checked").val();
	        if(answer_option)
	        {
	           	$(document).find('.nav-tabs #question'+question_id).removeClass('yellow');
	            $(document).find('.nav-tabs #question'+question_id).addClass('green');  
	            // $('.nav-tabs #question'+question_id).css("background-color", "green", 'important');
	            // $('.nav-tabs #question'+question_id).css("color", "white", 'important');
	        }
	        else
	        {
	        	// $('.nav-tabs #question'+question_id).css("background-color", "yellow", 'important');
	        	(document).find('.nav-tabs #question'+question_id).addClass('yellow');
	        }
			
			var attempted_questions = $('.nav-tabs .green').length
          	$('#attempted_questions').val(attempted_questions);

	    }
	    else
	    {
	        jQuery('#myTabs a:first').tab('show');
	    }
	});

	jQuery('body').on('click','.previous-tab', function(){
	    var prev = jQuery('.nav-tabs > .active').prev('li')
	    if(prev.length)
	    {
	        prev.find('a').trigger('click');

	        question_id = parseInt($(this).attr('id'));
	        var answer_option = $("input[name='answer_"+question_id+"']:checked").val();
	        if(answer_option)
	        {
	           	$(document).find('.nav-tabs #question'+question_id).removeClass('yellow');
	            $(document).find('.nav-tabs #question'+question_id).addClass('green');  
	            // $('.nav-tabs #question'+question_id).css("background-color", "green", 'important');
	            // $('.nav-tabs #question'+question_id).css("color", "white", 'important');
	        }
	        else
	        {
	        	// $('.nav-tabs #question'+question_id).css("background-color", "yellow", 'important');
	        	(document).find('.nav-tabs #question'+question_id).addClass('yellow');
	        }

	        var attempted_questions = $('.nav-tabs .green').length
          	$('#attempted_questions').val(attempted_questions);

	    }
	    else
	    {
	        jQuery('#myTabs a:last').tab('show');
	    }
	});


	jQuery('body').on('click','.submit_btn', function(){
	      
	        question_id = parseInt($(this).attr('id'));
	        var answer_option = $("input[name='answer_"+question_id+"']:checked").val();
	        if(answer_option)
	        {
	           	$(document).find('.nav-tabs #question'+question_id).removeClass('yellow');
	            $(document).find('.nav-tabs #question'+question_id).addClass('green');  
	            // $('.nav-tabs #question'+question_id).css("background-color", "green", 'important');
	            // $('.nav-tabs #question'+question_id).css("color", "white", 'important');
	        }
	        else
	        {
	        	// $('.nav-tabs #question'+question_id).css("background-color", "yellow", 'important');
	        	(document).find('.nav-tabs #question'+question_id).addClass('yellow');
	        }
			
			var attempted_questions = $('.nav-tabs .green').length
          	$('#attempted_questions').val(attempted_questions);
	     
	});

  // submit report form using ajax

    $(document).on('click', '.save_report', function(event){  
        event.preventDefault();
        var description = $('#description').val();  
        var que_id = $('#que_id').val();  

        if(description != '')  
        {  
            $.ajax({  
                 url: base_url+'exam/report',  
                 method:'POST',  
                 dataType: 'json',
                 data:{question_id:que_id,description:description},  
                 success:function(data)  
                 {  
                    if(data.response == false)
                    {
                      alert(data.reposn);  
                    }
                    else
                    {
                        alert("Reported successfullly");
                        $('#description').val("");
                        $('#que_id').val("");
                        $('#report_question').modal('hide');
                    } 
                 }  
            });  
        }  
        else  
        {  
            alert("Fields are Required");  
        }  
   });


});





// code used for dissable inspecting the page source and open webcam for take a picture

    function disableF5(e) {
      	if ((e.which || e.keyCode) == 116)
          e.preventDefault();
  	};

  	$(document).on("keydown", disableF5);




  	// $(window).load(function() {
  	$(window).on('load', function(){

        $(document).bind("contextmenu",function(e){
            return false;
        });

        document.onkeydown = function (e) {   //F5 Key code
              var event = window.event || e;
              if (event.keyCode == 116) {
                  event.keyCode = 0;
                  return false;
              }
        }

        $(document).bind('keypress keydown keyup', function (e) {
            if (e.which === 82 && e.ctrlKey) {    // Ctrl +R key code
               // console.log('blocked');
               return false;
            }
        });  

       
        // category variable is defined in examination page internal js top side
        // alert(category);
        if(category == 'final')
        {
      	//Starting DOM
        	navigator.getMedia = ( navigator.getUserMedia || // use the proper vendor prefix
        	                       navigator.webkitGetUserMedia ||
        	                       navigator.mozGetUserMedia ||
        	                       navigator.msGetUserMedia);

      	    navigator.getMedia({video: true,audio:true}, function() {

      	     // webcam is available
      	    }, function() {
      	       	// $(".question-section").hide();
                $("#warning").text("Note: Please provide the permission to camera and microphone, If the access is not provided your test will be considered as invalid.");
      	        // alert("Please Allow permission to camera and mic else change broweser and try again <br> thank you");
      	       	// window.close();
      	    });

          	$(document).bind("contextmenu",function(e){
              	return false;
          	});

          	document.onkeydown = function (e) {   //F5 Key code
                var event = window.event || e;
                if (event.keyCode == 116) {
                    event.keyCode = 0;
                    return false;
                }
          	}

          	$(document).bind('keypress keydown keyup', function (e) {
              	if (e.which === 82 && e.ctrlKey) 
              	{    // Ctrl +R key code
                 	return false;
              	}
          	});

          	$(document).on("click",".submit_btn",function(e){
              	e.preventDefault();
              	// $('.container').waitMe({
               //   	effect : 'stretch',
               //   	text : 'Loading...'
              	// });
             
             	$("#myForm").submit();
          	});
           
         	Webcam.set({
         	 	width: 300,
              	height: 300,
              	image_format: 'jpeg',
              	jpeg_quality: 100
          	});

           	Webcam.attach( '#my_camera' );
            // try
            // {
            //    startRecording();
            // }
            // catch(err)
            // {
            //  console.log(err.message);
            // }
           
           	setInterval(function () {
              	take_snapshot();
          	}, 600000);
            // }, 5000);
           
        }
         //120000
   	});



   	function take_snapshot() 
    {
        var test_id = $('#test_id').val();
        var subject_id = $('#subject_id').val();
    		Webcam.snap( function(data_uri) {
    			Webcam.upload( data_uri, base_url+"exam/uploadImage?test_id="+test_id+"&subject_id="+subject_id, function(response) {
          		true;
    			});	
    		});
    }


  function report_question_modal(question_id) 
  {
    $('#report_question').modal('show');
    $('#que_id').val(question_id);
  }