// background load should be moved to templates
// append slide div in section with background image
function loadSlideBackground() {

	console.log("> loadSlideBackground()");
	console.log("> Section Slide = " + sectionName + slideName);
	var slideContent = doObj[sectionName][slideName]['background'];
	console.log("> Slide background = " + slideContent);
	console.log(slideContent);
	
	console.log("loadbg",slideContent);
    // if background defined
	if (slideContent) {
		console.log("> Slide background content found.");
		var image = slideContent['image'];
		var color = slideContent['color'];
		if (image) {
			console.log("> Slide background image found. image: " +image );
			console.log("> ss: " +ss );
			console.log("> ssId: " +ssId );
			$(ssId)
				.append('<div class="slide" id="' + ss + 'background"><img src="' + image + '" alt="" ></img></div>');
			
			$(ssId +'background').css({
				'position': "absolute",
				"z-index": "0"
			});
		}
		if (color) {
			console.log("> Slide background color found. color: " +color );
			$(ssId)
				.css('background',color);
		}
	}
}

// check button
function checkTestButton(thisTest,tO,p,randPlaces,backWidth,itemsCount,answered) {
	
   // tO = tO || "default value";
    //p = p || "default";
   // ssId = ssId || "default";
    
	//tO.get_answer_result = get_answer_result;
	if (typeof test_report != 'undefined' ) {
		if (test_report) {
			
			tO.get_answer_result = get_answer_result;
			/*
			$('#finish_button')
				.bind('click', function(){
					get_answer_result();							
				});
				*/
			//tO.get_answer_result =  get_answer_result;
			//get_answer_result();
		}
		else {
			use_test_button();
		}
	}
	else{
		use_test_button();
	}
	function get_answer_result() {
		var answer;
		if (p != undefined){
			answer = thisTest(tO,p,ssId);                                       
		}else{
			answer = thisTest(tO,ssId,randPlaces,backWidth,itemsCount,answered); 
                        console.log("BIG3:",answered);
		}
		tO.answer_result = answer;
		console.log("Test answer is: " + answer );
	}
	
	function use_test_button() {
		$(ssId)
			.append('<div id="' + ss + 'check"></div>');
		$(ssId+'check')
			//.attr('class','drag')
			.css({
				"position":"absolute",
				'width':'225px',
				'height':'91px',
				"background":" url(img/tikrinti_button.png) no-repeat 50% 50%",  // width = 175px; height = 41px
				'top':'618px',	// real button place - 643px
				'left':'930px',  //real button place - 955px
				"cursor": "pointer",
				"z-index": "1000"
			})
			.bind('click', function(){
                            
                            if ($("#sectionNoInner").length > 0){
                                var cS = inSlide + 1;
                                $(this).addClass("pushed");
                                //alert(inSlide);
                                $("#sectionNoInner #taskNumber" + cS).css("color","black");//addClass("taskShow");
                            }
			    get_answer_result();							
			});
	}
}

// test answer handlers
var answerObj ={}

// test answer handler
function testAnswer(correct) {
	// true - correct, false - incorrect
	if (typeof test_report != 'undefined') {
		if (test_report) {
		}
		else {
			test_answer();
		}	
	}
	else{
		test_answer();
	}
	
	function test_answer() {
		if (correct) {
			console.log('Answer is right');
			testAnswerCorrect();
		}
		else{
			console.log('Answer is wrong');
			testAnswerInCorrect();
		}
	}
	
}
// correct answer popup div with texts
function testAnswerCorrect() {
	var tO = tObj[sectionName][slideName];
	answerObj.Image = 'img/correct.png';
	answerObj.Color = 'rgb(0,168,8)';
	answerObj.Caption = tO.CorrectCaption;
	answerObj.Text = tO.CorrectText;
	appendTestAnswer();
}
// incorrect answer popup div with texts
function testAnswerInCorrect() {
	var tO = tObj[sectionName][slideName];
	answerObj.Image = 'img/fault.png';
	answerObj.Color = 'rgb(255,0,0)';
	answerObj.Caption = tO.FaultCaption;
	answerObj.Text = tO.FaultText;
	appendTestAnswer();
}
// close test answer div
function testAnswerClose(ssIdAnswer) {
    $(ssIdAnswer).remove();
	$(ssIdAnswer + '_background').remove();
}
// test answer div appending
function appendTestAnswer() {
	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
	$(ssId)
		.append('<div id="' + ss + 'answer_background"></div>');
	$(ssId+'answer_background')
		.css({
				"position":"absolute",
				'width':'1160px',
				//"background":" #ff0000",
				'height':'718px',
				'top':'0px',
				'left':'0px',
				"z-index": "1000"
			});
		
	// Append answer div with image
	$(ssId)
		.append('<div id="' + ss + 'answer"></div>');
	$(ssId+'answer')
		//.attr('class','drag')
		.css({
			"position":"absolute",
			'width':'615px',
			'height':'424px',
			"background":" url("+Image+") no-repeat 50% 50%",
			'top':'150px',
			'left':'300px',
			//"cursor": "pointer",
			"z-index": "1000"
		});
	
	// answer Caption
	$(ssId+'answer')
		.append('<div id="' + ss + 'CorrectCaption">'+Caption+'</div>');
	$(ssId+'CorrectCaption')
		//.attr('class','drag')
		.css({
			"position":"absolute",
			'width':'345px',
			'height':'41px',
			'top':'65px',
			'left':'128px',
			//	"cursor": "pointer",
			"z-index": "1000",
				'text-indent':'0px',
				'font-family':'Arial',
				'font-size': '30px',
				'color': Color,
				
				//'text-align':'center',
				'line-height': '48px'
		});
	
	// answer Text
	$(ssId+'answer')
		.append('<div id="' + ss + 'CorrectText">'+Text+'</div>');
	$(ssId+'CorrectText')
		//.attr('class','drag')
		.css({
			"position":"absolute",
			'width':'472px',
			'height':'170px',
			'top':'144px',
			'left':'63px',
			//"cursor": "pointer",
			"z-index": "1000",
			'text-indent':'0px',
				'font-family':'Arial',
				'font-size': '24px',
				//'color': '#000000',
				
				//'text-align':'center',
				'line-height': '33px'
		});
	
	
	// close button
	$(ssId+'answer')
		.append('<div id="' + ss + 'close"></div>');
	$(ssId+'close')
		.css({
			"position":"absolute",
			'width':'175px',
			'height':'41px',
			"background":" url(img/uzdaryti_button.png) no-repeat 50% 50%",
			'top':'347px',
			'left':'225px',
			"cursor": "pointer",
			"z-index": "1000"
		})
		.bind('click', function(){ testAnswerClose(ssId+'answer'); });
}