
	wps.searchClient = function(container){
		this.instID = 'wps.search.' + parseInt(Math.random()*1000000);
		this.container = container;
		this.itemsPerPage = 5;
		this.currPage = 0;
	}
	
	wps.searchClient = wps.searchClient.extendsFrom(wps.base);
	
	wps.searchClient.prototype.doSearch = function(searchStr){
		if (!searchStr) searchStr = this.lastSeachStr;
		if ((!searchStr) || (searchStr.length < 3) ){
			alert('Helaas, dat is geen geldig zoekwoord, of een te kort zoekwoord...');
			return;
		}
		this.lastSeachStr =	searchStr;
		new Effect.BlindDown(this.container);
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('search', this.buildResults.bind(this), this);
		wpsRPC.call('search', "searchStr=" + searchStr);		
		
	}
	
	wps.searchClient.prototype.throwError = function(msg){
	}
	
	wps.searchClient.prototype.buildResults = function(req){
		var dom = req.responseXML;
		this.pulsators = new Array();
		//this.container.innerHTML = '';
		var rows = dom.getElementsByTagName("row");
		this.rowCount = rows.length;
		var endRow = (this.itemsPerPage * (this.currPage+1));
		if ( endRow > this.rowCount ) endRow = this.rowCount;
		var startRow = (this.currPage * this.itemsPerPage);
		if (this.resultsContainer){
			$('searchResults').removeChild(this.resultsContainer);
		}
		this.resultsContainer = Builder.node('div');
		$('searchResults').appendChild(this.resultsContainer);
		for (var i = startRow;i<endRow;i++){
			this.resultsContainer.appendChild(this.writeRow(rows[i], i));
		}
		if (this.currPage > 0) {
			this.multiElemEffect('navigatorPrevious', 'Appear');
		}else{
			this.multiElemEffect('navigatorPrevious', 'Fade');
		}
		if (endRow < this.rowCount) {
			this.multiElemEffect('navigatorNext', 'Appear');
		}else{
			this.multiElemEffect('navigatorNext', 'Fade');
		}
		$('rowCount').innerHTML = '';
		$('rowCount').appendChild(document.createTextNode(this.rowCount+ ' resultaten. Pagina '+ (this.currPage +1) + ' v/d ' + parseInt((this.rowCount/this.itemsPerPage)) ));
		new Effect.Appear($('rowCount'), {duration : 0.5});
		new Effect.Appear($('closeMe'), {duration : 0.5});
		(this.pulsators).each(
			function(pulsator){
				try{
					new Effect.Pulsate($(pulsator));
				}catch(e){}	
			}
		)
	} 
	
	wps.searchClient.prototype.writeRow = function(row, rowNum){
 		var container = Builder.node('div');
		container.className='searchResultRow';
		var fields = row.getElementsByTagName('field');

		// assign XML field values to local variables
		for (var i=0;i<fields.length; i++){
			var currKey = fields[i].getAttribute('name');
			if (currKey == 'xml'){
				xml = '';
				if (fields[i].getElementsByTagName('xml').length == 0){
					xml = fields[i].childNodes.item(0).nodeValue
				}else{
					if (fields[i].getElementsByTagName('xml').item(0).hasChildNodes()){
						for (var y=0;y<fields[i].getElementsByTagName('xml').item(0).childNodes.length;y++){
							var currPara = fields[i].getElementsByTagName('xml').item(0).childNodes.item(y);
							if (currPara){
								if (currPara.hasChildNodes()){
									var str = new String(currPara.childNodes[0].nodeValue);
									if (str.stripTags() != 'null') xml += str.stripTags();
								}
							}
						}
					}
				}
			}
			if (fields[i].hasChildNodes()){
				var currValue = fields[i].childNodes[0].nodeValue; 
			}else{
				var currValue ='';
			}
			switch (currKey){
				case 'lastmodified':
					var lastModified = currValue
					break;
				case 'docpath':
					var docPath = currValue;
					break;
				case 'nav_name':
					var nav_name = currValue;
					nav_name = nav_name.replace(/&amp;/gi,'&');
					break;
				case 'name':
					var title = currValue;
					break;
				case 'title':
					var title = currValue;
					break;
				case 'plugin':
					var plugin = currValue;
					break;
				case 'id':
					var id = currValue;
					break;
			}
		}
		// create GUI for one row, using local var's assigned above
		container.setAttribute('id', this.rowCount+currKey); 
		//start with the current count of the result 
		var currRec = currValue;
		container.appendChild(document.createTextNode((rowNum + 1)+ '.  '));
		// exception for results found in calendar
		if (plugin == 'calendar'){
			var docTitleLink = Builder.node('a', {className:'searchResultsRowHead', target:'_blank'}, [document.createTextNode(title)]);
			docTitleLink.calID = id;
			container.appendChild(docTitleLink);
			$(docTitleLink).observe('click', this.openCalWindow.bindAsEventListener(this))
		}else{
			var docTitleLink = Builder.node('a', {href:nav_name, className:'searchResultsRowHead', target:'_blank'}, [document.createTextNode(title)]);
			container.appendChild(docTitleLink);
		}
		
		//container.appendChild(document.createTextNode('  (Gevonden in : ' + plugin + ')'));
		container.appendChild(Builder.node('br'));
		
		var aNewTextNode = document.createTextNode(xml.substr(0, 256));
		container.appendChild(this.setPulsateSearchStr(aNewTextNode));
		//container.appendChild(aNewTextNode);
		// end row with a separator
		var separator = Builder.node('hr');
		container.appendChild(separator);
		return container;
		//this.container.appendChild(container);
	}
	
	wps.searchClient.prototype.openCalWindow = function(event){
		var calID = event.element().calID;
		window.open(gRelRootPath + 'cal/eventdisplay.php?id=' + calID , 'mssgDisplay', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=340,height=400');
	}
	
	wps.searchClient.prototype.setPulsateSearchStr = function(sourceNode){
		if (!this.lastSeachStr) return sourceNode;
		var i = 0;
		var newText= '';
		var searchString = new String(this.lastSeachStr);
		searchString = searchString.toLowerCase();
		var abb = new String(sourceNode.nodeValue);
	 	var lcAbb = abb.toLowerCase();
		var indexOf = lcAbb.indexOf(searchString);
		while (abb.length > 0) {
		    i = lcAbb.indexOf(searchString, i);
		    if (i < 0) {
		      newText += abb;
		      abb = "";
		    } else {
	    		  this.pulsators.push('repl_' + this.pulsators.length);
	    		  highlightStartTag =  '<span style="float:none;background:#adcbef;" id="repl_' + this.pulsators.length + '"  >';
	    		  var highlightEndTag = '</span>';
				  if (abb.lastIndexOf(">", i) >= abb.lastIndexOf("<", i)) {
						if (lcAbb.lastIndexOf("/script>", i) >= lcAbb.lastIndexOf("<script", i)) {
							newText += abb.substring(0, i) + highlightStartTag + abb.substr(i, searchString.length) + highlightEndTag;
		          			abb = abb.substr(i + searchString.length);
		          			lcAbb = abb.toLowerCase();
		          			i = -1;
		          		}
		          }
		     }
		}
		var retNode = document.createElement('span');
		retNode.innerHTML = newText;
		return retNode;
	}	
	
	wps.searchClient.prototype.multiElemEffect = function(className, effect, options){
		$A(this.container.getElementsByClassName(className)).each(
			function(elem){
				new Effect[effect](elem, options);
			}
		)
	}
	
	wps.searchClient.prototype.waiter = function(){
		$('searchWaiter').style.display = 'block';
	}
	
	wps.searchClient.prototype.unWaiter = function(){
		$('searchWaiter').style.display = 'none';
	}
