/* T2 test template
 * requires:
 * jquery.js
 * jquery.ui.js draggable
 */

var minx=10,
	maxx=350,
	miny=120,
	maxy=600;
    
var XYdone=0; // if allRandomXY already made, it is set to 1;
var rightorder = [];

var orderx = [];
var ordery = [];
var T1level=0;

function templateT2script() {
	
	var sectionName = 'section'+ (inSection+1);
	var slideName = 'slide' + (inSlide +1);
	var ssId='#'+sectionName+slideName; // section and slide name
	var ss=sectionName+slideName; // section and slide name
	console.log("function templateT1script(); sectionName=" +sectionName + "; slideName=" +slideName);
	
	var images_f = doContent[sectionName][slideName]['images_f'];
	var images_m = doContent[sectionName][slideName]['images_m'];
	var f_imageBack = doContent[sectionName][slideName]['image_f']; // femail image
	var m_imageBack = doContent[sectionName][slideName]['image_m'];	//mail image
	
	var i = 0;
	
	var t,l;
	
	t = 131;
	l = 30;
	
	// f images
	for (image in images_f) {
		i++;   // count images
		
		console.log("imagef" +image);
		$(ssId)
//			.append('<div class="drag" id="' +ss+'imagef' + i + '"></div>'); //drag.js
			.append('<div class="imagef" id="' +ss+'imagef' + i + '"></div>');
		$(ssId+'imagef' + i)
			//.attr('class','imagef')
			.css({
				"position":"absolute",
				//"border": "1px solid rgb(0,255,0)",
				'width':'204px',
				'height':'454px',
				"background":"  url("+images_f[image]+") no-repeat 2px 2px, url("+f_imageBack+") no-repeat 0px 0px",
				//'top':'131px',
				//'left':'478px',
				'top':'0px',
				'left':'0px',
				//"cursor": "move",
				"z-index": "6"
			})
			.animate({ 'top': t + 'px', 'left': l + 'px'}, 1000, function(){});
		
		l+=224;// distance between images left
	}
	
	i = 0;
	t = 380;
	l = 30;
	
	// m images
	for (image in images_m) {
		i++;   // count images
		
		console.log("imagem" +image);
		$(ssId)
			.append('<div class="imagem" id="' +ss+image + '"></div>');
		$(ssId+image)
			//.attr('class','imagem')
			.css({
				"position":"absolute",
				//"border": "1px solid rgb(0,255,0)",
				'width':'204px',
				'height':'240px',
				"background":"  url("+images_m[image]+") no-repeat 2px 32px, url("+m_imageBack+") no-repeat 0px 0px",
				//'top':'131px',
				//'left':'478px',
				'top':'140px',
				'left':'30px',
				"cursor": "move",
				"z-index": "6"
			})
			.animate({ 'top': t + 'px', 'left': l + 'px'}, 1000, function(){});
			//.bind('mouseover',function(){ alert('hover'); });
		
		l+=224;// distance between images left
	}
	$(ssId)
	.append('<div id="' + ss + 'check"></div>');
	$(ssId+'check')
	//.attr('class','drag')
	.css({
		"position":"absolute",
		'width':'250px',
		'height':'100px',
		"background":" url(img/tikrinti_button.png) no-repeat 50% 50%",
		'top':'600px',
		'left':'890px',
		"cursor": "pointer",
		"z-index": "9"
	})
	.bind('click', function(){ T2check(); });
	
	 $( ".imagem" ).draggable({
		snap: '.imagef',
		containment:ssId,
		snapMode: "inner",
		snapTolerance: 20,
		zIndex: 100
        
	});
}

$(document).mousemove(function(e){
   window.mouseXPos = e.pageX;
   window.mouseYPos = e.pageY;
}); 



function T2check() {
    var sectionName = 'section'+ (inSection+1);
    var slideName = 'slide' + (inSlide +1);
    var ssId='#'+sectionName+slideName; // section and slide name
    var ss=sectionName+slideName; // section and slide name
    
    var imagesm = doContent[sectionName][slideName]['images_m'];
	var answer = doContent[sectionName][slideName]['answer'];
	console.log("T2 answer:" + answer);
	
	var leftX = [30,254,478,702,926];
    var i=0,j=0;
    var xPrev=0;
    var correct = false;

    for (imagem in imagesm) {
		i++;
		var x = parseInt($(ssId+'imagem' + i ).css('left'), 10); // get integer
		console.log('imagem' + i + ' check x = '+x );
		if (x == leftX[j]) {
			console.log('Correct'+'imagem' + i + ' check x = '+x + '; leftX = '+leftX[j]);
			correct = true;
		}
		else{
			console.log('Incorect '+'imagem' + i + ' check x = '+x + '; leftX = '+leftX[j]);
			correct = false;
			break;
		}
		j++;
    }
    if (correct) {
		console.log('Answer is right');
		T2answerCorrect();
    }
    else{
		console.log('Answer is wrong');
		T2answerInCorrect();
    }
    
}

function T2answerCorrect() {
	var sectionName = 'section'+ (inSection+1);
	var slideName = 'slide' + (inSlide +1);
	var ssId='#'+sectionName+slideName; // section and slide name
	var ss=sectionName+slideName; // section and slide name
	
	$(ssId)
	.append('<div id="' + ss + 'answer"></div>');
	$(ssId+'answer')
	.attr('class','drag')
	.css({
		"position":"absolute",
		'width':'606px',
		'height':'414px',
		"background":" url(img/teisingai.png) no-repeat 50% 50%",
		'top':'150px',
		'left':'300px',
		"cursor": "pointer",
		"z-index": "9"
	});
	$(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':'200px',
		'left':'225px',
		"cursor": "pointer",
		"z-index": "9"
	})
	.bind('click', function(){ T2Close(); });
}

function T2answerInCorrect() {
	var sectionName = 'section'+ (inSection+1);
	var slideName = 'slide' + (inSlide +1);
	var ssId='#'+sectionName+slideName; // section and slide name
	var ss=sectionName+slideName; // section and slide name
	
	$(ssId)
	.append('<div id="' + ss + 'answer"></div>');
	$(ssId+'answer')
	.attr('class','drag')
	.css({
		"position":"absolute",
		'width':'606px',
		'height':'418px',
		"background":" url(img/neteisingai.png) no-repeat 50% 50%",
		'top':'150px',
		'left':'300px',
		"cursor": "pointer",
		"z-index": "9"
	});
	$(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':'200px',
		'left':'225px',
		"cursor": "pointer",
		"z-index": "9"
	})
	.bind('click', function(){ T2Close(); });
}

function T2Close() {
     var sectionName = 'section'+ (inSection+1);
    var slideName = 'slide' + (inSlide +1);
    var ssId='#'+sectionName+slideName; // section and slide name	
    $(ssId+'answer').remove();
    $(ssId+'close').remove();
}


