var BoardLoader = {		
	
	initialize : function(
		oInput,
		oCorporateContainer,
		oOpenedContainer,
		oOptions,
		oLabels
		) {
		
		this.oOptions = {	
			sUrl                : '/board/',
			iDelay              : 1000,
			iMinSearchLength    : 3,
			sInvisibleClassName : 'invisible',
			sDisabledClassName  : 'progress',
			oProgressElement    : null,
			oDisabledElement    : null
		};
			
		this.oInput = oInput;
		this.oCorporateContainer = oCorporateContainer;
		this.oOpenedContainer = oOpenedContainer;
		this.sCurrentValue = '';
		this.sCurrentUid = '';		
		this.oOptions = Common.Object.extend(oOptions, this.oOptions);	
		this.oLabels = oLabels;
		this.oCurrentRegExp = null;
		this.sCurrentQuery = '';
		this.sCurrentReplace = '';

		this.aDefaults = [];
		
		this.saveDefaults();
		
		Common.Event.add(
			this.oInput,
			'keyup',
			function() {
			
				BoardLoader.process();
				
			}
			);				
		
	},
	
	saveDefaults : function() {
	
		this.aDefaults['corporate'] = this.oCorporateContainer.innerHTML;
		this.aDefaults['opened'] = this.oOpenedContainer.innerHTML;		
	
	},
	
	restoreDefaults : function() {
	
		this.oCorporateContainer.innerHTML = this.aDefaults['corporate'];
		this.oOpenedContainer.innerHTML = this.aDefaults['opened'];
		this.sCurrentUid = '';
		this.disableProgress();
	
	},
	
	process : function() {
	
		if(!this.checkForMinLength()) {
		
			this.restoreDefaults();
			
			return false;
			
		}
	
		if(this.checkForChange()) {
		
			this.enableProgress();
		
			this.sCurrentValue = this.oInput.value;
			setTimeout('BoardLoader.loadContent(\'' + this.sCurrentValue + '\')', this.oOptions.iDelay);
		
		}
		
	},
	
	loadContent : function(sValue) {			
		
		if(this.oInput.value == sValue) {
		
			this.sCurrentValue = this.oInput.value;									
										
			var oAjaxHelper = new Ajax.Loader(
				this,
				this.oOptions.sUrl,
				'POST',
				{
					text : escape(this.oInput.value)
				},
				{
					bIgnoreCache : true
				}
			);
		
			this.sCurrentUid = oAjaxHelper.send();
				
		}	
	
	},
	
	checkForMinLength : function() {
	
		return this.oInput.value.length >= this.oOptions.iMinSearchLength;		
	
	},
	
	checkForChange : function() {
	
		if(this.oCurrentRegExp && this.sCurrentReplace) {
			
			var sMatchedQuery = this.oInput.value.toLowerCase().replace(this.oCurrentRegExp, this.sCurrentReplace);
			
			if(sMatchedQuery.replace(/^\s*|\s*$/g, '').replace(/\s{2,}/g, ' ').replace(/\s/g, '|') == this.sCurrentQuery) {
				return false;				
			}
			
		}
		
		return this.sCurrentValue != this.oInput.value;
	
	},
	
	ajaxUpdate : function(oRequest) {
		
		var oSearchContainer = oRequest.responseXML.getElementsByTagName('search')[0];
			
		if(this.sCurrentUid == oSearchContainer.getAttribute('uid')) {					
		
			this.oCurrentRegExp = new RegExp(oSearchContainer.getAttribute('match'));
			this.sCurrentReplace = oSearchContainer.getAttribute('replace');
			this.sCurrentQuery = oSearchContainer.getAttribute('query');
		
			var 
				oCorporateContainer = oRequest.responseXML.getElementsByTagName('corporate')[0].getElementsByTagName('programs')[0],
				oOpenedContainer = oRequest.responseXML.getElementsByTagName('opened')[0].getElementsByTagName('programs')[0],
				sHtml = '',
				oChild = null,
				iCounter = 1,
				oDate = null,
				aCompanies = [],
				bCorporateFound = false,
				bOpenedFound = false
				;				
			
			for(var i = 0; i < oCorporateContainer.childNodes.length; i++) {
				
				oChild = oCorporateContainer.childNodes[i];
				
				if(oChild.nodeType == 1) {
				
					bCorporateFound = true;
				
					switch(oChild.tagName) {
					
						case 'company':
						
							aCompanies[oChild.getAttribute('id')] = oChild.getElementsByTagName('name')[0].firstChild.nodeValue;
						
						break;
						
						case 'program':												
				
							sHtml += '<div class="pos_' + iCounter++ + '"><dl><dt><a href="' + oChild.getAttribute('href') + '">' + oChild.getElementsByTagName('name')[0].firstChild.nodeValue + '</a></dt>';
																					
							if(oChild.getAttribute('company-id')) {							
								sHtml += '<dd>' + aCompanies[oChild.getAttribute('company-id')] + '</dd>';							
							}
							
							sHtml += '</dl></div>';
							
						break;
							
						default: 
						break;												
					
					}
					
				}
																
			}						
			
			this.oCorporateContainer.innerHTML = sHtml;
			
			sHtml = '';
			iCounter = 1;
		
			for(var i = 0; i < oOpenedContainer.childNodes.length; i++) {
				
				oChild = oOpenedContainer.childNodes[i];
				
				if(oChild.nodeType == 1 && oChild.tagName.toLowerCase() == 'program') {	
				
					bOpenedFound = true;
				
					oDate = oChild.getElementsByTagName('begin')[0];
					sHtml += (i > 0? ' ' : '') + '<span class="date">' + (oDate? oDate.getAttribute('text') : '') + '</span><span class="size_' + iCounter++ + '"><a href="' + oChild.getAttribute('href') + '">' + oChild.getElementsByTagName('name')[0].firstChild.nodeValue + '</a></span>';
				
				}
				
			}
		
			this.oOpenedContainer.innerHTML = sHtml;
						
			if(!(bCorporateFound || bOpenedFound)) {
			
				this.oCorporateContainer.innerHTML = this.oLabels.corporate_not_found;
				this.oOpenedContainer.innerHTML = this.oLabels.opened_not_found;
				
			}
			else if(!bCorporateFound) {
				this.oCorporateContainer.innerHTML = this.oLabels.corporate_not_found_only;
			}
			else if(!bOpenedFound) {
				this.oOpenedContainer.innerHTML = this.oLabels.opened_not_found_only;
			}						
			
			this.disableProgress();
			
		}
	
	},
	
	enableProgress : function() {
	
		if(this.oOptions.oProgressElement) {
			Common.Class.remove(this.oOptions.oProgressElement, this.oOptions.sInvisibleClassName);
		}
		
		if(this.oOptions.oDisabledElement) {
			Common.Class.add(this.oOptions.oDisabledElement, this.oOptions.sDisabledClassName);
		}
	
	},
	
	disableProgress : function() {
	
		if(this.oOptions.oProgressElement) {
			Common.Class.add(this.oOptions.oProgressElement, this.oOptions.sInvisibleClassName);
		}
		
		if(this.oOptions.oDisabledElement) {
			Common.Class.remove(this.oOptions.oDisabledElement, this.oOptions.sDisabledClassName);
		}
	
	},
		
	ajaxError : function() {
	}

};