var globalIndexObj = new Object(); // to hold all the word index data for each product 
var globalFileInfoObj = new Object(); // to hold file info data
var globalFileIndexObj = new Object(); // to hold the file content array of each product

var w;	//for storing index array
var fil; // for storing the file info array
var productsObj; //for storing the product details
var productIdArray; // for storing the installed product ids

var synonymsArray;	//for storing the synonyms
var stopWordsArray;	//for storing the stop words
var keyWordArray; //for building the auto complete array
var fuzzySearch; //for fuzzy search

var productCounter = 0; //incremental counter for the product

/* vars relativePath, webhelpPath, and searchFolder set in XSLT transform; 
   values may be overridden using XSLT parameter */

//to find the IE version and customize some function based on version
var isIEOldVersion = findIEVersion(); 

//Hide the case sensitive option for Asian language

if(typeof language != "undefined"){
	if(language=="zh" || language=="zh_cn" || language=="zh_tw" ||  language=="zh-cn" || language=="zh-tw" || language=="ja" || language=="ko" || language=="th"){ 	
		languageType = "asian";	
	}else if(language=="en"){
		languageType = "english";
	}else{
		languageType = "others";
	}	
}else{
	languageType = "english";
} 

//to load the index array for all products
loadProductArray();

/* For checking IE old version or not */
function findIEVersion(){	
	var ver = 999;	
    var isIEOldVersion = false;
	if (navigator.appName == 'Microsoft Internet Explorer'){
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null){
			ver = parseFloat( RegExp.$1 );
		}
	}	
	if ( ver < 10.0 ){
	   isIEOldVersion = true;
    }
	return isIEOldVersion;	
}

// to create one parent indexing data for each product 
// by loading the three indexing files
function loadProductArray(){
	//the product file path
	var scriptFileName = relativePath + productFolder + searchFolder + '/products.js';			
		
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function(){
		setUpProductSelectionBox();
	};	
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;	
	}else{
		script.onload = callBackFun;
	}	
}

// to create the product selection list
function setUpProductSelectionBox(){
	var currentPath = window.location.pathname;	//current local machine path
	var option;
	var productCount = 0;
	productIdArray = new Array();
	
	//iterate all the products
	for(var currProduct in productsObj){
		productIdArray.push(currProduct); 
		productCount++;
		
		//to dynamically add options to the product select box
		option = document.createElement("option");
		option.text = productsObj[currProduct];
		option.value = currProduct;		
		document.ditaSearch_Form.productSelect.add(option);
		if(currentPath.indexOf(currProduct) != -1){			
			//to select the currently searched product in the select box
			document.ditaSearch_Form.productSelect.value = currProduct; 
		}
	}
	
	// to hide the product selection box if there is only one product 
	if(productCount == 1){
		document.getElementById("productSelItem").style.display="none";	
	}		
	createGlobalIndexArray();	
}

// to create one global indexing array that holds
// the indexing data for all products
function createGlobalIndexArray(){
	
	w = new Object(); //create an indexing array		
	if(productCounter < productIdArray.length){				
		var productId 	= productIdArray[productCounter];
		var productPath = relativePath  + productId + webhelpPath + 'search';
		createProductIndexArray(1, productPath, productId);
		productCounter++;
	}else{		
		productCounter = 0;
		createGlobalFileInfoArray();			
	}		
}

// to create one parent indexing data for each product 
// by loading the three indexing files
function createProductIndexArray(fileNo, productPath, productId){		
	
	//build the indexing file name using the product id
	var scriptFileName = productPath + '/index-' + fileNo + '.js';			
		
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);		
	
	// executes when the script file is fully loaded
	var callBackFun = function() {		
		if(fileNo == 3){			
			var temp = JSON.parse(JSON.stringify(w));
			//assign the current product index array to parent array				
			globalIndexObj[productId] = temp; 			
			createGlobalIndexArray();			
		}else{			
			createProductIndexArray(++fileNo, productPath, productId);			
		}
	};	
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}	
}	

// to create one global indexing array that holds
// the file info for all products
function createGlobalFileInfoArray(){		
	
	if(productCounter < productIdArray.length){				
		var productId 	= productIdArray[productCounter];
		var productPath = relativePath  + productId + webhelpPath + 'search';		
		createFileInfoArray(productPath, productId);
		productCounter++;
	}else{									
		productCounter = 0;		
		createGlobalFileIndexArray();	
	}	
}	

// to create file info data for each product 
// by loading the htmlFileInfoList file
function createFileInfoArray(productPath, productId){		
	
	//build the indexing file name using the product id		
	var scriptFileName = productPath + '/htmlFileInfoList.js';			
		
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function() { 		
		var temp = JSON.parse(JSON.stringify(fil));			
		//assign the current file info data to parent object				
		globalFileInfoObj[productId] = temp; 			
		createGlobalFileInfoArray();			
	};	
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}		
}


//to create one global indexing array that holds
//the help file content for all products
function createGlobalFileIndexArray(){		
	
	if(productCounter < productIdArray.length){				
		var productId 	= productIdArray[productCounter];
		var productPath = relativePath  + productId + webhelpPath + 'search';		
		createFileIndexArray(productPath, productId);
		productCounter++;
	}else{									
		productCounter = 0;		
		loadSynonymArray();	
	}	
}	

//to create file info data for each product 
//by loading the htmlFileInfoList file
function createFileIndexArray(productPath, productId){		
	
	//build the indexing file name using the product id		
	var scriptFileName = productPath + '/file-index.js';			
		
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function() { 		
		var temp = JSON.parse(JSON.stringify(f));			
		//assign the current file info data to parent object				
		globalFileIndexObj[productId] = temp; 			
		createGlobalFileIndexArray();			
	};		
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}		
}


// to create a synonym array
// by loading the synonym file
function loadSynonymArray(){
	
	//the synonym file path
	var scriptFileName = 'search/synonyms.js';			
	
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function() {	
		loadStopWordsArray();
	};		
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}			
}

// to create a stop words array
// by loading the stop words file
function loadStopWordsArray(){
	
	//the synonym file path
	var scriptFileName = 'search/stopwords.js';		
	
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function() { 		
		loadAutoCompleteJs();		
	};		
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}		
}

// to create a keyword array for auto complete
function buildAutoCompleteArray(){
	keyWordArray = new Array();
	var currProduct = document.ditaSearch_Form.productSelect.value; 
	
	for (var product in globalIndexObj) {
		w = globalIndexObj[product];
		for (var key in w) {	
			if(key.length > 1 && key.indexOf('_') == -1){
				keyWordArray.push(key);	
			}
		}	
	}
	keyWordArray = unique(keyWordArray);
}

// to create a keyword array for auto complete
function loadAutoCompleteJs(){
	buildAutoCompleteArray();
	
	//the synonym file path
	var scriptFileName = '../common/autocomplete.js';			
	
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function() { 			
		initializeFuzzySearch();
	};		
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}			
}

// to load the fuzzy search JS file
function initializeFuzzySearch(){
	
	//the synonym file path
	var scriptFileName = '../common/fuse.min.js';			
	
	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);

	// executes when the script file is fully loaded	
	var callBackFun = function() { 			
		var options = {
			caseSensitive: false,
			includeScore: false,
			shouldSort: true,
			threshold: 0.5,
			location: 0,
			distance: 60,
			maxPatternLength: 32			
		};
		fuzzySearch = new Fuse(keyWordArray, options);		
		loadSearchLogicJs();
	};
		
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}		
}

// to load the search logic Java script file.
function loadSearchLogicJs(){	
	
	//load the search logic file based on the language
	var scriptFileName = '';	
	if(languageType == "asian"){        
		scriptFileName = 'search/nwSearchFnt_asian.js';		
	}else if(languageType == "others"){ 
		scriptFileName = 'search/nwSearchFnt_others.js';		
	}else{ //english 		
		scriptFileName = 'search/nwSearchFnt.js';		
	} 	

	/* Adding the script dynamically to the page*/
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = scriptFileName;
	document.getElementsByTagName("head")[0].appendChild(script);
	
	// executes when the script file is fully loaded	
	var callBackFun = function() { 			
		//do nothing ///		
	};		
	if(isIEOldVersion){
		script.onreadystatechange = callBackFun;
	}else{
		script.onload = callBackFun;
	}			
}

// Array.unique( strict ) - Remove duplicate values
function unique(tab) {
    var a = new Array();
    var i;
    var l = tab.length;

    if (tab[0] != undefined) {
        a[0] = tab[0];
    }
    else {
        return -1;
    }

    for (i = 1; i < l; i++) {
        if (indexof(a, tab[i], 0) < 0) {
            a.push(tab[i]);
        }
    }
    return a;
}

function indexof(tab, element, begin) {
    for (var i = begin; i < tab.length; i++) {
        if (tab[i] == element) {
            return i;
        }
    }
    return -1;
}
