function CompaniesDirector(
	oSearchInput,
	oTrainersCountInput,
	oYearsCountInput,
	oItems,
	sReplaceString,
	oOptions
	) {
			
	this.oSearchInput = oSearchInput;
	this.oTrainersCountInput = oTrainersCountInput;
	this.oYearsCountInput = oYearsCountInput;
	this.oItems = oItems;
	this.sReplaceString = sReplaceString;
	this.oOptions = oOptions;
			
	var oThis = this;				
			
	oSearchInput.onkeyup = oSearchInput.onblur = oTrainersCountInput.onchange = oYearsCountInput.onchange = function() {
			
		oThis.update();
			
	}
			
}
		
CompaniesDirector.prototype = {
		
	update : function() {
			
		var
			sValue = this.oSearchInput.value.toLowerCase(),
			iTrainersCount = parseInt(this.oTrainersCountInput.value),				
			iYearsCount = parseInt(this.oYearsCountInput.value),
			i = 0,
			iLength = this.oItems.length,
			oCurrentItem = null,
			oCurrentGroup = this.oItems[0].oGroupElement? this.oItems[0].oGroupElement : null,
			sCurrentGroupValue = this.oItems[0].sGroupValue? this.oItems[0].sGroupValue : '',
			bCurrentGroupShow = false,
			bCurrentItemShow = false,
			bWithHilightShow = false,
			bCurrentGroupDisable = true,
			bCheckCountComplete = false,
			oRegExp = new RegExp('(.*?)(' + sValue + ')(.*?)', 'gi');
			;
				
		while(i < iLength) {
				
			oCurrentItem = this.oItems[i];
							
			if(oCurrentGroup &&
				(oCurrentItem.sGroupValue != sCurrentGroupValue ||
				i == iLength - 1)) {
					
				if(i == iLength - 1) {																
					
					if(oCurrentItem.sGroupValue != sCurrentGroupValue) {															
						
						oCurrentGroup.className = bCurrentGroupShow? (bCurrentGroupDisable? this.oOptions.sInactiveClassName : '') : this.oOptions.sInvisibleClassName;
						bCurrentGroupShow = this.checkValue(oCurrentItem.sValue, sValue);	
						bCurrentGroupDisable = !this.checkCount(oCurrentItem.iTrainersCount, iTrainersCount, oCurrentItem.iYearsCount, iYearsCount);
						
					}
					else {
						
						if(this.checkCount(oCurrentItem.iTrainersCount, iTrainersCount, oCurrentItem.iYearsCount, iYearsCount)) {
							bCurrentGroupDisable = false;
						}					
						
						if(!bCurrentGroupShow) {
							bCurrentGroupShow = this.checkValue(oCurrentItem.sValue, sValue);						
						}
						
					}																							
					
					oCurrentGroup = oCurrentItem.oGroupElement;					
				}								
				
				oCurrentGroup.className = bCurrentGroupShow? (bCurrentGroupDisable? this.oOptions.sInactiveClassName : '') : this.oOptions.sInvisibleClassName;							
						
				oCurrentGroup = oCurrentItem.oGroupElement;				
				sCurrentGroupValue = oCurrentItem.sGroupValue;																			
						
				bCurrentGroupShow = false;
				bCurrentGroupDisable = true;								
						
			}
			
			bCheckCountComplete = this.checkCount(oCurrentItem.iTrainersCount, iTrainersCount, oCurrentItem.iYearsCount, iYearsCount);
			
			if(bCheckCountComplete) {
				bCurrentGroupDisable = false;
			}
			
			bCurrentItemShow = this.checkValue(oCurrentItem.sValue, sValue);												
			bWithHilightShow = false;
										
			if(bCurrentItemShow) {								
					
				bCurrentGroupShow = true;						
						
				if(sValue.length >= this.oOptions.iMinSearchLength) {	
					bWithHilightShow = true;							
				}
				else {
					bWithHilightShow = false;							
				}										
								
				if(bCheckCountComplete) {
					
					this.drawItem(
						oCurrentItem,
						bWithHilightShow? oCurrentItem.sValue.replace(oRegExp, this.sReplaceString) : oCurrentItem.sValue,
						true
					);	
							
					oCurrentItem.oElement.className = oCurrentItem.sDefaultClassName;																						
						
				}
				else {
													
					this.drawItem(
						oCurrentItem,
						bWithHilightShow? oCurrentItem.sValue.replace(oRegExp, this.sReplaceString) : oCurrentItem.sValue,
						false
					);														
							
					oCurrentItem.oElement.className += ' ' + this.oOptions.sInactiveClassName;						
						
				}
						
			}
			else {
					
				this.drawItem(
					oCurrentItem,
					oCurrentItem.sValue,
					false
				);
							
				oCurrentItem.oElement.className += ' ' + this.oOptions.sInactiveClassName;
							
			}
														
			i++;
				
		}
			
	},		
			
	checkCount : function(					
		iItemTrainersCount,
		iCheckedTrainersCount,				
		iItemYearsCount,
		iCheckedYearsCount
		) {
				
		return (iItemTrainersCount >= iCheckedTrainersCount) && (iItemYearsCount >= iCheckedYearsCount);
				
	},
			
	checkValue : function(	
		sItemValue,
		sCheckedValue
		) {
				
		return sCheckedValue.length >= this.oOptions.iMinSearchLength? sItemValue.toLowerCase().indexOf(sCheckedValue) > -1 : true;
				
	},
			
	drawItem : function(
		oItem,
		sHTML,
		bWithLinkShow
	) {
			
		oItem.oElement.innerHTML = (bWithLinkShow? '<a href="' + oItem.sHref + '">' + sHTML + '</a>' : '<span class="inner">' + sHTML + '</span>') + (oItem.sAddString? '&nbsp;' + oItem.sAddString : '');
			
	}
		
};
