/* 3e test template
 * sort items in 3 columns
 * requires:
 * jquery.js
 * player_test_template.js
 * iliustraData.js
 */

console.log('> template3e.js is iniciated;');

function template3escript() {
	console.log("function template3escript(); sectionName=" +sectionName + "; slideName=" +slideName);	
	
         tObj = getObject();
    
        var mainVars = initMainVars(tObj,sectionName,slideName);
	var tO = mainVars.tO;
        var items = tO['items'];  	
	//var captions = tO['captions'];    
    
    // count items
	var itemsCount = tO['items_count']; 	console.log('items count: ' +itemsCount);

	if (tO['background']) {
		loadSlideBackground();
	}
    
    var p = {	// properties for 3 captions
		end :{
			min:{
				x:10,
				y:500
			},
			max:{
				x:600,
				y:590
			},
		},
		left:{
			caption_1 : 30,
			caption_2 : 430,
			caption_3 : 830
		},
		caption :{
			'width':'370px',
			"position":"absolute",
			'height':'60px',
			'top':'165px',
			"z-index": "10",
			'background-image': 'url("img/fonas_1.jpg")',
			'color' : '#ffffff',
			'text-indent':'0px',
			'font-family':'Arial',
			'font-size': '24px',
			'text-align':'center',
			'line-height' : '60px',
			'border-bottom': '1px solid #a9c7e1'
		},
		container :{
			'width':'370px',			
			"position":"absolute",
			'height':'320px',
			'top':'224px',
			'background-image': 'url("img/fonas_1.jpg")',
			'background-repeat': 'no-repeat',
			'background-position': '0 -84px',
			"z-index": "7"
		},
		text:{
			css	:{
				main	:{
					"position":"absolute",
					'width':'360px',
					'height': 'auto',
					'top':'350px',
					'left':'200px',
					"cursor": "move",
					"z-index": "10",
					"border": "1px solid rgb(255,255,255)",
					//'opacity':'0.5',
					"background":"#e0f7fd",
					"-webkit-box-shadow": "0px 2px 1px 0px rgba(0,0,0,0.3)",
					"-moz-box-shadow": "0px 2px 1px 0px rgba(0,0,0,0.3)",
					"box-shadow": "0px 2px 1px 0px rgba(0,0,0,0.3)"
				},
				text:{
					"position":"absolute",
					'width':'311px',
					'margin-top' : '4px',
					'margin-bottom':'4px',
					'margin-right':'10px',
					'margin-left':'10px',
					"z-index": "7",
					'text-indent':'0px',
					'font-family':'Arial',
					'font-size': '21px',
					'text-align':'left',
					'line-height': '28px'
				},
				hover_on:{
					"background":"#e0f7fd"
				},
				hover_out:{
					"background":"#e0f7fd"
				},
			}
		}
	}	
	

	var p2 = {	// properties for 2 captions
		end :{
			min:{
				x:10,
				y:450
			},
			max:{
				x:600,
				y:560
			},
		},
		left:{
			caption_1 : 50,
			caption_2 : 655
		},
		caption :{
			"position":"absolute",
			'width':'555px',
			'height':'60px',
			'top':'165px',
			"z-index": "10",
			'background-image': 'url("img/fonas_1.jpg")',
			'color' : '#ffffff',
			'text-indent':'0px',
			'font-family':'Arial',
			'font-size': '24px',
			'text-align':'center',
			'line-height' : '60px',
			'border-bottom': '1px solid #a9c7e1'
		},
		photo:{
			css	:{
				'width'	:'300px',
				'height':'262px',
				"z-index": "7",
				"float": "left",
				"margin-left": "24px",
				"margin-top": "32px"
			}
		},
		container :{
			"position":"absolute",
			'width':'555px',
			'height':'320px',
			'top':'224px',
			'background-image': 'url("img/fonas_1.jpg")',
			'background-repeat': 'no-repeat',
			'background-position': '0 -84px',
			"z-index": "7"
		},
		text:{
			css	:{
				main	:{
					"position":"absolute",
					'width':'545px',
					'height': 'auto',
					'top':'350px',
					'left':'200px',
					"cursor": "move",
					"z-index": "10",
					"border": "1px solid rgb(255,255,255)",
					//'opacity':'0.5',
					"background":"#e0f7fd",
					"-webkit-box-shadow": "0px 2px 1px 0px rgba(0,0,0,0.3)",
					"-moz-box-shadow": "0px 2px 1px 0px rgba(0,0,0,0.3)",
					"box-shadow": "0px 2px 1px 0px rgba(0,0,0,0.3)"

				},
				text:{
					"position":"absolute",
					'width':'488px',
					'margin-top' : '4px',
					'margin-bottom':'4px',
					'margin-right':'10px',
					'margin-left':'10px',
					"z-index": "7",
					'text-indent':'0px',
					'font-family':'Arial',
					'font-size': '21px',
					'text-align':'left',
					'line-height': '28px'
				},
				hover_on:{
					"background":"#e0f7fd"
				},
				hover_out:{
					"background":"#e0f7fd"
				},
			}
		}
	}	
	
	var captions_count = tO['captions_count'];
	
	if (captions_count == 2) {
		p =p2;
	}
	
	var zIndexChange=20;
	
	var drop_in = {
		text:{},
		order:{
			caption_1 :[],
			caption_2 :[],
			caption_3 :[]
			},
		height:{},
		items :{},
		caption: {},
		caption_1 :{},
		caption_2 :{},
		caption_3 :{}
	}
	
	if ((tO.photo != '') && (tO.photo != null)) {
		p.end.min.y = 450;
		p.end.max.y = 550;
	}
	
	// generate random end points for items x and y ;
	randomise_items_end_XY(itemsCount,p);
	
	// randomise items order
	var randPlaces = randomArray(itemsCount);
	
	items = change_items_to_random(items, randPlaces);
	
	//  get true - correct ; or falls - incorrect
	this.checkAnswer = checkTestAnswer_3e;
	// check buttton and function to check
	checkTestButton(this.checkAnswer,tO,null,randPlaces,drop_in);

	loadSlideBackground();
	
	appendQuestion(ssId, ss, tO, 3);
	
	if ((tO.photo != '') && (tO.photo != null)) {
		append_photo_3e('../'+tO.photo,p);
	}
	
	append_captions(tO.captions,p,drop_in);
	
    append_items(items,p,4,null,ss,ssId,drop_in);
    
    if ($('.img_3e').length == 0) {
		var heights = [0, 0, 0];
		var k = 0;
		var nm = undefined;
		for (item in drop_in.caption) {
			num = tO['answer'][randPlaces[k]] - 1;
			h = $(ssId + 'item_' + k).outerHeight() + 2;
			console.log('itm', item, num, k, h);
			heights[num] = heights[num] + h;
			k = k + 1;
		}
		max_c_h = Math.max(heights[0], Math.max(heights[1], heights[2]));
		$(ssId + 'caption_2container').css('height', max_c_h + 20);
		$(ssId + 'caption_1container').css('height', max_c_h + 20);
		$(ssId + 'caption_3container').css('height', max_c_h + 20);
		
		var item;
		for (item in items) {
			var e = p.end[item];
			e.top = e.top - 450 + $(ssId + 'caption_2container').position().top + max_c_h;
			$(ssId + item).animate({ 'top': e.top + 'px', 'left': e.left + 'px'}, 800, function(){});
		}
	} else {
		max_c_h = 320;
		for (item in items) {
			var e = p.end[item];
			$(ssId + item).animate({ 'top': e.top + 'px', 'left': e.left + 'px'}, 800, function(){});
		}
	}
	
	console.log('caption heights', heights);
	
	var clname = "."+sectionName+slideName + "items_3e";
        image_mDraggable(clname,ssId,zIndexChange,3);

	console.log('drop_in:', drop_in);
		
	if ($('.img_3e').length > 0) {
		if ($(ssId + 'caption_3').length) {
		} else {
			console.log('got it');
			$('.container').css('width', '553px');
			$('.container').css('height', '300px');
			$(ssId + 'caption_2').css('width', '553px');
			$(ssId + 'caption_1').css('width', '553px');
		}
	}
	
	if ((tO.photo != '') && (tO.photo != null)) {
		normalize_height(750);
	} else {
		hh = $(ssId + 'caption_2container').position().top + max_c_h + 150;
		normalize_height(hh);
	}
}

