/* T2 test template
 * requires:
 * jquery.js
 * drag.js
 */

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

function templateT1script() {
	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 = doContent[sectionName][slideName]['images']
	
	var i = 0;
	
	// generate random x and y;
	
	if (!XYdone) { // if randomise done, dont repeat it again, reopen slide in same situation as left.
		randInOrder();
		randomise();
	
		for (image in images) {
		 // get random coordinates for each image
		 // old random
		//var x = rand(minx,maxx);
		//var y = rand(miny,maxy);
		
		var x = allRandomXY[i][0];
		var y = allRandomXY[i][1];
		console.log('Random image position: x='+x+' ; y='+y);
		i++;   // count images
		console.log("image" +image);
		$(ssId)
			.append('<div class="drag" id="' +ss+'image' + i + '"></div>');
		$(ssId+'image' + i)
			.attr('class','drag')
			.css({
			"position":"absolute",
			"border": "1px #00ff00",
			'width':'788px',
			'height':'56px',
			"background":" url("+images[image]+") no-repeat 50% 50%",
			'top':'350px',
			'left':'200px',
			"cursor": "move",
			"z-index": "6"
			})
			.animate({ 'top': y + 'px', 'left': x + 'px'}, 2000, function(){});
		}
		
		$(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(){ T1check(); });
	}
}

function T1check() {
    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 images = doContent[sectionName][slideName]['images'];
    var i=0;
    var xPrev=0;
    var correct = false;
    loop1:
    for (image in images) {
	i++;
	var x = parseInt($(ssId+'image' + i ).css('top'), 10);
	console.log(image + ' check x = '+x +'; xPrev='+ xPrev);
	if (x > xPrev) {
	    console.log('Correct');
	    correct = true;
	    xPrev = x;
	}
	else{
	    correct = false;
	    break loop1;
	}
    }
    //correct = true;
    if (correct) {
	console.log('Answer is right');
	T1answerCorrect();
    }
    else{
	console.log('Answer is wrong');
	T1answerInCorrect();
    }
    
}

function T1answerCorrect() {
	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(){ T1Close(); });
}

function T1answerInCorrect() {
	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(){ T1Close(); });
}

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


function rand(min,max) {
    var x = (Math.random() * (max - min) + min).toFixed(0);
    return x;
}

// make x and y with diviation for 6 images
function randomise() {// nice random
	var i = 6;
	
	console.log('minx='+minx+'; miny='+miny);
	 
	var dx = ((maxx-minx)/i).toFixed(0);
	var dy = ((maxy-miny)/i).toFixed(0);
	
	console.log('dx='+dx+'; dy='+dy);
	
	for (var j = 0; j<6;j++) {
	var xy =[];
	
	
	var x;
	var y;	
	
	x = +( (orderx[j] * dx)+ (dx *0.5* Math.random())).toFixed(0);
	y = +( (ordery[j] * dy)+ (dy *0.5* Math.random())).toFixed(0);
	console.log('final'+j+': x='+x+'; y='+y);
	
	x += +minx
	y += +miny
	console.log('final'+j+': x='+x+'; y='+y);
	xy.push(x);
	xy.push(y);
	allRandomXY[j] = xy;
	}
	console.log('allRandomXY :');
	console.log(allRandomXY);
	XYdone =1;
}


// randomly get 6 x and 6 y numebers
function randInOrder() {
    for ( var i=0;i<6;i++) {
	var x=rrx();
	var y=rry();
	orderx.push(x);
	ordery.push(y);
	console.log('orderx['+i+']='+orderx[i] );
	console.log('ordery['+i+']='+ordery[i] );
    }
    console.log('final orderx[] and ordery[]:');
    console.log(orderx);
    console.log(ordery);
}

function rrx(){
    T1level++;
    console.log('xlevel+='+T1level);
    var r = (Math.random()*5).toFixed(0);
    console.log('rx='+r);
    
    for ( var j=0;j<6;j++) {
		console.log('orderx['+j+']='+orderx[j] +'; r=' +r);
		if (orderx[j] === r) {
			console.log('orderx[j]===r');
			r = rrx();
		}
    }
    T1level--;
    console.log('xlevel-='+T1level);
    return r;
}
function rry(){
	T1level++;
	console.log('ylevel+='+T1level);
	var r = (Math.random()*5).toFixed(0);
	console.log('ry='+r);
	for ( var j=0;j<6;j++) {
		console.log('ordery['+j+']='+ordery[j] +'; r=' +r);
		if (ordery[j] === r) {
			console.log('ordery['+j+']='+ordery[j] +'=== r=' +r);
			r = rry();
		}
	}
	T1level--;
	console.log('ylevel-='+T1level);
	return r;
}

