

/*
 * function modularsearchChangeVisibility()
 */
function modularsearchChangeVisibility(moduleResultsID,buttonid,morelinkID) {
	module = $(moduleResultsID);
	button = $(buttonid);
	moreLink = $(morelinkID);
	
	// check if the module is currently visible
	if(module.visible()) {
		// hide the module
		module.hide();
		// change the arrow icon for the button
		button.className = 'button openButton';
		// hide the moreLink
		moreLink.hide();
	} else {
		// show the module
		module.show();
		// change the arrow icon for the button
		button.className = 'button closeButton';
		// hide the moreLink
		moreLink.show();
	}
}

/*
 * modularsearchInvokeAll()
 * 
 * is called on submit of the keyword search form to call all modules
 */
function modularsearchInvokeAll(keyword) {
	var keyword = $(keyword).value;
	
	tx_modularsearch_modules = tx_modularsearch_modules.compact();
	tx_modularsearch_modules.each(function(value,index) {
		// reset the perPage value before executing the next hole search request
		value[3] = value[5];
		
		// call invoke function for every module
		modularsearchInvoke(keyword,value,index+1,value[3],0);
	});
}

/*
 * modularsearchInvoke()
 * 
 * is called by the modularsearchInvokeAll or by the more links of a single module
 */
function modularsearchInvoke(keyword, configuration, index, perPage, raisePerPage) {
	// decide which method should be used
	var method = 'get';
	if(configuration[2] == 1) {
		method = 'post';
	}
	
	// raise the perPage if the moreLink was pressed
	// Use the perPage as number
	if(raisePerPage > 0) {
		configuration[3] = parseFloat(configuration[3]);
		configuration[5] = parseFloat(configuration[5]);
		configuration[3] = configuration[3]+configuration[5];
		perPage = configuration[3];
	} 
	
	// evaluates the json to a string to replace the markers keyword and perpage
	var parameters = configuration[1].sub('###keyword###', keyword);
	parameters = parameters.sub('###perpage###', perPage);
	
	// Do the ajax request
	new Ajax.Request(configuration[0],
	{
		method: method,
		parameters: parameters.evalJSON(),
		onLoading: function(transport) {
			$('button-'+index).className = 'button';
			$('button-'+index).innerHTML = "<img src=\"/fileadmin/template/images/onloading.gif\"/>";
		},
		onSuccess: function(transport) {
			// remove the onloading image
			$('button-'+index).innerHTML = "";
			
			// get the first line of the response with the count
			var split = transport.responseText.split(configuration[4]);
			
			// extract the count out of the first line
			var lastCount = split[0].match(/(\d+)/g);
			
			// check if there're any results
			if(Object.isArray(lastCount)) {
				
				var countOfRecords = 0;
				lastCount.each(function(value,index) {
					
					// take the last number as countOfRecords
					countOfRecords = value;
				});
				countOfRecords = parseFloat(countOfRecords);
				
				if(countOfRecords > 0) {
					// There are results, show the moreLink
					
					$('button-'+index).addClassName('closeButton');
					
					// write the count to the right div
					$('count-'+index).innerHTML = split[0];
					
					// write the rest of the response to the right div - substr length of first line + number of signs for length of splitConfigurationString from flexform
					$('results-'+index).innerHTML = transport.responseText.substr(split[0].length + configuration[4].length);
					
					// show the hidden divs
					if(configuration[3] < countOfRecords) {
						$('moreLink-'+index).show();
						$('results-'+index).show();
					} else {
						$('moreLink-'+index).hide();
						$('results-'+index).show();
					}
				} else {
					$('count-'+index).innerHTML = 'Keine Resultate gefunden.';
					$('moreLink-'+index).hide();
					$('results-'+index).hide();
				}
			} else {
				$('count-'+index).innerHTML = 'Keine Resultate gefunden.';
				$('moreLink-'+index).hide();
				$('results-'+index).hide();
			}
		},
		onFailure: function(transport) {
			$('count-'+index).innerHTML = 'Keine Resultate gefunden.';
			$('moreLink-'+index).hide();
			$('results-'+index).hide();
		}
	});
}

// check input field onload of page and do search if it's not empty
Event.observe(window, 'load', function() {
	if($('modularSearchKeyword').value !== '') {
		modularsearchInvokeAll('modularSearchKeyword')
	}
});

