var autoModeQuestionIndex = [];
function setAutoMode(){
	$("#homePage").remove();
	$('.iPage').show();
	$('.iPage #tree').remove();
	$(".tg-mode").remove();
	//console.log($('#mainContainer').width())
	$('.iPage .rtContainer').css('width',"100%");
//	$('.rtContainer #header').css('height',"24.2%");
	$('.rtContainer #header').css('height',"24.5%");
	
	setAutoHeader();
	setQuestType();
	setHeaderText()
	
	$('#header #lastQuest-btn').on('click', function(){
		//console.log('last PAPER')
		loadFile()
		//setAddToDetail("", true);//this function is addToTestDetail.js file
	});
}
function setAutoHeader(){	
	if($(".rtContainer #header").children().length>0)
		$(".rtContainer #header").children().remove();
	
	var img = $('<img id="headLogo">'); 
	img.attr('src', "images/RS-Icon.png");
	$("#header").append("<div class='logoSection'></div>");
	$(".logoSection").append(img);
	//$(".logoSection").append("<p>Ratna Sagar</p>");
	var headerBtns = $('<div id="wrapper-button"> <button id="homeBtn1" class=btn-prop onclick="window.location.reload()">Home</button> <button id="previewTest" class=btn-prop>Preview Test </button><button id="lastQuest-btn" class="btn-prop">Last Paper</button> <button id="QuesSelectedBtn" class=btn-prop><span class="btn-prop-inner">Total Questions Selected</span></button> <button id="cartItems" class="btn-prop"><span class="btn-prop-inner">(0)</span></button> <p class="total_number">Total number of questions in book 6 - 510</p></div>');
	/* var headerBtns = $('<div id="wrapper-button"> <button id="homeBtn1" class=btn-prop onclick="window.location.reload()">Home</button> <button id="previewTest" class=btn-prop>Preview Test </button> <button id="QuesSelectedBtn" class=btn-prop>Total Questions Selected</button> <button id="cartItems" class="btn-prop">(0)</button> <p class="total_number">Total number of questions in book 6 - 510</p></div>'); */
		
	$("#header").append(headerBtns);
	
	$('#header #previewTest').on('click', function(){
		submitQuestions();
	});
}
function setQuestType()
{
	if($("#questBase").children().length>0)
		$("#questBase").children().remove();
	
	//console.log("setQuestType-1-"+questions.length)
	//console.log("setQuestType-2-"+treeStructure)
	//console.log("setQuestType-3-"+questions[0][treeStructure[0]])
	
	var questionTypeArr = [];
	var quesTypeTxtArr = [];
	var typeIndx=treeStructure.indexOf("type");
	
	let allSub = {};
	if(questionTypeArr.length == 0){
		for(var i=0; i<questions.length; i++)
		{
			console.log(questions[i].subject);	
			let c = allSub[questions[i].subject] || 0;
			allSub[questions[i].subject] = ++c;
			if(quesTypeTxtArr.indexOf(questions[i][treeStructure[typeIndx]]) == -1){
				quesTypeTxtArr.push(questions[i][treeStructure[typeIndx]]);
				questionTypeArr.push(i);
			}
		}		
	}
	console.log("ALL SUBS: ", allSub);
	ALL_SUB = Object.keys(allSub);
	//questions[i][treeStructure[j]]
	//questions[questionTypeArr[i]][treeStructure[typeIndx]]
	//console.log(questionTypeArr);
	for(var i=0; i<questionTypeArr.length; i++){
		$("#questBase").append('<div class=option-quest><div class="col2"><div class="column"><span class="hang">'+(i+1)+'. </span><span>'+questions[questionTypeArr[i]][treeStructure[typeIndx]]+'</span></div><div class="column"><input type="checkbox" class=autoCheckBox id="autoCheckBox'+questionTypeArr[i]+'"'+'/><label class="fright" for="autoCheckBox'+questionTypeArr[i]+'"'+'></label></div></div></div>');
	}
	$("#questBase .option-quest").each(function(){
		$(this).find('.fright').hide();
	});
	$("#questBase .option-quest").on('click', function(){
		//console.log($(this).find('.autoCheckBox').attr('id'));
		questTypeIndex = $(this).find('.autoCheckBox').attr('id').split('autoCheckBox')[1];
		removeBars($("#questBase .option-quest"))
		//setChapterType();
	});
	$("#questBase .option-quest").css("cursor", "pointer");
	setAutoBreadCrum("questionType");
}

var questTypeIndex;
function setChapterType()
{
	if($("#questBase").children().length>0)
		$("#questBase").children().remove();
	
	var typeIndex=0;
	for(; typeIndex<treeStructure.length; typeIndex++)
	{
		if(treeStructure[typeIndex] == 'type')
			break;
	}
	
	//console.log("setChapterType==="+questions[questTypeIndex][treeStructure[typeIndex]]);
	
	var questTypeTxt = questions[questTypeIndex][treeStructure[typeIndex]];
	
	var chapterTypeArr = [];
	var chapterTypeTxtArr = [];
	var chapterIndx=0;
	for(; chapterIndx<treeStructure.length; chapterIndx++)
	{
		if(treeStructure[chapterIndx] == 'chapter')
			break;
	}
	
	if(chapterTypeArr.length == 0){
		for(var i=0; i<questions.length; i++)
		{
			if(questions[i][treeStructure[typeIndex]] == questTypeTxt)
			{
				if(chapterTypeTxtArr.indexOf(questions[i][treeStructure[chapterIndx]]) == -1){
					chapterTypeTxtArr.push(questions[i][treeStructure[chapterIndx]]);
					chapterTypeArr.push(i);
				}
			}
		}
	}
	//questions[i][treeStructure[j]]
	//questions[chapterTypeArr[i]][treeStructure[chapterIndx]]
	//console.log(chapterTypeArr);
	
	$("#questBase").append('<div class="option-quest emptyBlock"><div class="col2"><div class="column"><span class="hang ">'+'  &#x00A0;&#x00A0;'+'</span><span>'+''+'</span></div><div class="column"><button id="addQuest" class="select-btn-prop fright">Submit</button></div></div></div>');
	
	for(var i=0; i<chapterTypeArr.length; i++){
		$("#questBase").append('<div class="option-quest"><div class="col2"><div class="column"><span class="hang">'+(i+1)+'. </span><span>'+questions[chapterTypeArr[i]][treeStructure[chapterIndx]]+'</span></div><div class="column"><input type="checkbox" class=autoCheckBox id="autoCheckBox'+chapterTypeArr[i]+'"'+'/><label class="fright" for="autoCheckBox'+chapterTypeArr[i]+'"'+'></label></div></div></div>');
	}
		
	$("#questBase").append('<div class="option-quest emptyBlock"><div class="col2"><div class="column"><span class="hang">'+'  &#x00A0;&#x00A0;'+'</span><span>'+''+'</span></div><div class="column"><button id="addQuest" class="select-btn-prop fright">Submit</button></div></div></div>');
		
	$('#questBase #addQuest').on('click', function(){
		submitQuestions();
	});
	setAutoBreadCrum("chapter");
}
function removeBars(qBars){
	var i = 0;
	var topArr = [];
	var w, h, l;
	
	$(".breadcrumb").children().prop('disabled', true).css("pointer-events", "none").fadeTo(0,0.5);	
	
	qBars.each(function(){		
		w = $(this).clientWidth;
		h = $(this).clientHeight;
		topArr.push($(this).position().top);
		l = $(this).position().left;
	});
	qBars.each(function(i){		
		$(this).css({'position':'absolute',
						'width':'1000px',
						'height':h,
						'top':topArr[i],
						'left':l
						});	
		$(this).animate({
			left:'350px',
            opacity: '0'
        },600 ,function(){
			$(this).remove();
			i++;
			if(i==qBars.length)
				setChapterType();
		} ); 
	});
}
function submitQuestions(){
	
	$("#questBase").find('.autoCheckBox').each(function(){
			//console.log("CHECK=="+$(this).prop('checked'));
			if($(this).prop('checked')){
				var index = $(this).attr('id').split('autoCheckBox')[1];
				getAllQuestionIndex(index);
			}
		});		
	saveAutoModeQuestion();
		
}
function getAllQuestionIndex(index)
{
	var typeIndex=0;
	for(; typeIndex<treeStructure.length; typeIndex++)
	{
		if(treeStructure[typeIndex] == 'type')
			break;
	}
	var chapterIndx=0;
	for(; chapterIndx<treeStructure.length; chapterIndx++)
	{
		if(treeStructure[chapterIndx] == 'chapter')
			break;
	}
		
	var chapterTxt = questions[index][treeStructure[chapterIndx]];
	var chapterTypeTxt = questions[index][treeStructure[typeIndex]];
	
	for(var i=0; i<questions.length; i++){
		if(chapterTxt == questions[i][treeStructure[chapterIndx]] && chapterTypeTxt == questions[i][treeStructure[typeIndex]])
		//saveQuestion('autoMode', i);//addToTestDetail.js
		//push({id:'replace-quest-txt'+remainQuestIndexArr[l], path:questions[remainQuestIndexArr[l]].question.image, codeStr:""});
		autoModeQuestionIndex.push({id:i, path:questions[i].question.image, codeStr:""});
	}
}
var ind = -1;
var urlTemp = [];
function saveAutoModeQuestion()
{
	ind += 1;
	//console.log("saveAutoModeQuestion(1)====="+autoModeQuestionIndex.length);
	if(ind < autoModeQuestionIndex.length)
	{
		//console.log("saveAutoModeQuestion(2)====="+autoModeQuestionIndex[ind].id);
		if(autoModeQuestionIndex[ind].path == '')//NO image question
		{
			saveQuestAutoMode('autoMode', autoModeQuestionIndex[ind].id, '');//this function is addToTestDetail.js file
			saveAutoModeQuestion();
		}
		else{
			var script = document.createElement('script');
			/* script.src = ""+autoModeQuestionIndex[ind].path.split(".")[0]+".js";
			urlTemp.push(""+autoModeQuestionIndex[ind].path.split(".")[0]+".js"); */
			script.src = createJsFileName(autoModeQuestionIndex[ind].path);
			urlTemp.push(createJsFileName(autoModeQuestionIndex[ind].path));
			script.onload =  function(){
				//console.log(autoModeQuestionIndex[ind].id+"=====saveAutoModeQuestion(3)====="+autoModeQuestionIndex[ind].path);
				saveQuestAutoMode('autoMode', autoModeQuestionIndex[ind].id, img);//this function is addToTestDetail.js file
				saveAutoModeQuestion();			  
			};
			document.head.appendChild(script);
		}
	}else{
		$('script').each(function(i){
			if(urlTemp.indexOf($(this).attr('src')) > -1)
				$(this).remove();
		});
		ind = -1;
		autoModeQuestionIndex.length = 0;
		setAddToDetail("autoMode");//this function is addToTestDetail.js file
	}
}
function setAutoBreadCrum(levelStr)
{
	var breadCrumArr = [];
	
	switch(levelStr)
	{
		case "questionType": breadCrumArr = [questions[0][treeStructure[0]],'Question Types'];
			break;
		case "chapter": breadCrumArr = [questions[0][treeStructure[0]],'Question Types','Chapter'];
	}
	console.log(breadCrumArr);
	
	if($(".breadcrumb").length>0)
		$(".breadcrumb").remove();
	
	var str = "<ul class=breadcrumb>";
	for(var j=0; j<breadCrumArr.length; j++){
		str += "<li><a class="+breadCrumArr[j]+" href=#>"+breadCrumArr[j]+"</a></li>";
	}	
	
	str += '</ul>';		
		$("#breadCrum").append(str);
	
	$('#breadCrum').find('li a').on('click', function(e){
		//console.log("bread crum click="+$(e.target).attr('class'));
		if($(e.target).attr('class') == "Chapter")
			setChapterType();
		else if($(e.target).attr('class') == "Question")
			setQuestType();
	});
}