var BoxInRoundArray = new Array();
var BoxInRoundArrayOverwrite = new Array();
var _addHeight = 0;
var allWays = '';


//window.onload = function() {
		var old_hash = location.hash;	
		window.setInterval(function() {									
					if (old_hash != location.hash && location.hash.indexOf('c')==1) {
						_divOpenClose ( old_hash.replace('#_c','') );
						old_hash = location.hash;
						_openByHash();
					}
				}, 300);
		//_openByHash();
//}

function _openByHash() {
		var h_hash = location.hash;			
		h_id = h_hash.replace('#c','');
		_divOpenClose ( h_id, 0 );
	}

function _divHeightMC( div, button, newHeight, buttonUrl, speed, originalHeightDiv, Mopacity ) {
    if (speed) {
      //alert ( document.getElementById( getHeightDiv ).offsetHeight );
      //speed = 1000/(document.getElementById( getHeightDiv ).offsetHeight * speed);
      }
		/*
		new Effect.Morph( (div), { 
							style: { 
								height: newHeight + 'px',		
								opacity: Mopacity
							}, 
							duration: speed
							});	
		*/
		new Effect.Morph( (div), { 
							style: { 
								height: newHeight + 'px'
							}, 
							duration: speed
							});	
		
		$(button).src = buttonUrl;
}

function _divOpenClose ( id, overWriteOpen, allWays ) {	 //id: id; 	overWriteOpen: [1,2] standardMässig eingeklappt [2] = sofort	 

	

	boxContent = 'boxContent_' + id;
	movePIButton = 'movePIButton_' + id;
	getHeightDiv = 'getHeightDiv_' + id;		
	speed = 0.3;
		
	if ( !document.getElementById(boxContent) ) return false;	
		
	$(boxContent).setStyle( { 'overflow':'hidden' } );
	
	Prototype.Browser.IELESS7 = Prototype.Browser.IE &&
							parseInt(navigator.userAgent.substring
							(navigator.userAgent.indexOf("MSIE")+5)) < 7;
	imgType = 'png';
	if ( Prototype.Browser.IELESS7) imgType = 'gif'; //gifs für ältere browser
	
	//mindesthöhe auf basis der ersten überschrift setzen
	stdHeight = 22;	
	firstH_Height = false;
	//alert($(boxContent).select("h1"));
	//if ( $(boxContent).select("h1") != "" ) firstH_Height = $(boxContent).select("h1")[0].getHeight();	
	if ( $(boxContent).select("header") != "" ) firstH_Height = $(boxContent).select("header")[0].getHeight();
	if ( !firstH_Height && $(boxContent).select("h1") != ""  ) firstH_Height = $(boxContent).select("h1")[0].getHeight();
	if ( !firstH_Height && $(boxContent).select("h2") != ""  ) firstH_Height = $(boxContent).select("h2")[0].getHeight();
	if ( !firstH_Height && $(boxContent).select("h3") != ""  ) firstH_Height = $(boxContent).select("h3")[0].getHeight();		
	if ( firstH_Height ) stdHeight = firstH_Height;
			
	if (overWriteOpen == 1) BoxInRoundArray[id] = true; //foldIn	
	if (overWriteOpen == 2) {	
		BoxInRoundArray[id] = true; //foldIn
		speed = 0;
		}
	
	if (BoxInRoundArrayOverwrite[id]) {
			//allWays	= BoxInRoundArrayOverwrite[id];
			}
	
	if (allWays == 'open') {
			BoxInRoundArray[id] = false; //foldOut
			BoxInRoundArrayOverwrite[id] = allWays;
			}
	if (allWays == 'close') {
			BoxInRoundArray[id] = true; //foldIn
			BoxInRoundArrayOverwrite[id] = allWays;
			}
	
	if (_addHeight) stdHeight = stdHeight + _addHeight;
	
	if (!BoxInRoundArray[id]) { //foldout
				BoxInRoundArray[id] = true;
				//old_hash ='#c'+id;
				//location.hash = '#c'+id;        
				_divHeightMC(boxContent ,movePIButton , document.getElementById( getHeightDiv ).offsetHeight, foldoutImgOut + imgType, speed, getHeightDiv, '1');				
				
        //        
        for (var ia = 0; ia < foldOutElements.length; ++ia) {
          
          if (foldOutElements[ia] != id) { 
               BoxInRoundArray[foldOutElements[ia]] = false;
               boxContentC = 'boxContent_' + foldOutElements[ia];
	              movePIButtonC = 'movePIButton_' + foldOutElements[ia];
	              getHeightDivC = 'getHeightDiv_' + foldOutElements[ia];
                firstH_HeightC = false;
                if ( $(boxContentC).select("header") != "" ) firstH_HeightC = $(boxContentC).select("header")[0].getHeight();
              	if ( !firstH_HeightC && $(boxContentC).select("h1") != ""  ) firstH_HeightC = $(boxContentC).select("h1")[0].getHeight();
              	if ( !firstH_HeightC && $(boxContentC).select("h2") != ""  ) firstH_HeightC = $(boxContentC).select("h2")[0].getHeight();
              	if ( !firstH_HeightC && $(boxContentC).select("h3") != ""  ) firstH_HeightC = $(boxContentC).select("h3")[0].getHeight();		
              	if ( firstH_Height ) stdHeightC = firstH_HeightC;
                
                
                
                _divHeightMC(boxContentC ,movePIButtonC , stdHeightC, foldoutImgIn + imgType, speed, getHeightDivC, '0.7');
				
              }
          }
        
        } else {    //foldin
				BoxInRoundArray[id] = false;
				_divHeightMC(boxContent ,movePIButton , stdHeight, foldoutImgIn + imgType, speed, getHeightDiv, '0.7');
				}
	}