/* 4b test template
 * requires:
 * jquery.js
 * player_test_template.js
 * iliustraData.js
 */

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

function template4bscript() {
	console.log("function template4bscript(); sectionName=" +sectionName + "; slideName=" +slideName);	
	
         tObj = getObject();
    
        var mainVars = initMainVars(tObj,sectionName,slideName);
	var tO = mainVars.tO;
        var items = tO['items'];
        
        
    item_dim = tO['caption'].split('*/');
    if (item_dim.length > 1) {
		
		var sst = String(item_dim[1]);
		var item = sst.replace(/<\/p>/gi, '');
		
		var itm = item.split(',');
		line_h = itm[1]+'px';
		
		itemt_w = parseInt(itm[0]);
		item_w = (itemt_w + 1) + 'px';
		itemt_w = itemt_w + 'px';
		
	} else {
		item_w = '246px';
		itemt_w = '245px';
		line_h = '38px';
	}
        
	// count items
	var itemsCount = tO['items_count']; 	console.log('items count: ' +itemsCount);
	
	if (tO['background']) {
		loadSlideBackground();
	}
    
    var p = {	// properties
		end :{
			min:{
				x:5,
				y:5
			},
			max:{
				x:680,
				y:200
			},
		},
		photo:{
			css	:{
				"position":"absolute",
				'width'	:'800px',
				'top'	: '130px',
				'left' 	: '30px',
				"z-index": "6"
			}
		},
		text:{
			css	:{
				main	:{
					"position":"absolute",
					'width':item_w,
					'height':'34px',
					'top':'350px',
					'left':'200px',
					"cursor": "move",
					"z-index": "10",
					"border": "1px solid #1fc8ff",
					"background":"white"
				},
				text:{
					"position":"absolute",
					'width':itemt_w,
					'height':'34px',
					'top':'0px',
					'left':'0px',
					"z-index": "7",
					'text-indent':'0px',
					'font-family':'Arial',
					'font-size': '24px',
					'text-align':'center',
					'line-height': line_h
				}
			}
		}
	}	
	
	var zIndexChange=20;
	
	var answered ={};

	if (tO.photo != '') {
		append_photo(tO.photo,p);
		img = $(ssId+'_photo_div > img');
		img.css('width', '800px');
		
		p.text.css.text.height = 'auto';
		p.text.css.main.height = 'auto';
		p.text.css.text.position = 'unset';
		p.text.css.text.line_height = 'unset';
	}

	appendQuestion(ssId, ss, tO, 4);
	append_caption_text_4b(tO,p,answered);
	$(ssId+'caption').css('z-index', '7');
	
	all_h = $(ssId+'caption').position().top + $(ssId+'caption').outerHeight();
	console.log('all_h', all_h);
	p.end.max.y = all_h + 200;
	p.end.min.y = all_h + 30;	
	
	// generate random end points for items x and y ;
	randomise_items_end_XY(itemsCount,p);
	// randomise items order
	var randPlaces = randomArray(itemsCount);
	
	//  get true - correct ; or falls - incorrect
	this.checkAnswer = checkTestAnswer_4b;
	// check buttton and function to check
	checkTestButton(this.checkAnswer,tO,null,randPlaces,null,null,answered);
	
	items = change_items_to_random(items, randPlaces);
	
	append_items(items,p,5,null,ss,ssId);
        
        var clname = "."+sectionName+slideName + "items_4b";	
	image_mDraggable_4b(clname,ssId,zIndexChange);	
	
	if (item_dim.length > 1) {
		
		$('.drp').css('width', (parseInt(item_w)+2)+'px');
		$('.drp2').css('width', (parseInt(item_w)+2)+'px');
		
		item_h =  0;
		$('.ui-draggable').each( function() {
				if ($(this).children('div').children('img').length) {
				} else {
					item_h = Math.max($(this).outerHeight(), item_h);
				}	
			});
			
		$('.ui-draggable').css('height', item_h+'px');	
		$('.ui-draggable > div').css('height', item_h+'px');
		$('.ui-draggable > div').css('display', 'table-cell');
		$('.ui-draggable > div').css('vertical-align','middle');
			
		$('.drp').css('height', (item_h+2)+'px');
		$('.drp2').css('height', (item_h+2)+'px');
		
		$(ssId+'_photo_div > img').css('width', 'initial');
		
		
		tp = $(ssId + 'question').position().top + $(ssId + 'question').outerHeight()+30;
		$('.ui-draggable').each( function() {
				if ($(this).children('div').children('img').length) {
				} else {
					$(this).css('top', tp+'px');
					tp = tp + item_h + 8;
					$(this).css('left', (1210-parseInt(item_w))+'px');
				}
				
			});
			
	}
	
	$(ssId).append('<img id="'+ss+'_fake2" style="position: absolute; left: -1000px; top: -1000px">');
	$(ssId+'_fake2').on('load', function(){
			
			var ih = $(this).height();
			
			$('.ui-draggable').css('height', ih+'px');	
			$('.ui-draggable > div').css('height', ih+'px');
				
			$('.drp').css('height', (ih+2)+'px');
			$('.drp2').css('height', (ih+2)+'px');
			
			ctp = $(ssId + 'question').position().top + $(ssId + 'question').outerHeight() + 30;
			
			tp = ctp;
			lf = 0;
			$('.ui-draggable').each( function() {
					$(this).css('top', tp+'px');
					tp = tp + ih + 8;
					$(this).css('left', (1210-parseInt(item_w)-lf)+'px');
					
					if (tp >  300) {
						tp = ctp;
						lf = lf + 72;
					}
				});	
			
		});
	
		
	all_img = true;	
	for (i=0; i< item_dim.length; i++) {
		if (items['item_'+i].indexOf('assets') == -1) {
			all_img = false;
		}
	}	
	
	if ((tO.photo != '') && (tO.photo != null)) {
		
		if (all_img) {
			itm_d = items['item_'+0];
			itm_d = itm_d.replace('==', '');
			itm_d = itm_d.replace('--', '');
			$(ssId+'_fake2').attr('src', itm_d);	
		} else {
			
			w = '128px';
			h = '128px';
			
			sizes = tO['caption'].split('*/')[1].replace('</p>', '');
			sizes = sizes.split(',');
			if (sizes.length == 4) {
				w = sizes[2] + 'px';
				h = sizes[3] + 'px';
			}
			
			$('.for_img').css({'width': w, 'height':h});
			$('.for_img > div').css({'width': w, 'height':h});
			
			tp = $(ssId + 'question').position().top + $(ssId + 'question').outerHeight()+30;
			$('.ui-draggable').each( function() {
					if ($(this).children('div').children('img').length) {
						$(this).css({'width': w, 'height': h, 'top': tp+'px', 'left': '898px'});
						tp = tp + 135;
					} 				
				});
			
		}
		
		normalize_height(700);
		
		tp = $(ssId + 'question').position().top + $(ssId + 'question').outerHeight() + 30;
		$(ssId + '_photo_div').css('top', tp+'px');
		
		delta = 130 - tp;
		
		$('.ui-droppable').each( function() {
				tt = $(this).position().top - delta;
				$(this).css('top', tt + 'px');
			})	
		
		$(ssId).append('<img id="'+ss+'_fake3" style="position: absolute; left: -1000px; top: -1000px">');
		$(ssId+'_fake3').on('load', function(){
				all_h = $(this).outerHeight() + $(ssId+'_photo_div').position().top;
				normalize_height(all_h);
			});
			
		$(ssId+'_fake3').attr('src', tO.photo);	
		
		
	} else {
		all_h = p.end.max.y + 30;
		normalize_height(all_h);
	}
	
}
