/* JavaScripts for E-Herold, Version 0.2 */
/* Author Florian Sutter */

/////////////////////////////////////////////////////////////////////////////
// GLOBALS

// CONSTANTS
var C_ALL                 = 0;
var C_COAT_OF_ARMS 				= 1;
var C_GENEALOGICAL_TREE 	= 2;
var C_ANIM_COLOR_ARRAY 		= new Array("#ffffff","#eef7fd","#ddeffa","#cce7f8","#b9def5","#a8d5f2","#97cdf0","#86c5ed","#74bdea","#63b4e8","#51ace5","#3fa4e3","#2e9be0","#2092da","#1e87c9","#1b7bb8","#186fa5");
var C_SCROLL_INCREMENT 		= 10;
var C_MAX_SCROLL_AMOUNT 	= 110;
var C_STR_SEARCH_TEXT 		= "Suchbegriff eingeben"
var C_STR_CONNECTION_ERROR 	= "Der Server antwortet nicht.\n\n" +
							  "Bitte starten Sie den Browser neu und laden erneut die Seite.";

var currentDbNum = -1;
var currentEntryView = C_COAT_OF_ARMS;
var coatOfArmsLink = "";
var genealogicalTreeLink = "";
var searchIsActive = false;
var bookIsOpen = false;


// ANIMATION
var animationCounter = 0;
var animationDirection = 0;

// SCROLLING
scrolled = false;
curScrollAmount = 0;



/////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS


/**
 * isNumber
 * 
 * Checks if string contains numbers only.
 * 
 * @return true if string contains only numbers, false otherwise
 */ 
function isNumber(str) {
  for(var position=0; position<str.length; position++){
	var chr = str.charAt(position)
        if  ( (chr < "0") || (chr > "9") ) 
              return false;
  };      
  return true;
};


/////////////////////////////////////////////////////////////////////////////
// HANDLER


/**
 * init
 * 
 * Init method for the onload event of the html body element. Used
 * for initial tasks such as attatching handlers to elements. 
 * 
 * @param dbNum - Database key for this coat of arms entry
 */
function init() {
	
	// attach mouseover effects to menu
	var menu = document.getElementById("menuList");
	for(var i=0; i < menu.childNodes.length; i++) {
		var child = menu.childNodes.item(i);
		if(child.tagName == "li" || child.tagName == "LI") {
			child.onmouseover = handleOnMouseOverMenu;
			child.onmouseout = handleOnMouseOutMenu;
		}	
	}
	
	// attach mouseover effects to external link list
	var externalLinkList = document.getElementById("externalLinkList");
	for(var j=0; j < externalLinkList.childNodes.length; j++) {
		var child = externalLinkList.childNodes.item(j);
		if(child.tagName == "li" || child.tagName == "LI") {
			child.onmouseover = handleOnMouseOverExternalLink;
			child.onmouseout = handleOnMouseOutExternalLink;
		}	
	}
	
	// attach onclick effect to book
//	var book = document.getElementById("book");
//	book.onclick = handleOnClickBook;
	
	// set start page in description frame
	//top.frames['entryDescriptionFrame'].location.replace('/eherold/html/start.html');

  // preload images
  preloadImages();

	// load startup variables if entry is accessed by get parameter directly
	loadStartupValues();

}

/**
 * handleOnClickHeader
 * 
 * Action handler for onclick event of
 * header field.
 * 
 */
function handleOnClickHeader() {
 	resetApp();
}


/**
 * handleOnClickLink
 * 
 * Event handler for onclick on a link. (instead of ancor -> 
 * browser back button)
 */
function handleOnClickLink(destination) {
	
	if(destination == "home") {
		resetApp(); 
	} else if(destination == "impressum") {
		switchToOpenBook();
		
		top.frames['entryDescriptionFrame'].location.replace('/eherold/html/impressum.html');
		
	} else if(destination == "contact") {
		switchToOpenBook();
		
		top.frames['entryDescriptionFrame'].location.replace('/eherold/jsp/ContactForm.jsp');
		
	} else if(destination == "help") {
		switchToOpenBook();
		
		top.frames['entryDescriptionFrame'].location.replace('/eherold/html/help.html');
		
	} else if(destination == "introduction") {
		switchToOpenBook();
		
		top.frames['entryDescriptionFrame'].location.replace('/eherold/html/einfuehrung.html');
	
	} else if(destination == "wappenHerold") {
		switchToOpenBook();
		
		top.frames['entryDescriptionFrame'].location.replace('/eherold/html/wappen_herold.html');
	
	} else if(destination == "wappenrolle") {
		switchToOpenBook();
		
		top.frames['entryDescriptionFrame'].location.replace('/eherold/html/wappenrolle.html');
	
	}
	

}


/**
 * handleOnClickBook
 * 
 * Event handler for onclick on book at beginning. Makes layers of the
 * start page (main menu) visible
 */
function handleOnClickBook() {
	
	var book = document.getElementById("book");
	book.onclick = "";
	book.style.cursor = "auto";
		
	switchToOpenBook();
	
	top.frames['entryDescriptionFrame'].location.replace('/eherold/html/start.html');
	
}



/**
 * hanldeOnClickBackward
 * 
 * Event handler for onclick on backward button names 'navButtonBkd'.
 * Performs a database lookup request, if target field of button
 * contains a valid value for 'dbNum'
 */
function handleOnClickBackward() {
	
	var fastBackwardButton = document.getElementById("navButtonBkd");
	var target = fastBackwardButton.target;
	if( isNumber(target) ) {
		var dbNum = parseInt(target);
		fetchDBEntry(dbNum);	
	}
}

/**
 * hanldeOnClickStart
 * 
 * Event handler for onclick on backward button names 'navButtonStart'.
 * Switches back to start menu.
 */
function handleOnClickStart() {
	
	// switch left page to start page view
	var passepartout = document.getElementById("passepartout");
	passepartout.style.visibility = "hidden";
	passepartout.style.display = "none";
	var startLayer = document.getElementById("startLayer");
	startLayer.style.visibility = "visible";
	startLayer.style.display = "block";
	
	// put link of start page into iframe
	top.frames['entryDescriptionFrame'].location.replace('/eherold/html/start.html');
	
	// set buttons visible
	var navButtonBkd = document.getElementById("navButtonBkd");
	navButtonBkd.style.visibility = "hidden";
	var navButtonStart = document.getElementById("navButtonStart");
	navButtonStart.style.visibility = "hidden";
	var navSwitchButton = document.getElementById("navSwitchButton"); 
	navSwitchButton.style.visibility = "hidden";
	var navButtonFwd = document.getElementById("navButtonFwd");
	navButtonFwd.style.visibility = "hidden";
}

/**
 * hanldeOnClickSwitch
 * 
 * Event handler for onclick on backward button names 'navButtonSwitch'.
 * Switches form description view to genealogical table view.
 * 
 * Globals:
 * 	C_COAT_OF_ARMS
 * 	C_GENEALOGICAL_TREE
 *  currentDbNum
 *  currentEntryView
 *  coatOfArmsLink
 *  genealogicalTreeLink
 *  
 */
function handleOnClickSwitch() {
	
	if(currentDbNum > 0) {
		if(currentEntryView == C_COAT_OF_ARMS) {
			top.frames['entryDescriptionFrame'].location.replace(genealogicalTreeLink);
			currentEntryView = C_GENEALOGICAL_TREE;
			
			activateButtons(C_GENEALOGICAL_TREE);
			
		} else {
					top.frames['entryDescriptionFrame'].location.replace(coatOfArmsLink);			
			currentEntryView = C_COAT_OF_ARMS;
			
			deactivateButtons(C_GENEALOGICAL_TREE);
		}
	}
}


/**
 * hanldeOnClickForward
 * 
 * Event handler for onclick on backward button names 'navButtonFwd'.
 * Performs a database lookup request, if target field of button
 * contains a valid value for 'dbNum'
 */
function handleOnClickForward() {
	
	var fastForwardButton = document.getElementById("navButtonFwd");
	var target = fastForwardButton.target;
	if( isNumber(target) ) {
		var dbNum = parseInt(target);
		fetchDBEntry(dbNum);
	}
}


/**
 * hanldeOnMouseOverMenu
 * 
 * Event handler for onmouseover on menu entry. Enlarges
 * attached element.
 */
function handleOnMouseOverMenu() {	
	this.style.paddingLeft = "5px";
	this.style.fontWeight = "bold";
}


/**
 * hanldeOnMouseOutMenu
 * 
 * Event handler for onmouseout on menu entry. Shirks
 * attached element.
 */
function handleOnMouseOutMenu() {
	this.style.padding = "0px";
	this.style.paddingBottom = "5px";
	this.style.fontWeight = "normal";
}


/**
 * hanldeOnMouseOverExternalLink
 * 
 * Event handler for onmouseover on external link list. Makes
 * the P element below the external link visible.
 */
function handleOnMouseOverExternalLink() {	
	this.style.paddingLeft = "5px";
	this.style.fontWeight = "bold";
	
	var childNodeList = this.childNodes;
	for(var i=0; i < childNodeList.length; i++) {
		var child = childNodeList.item(i);
		if(child.tagName == "div" || child.tagName == "DIV") {
			child.style.visibility = "visible";
			child.style.display = "block";
		}		
	}
}


/**
 * hanldeOnMouseOutExternalLink
 * 
 * Event handler for onmouseout on external link list. Hides
 * the P element below the external link.
 */
function handleOnMouseOutExternalLink() {
	this.style.padding = "0px";
	this.style.paddingBottom = "5px";
	this.style.fontWeight = "normal";
	
	var childNodeList = this.childNodes;
	for(var i=0; i < childNodeList.length; i++) {
		var child = childNodeList.item(i);
		if(child.tagName == "div" || child.tagName == "DIV") {
			child.style.visibility = "hidden";
			child.style.display = "none";
		}		
	}
}


/**
 * handleOnClickSearchField
 * 
 * Event handler for onclick on search input field. Removes 
 * standard text if it is in the input field.
 */
function handleOnClickSearchField() {
	var searchField = document.getElementById("searchField");
	if(searchField.value == C_STR_SEARCH_TEXT)
			searchField.value = '';
}


/**
 * handleOnBlurSearchField
 * 
 * Event handler for onblur on search input field. Inserts
 * standard text into search input field if it's empty.
 * 
 * Globals:
 * 	C_STR_SEARCH_TEXT
 */
function handleOnBlurSearchField() {
	var searchField = document.getElementById("searchField");
	if(searchField.value == "")
			searchField.value = C_STR_SEARCH_TEXT;
}


/**
 * handleOnSubmitForm
 * 
 * Event handler for onsubmit on search input field. Checks if
 * field is ready to allow a database lookup.
 * 
 * Globals:
 * 	C_STR_SEARCH_TEXT
 */
function handleOnSubmitForm() {
	
	var book = document.getElementById("book");
	if(book.onclick != "") {
		handleOnClickBook();
	}
	
	var searchField = document.getElementById("searchField");
	if(searchField.value != '') {
		searchIsActive = true;
		fetchListEntries('');
	} else {
		searchField.value = C_STR_SEARCH_TEXT;
	}
	// return false to avoid sending the form and reloading the whole page.
	return false;
}


/**
 * handleOnClickLetterButton
 * 
 * Invokes a lookup for the letter given by the parameter. The
 * entry names returned by the called function are added to the
 * drop down list.
 * 
 * @param letter - letter which is used to get the corresponding entries for the list
 */
function handleOnClickLetterButton(letter) {
	searchIsActive = false;
	
	var book = document.getElementById("book");
	if(book.onclick != "") {
		handleOnClickBook();
	}
	
	fetchListEntries(letter);
}
	
	
/**
 * handleOnClickButton
 * 
 * Handles a click on a button according to the 
 * button function.
 * 
 * @param letter - button which was pressed
 */
function handleOnClickButton(buttonID) {

	switch(buttonID) {
	
		case "zoom":
		  if(currentDbNum > 0) {
		  	var newwindow = window.open("/eherold/GenealogicalTreeFetcher?dbNum=" + currentDbNum + "&zoom=true", 'Stammreihe','scrollbars=1,height=600,width=500');
				if (window.focus) {newwindow.focus()}
		  }
		  break;
	
	}

}



/////////////////////////////////////////////////////////////////////////////
// ANIMATION

/**
 * scrollDown
 * 
 * Scrolls down for a distance defined in C_MAX_SCROLL_AMOUNT
 * 
 * Globals:
 * 	C_MAX_SCROLL_AMOUNT
 *  C_SCROLL_INCREMENT
 *  curScrollAmount
 *  scrolled
 *  
 */
function scrollDown() {
	if(scrolled == false) {
		if(curScrollAmount < C_MAX_SCROLL_AMOUNT) {
		    window.scrollBy(0, C_SCROLL_INCREMENT);
		    setTimeout("scrollDown()", 10);
		    curScrollAmount = curScrollAmount + C_SCROLL_INCREMENT;
		} else {
			scrolled = true;
		}
	}
}


/**
 * animateBackground
 * 
 * Glowing effect for the dropdown field for names of enrties.
 * 
 * Globals:
 * 	C_ANIM_COLOR_ARRAY
 * 	animationDirection
 * 	animationCounter
 */
function animateListBackground() {

	var nameSelector = document.getElementById("nameSelector");	
	if(animationDirection == 0) {
		if(animationCounter < C_ANIM_COLOR_ARRAY.length) {
			nameSelector.style.backgroundColor = C_ANIM_COLOR_ARRAY[animationCounter];
			animationCounter++;
			window.setTimeout("animateListBackground()", 10)
		} else {
			animationCounter--;
			animationDirection = 1;
			window.setTimeout("animateListBackground()", 10)
		}
	} else {
		if(animationCounter >= 0) {
			nameSelector.style.backgroundColor = C_ANIM_COLOR_ARRAY[animationCounter];
			animationCounter--;
			window.setTimeout("animateListBackground()", 10)
		} else {
			animationCounter = 0;
			animationDirection = 0;
			nameSelector.style.background = "white";
		}
	}
}




/////////////////////////////////////////////////////////////////////////////
// DATABASE LOOKUPS (AJAX FUNCTIONS)

/**
 * fetchDBEntry
 * 
 * Fetches an entry from the database, if dbNum contains a valid database key or
 * if search is active and the search string is submitted to the database 
 * entry fechter servlet.
 * 
 * Globals:
 * 	currentDbNum
 * 	currentEntryView
 *  coatOfArmsLink
 * 	genealogicalTreeLink
 * 
 * @param dbNum - Database key for this coat of arms entry
 */
function fetchDBEntry(dbNum) {

	if(!bookIsOpen) {
	  switchToOpenBook();
	}

	// check for valid value in parameter 'dbNum'
	if(dbNum == null) {
		// search selected option in case function was called with no argument
		var nameSelector = document.getElementById("nameSelector");
		var options = nameSelector.getElementsByTagName("option");
		for (i = 0; i < options.length; i++) {
			if (options[i].selected == 1) {
				dbNum = options[i].getAttribute("dbNum");
			}
		}
	}
    
	// check if search is active 
	var searchString = '';
	if(searchIsActive) {
		var searchField = document.getElementById("searchField");
		if(searchField.value != '')
			searchString = searchField.value;
	}
	
	// set loading image path to the coat of arms image tag
	var coaImg = document.getElementById("entryCoatOfArms");
	coaImg.src = '/eherold/images/gui/ajax-loader.gif';
	top.frames['entryDescriptionFrame'].location.replace('/eherold/html/loading.html');
	
	// perform ajax request
	new Ajax.Request('/eherold/EntryFetcher', {
		method :'get',
		parameters : {dbNum :dbNum, searchString :searchString},
		onSuccess : function(transport) {
			
			// fetch result
			var response = transport.responseXML;
			if (response == null) {
				alert(C_STR_CONNECTION_ERROR);
				return;
			}

			///// update name field
			var nameTexts = response.getElementsByTagName("name");
			var nameField = document.getElementById("entryNameField");
			// create text node
			var newNameText = nameTexts[0].firstChild.nodeValue;
			var newNameTextNode = document.createTextNode(newNameText);
			// remove old text
			while(nameField.firstChild != null) {
				nameField.removeChild(nameField.firstChild);
			}
			// append new text node
			nameField.appendChild(newNameTextNode);
			
			///// update location field
			var locationTexts = response.getElementsByTagName("location");
			var newLocationText = "";
			// check if a location has been send
			if(locationTexts[0].firstChild != null) {
				newLocationText = locationTexts[0].firstChild.nodeValue;
			}
			var locationField = document.getElementById("entryFromField");
			// create text node
			var newLocationTextNode = document.createTextNode(newLocationText);
			// remove old text
			while(locationField.firstChild != null) {
				locationField.removeChild(locationField.firstChild);
			}
			// append new text node
			locationField.appendChild(newLocationTextNode);			


				
			///// update description-link
			var descriptionLinks = response.getElementsByTagName("description-link");
		  top.frames['entryDescriptionFrame'].location.replace(descriptionLinks[0].firstChild.nodeValue);
			coatOfArmsLink = descriptionLinks[0].firstChild.nodeValue;
			var genealogicalTreeLinks = response.getElementsByTagName("genealogicalTree-link");			
			genealogicalTreeLink = genealogicalTreeLinks[0].firstChild.nodeValue;
	
			///// set new image path
			var imagePaths = response.getElementsByTagName("image-path");
			var coaImg = document.getElementById("entryCoatOfArms");
			coaImg.src = imagePaths[0].firstChild.nodeValue;
			
			///// change navigation button links
			// fast backward button
			var buttonTargetsFastBackward = response.getElementsByTagName("buttonFastBackward");
			var buttonFastBackward = document.getElementById("navButtonBkd");
			buttonFastBackward.target = buttonTargetsFastBackward[0].firstChild.nodeValue;			
			// fast forward button
			var buttonTargetsFastForward = response.getElementsByTagName("buttonFastForward");
			var buttonFastForward = document.getElementById("navButtonFwd");
			buttonFastForward.target = buttonTargetsFastForward[0].firstChild.nodeValue;
			
			///// mark active list option in drop down list
			var nameSelector = document.getElementById('nameSelector');    	
	    	for(var i = 0; i < nameSelector.childNodes.length; i++) {
	    		var option = nameSelector.childNodes.item(i);
	    		var optionsDbNum = option.getAttribute("dbnum");
	    		
	    		if(optionsDbNum != null) {
	    			if(isNumber(optionsDbNum)) {
		    			if(parseInt(optionsDbNum) == dbNum) {
		    				option.selected = true;
		    			} else {
		    				option.selected = false;
		    			}
		    		}
	    		}
	    	}
			
	    	// save dbNum and view type to globals
			currentDbNum = dbNum;
			currentEntryView = C_COAT_OF_ARMS;
			
			// switch left page to coat of arms view
			var startLayer = document.getElementById("startLayer");
			startLayer.style.visibility = "hidden";
			startLayer.style.display = "none";
			var entryDescription = document.getElementById("passepartout");
			entryDescription.style.visibility = "visible";
			entryDescription.style.display = "block";
			
			// set buttons visible
			var navButtonBkd = document.getElementById("navButtonBkd");
			navButtonBkd.style.visibility = "visible";
			var navButtonStart = document.getElementById("navButtonStart");
			navButtonStart.style.visibility = "visible";
			var navSwitchButton = document.getElementById("navSwitchButton"); 
			navSwitchButton.style.visibility = "visible";
			var navButtonFwd = document.getElementById("navButtonFwd");
			navButtonFwd.style.visibility = "visible";
			
			// make all buttons in entry panel invisible
			deactivateButtons(C_ALL);
		},
		onFailure : function() {
			alert(C_STR_CONNECTION_ERROR);
		}
	});	
		
}


/**
 * fetchDBEntry
 * 
 * Fetches all entry names from the servlet, which are given
 * by the search string or the letter. If the variable 'searchIsActive'
 * contains 'true' letter is ignored and the search string from the
 * input box is taken for entry name lookup.
 * 
 * Globals:
 * 	searchIsActive
 * 
 * @param dbNum - Database key for this coat of arms entry
 */
function fetchListEntries(letter) {
		
	var searchString = '';
	if(searchIsActive) {
		var searchField = document.getElementById("searchField");
		if(searchField.value != '')
			searchString = searchField.value;
	}
	
	new Ajax.Request('/eherold/ListEntriesFetcher',
	  {
	    method:'get',
	    parameters: {letter: letter, searchString: searchString},
	    onSuccess: function(transport){
	    	
	    	// fetch result
	    	var response = transport.responseXML;
	    	if(response == null) {
	    	  alert(C_STR_CONNECTION_ERROR);
	    	  return;	
	    	}
	    	
	    	// remove old entries from listbox
	    	var nameSelector = document.getElementById('nameSelector');    	
	    	while(nameSelector.hasChildNodes()) {
	    		nameSelector.removeChild(nameSelector.firstChild);
	    	}
	    	
	    	// copy new options into the list
	    	var options = response.getElementsByTagName("option");
	    	var i=0;
	    	for(i=0; i < options.length;i++) {
	    		var newOption = document.createElement("option");
	    		
	    		// insert name
	    		var nameStrings = options[i].getElementsByTagName("name");
	    		var optionsText = nameStrings[0].firstChild.nodeValue;
	    		var textNode = document.createTextNode(optionsText);
	    		newOption.appendChild(textNode);
	    		
	    		// insert attributes
	    		var dbNums = options[i].getElementsByTagName("dbNum");
	    		newOption.setAttribute("dbNum", dbNums[0].firstChild.nodeValue);
	    		
	    		// append child to name selector object (listbox with names)
	    		nameSelector.appendChild(newOption);
	    		
	    		// load first entry
	    		if(i == 0) {
	    			window.setTimeout("fetchDBEntry(" + dbNums[0].firstChild.nodeValue + ")", 1);
	    		}
	    	}
			if(i == 0) {
		    	// insert one blank option at the beginning
	    		var newOption = document.createElement("option");
	    		var textNode = document.createTextNode("(Keine Einträge)");
	    		newOption.appendChild(textNode);
	    		nameSelector.appendChild(newOption);
	    		
	    		window.setTimeout("handleOnClickStart()", 1);
			}
			window.setTimeout("animateListBackground()", 1);
			
	    },
	    onFailure: function(){ alert(C_STR_CONNECTION_ERROR) }
	  });
}

/**
 * resetApp  
 *
 * Used to reset the application to the intro page view.
 * 
 */
function resetApp() {
 	
 	// reset book element to initial attributes
	var book = document.getElementById("book");
	book.onclick = handleOnClickBook;
	book.style.backgroundImage = "url(./images/gui/book/bookClosed.jpg)";
	book.style.cursor = "pointer";
	
	// make start layers invisible
	var startLayer = document.getElementById("startLayer");
	startLayer.style.visibility = "hidden";
	startLayer.style.display = "none";
	var entryDescription = document.getElementById("entryDescription");
	entryDescription.style.visibility = "hidden";
	entryDescription.style.display = "none";
	
	// switch left page to start page view
	var passepartout = document.getElementById("passepartout");
	passepartout.style.visibility = "hidden";
	passepartout.style.display = "none";

	// set buttons invisible
	var navButtonBkd = document.getElementById("navButtonBkd");
	navButtonBkd.style.visibility = "hidden";
	var navButtonStart = document.getElementById("navButtonStart");
	navButtonStart.style.visibility = "hidden";
	var navSwitchButton = document.getElementById("navSwitchButton"); 
	navSwitchButton.style.visibility = "hidden";
	var navButtonFwd = document.getElementById("navButtonFwd");
	navButtonFwd.style.visibility = "hidden";
	
	// make all buttons in entry panel invisible
	deactivateButtons(C_ALL);
	
	// save state of book
	bookIsOpen = false;
 	
}


/**
 * switchToOpenBook
 * 
 * Switches the GUI to the open book.
 */
function switchToOpenBook() {
	
	var book = document.getElementById("book");
	book.style.backgroundImage = "url(./images/gui/book/book.jpg)";
	
	// make start layers visible
	var startLayer = document.getElementById("startLayer");
	startLayer.style.visibility = "visible";
	startLayer.style.display = "block";
	var entryDescription = document.getElementById("entryDescription");
	entryDescription.style.visibility = "visible";
	entryDescription.style.display = "block";
	
	// make passepartout invisible
	var passepartout = document.getElementById("passepartout");
	passepartout.style.visibility = "hidden";
	passepartout.style.display = "none";

	// set buttons invisible
	var navButtonBkd = document.getElementById("navButtonBkd");
	navButtonBkd.style.visibility = "hidden";
	var navButtonStart = document.getElementById("navButtonStart");
	navButtonStart.style.visibility = "hidden";
	var navSwitchButton = document.getElementById("navSwitchButton"); 
	navSwitchButton.style.visibility = "hidden";
	var navButtonFwd = document.getElementById("navButtonFwd");
	navButtonFwd.style.visibility = "hidden";
	
	// make all buttons in entry panel invisible
	deactivateButtons(C_ALL);
	
	// save state of book
	bookIsOpen = true;
	
}


/**
 * preloadImages
 * 
 * Preloads images used in the gui.
 */
function preloadImages() {
	
	// image count
  var imageCount = 37;

  // create image object and filepath array
  var imageObj = new Image();
  var images = new Array();

	// preload book image	
	images[0] = '/eherold/images/gui/book/book.jpg';

	// preload closed book image
	images[1] = '/eherold/images/gui/book/bookClosed.jpg';

	// preload button images	
	images[2] = '/eherold/images/gui/buttons/coa_a.jpg';
  images[3] = '/eherold/images/gui/buttons/coa_b.jpg';
  images[4] = '/eherold/images/gui/buttons/coa_c.jpg';
  images[5] = '/eherold/images/gui/buttons/coa_d.jpg';
  images[6] = '/eherold/images/gui/buttons/coa_e.jpg';
  images[7] = '/eherold/images/gui/buttons/coa_f.jpg';
  images[8] = '/eherold/images/gui/buttons/coa_g.jpg';
  images[9] = '/eherold/images/gui/buttons/coa_h.jpg';
  images[10] = '/eherold/images/gui/buttons/coa_i.jpg';
  images[11] = '/eherold/images/gui/buttons/coa_j.jpg';
  images[12] = '/eherold/images/gui/buttons/coa_k.jpg';
  images[13] = '/eherold/images/gui/buttons/coa_l.jpg';
  images[14] = '/eherold/images/gui/buttons/coa_m.jpg';
  images[15] = '/eherold/images/gui/buttons/coa_n.jpg';
  images[16] = '/eherold/images/gui/buttons/coa_o.jpg';
  images[17] = '/eherold/images/gui/buttons/coa_p.jpg';
  images[18] = '/eherold/images/gui/buttons/coa_q.jpg';
  images[19] = '/eherold/images/gui/buttons/coa_r.jpg';
  images[20] = '/eherold/images/gui/buttons/coa_v.jpg';
  images[21] = '/eherold/images/gui/buttons/coa_w.jpg';
  images[22] = '/eherold/images/gui/buttons/coa_x.jpg';
  images[23] = '/eherold/images/gui/buttons/coa_y.jpg';
  images[24] = '/eherold/images/gui/buttons/coa_z.jpg';
	
	// preload seel images
	images[25] = '/eherold/images/gui/book/seel_gt_.png';
	images[26] = '/eherold/images/gui/book/seel_start_.png';
	images[27] = '/eherold/images/gui/book/seel_forward_.png';
	images[28] = '/eherold/images/gui/book/seel_back_.png';
	images[29] = '/eherold/images/gui/book/seel_gt__.png';
	images[30] = '/eherold/images/gui/book/seel_start__.png';
	images[31] = '/eherold/images/gui/book/seel_forward__.png';
	images[32] = '/eherold/images/gui/book/seel_back__.png';
	
	// preload border images
  images[33] = '/eherold/images/gui/border/border_bottom.jpg';
  images[34] = '/eherold/images/gui/border/border_left_middle.jpg';
  images[35] = '/eherold/images/gui/border/border_right_middle.jpg';
  images[36] = '/eherold/images/gui/border/border_top.jpg';
	
  // start preloading
  for(i = 0; i <= imageCount; i++) 
  {
  	var image = new Image();
  	image.src = images[i];
  }
  
}


/**
 * activateButtons
 * 
 * Enables visibility for button.
 */
function activateButtons(buttonID) {

	switch(buttonID) {
	
	  case C_GENEALOGICAL_TREE:
	  	
	  	// make button visible
			var button = document.getElementById("entryDescButtonZoom");
			button.style.visibility = "visible";
			button.style.display = "block";
	  	break;	  	
	}

}


/**
 * deactivateButtons
 * 
 * Disables visibility for button.
 */
function deactivateButtons(buttonID) {

	switch(buttonID) {
	
		case C_ALL:
			deactivateButtons(C_GENEALOGICAL_TREE);
			break;
			
	  case C_GENEALOGICAL_TREE:
	  	
	  	// make button invisible
			var button = document.getElementById("entryDescButtonZoom");
			button.style.visibility = "hidden";
			button.style.display = "none";
	  	break;	  	
	}

}


function zoomImage() {
	
	var entryCoatOfArmsNode = document.getElementById("entryCoatOfArms");
	var link = entryCoatOfArmsNode.src;
	
	var newwindow = window.open(link, 'Bild','scrollbars=1,height=600,width=600');
	if (window.focus) {newwindow.focus()}
	

}