SearchWordsPage = function()
{
	this.pageName = "";
	this.globalIndex = -1;
	this.searchWordsStr = "";
};

/*  ----------------------------- */
SearchWords = function()
{
	this.pages = new Array();
	//this.pagesCount = "";
};

SearchWords.prototype.AddPage = function()
{
	var p = new SearchWordsPage();
	this.pages[this.pages.length] = p;
	return p;
};

//String parent.SearchWords.setDataStringByID(int PageID;String ColumnName,String value)
SearchWords.prototype.findPagesWithWord = function(wordToSearch, ignoreCase)
{
	if (!player) var player = getPlayer(); //pg2-BSH_search_words_multiversions
	
	var pagesFound = new Array();
	wordToSearch = trim(wordToSearch); //remove all spaces from the begin and the end of word to be searched for
	var isManyWords = (wordToSearch.indexOf(" ") > -1); //pg2
	
	if (isManyWords) {
		var doubleSpace = "  ";
		while (wordToSearch.indexOf(doubleSpace) > -1) { //pg2-replace all double spaces to single one
			wordToSearch = wordToSearch.replace(doubleSpace, " "); 
		};
		while (wordToSearch.indexOf(" ") > -1) { //pg2-replace all spaces to ":" - so it would be similar to all texts extracted from the page
			wordToSearch = wordToSearch.replace(" ", ":");
		};
		if (ignoreCase) {
			wordToSearch = wordToSearch.toUpperCase();
		};
		
		for (var i = 0; i < this.pages.length; i++) {
			//var searchWordsFromPageStr = this.pages[i].searchWordsStr;
			//while (searchWordsFromPageStr.indexOf("&nbsp;") > -1) { //pg2-if between words were two spaces, one is as ":", and the second is 'sticked' to some word (at its end) as "&nbsp;"
				//searchWordsFromPageStr = searchWordsFromPageStr.replace("&nbsp;", "");
			//};
			
			var verId = ""; //pg2-BSH_search_words_multiversions
			if (player.courseVersion > 0) verId = player.courseVersion;
			if (typeof(this.pages[i]["searchWordsStr" + verId]) == 'undefined') continue; //pg2-bsh_search_words -another page type (link, etc)
			var wordsNoNbsp = this.pages[i]["searchWordsStr" + verId];
			wordsNoNbsp = wordsNoNbsp.replace(new RegExp('&nbsp;', "g"), ':');
			var searchWordsFromPageStr = wordsNoNbsp;
			
			if (ignoreCase) {
				searchWordsFromPageStr = searchWordsFromPageStr.toUpperCase();
			};
			
			var found = true; //pg2-BSH_search_words_multiversions -now, look word by word, search for sub-phrases
			var words2SearchList = wordToSearch.split(':');
			for (var j=0; j<words2SearchList.length; j++) {
				if (searchWordsFromPageStr.indexOf(words2SearchList[j]) == -1) {
					found = false;
					break;
				};
			};
			delete words2SearchList;
			if (found) {
				pagesFound[pagesFound.length] = i;
			};
			
			//if (searchWordsFromPageStr.indexOf(wordToSearch) > -1) { //pg2-BSH_search_words_multiversions -removed -now, look word by word, search for sub-phrases
				//pagesFound[pagesFound.length] = i;
			//};
		};
	} else { //pg2-szukanie pojedynczego slowa
		for (var i = 0; i < this.pages.length; i++) {
			
			var verId = ""; //pg2-BSH_search_words_multiversions
			if (player.courseVersion > 0) verId = player.courseVersion;
			if (typeof(this.pages[i]["searchWordsStr" + verId]) == 'undefined') continue; //pg2-bsh_search_words -another page type (link, etc)
			var wordsNoNbsp = this.pages[i]["searchWordsStr" + verId];
			wordsNoNbsp = wordsNoNbsp.replace(new RegExp('&nbsp;', "g"), ':');
			
			var searchWordsArr = wordsNoNbsp.split(":");
			
			//var searchWordsArr = this.pages[i].searchWordsStr.split(":");
			for (var j = 0; j < searchWordsArr.length; j++) {
				if (ignoreCase) {
					//if (searchWordsArr[j].toUpperCase() == wordToSearch) {
					if (searchWordsArr[j].toUpperCase().indexOf(wordToSearch.toUpperCase()) > -1) {
						pagesFound[pagesFound.length] = i;
						break;
					};
				} else {
					//if (searchWordsArr[j] == wordToSearch) {
					if (searchWordsArr[j].indexOf(wordToSearch) > -1) {
						pagesFound[pagesFound.length] = i;
						break;
					};
				};
			};
		};
	};
	return pagesFound;
};

SearchWords.prototype.toString = function()
{
	return "SearchWords!!!";
};

var sw = new SearchWords();
