// 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() {
		
		if ((tO['answer'] == '') && (tO['template'] == '6a')) {
			chk_bg = 'mygtukas_sekmes_normal.png';
			chk_bg_o = 'mygtukas_sekmes_over.png';
			w = '285px';
			l = '926px';
		} else {
			chk_bg = 'tikrinti_button.png';
			chk_bg_o = 'tikrinti_button_over.png';
			w = '195px';
			l = '1016px';
		}
		
		$(ssId)
			.append('<div id="' + ss + 'check"></div>');
		$(ssId+'check')
			//.attr('class','drag')
			.css({
				"position":"absolute",
				'width':w,
				'height':'91px',
				"background":" url(img/"+chk_bg+") no-repeat 50% 50%",  // width = 175px; height = 41px
				'top': '643px',	// real button place - 643px
				'left': l,  //real button place - 955px
				"cursor": "pointer",
				"z-index": "1000"
			})
			.hover(
				function() {
					$( this ).css("background", "url(img/"+chk_bg_o+") no-repeat 50% 50%");
				},
				function() {
					$( this ).css("background", "url(img/"+chk_bg+") no-repeat 50% 50%");
				}
			)
			.bind('click', function(){
				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 = '#36b36b';
	answerObj.Caption = tO.CorrectCaption;
	console.log('testing type', tO);
	answerObj.green = true;
	answerObj.needText = false;
	if ((tO['answer'] == '') && (tO['template'] == '6a')) {
		answerObj.needText = true;
		answerObj.Text = tO.FaultText['#cdata'];
	} else {
		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 = '#ff7a04';
	answerObj.Caption = tO.FaultCaption;
	answerObj.Text = tO.FaultText['#cdata'];
	answerObj.green = false;
	answerObj.needText = true;
	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':'1260px',
				//"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':'920px',
            'height':'auto',
            "background":"#ffffff",
            "border":"2px solid #ff7a03",
            'top':'0px',
            'left':'190px',
            //"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':'920px',
			'height':'41px',
			'text-align':'center',
			'margin-top':'30px',
			//'left':'128px',
			//	"cursor": "pointer",
			"z-index": "1000",
				'text-indent':'0px',
				'font-family':'Arial',
				'font-weight':'300',
				'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':'800px',
			'height':'auto',
			'margin-top':'20px',
			'margin-left':'60px',
			//"cursor": "pointer",
			"z-index": "1000",
			'text-indent':'0px',
				'font-family':'Arial',
				'font-size': '19px',
				//'color': '#000000',
				
				//'text-align':'center',
				'line-height': '26px'
		});
	
	zal = '';
	if (answerObj.green == true) {
		zal = '_zalia';
	}
	
	// close button
	$(ssId+'answer')
		.append('<div id="' + ss + 'close"></div>');
	$(ssId+'close')
        .css({
            //"position":"absolute",
            'width':'134px',
            'height':'54px',
            "background":" url(img/close_new"+zal+".png) no-repeat",
            'bottom':'60px',
            'left':'50%',
            'margin-left':'390px',
            "cursor": "pointer",
            "z-index": "1000",
            "margin-top":'20px',
            "margin-bottom":'30px'
        })
        .hover(
            function() {
				if (answerObj.green == true) {
					$( this ).css("background", "url(img/close_new_zalia_.png) no-repeat 50% 50%");
				} else {
					$( this ).css("background", "url(img/close_new_hover.png) no-repeat 50% 50%");
				}
            },
            function() {
				if (answerObj.green == true) {
					$( this ).css("background", "url(img/close_new_zalia.png) no-repeat 50% 50%");
				} else {
					$( this ).css("background", "url(img/close_new.png) no-repeat 50% 50%");
				}
            }
        )
		.bind('click', function(){ testAnswerClose(ssId+'answer'); });
		
	if (answerObj.green == true) {
		if (answerObj.needText == false) {
			$(ssId+'CorrectText').css('display', 'none');
			$(ssId+'CorrectCaption').css('margin-top', '30px');
		} else {
			$(ssId+'CorrectCaption').html('• • •');
			$(ssId+'CorrectCaption').css('margin-top', '30px');
		}
		$(ssId+'answer').css('border-color', '#61cac6');
		$(ssId+'close').css('margin-top', '30px');
		$(ssId+'close').css('margin-bottom', '30px');
	}
	
	$(ssId+'answer').draggable();

}
