/**
 * @author nbu
 * @created 18.03.2008
 *
 * benoetigt: prototype.js; scripts.js; effects.js; clavisAutoCompleter.js
 *
 */
var searchRequestObject = null;
var searchFieldsTagName = 'furtherEduSearchFields';
var searchResultsTagName = 'furtherEduSearchResults';
var searchLoadIndicatorTagName = 'furtherEduSearchLoadIndicator';
var searchFieldsTag;
var searchResultsTag;
var searchLoadIndicatorTag;
var elemsInitialized = false;
/**
 *
 */
initElems = function() {
	searchFieldsTag = $(searchFieldsTagName);
	searchResultsTag = $(searchResultsTagName);
	searchLoadIndicatorTag = $(searchLoadIndicatorTagName);
	elemsInitialized = true;
}
/**
 *
 * standard suchabfrage
 *
 */
makeUpdateSearchRequest = function(updateTag, url, params) {
	if (!elemsInitialized)
		initElems();
	if (searchRequestObject != null) {
		searchRequestObject.transport.abort();
	}
	searchRequestObject = new Ajax.Updater(updateTag, url, {
		onComplete : function() {
			searchLoadIndicatorTag.hide();
			searchResultsTag.show();
			searchRequestObject = null;
		},
		onCreate : function() {
			searchResultsTag.hide();
			searchLoadIndicatorTag.show();
		},
		parameters :params
	});
}
/**
 *
 * suchnavigation
 *
 */
setFurtherEduSearch = function(searchName) {
	if (!elemsInitialized)
		initElems();
	if (searchName == 'Suche') {
		if (!searchFieldsTag.visible()) {
			searchFieldsTag.show();
			searchLoadIndicatorTag.style.marginTop = '';
		}
	} else {
		if (searchFieldsTag.visible()) {
			searchFieldsTag.hide();
			searchLoadIndicatorTag.style.marginTop = (searchFieldsTag.getHeight() / 2) + 'px';
		}
	}
	makeUpdateSearchRequest(searchResultsTagName, 'agSetFurtherEducationSearch?openAgent', {
		search :searchName
	});
}
/**
 *
 * standardsuche
 *
 */
searchFurtherEdu = function(params) {
	if (!elemsInitialized)
		initElems();
	var parameters = {};
	if (!Object.isUndefined(params))
		Object.extend(parameters, params);
	Object.extend(parameters, $('fmFurtherEducationSearch').serialize(true));
	makeUpdateSearchRequest(searchResultsTagName, 'agSearchFurtherEducation?openAgent', parameters);
}
/**
 *
 * smartsuche
 *
 */
smartSearchFurtherEdu = function(params) {
	if (!elemsInitialized)
		initElems();
	if (!($F('smartSearchField').blank())) {
		var paramObj = {};
		if (!Object.isUndefined(params)) {
			Object.extend(paramObj, params);
		}
		Object.extend(paramObj, $('fmFurtherEducationSmartSearch').serialize(true));
		if (autoCompleteRequest != null) {
			autoCompleteRequest.transport.abort();
		}
		if (searchRequestObject != null) {
			searchRequestObject.transport.abort();
		}
		searchRequestObject = new Ajax.Request('agSmartSearchFurtherEducation?openAgent', {
			bRedirect :false,
			onComplete : function() {
				searchLoadIndicatorTag.hide();
				searchResultsTag.show();
				searchRequestObject = null;
			},
			onCreate : function() {
				searchResultsTag.hide();
				searchLoadIndicatorTag.show();
			},
			onSuccess : function(transport) {
				var sResponse = transport.responseText;
				this.bRedirect = sResponse.isJSON();
				if (this.bRedirect) {
					var jsonResponse = sResponse.evalJSON(true);
					var docUrl = jsonResponse.filePath + '/0/' + jsonResponse.docUNID
					searchResultsTag.update('<p style="padding-bottom:16px;">Sie werden weitergeleitet.</p>' + '<p>Sollte Ihr Browser die automatische Weiterleitung nicht unterstützen, klicken sie bitte <a id="smartSearchRedirect" href="' + docUrl + '">hier</a></p>');
					window.location.href = docUrl;
				} else {
					searchResultsTag.update(sResponse);
				}
			},
			parameters :paramObj
		});
	}
}
/**
 * ein-/ausblenden der sectionlists
 */
shSection = function(el) {
	el = new Element.extend(el);
	el.blur();
	var anchImg = el.childElements();
	var sect = el.next(0);
	searchResultsTag.select('.infoBox').invoke('hide');
	if (!sect.visible()) {
		anchImg[0].hide();
		anchImg[1].show();
		new Effect.BlindDown(sect, {
			duration :0.15
		});
		new Effect.Appear(sect, {
			duration :0.15
		});
	} else {
		anchImg[1].hide();
		anchImg[0].show();
		new Effect.Fade(sect, {
			duration :0.15
		});
		new Effect.BlindUp(sect, {
			duration :0.15
		});
	}
}
/**
 * infobox functionen
 */
showInfoBox = function(infoBox) {
	if (!elemsInitialized)
		initElems();
	infoBox = new Element.extend(infoBox);
	var handle = infoBox.previous(0);
	searchResultsTag.select('.infoBox').invoke('hide');
	infoBox.style.position = 'absolute';
	Position.clone(handle, infoBox, {
		setHeight :false,
		setWidth :false,
		offsetTop :handle.offsetHeight - 20,
		offsetLeft :50
	});
	new Effect.Appear(infoBox, {
		duration :0.1
	});
}
hideInfoBox = function(infoBox) {
	infoBox = new Element.extend(infoBox);
	/*
	 * new Effect.Fade(infoBox, { duration :0.1 });
	 */
	infoBox.hide();
}
showInfoBoxByHandle = function(handle) {
	handle = new Element.extend(handle);
	showInfoBox(handle.next(0));
}
hideInfoBoxByHandle = function(handle) {
	handle = new Element.extend(handle);
	hideInfoBox(handle.next(0));
}
hideInfoBoxByClose = function(handle) {
	handle = new Element.extend(handle);
	hideInfoBox(handle.up(0));
}
toggleInfoBoxByHandle = function(handle) {
	handle = new Element.extend(handle);
	var iBox = handle.next(0);
	if (iBox.visible()) {
		hideInfoBox(iBox);
	} else {
		showInfoBox(iBox);
	}
}
/**
 * initialisieren der autocompletion smartsuche
 */
initSmartSearch = function() {
	smartSearchAutoCompleter = new Ajax.ClavisAutoCompleter('smartSearchField', 'smartSearchAutoComplete', 'smartSearchShowResults', 'agAutoCompleteSmartSearchFurtherEducation?OpenAgent', {
		afterUpdateElement : function(field, value) {
			if (value != null) {
				smartSearchFurtherEdu( {
					autoCompleted :'1'
				});
			} else {
				smartSearchFurtherEdu( {
					autoCompleted :'0'
				});
			}
		},
		frequency :0.2
	})
}
