/* Start Test Script */			
$(document).ready( function(){

	var testname, testid, testd;
	
	//check if the status is in testing	
	var intesting=0;	

	/*			
		Different test options
	*/	
	$('div#testlist').find("a").click( function(){
		//name of the test
		testname = $(this).attr("tname");
		//id for test type
		testid = $(this).attr("tid");
		//title
		testd = "Demo | " + $(this).attr("td");
				
		//hacks and initializing modal
		$('body').append($('#diabox'));	
		//initializing modal title
		$('div.jqmdTC').empty().append(testd);
		//initialize test state
		intesting=1;
			
		//initializing modal content
		$('div.jqmdMSG').append($('#testing').show());
		
		//initialize loading 
		$('#choices').empty().addClass('loading');				
		$('#testsubject').empty().css(InsertSmallLoadingOptions).addClass('smallloading');
		$('#questioncount').empty().css(InsertSmallLoadingOptions).addClass('smallloading');
		$('#testerror').empty();			

		/*	
			Start Test	
		*/
		$.getJSON("demoinitialize.php", {mytestid: testid, mytestname: testname}, function(data){ParseTestData(data)});
		
		//show Modal	
		//$('#diabox').jqm({trigger: false, target: false, modal: true, closeClass: false, overlay: 95}).jqmShow(); 
		$('#diabox').jqm({overlay: 80, modal: true, trigger: false}).jqmShow(); 
		// Close Button Highlighting Javascript provided in ex3a.
		$('input.jqmdX').hover(
			function(){ $(this).addClass('jqmdXFocus'); }, 
			function(){ $(this).removeClass('jqmdXFocus'); })
		  .focus( function(){ this.hideFocus=true; $(this).addClass('jqmdXFocus'); })
		  .blur( function(){ $(this).removeClass('jqmdXFocus'); 
		});	
		// Work around for IE's lack of :focus CSS selector
		if($.browser.msie){
			$('input')
			.focus(function(){$(this).addClass('iefocus');})
			.blur(function(){$(this).removeClass('iefocus');});
		}	  
	});	//end click
	
	/***************************************
	Test related functions
	put here since start test requires 
	thiese function as well
	*****************************************/	
	var InsertSmallLoadingOptions={
		"width" : "15px",
		"height" : "15px"
	};
	var RemoveSmallLoadingOptions={
		"width" : "auto",
		"height" : "auto"
	};	
	
	function CleanTestMessage(){
		$('#choices').empty();
		$('#testsubject').empty();
		$('#questioncount').empty();		
		$('#testerror').empty();
	}
	function CleanTmpTestMessage(){
		$('#tmptestsubject').empty();
		$('#tmpquestioncount').empty();
		$('#tmptestdisplay').empty();	
	}	
	function TestLoading(){
		$('#choices').empty().addClass('loading');				
		$('#testsubject').empty().css(InsertSmallLoadingOptions).addClass('smallloading');
		$('#questioncount').empty().css(InsertSmallLoadingOptions).addClass('smallloading');
		$('#testerror').empty();
		$('#testsubmit').hide();
	}
	function TestResponse(t){		
		ParseTestData(t);
		$('#testsubmit').show();
	}	
	function ParseTestData(t){
		/*			
			parse json data and display the test
			#1 - no answer selected
			#2 - test completed
			otherwise, show test subjects
		*/
		CleanTestMessage;
		if(t == '1'){
			//show error code
			$('#testerror').append('error => You must make a selection').slideDown('888');
			
			//show test
			$('#questioncount').removeClass('smallloading').css(RemoveSmallLoadingOptions)
				.append($('#tmpquestioncount').contents().clone());
			$('#testsubject').removeClass('smallloading').css(RemoveSmallLoadingOptions)
				.append($('#tmptestsubject').contents().clone());			

			var cachehtml = '<ul>';
			var cachetid='';
			var cachetname='';
			var index=0;
			$('#tmptestdisplay').find('li').each( function(){
				cachetid = $(this).attr('tid');
				cachetname = $(this).attr('tname');				
				cachehtml += '<li><input id="t_' + index +'" name="studentanswer" type="radio" value="'+ cachetid +'" /><label for="t_'+ index +'">'+ cachetname +'</label></li>';
				index++;
			});
			cachehtml += '</ul>';			
			$('#choices').removeClass('loading').append(cachehtml);
				
		}else if(t =='2'){
			intesting = 0;
			//finish test
			$('#testing').appendTo('body').hide();
			
			//load demo score
			$.getScript("demoreport.php", function(r){
				$('.score').empty().append(r + "%");
				$('div.jqmdMSG').append($('#finishtest').show());			
			});
			
		}else{					
			var cache='';
			var html = '<ul>';
			$.each(t.choices, function(i, entry){						
				html += '<li><input id="t_' + i +'" name="studentanswer" type="radio" value="'+ entry.id +'" /><label for="t_'+ i +'">'+ entry.type +'</label></li>';

				cache += '<li tid="' + entry.id +'" tname="'+ entry.type +'"></li>';
			});
			html += '</ul>';
			
			//cache the test
			$('#tmptestsubject').empty().append(t.question);
			$('#tmpquestioncount').empty().append(t.num);
			$('#tmptestdisplay').empty().append(cache);			
			
			
			$('#questioncount').removeClass('smallloading').css(RemoveSmallLoadingOptions).append(t.num);
			$('#testsubject').removeClass('smallloading').css(RemoveSmallLoadingOptions).append(t.question);			
			$('#choices').removeClass('loading').append(html);
		}
	}
	/***************************************
		end
	*****************************************/	
	
	/*			
		3. Testing submission
	*/
	var TestOptions ={
		dataType: 'json',
		beforeSubmit: TestLoading,
		success: TestResponse
	};	
	$('#testform').submit( function() {
		$(this).ajaxSubmit(TestOptions);
		return false;
	});
	
	/*
		4. Exit confirmation function
	*/
	function ConfirmExit(testingstatus){	
		//if during the middle of test,
		if(testingstatus == 1){
			$('body').append($('#exit'));
			$('#exit').jqm({overlay: 80, modal: true, trigger: false}).jqmShow(); // overlay modal				
			$('#exit').find(':submit').click( function(){
				//load the confirmation window
				if($(this).val() == 'Yes'){	
					$('#exit').jqmHide();
					$('#diabox').jqmHide();
					$('body').append($('#testing').hide());
					$('body').append($('#finishtest').hide());					
					//clean html					
					$('#choices').empty();
					$('#testsubject').empty();
					$('#questioncount').empty();		
					$('#testerror').empty();
					$('#tmptestsubject').empty();
					$('#tmpquestioncount').empty();
					$('#tmptestdisplay').empty();

					intesting = 0;
				}else{					
					$('#exit').jqmHide();
					$('body').append($('#exit').hide());					
				}
			});
		}else{
			$('#r').empty();
			$('#diabox').jqmHide();			
			$('body').append($('#finishtest').hide());					
			//$('body').append($('#userinfo').hide());
			$('body').append($('#exit').hide());
			$('body').append($('#testing').hide());
			$('body').append($('#score').hide());
		}
	}

	
	/*
		Close button on modal window
	*/
	$('.jqmClose',$('#diabox')).click( function(){	
		ConfirmExit(intesting);		
	});
	
})	//end script

