/* author:	Richard Schmid, infolox GmbH, http://www.infolox.de */

function HWSearchModel(){
	this.allItems = null;
	this.synonyms = null;
	
	this.variants = new Array();
	this.spaceVariants = new Array();
	this.strippedVariants = new Array();
	
	this.searchWord = null;
	this.sSearchWord = null;
	this.foundItems = new Array();
	
	this.setItemsList = function( items ){
		this.allItems = items;
	};
	
	this.setSynonymsList = function( synonyms ){
		this.synonyms = synonyms;
	};
	
	this.setSearchWord = function( str ){
		this.sSearchWord = str;
		this.searchWord = str.toLowerCase();
	};
	
	this.getFoundItems = function(){
		return this.foundItems;
	};
	
	this.getVariants = function(){
		return this.variants;
	};

	this.getSearchWord = function(){
		return this.sSearchWord;
	};

	this.init = function(){
		if (!this.__prepareVariants()) {
			return false;
		}
		
		var sGID = "";
		try {
			for (var i = 0; i < this.allItems.length; i++) {
				if (this.searchWord == "showall") {
					this.__appendItem(this.allItems[i]);
					continue;
				}

				sGID = this.allItems[i]['group_id'] + ", i=" + String(i);
				if (this.__checkString(this.allItems[i]['group_name'])) {
					this.__appendItem(this.allItems[i]);
					continue;
				}
				
				if (this.__checkString( this.allItems[i]['keywords'])) {
					this.__appendItem(this.allItems[i]);
					continue;
				}
				
				//if (this.allItems[i]['subgroups'].length >=1) continue;
				for (var j = 0; j < this.allItems[i]['subgroups'].length; j++ ) {
					var bFoundTypeNo = false;
					//if (this.allItems[i]['subgroups'][j]['typenumbers'].length <= 0) continue;
					//if (this.allItems[i]['subgroups'][j].length <= 0) continue;
					for (var k = 0; k < this.allItems[i]['subgroups'][j]['typenumbers'].length; k++) {
						//if (this.allItems[i]['subgroups'][j]['typenumbers'][k].length <= 0) continue;
						if (this.__checkString(this.allItems[i]['subgroups'][j]['typenumbers'][k], true)) {
							if (!this.allItems[i]['subgroups'][j]['sg_is_sg']) {
								this.allItems[i]['addhit'] = true;
							}
							else {
								this.allItems[i]['addhit'] = false;
							}
							this.__appendItem(this.allItems[i]);
							bFoundTypeNo = true;
							break;
						}
					}
					if (bFoundTypeNo) break;
				}
			}
		}
		catch(e) {
			alert("SearchModel failed!\nGroupID:"+sGID+"\n"+e);
			return false;
		}
		
		this.__specialReordering();
	};
	
	this.__prepareVariants = function(){
		try {
			var aWords = this.searchWord.split(" ");
			if (aWords.length > 1) {
				var iWords = aWords.length;
				var aSV = [];
				if (iWords == 2) {
					aSV[aSV.length] = aWords[0]+aWords[1];
				}
				else if (iWords == 3) {
					aSV[aSV.length] = aWords[0]+aWords[1];
					aSV[aSV.length] = aWords[1]+aWords[2];
					aSV[aSV.length] = aWords[0]+aWords[1]+aWords[2];
				}
				else if (iWords == 4) {
					aSV[aSV.length] = aWords[0]+aWords[1];
					aSV[aSV.length] = aWords[1]+aWords[2];
					aSV[aSV.length] = aWords[2]+aWords[3];
					aSV[aSV.length] = aWords[0]+aWords[1]+aWords[2];
					aSV[aSV.length] = aWords[1]+aWords[2]+aWords[3];
					aSV[aSV.length] = aWords[0]+aWords[1]+aWords[2]+aWords[3];
				}
				else if (iWords >= 5) {
					aSV[aSV.length] = aWords[0]+aWords[1];
					aSV[aSV.length] = aWords[1]+aWords[2];
					aSV[aSV.length] = aWords[2]+aWords[3];
					aSV[aSV.length] = aWords[3]+aWords[4];
					//incomplete
				}
				this.spaceVariants = aSV;
			}

			this.variants = [];
			if (this.synonyms.length >= 1) {
				for (var i in this.synonyms) {
					if (this.synonyms[i]['original'].toLowerCase() == this.searchWord) {
						this.variants[this.variants.length] = this.synonyms[i]['synonym'].toLowerCase();
					}
				}
			}
						
			var stripped_search_word = this.searchWord.replace(":","").replace(" ","").replace("-","").replace("_","").replace("/","").replace(".","").replace(",","");
			this.strippedVariants = [ stripped_search_word ];
			if (this.synonyms.length >= 1) {
				for( var i in this.synonyms ){
					if( this.synonyms[i]['original'].toLowerCase() == stripped_search_word ){
						this.strippedVariants[this.strippedVariants.length] = this.synonyms[i]['synonym'].replace(":","").replace("-","").replace("_","").replace("/","").replace(".","").replace(",","").toLowerCase();
					}
				}
			}
		}
		catch(e) {
			alert("Prepare variants failed!\n"+e);
			return false;
		}
		
		return true;
	};
	
	this.__checkString = function(haystack, use_stripped) {
		try {
			var sCmp=haystack.toLowerCase();
			if (use_stripped==null) {
				if (sCmp.indexOf(this.searchWord) != -1)
					return true;
	
				for (var j in this.spaceVariants) {
					if (sCmp.indexOf(this.spaceVariants[j]) != -1) {
						return true;
					}
				}
			}
			var aVariants = null;
			if (use_stripped==null) {
				aVariants = this.variants;
			}
			else {
				sCmp = sCmp.replace(":","").replace("-","").replace("_","").replace("/","").replace(".","").replace(",","");
				aVariants = this.strippedVariants;
			}
			for (var i in aVariants) {
				if (sCmp == aVariants[i]) {
					return true;
				}
				else if (sCmp.indexOf(aVariants[i]) != -1) {
					return true;
				}
			}
			
			return false;
		}
		catch(e) {
			return false;
		}
	};
	
	this.__appendItem = function( item ) {
		var found = false;
		
		for (var i = 0; i < this.foundItems.length; i++) {
			if (this.foundItems[i]['group_id'] == item['group_id']) {
				found = true;
			}
		}
		
		if (!found) {
			this.foundItems[this.foundItems.length] = item;
		}
	};
	
	this.__specialReordering = function(){
		var new_items = [];
		
		for (var i = 0; i < this.foundItems.length; i++) {
			if (!this.foundItems[i]['group_is_appl']) {
				if (!this.foundItems[i]['addhit']) {
					var item = this.__getClone(this.foundItems[i]);
					new_items[new_items.length] = item;
				}
			}
		}
		
		for (var i = 0; i < this.foundItems.length; i++) {
			if (!this.foundItems[i]['group_is_appl']) {
				if (this.foundItems[i]['addhit']) {
					var item = this.__getClone(this.foundItems[i]);
					new_items[new_items.length] = item;
				}
			}
		}

		for (var i = 0; i < this.foundItems.length; i++) {
			if (this.foundItems[i]['group_is_appl']) {
				var item = this.__getClone(this.foundItems[i]);
				new_items[new_items.length] = item;
			}
		}

		this.foundItems = new_items;
	};
	
	this.__getClone = function( item ){
		var clone = {};
		for( var key in item ){
			clone[key] = item[key];
		}
		
		return clone;
	};
};
