//dnabeth 04 10 2015 change searchText to searchTextFAQ  
var FAQOnLoad = function(){

	//$('.searchText').bind('keyup' , searchKeyPress);
	$('#faqmenu ul li').click(function(){
		$('#faqmenu ul li').removeClass('selected');
		$(this).addClass('selected');
		
		$('#questionContainer').empty();
		$('#accordeon').empty();
		$('#separator').hide();
		
		
		//$('.answerContainer').empty();
		loadArticle();
		
	});
	
	//$('.search .bigButtonOrange').click(function(){
	//	$('#questionContainer').empty();
	//	loadSearch();
	//});
	
	var searchTextFAQvalue = $("[id$='searchTextFAQ']").val();
	UTILS.compileDustTemplates();
	if (searchTextFAQvalue  === '')
		$('#faqmenu ul li').first().click();
	if (searchTextFAQvalue  !== '')
		loadSearchonClick();
}

var loadArticle = function(){
	var isGeneralArticles = false;
	var isTypeArticles = false;
	
	$('#zeroResults, #noFaqForSelection').hide();

	var categorie = $('#faqmenu ul li.selected').attr('data-id');
	var map = new Object();
	
	if (categorie != 'Most'){		
		//HK FAQ
		$('[data-univers="' + UTILS.universParentName + '"][data-id="' + categorie +'"] .categoryItem').each(function (index , elem){
			var name = $(elem).find('.categoryName').val();
			var label = $(elem).find('.categoryLabel').val();
			map[name]= label ;
		});
	}

	let aaaaa = 0;
	let nnnnn = 1;
	let updateAaaaa = i => {
		aaaaa += i;
		if (--nnnnn <= 0 && aaaaa == 0) $('#noFaqForSelection').show();
	};
	
	Web_FAQ.loadArticles(categorie,UTILS.universName, UTILS.language, map , function(result, event) {
	
		if(event.status){
			if (result.length > 0){
				isGeneralArticles = true ;
			}
			
			if (isTypeArticles == true && isGeneralArticles== true ){
				$('#separator').show();
			}

		
			
			UTILS.dustRender('tmpl_articles',result, $('#questionContainer'));
		}

		updateAaaaa(result?.length || 0);
	},
	{escape:false});
	
	if (categorie === 'Verticality' || categorie === 'Lighting' || categorie === 'Products') {
		++nnnnn;

		Web_FAQ.loadArticlesbyType(categorie,UTILS.universName, UTILS.language, map , function(result, event) {
	
		if(event.status){
			if (result.length> 0){
				isTypeArticles = true; 
			}
			if (isTypeArticles == true && isGeneralArticles== true ){
				$('#separator').show();
			}	

			
			UTILS.dustRender('tmpl_articlesByType',result, $('#accordeon'));
			new petzl.controllers.Faq
		}

		updateAaaaa(result?.length || 0);
	},
	{escape:false});
	}

}


var submitListener = function(e){
		
		if(e.keyCode==13){
			loadSearchonClick();
		}
			
	}
var loadSearchonClick = function (){
		$('#questionContainer').empty();
		$('#accordeon').empty();
		$('#zeroResults, #noFaqForSelection').hide();
		$('#separator').hide();
		
		$('#faqmenu ul li').removeClass('selected');
		var text = $('#theQ').text()
		var fText = $('#theFamily').text();  
		var searchTextFAQvalue = $("[id$='searchTextFAQ']").val();
	
		if (text == ''){
			text = searchTextFAQvalue;
		}
			
		Web_FAQ.Searchs(text, fText, UTILS.universName, UTILS.language, function(result, event) {
			
			if(event.status){
			
				
				if (result.length > 0){
					UTILS.dustRender('tmpl_articles',result, $('#questionContainer'));
				}
				else{
					// UTILS.dustRender('tmpl_articles',result, $('#questionContainer'));
					// $('#zeroResults').show();
				}
				
				$('#theQ').remove();
				$('#theFamily').remove();
			}

			if (result?.length || 0 === 0) $('#zeroResults').show();
		},
		{escape:false});
	
}
	