var test_report;
var T0fullName='';
var T0_time;

function templateT0script() {
	var sectionName = 'section'+ (inSection);
	var slideName = 'slide' + (inSlide);
	var ssId='#'+sectionName+slideName; // section and slide name
	var ss=sectionName+slideName; // section and slide name
	console.log("function templateT0script(); sectionName=" +sectionName + "; slideName=" +slideName);
	
	$(ssId)
		.append('<div id="' +ss+'vp' + '">Vardas, pavardė</div>')
		.append('<input type = "text" id = "vardas_pavarde"/>')
		.append('<div id="' +ss+'mok' + '"></div>')
		.append('<div id="' +ss+'k_d' + '"></div>');
	
	$(ssId+'vp').css({
		'position':'absolute',
		'width':'200px',
		'height':'35px',
		'top':'226px',
		'left':'233px',
		//'border':'1px solid #00ff00',
		'z-index':'9',
		'color': '#a2a4a5',
		'text-indent':'7px',
		'font-family':'Arial',
		'font-size': '150%',
		'cursor': 'default',
		//'text-align':'center',
		'line-height': '35px'
	});
	$('#vardas_pavarde')
		.css({
			'position':'absolute',
			'width':'448px',
			'height':'33px',
			'top':'226px',
			'left':'434px',
			'color': '#a2a4a5',
			'text-indent':'10px',
			'font-family':'Arial',
			'font-size': '150%',
			//'border':'1px solid #00ff00',
			'z-index':'900	'
		})
		.bind('click', function(){
			$(this).css({'border':'1px solid rgb(160,160,160)'});
		});
	$(ssId+'mok').css({
		'position':'absolute',
		'width':'366px',
		'height':'149px',
		'top':'400px',
		'left':'200px',
		//'border':'1px solid #00ff00',
		"background":" url(img/mokymasis.png) no-repeat 50% 50%",
		'z-index':'9'
	})
		.bind('tap', function(){
			get_full_name();
			if (T0fullName == '') {
				name_enter_atraction();
			}
			else {
				test_report = false; // set if it is report style or not
				inSection = 1;                                
                                $("#vardas_pavarde").blur();                                
				T0select("mok");
                                
			}
		});
		
	$(ssId+'k_d').css({
		'position':'absolute',
		'width':'366px',
		'height':'149px',
		'top':'400px',
		'left':'590px',
		//'border':'1px solid #00ff00',
		"background":" url(img/kontrolinis_darbas.png) no-repeat 50% 50%",
		'z-index':'9',
	})
		.bind('tap', function(){
			get_full_name();
			if (T0fullName == '') {
				name_enter_atraction();
			}
			else {
				test_report = true; // set if it is report style or not
				inSection = 2;                                
                                $("#vardas_pavarde").blur();
				T0select("k_d");
			}
		});
		
	function get_full_name() {
		T0fullName = $('#vardas_pavarde').val();
		$('#vardas_pavarde').focus();
		console.log('Full name = ' + T0fullName);
	}
	function name_enter_atraction() {
		$('#vardas_pavarde')
			.css({'border':'2px solid rgb(255,72,0)'})
			.animate(
				{borderWidth: 2},
				500,
				function() {// Animation complete.
				}
			);
	}
		
	function T0select(testType) {         
		$('#allControls')
		.append('<div id="test_scrollbar"><div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div><div class="control viewport" id="sectionNumbers" ></div></div>')
		.append('<div class="control" id="scrollControl" ></div>')
		.append('<div class="control" id="ScrollBulletControl" ></div>');
                
                var popWnd = "<div class='confirmPopup'><div class='popMessage'>Ar tikrai norite baigti?</div><div class='pop_btns'><div class='popCancel'><a id='bt_cancel' href=''><img src='img/atsaukti_button.png' /></a></div><div class='popConfirm'><a id='bt_finish' href='#'><img src='img/baigti_button.png' /></a></div></div></div>";
		
		if (test_report) {
                    $("#slidesContainer").on("click","#bt_cancel",function(e){  
                             e.preventDefault();
                             $(".confirmPopup").remove();                                        
                    });                        

                    $("#slidesContainer").on("click","#bt_finish",function(e){
                          e.preventDefault();
                          $(".confirmPopup").remove();  
                          test_finish_report();                                        
                    }); 


                    $('#allControls').append('<div class="control" id="finish_button" ></div>');



                    $('#finish_button').bind('click',function(){  
                        if ($(".confirmPopup").length == 0){
                            $("#slidesContainer").append(popWnd);
                        }
                        //if (confirm("Ar tikrai norite baigti?")){
                          // test_finish_report();
                       // }                 
                    });
    //              $('#finish_button').bind('click',function(){ append_unfinished_alert() });
		}

		animatedellay =  0;
		moveSlide();
		animatedellay = 1000;
		
		//alert(testType);
                if (testType == "k_d"){
                    $('#timerControl').css("display", "block");
                    //$(".relation_container", parent.document.body).css("display","none");
                    window.parent.$(".relation_container").css("display","none");
                    T0timer();
                }else if (testType == "mok"){
                    $('#timerControl').text("");                    
                    $('#timerControl').css({display:"block", background:0});                    
                }
		
		// not used, needs each test check if it is finished or not
		function append_unfinished_alert() {
			var Caption = answerObj.Caption;
			var Text = answerObj.Text;
			var Image = answerObj.Image;
			var Color = answerObj.Color;
			
			
			// append background to prevent dragging and testing while answer is open
			$('#pageContainer')
				.append('<div id="unfinished_protection"></div>')
				.css({
					//	"position":"absolute"
					});
			$('#unfinished_protection')
				.css({
						"position":"absolute",
						'width':'1278px',
						'height':'718px',
						//'background':'#000000',
						//'opacity':'0.3',
						'top':'1px',
						'left':'1px',
						"z-index": "1000"
					});
			$('#unfinished_protection')
				.append('<div id="unfinished_panel"></div>');
			$('#unfinished_panel')
				.css({
					"position":"absolute",
					'width':'615px',
					'height':'424px',
					"background":" url(img/correct.png) no-repeat 50% 50%",
					'opacity':'1',
					'top':'150px',
					'left':'300px',
					"z-index": "1001"
				});
			
			// answer Text
			$('#unfinished_panel')
				.append('<div id="unfinished_text">Jūs neatlikote visų užduočių. Visos neatliktos užduotys bus laikomos atliktomis neteisingai.</div>');
			$('#unfinished_text')
				.css({
					"position":"absolute",
					'width':'472px',
					'height':'170px',
					'top':'144px',
					'left':'63px',
					"z-index": "1002",
					'text-indent':'0px',
						'font-family':'Arial',
						'font-size': '24px',
						'line-height': '33px'
				});
			
			
			// cancel button
			$('#unfinished_panel')
				.append('<div id="unfinished_cancel"></div>');
			$('#unfinished_cancel')
				.css({
					"position":"absolute",
					'width':'175px',
					'height':'41px',
					"background":" url(img/uzdaryti_button.png) no-repeat 50% 50%",
					'top':'347px',
					'left':'325px',
					"cursor": "pointer",
					"z-index": "1003"
				})
				.bind('click', function(){ 
						$('#unfinished_protection').remove();
					});
			
			// finish button
			$('#unfinished_panel')
				.append('<div id="unfinished_finish"></div>');
			$('#unfinished_finish')
				.css({
					"position":"absolute",
					'width':'118px',
					'height':'41px',
					"background":" transparent url(img/finish_normal.png) no-repeat 50% 50%",
					'top':'347px',
					'left':'125px',
					"cursor": "pointer",
					"z-index": "1003"
				})
				.bind('click', function(){ 
						test_finish_report();
					});
		}	
		
		// timer count funtion
		function T0timer() {
			var h,m,s;
			var hh,mm,ss;
			var timer = $('#timerControl');
			var time;
			var start = new Date;
			
			setInterval(function(){
				time = ((new Date - start)/1000).toFixed(0);	// get time difference in second between started time and now
				time = get_elapsed_time_string(time);	// convert to string
				T0_time = time; // global time variable				
                                $(timer).text(time);
			},
			1000);
			
			
			//convert timer to mm:ss or hh:mm:ss string
			function get_elapsed_time_string(total_seconds) {
				function pretty_time_string(num) {
					return ( num < 10 ? "0" : "" ) + num;
				}
				
				var hours = Math.floor(total_seconds / 3600);
				total_seconds = total_seconds % 3600;
				
				var minutes = Math.floor(total_seconds / 60);
				total_seconds = total_seconds % 60;
				
				var seconds = Math.floor(total_seconds);
				
				// Pad the minutes and seconds with leading zeros, if required
				hours = pretty_time_string(hours);
				minutes = pretty_time_string(minutes);
				seconds = pretty_time_string(seconds);
				
				// Compose the string for display
				if (hours == "00") {
					var currentTimeString = minutes + ":" + seconds;
				}
				else{
					var currentTimeString = hours + ":" + minutes + ":" + seconds;
				}
				return currentTimeString;
			}
		}
	}
}

