//jquery - 26.07.2010 - alexandru.grosu@sw-umwelttechnik.com

$(document).ready(function(){
	jQuery('.klappklasse').each(function(){
				//alert(jQuery(this).find('.klappbody a').length);
				if (jQuery(this).find('.klappbody a').length == 0){
					jQuery(this).find('.klappheadmax').css('background','none');
				}else{
				jQuery(this).find('.klappheadmax td.produkt-td-first').css('background-image','url(fileadmin/templates/images/tabelle-pfeil.jpg)');
				jQuery(this).find('.klappheadmax td.produkt-td-first').css('background-position','right');
				jQuery(this).find('.klappheadmax td.produkt-td-first').css('background-repeat','no-repeat');
				}
	});
});

// -------------------------- 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';
}

// modifications - 26.07.2010 - alexandru.grosu@sw-umwelttechnik.com
// this script failed its purpose...
function togglearrow(headline){
  var box = headline.nextSibling;

  while (box.nodeType != 1) box = box.nextSibling;
  if (box.getElementsByTagName('a').length < 1){
	headline.className = 'klappheadmax_noarrow';
  }
  else{
  	headline.className = 'klappheadmax_warrow';
  }
}

function toggleBoxState(headline){
  var boxId = getElementId(headline.parentNode);
  var box = headline.nextSibling;
  var docsempty = 0;

  // switch box header icon
  while (box.nodeType != 1) box = box.nextSibling;
//this if-statement fails its purpose if we do not use two separate classes with and without arrow background
  if (box.style.display == 'block') {
		if (box.getElementsByTagName('a').length < 1){
			headline.className = 'klappheadmax_noarrow';
			saveBoxState(boxId, 'closed');
		}
		else{
                        headline.className = 'klappheadmax';
                        saveBoxState(boxId, 'closed');
		}
  }
  else{
	if (box.getElementsByTagName('a').length < 1){
		headline.className = 'klapphead_noarrow';
		docsempty = 1;
		//saveBoxstate(boxId, 'closed');
	}
	else{
		headline.className = 'klapphead';
		saveBoxState(boxId, 'open');
	}
  }
  if (box.style.display == 'block') closeBox(box);
  else{
	if (docsempty == 1) closeBox(box);
	else{
		openBox(box);
	}
  }
  //docsempty = 0;
}

function closeBox(box){
      if (box.style) box.style.display = 'none';
}

function openBox(box){
      if (box.style) box.style.display = 'block';
}

