/**********************
 * Hier stehen die Funktionen im Zusammenhang mit der Personenanzeige.
 **********************/
 
function person_life_switchView(mode) {
	var divs = byId('person_section_life').getElementsByTagName('div');
	if(mode == 'detailed') {
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className && divs[i].className.indexOf('detailed') != -1)	divs[i].style.display = 'block';
			if(divs[i].className && divs[i].className.indexOf('simple') != -1)		divs[i].style.display = 'none';
		}		
		byId('person_link_life_simpleView').style.display = 'block';
		byId('person_link_life_detailedView').style.display = 'none';
	}else {
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className && divs[i].className.indexOf('detailed') != -1)	divs[i].style.display = 'none';
			if(divs[i].className && divs[i].className.indexOf('simple') != -1)		divs[i].style.display = 'block';
		}
		byId('person_link_life_simpleView').style.display = 'none';
		byId('person_link_life_detailedView').style.display = 'block';
	}
}

function person_bio_switchView(mode) {
	var divs = byId('person_bioData').getElementsByTagName('div');
	if(mode == 'detailed') {
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className && divs[i].className.indexOf('detailed') != -1)	divs[i].style.display = 'block';
			if(divs[i].className && divs[i].className.indexOf('simple') != -1)		divs[i].style.display = 'none';
		}		
		byId('person_link_bio_simpleView').style.display = 'block';
		byId('person_link_bio_detailedView').style.display = 'none';
	}else {
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className && divs[i].className.indexOf('detailed') != -1)	divs[i].style.display = 'none';
			if(divs[i].className && divs[i].className.indexOf('simple') != -1)		divs[i].style.display = 'block';
		}
		byId('person_link_bio_simpleView').style.display = 'none';
		byId('person_link_bio_detailedView').style.display = 'block';
	}
}

/**
*  Diese Funktion nimmt den angefragte Anzeigenamen entgegen
**/

function getDisplayNameSuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data);	}	
	byId('person_detailTitle').innerHTML = trim(data);	
	if(--requestsShortLoading == 0) byId('person_shortLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt den angefragten Informationsblock entgegen
**/

function getInformationBlockSuccess(t) {
	var data = trim(t.responseText);
	data = data.substring(data.indexOf('<div>') + '<div>'.length, data.lastIndexOf('</div>'));	
	if(data.indexOf('ERROR') != -1) { alert(data); }
	byId('person_iblock_summary').innerHTML = data;
	if(--requestsShortLoading == 0) byId('person_shortLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt die angefragte Kurzbiographie für die Details entgegen
**/

function getShortBioDetailsSuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data); }
	byId('person_iblock_shortBio').innerHTML = data;
	if(--requestsShortLoading == 0) byId('person_shortLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt die angefragte Institution entgegen
**/

function getInstitutionSuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data);	}	
	byId('person_iblock_institution').innerHTML = data;	
	if(--requestsShortLoading == 0) byId('person_shortLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt die angefragten Namen entgegen
**/

function getNamesSuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data);	}	
	//if(data.indexOf('class="nameComplete') == -1) byId('person_nameData').style.display = 'none';
	else byId('person_nameData').style.display = 'block';
	byId('person_nameData').innerHTML = data;
	if(--requestsLifeDatesLoading == 0)	byId('person_lifeDataLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt die angefragten Lebensdaten entgegen
**/

function getBirthDeathSuccess(t) {
	var data = trim(t.responseText);
	if(data.indexOf('ERROR') != -1) { alert(data); }	
	//if(data.indexOf('class="lifeComplete') == -1) byId('person_lifeData').style.display = 'none';
	else byId('person_lifeData').style.display = 'block';	
	byId('person_lifeData').innerHTML = data;	
	if(--requestsLifeDatesLoading == 0) byId('person_lifeDataLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt die angefragten Berufsdaten entgegen
**/


function getOccupationPlaceSummarySuccess(t) {
	var data = trim(t.responseText);
	if(data.indexOf('ERROR') != -1) { alert(data);	}
	//if(data.indexOf('class="workComplete') == -1) byId('person_workData').style.display = 'none';
	else byId('person_workData').style.display = 'block';
	byId('person_workData').innerHTML = data;
	if(--requestsLifeDatesLoading == 0) byId('person_lifeDataLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt die angefragten Berufsdaten entgegen
**/

function getBiographySuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data);	}
	//if(data.indexOf('class="information bioEntry"') == -1) byId('person_bioData').style.display = 'none';
	else byId('person_bioData').style.display = 'block';
	byId('person_bioData').innerHTML = data;
	byId('person_bioDataLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt den editorischen Kommentar entgegen
**/

function getEditorialCommentSuccess(t) {
	var data = trim(t.responseText);
	if(data.indexOf('ERROR') != -1) { alert(data);	}
	byId('person_editorialComment').innerHTML = getBreak(data);	
	if(--requestsCommentsLoading == 0) byId('person_commentLoading').style.display = 'none';
}

/**
*  Diese Funktion nimmt den Kommentar entgegen
**/

function getCommentSuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data);	}t
	byId('person_comment').innerHTML = getBreak(data);
	if(--requestsCommentsLoading == 0) byId('person_commentLoading').style.display = 'none';
}
/*
function getRevisionDescSuccess(t) {
	var data = trim(t.responseText);	
	if(data.indexOf('ERROR') != -1) { alert(data);	}
	//if(data.indexOf('class="information bioEntry"') == -1) byId('person_bioData').style.display = 'none';
	//else
	byId('person_revisionDesc').style.display = 'block';
	byId('person_revisionDesc').innerHTML = data;
	byId('person_revisionDescLoading').style.display = 'none';
}
*/

function person_externalOpenDetails(id) {
	if(lockRenewTimeout) releasePerson();
	person_selected = id;
	person_openDetails();
	openDatabase('person');
}

/**
*  Die Funktion öffnet die Detailansicht des markierten Eintrags
**/

var requestsShortLoading = 0;
var requestsLifeDatesLoading = 0;
var requestsCommentsLoading = 0;
var requestCount = 0;

function person_openDetails() {
	startRequest('person_detail');
	if(!person_selected) return;
	
	byId('person_detailTitle').innerHTML = '';
	byId('person_iblock_summary').innerHTML = '';
	byId('person_iblock_shortBio').innerHTML = '';
	byId('person_iblock_institution').innerHTML = '';
	//byId('person_nameData').innerHTML = '';
	//byId('person_lifeData').innerHTML = '';
	//byId('person_workData').innerHTML = '';
	byId('person_bioData').innerHTML = '';
	byId('person_editorialComment').innerHTML = '';
	byId('person_comment').innerHTML = '';
	
	byId('fffi-person_database').style.display = 'none';
	byId('fffi-person_options').style.display = 'none';
	byId('fffi-person_results').style.display = 'none';

	byId('fffi-person_details').style.display = 'block';
	
	byId('person_detailID').innerHTML = 'ID: ' + person_getActId();
	
	byId('person_shortLoading').style.display = 'block';
	byId('person_lifeDataLoading').style.display = 'block';
	byId('person_bioDataLoading').style.display = 'block';
	byId('person_commentLoading').style.display = 'block';
	requestsShortLoading = 3;
	requestsLifeDatesLoading = 3;
	requestsCommentsLoading = 2;
	// Die Anzahl der folgenden DB-Anfragen:
	requestCount = 10;
	
	new Ajax.Request('PHP/person/existRequestDisplayName.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getDisplayNameSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});	
	new Ajax.Request('PHP/person/existRequestInformationBlock.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getInformationBlockSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});			
	new Ajax.Request('PHP/person/existRequestShortBioDetails.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getShortBioDetailsSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestInstitution.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getInstitutionSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestNames.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getNamesSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestBirthDeath.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getBirthDeathSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestOccupationPlaceSummary.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) {
			getOccupationPlaceSummarySuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestBiography.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getBiographySuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestEditorialComment.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getEditorialCommentSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	new Ajax.Request('PHP/person/existRequestComment.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) { 
			getCommentSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	/*
	new Ajax.Request('PHP/person/existRequestRevisionDesc.php', {
		method: 'get', parameters: 'unique=' + new Date().getTime() + '&id=' + person_getActId(), 
		onSuccess: function(t) {
			getRevisionDescSuccess(t);
			person_countDisplayRequests();
		},
		onFailure: getPersonRequestsFailure
	});
	*/			
	person_life_switchView('simple');
}

function getPersonRequestsFailure(t) {
	finishRequest('person_detail');
	getRequestsFailure(t);
}

function person_countDisplayRequests() {
	requestCount--;
	if(requestCount==0) {
		person_showLifeData();
		person_showBioData()
		finishRequest('person_detail');
	}
}

/**
*  Die Funktion schließt die Detailansicht
**/

function person_closeDetails() {
	byId('fffi-person_database').style.display = 'block';
	byId('fffi-person_options').style.display = 'block';
	byId('fffi-person_results').style.display = 'block';
	
	person_hideLifeData();
	person_hideBioData()

	byId('fffi-person_details').style.display = 'none';

	if(lockRenewTimeout) releasePerson();
}

/**
 * Diese Funktion blendet die Lebensdaten aus.
 */

function person_hideLifeData() {
	byId('person_nameData').style['display'] = 'none';
	byId('person_lifeData').style['display'] = 'none';
	byId('person_workData').style['display'] = 'none';
	byId('person_lifeDataOptionsBack').style['display'] = 'none';
	byId('person_lifeDataTriangle').className = 'triangleClosed';
}

/**
 * Diese Funktion blendet die Lebensdaten ein.
 */ 

function person_showLifeData() {
	byId('person_nameData').style['display'] = 'block';
	byId('person_lifeData').style['display'] = 'block';
	byId('person_workData').style['display'] = 'block';
	byId('person_lifeDataOptionsBack').style['display'] = 'block';
	byId('person_lifeDataTriangle').className = 'triangleOpen';
	
	//  Wenn hier die jeweilige Bedingung erfüllt ist, heißt das, dass das entspr. Feld leer ist. Daher wird es ausgeblendet!
	if(byId('person_nameData').innerHTML.indexOf('nameComplete') == -1) byId('person_nameData').style.display = 'none';
	if(byId('person_lifeData').innerHTML.indexOf('lifeComplete') == -1) byId('person_lifeData').style.display = 'none';
	if(byId('person_workData').innerHTML.indexOf('workComplete') == -1) byId('person_workData').style.display = 'none';
}

/**
 * Diese Funktion blendet Die Biographie aus
 */
 
 function person_hideBioData() {
 	byId('person_bioData').style['display'] = 'none';
	byId('person_bioMenu').style['display'] = 'none';
	byId('person_biographyTriangle').className = 'triangleClosed';
 }
 
 /**
 * Diese Funktion blendet Die Biographie ein
 */
 
 function person_showBioData() {
 	byId('person_bioData').style['display'] = 'block';
	byId('person_bioMenu').style['display'] = 'block';
	byId('person_biographyTriangle').className = 'triangleOpen';
 }

/**
 * Diese Funktion blendet das Kommentarfeld aus
 */
 
 function person_hideCommentData() {
 	byId('person_editorialComment').parentNode.parentNode.style['display'] = 'none';
 	byId('person_comment').parentNode.parentNode.style['display'] = 'none';
 	byId('person_commentTriangle').className = 'triangleClosed';
 }
 
 /**
 * Diese Funktion blendet das Kommentarfeld ein
 */
 
 function person_showCommentData() {
 	byId('person_editorialComment').parentNode.parentNode.style['display'] = 'block';
 	if(user && user.viewScrapbook == '1') byId('person_comment').parentNode.parentNode.style['display'] = 'block';
 	byId('person_commentTriangle').className = 'triangleOpen';
 }
