// -------------------------- Cookie Functions

function getCookie(NameOfCookie)

{

	if (document.cookie.length > 0) {

		var begin = document.cookie.indexOf(NameOfCookie+"=");

		if (begin != -1) {

			begin += NameOfCookie.length+1;

			end = document.cookie.indexOf(";", begin);

			if (end == -1) end = document.cookie.length;

			return unescape(document.cookie.substring(begin, end));

		}

	}

	return null;

}



function setCookie(NameOfCookie, value, expiredays)

{

	var ExpireDate = new Date();

	if (expiredays == null) expiredays = 365; // keep the settings for a year

	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) +

		"; path=/";

}



function delCookie(NameOfCookie)

{

	if (getCookie(NameOfCookie)) {

		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +

		"; path=/";

	}

}



function saveBoxState(boxId, state)

{

	setCookie('boxState_' + boxId, state, null);

}



// -------------------------- Box Functions



function restoreBoxes()

{

	for (i = 0; i < document.getElementsByTagName('A').length; i++) {

		if (! document.getElementsByTagName('A')[i].getAttribute('id')) continue;

		restoreBoxState(document.getElementsByTagName('A')[i].getAttribute('id'));

	}

}



function restoreBoxState(boxId)

{

  var savedState = getCookie('boxState_' + boxId);

  var currentState = getBoxNodeState(boxId);

  //if (savedState) alert(boxId + " current:" + currentState + ", saved:" + savedState);

  if (savedState != null && currentState && savedState != currentState) {

	  var box = getBoxByContentId(boxId);

	  if (box) {

	  	//alert("setting state: " + savedState);

	  	if (savedState == 'open') openBox(box);

	  	else closeBox(box);

     }

  }

}



function getBoxHeadlineByContentId(Id)

{

	var box = document.getElementById(Id);

	box = box.nextSibling; // content div

	while (box.nodeType != 1) box = box.nextSibling;

	if (box.className == 'klappklasse') {

		box = box.firstChild; // klapp headline

		while (box.nodeType != 1) box = box.nextSibling;

		return box;

	}

	return false;

}



function getBoxByContentId(Id)

{

	var box = getBoxHeadlineByContentId(Id);

	if (box) {

		box = box.nextSibling; // klapp body

		while (box.nodeType != 1) box = box.nextSibling;

		if (box.className == 'klappbody') return box;

		else return false;

	}

	return false;

}



function getElementId(obj)

{

  var id = obj.previousSibling;

  while (id.nodeType != 1) id = id.previousSibling;

  id = id.getAttribute('id');

  return id;

}



function getBoxNodeState(boxId)

{

  var headline = getBoxHeadlineByContentId(boxId);

  if (headline.className == 'klapphead') return 'open';

  else return 'closed';

}



function toggleBoxState(headline)

{

  var boxId = getElementId(headline.parentNode);

  var box = headline.nextSibling;

  // switch box header icon

  while (box.nodeType != 1) box = box.nextSibling;

  if (box.style.display == 'block') {

    headline.className = 'klappheadmax';

	saveBoxState(boxId, 'closed');

  } else {

    headline.className = 'klapphead';

	saveBoxState(boxId, 'open');

  }



  // switch box visibility state

  if (box.style.display == 'block') closeBox(box);

  else openBox(box);

}



function closeBox(box)

{

      if (box.style) box.style.display = 'none';

}





function openBox(box)

{

      if (box.style) box.style.display = 'block';

}


