var sliding = 0;
var menuElem;

// Set is sliding value
function setSliding(a_ISliding){
	sliding = a_ISliding;
}

// Get is sliding value
function getSliding(){
	return sliding;
}

// show hovered and hide last visible (if exist)
function apear(thisObj, target, targetContainer)
{
	if ($(targetContainer)) {
		$(targetContainer).style.zIndex = 200;
	}
	
	var eldown = document.getElementById(target);
	var eldownTitle = getPreviousSibling(eldown);
		
	//  If element is visible do nothing
	if ($('panel-visible') == eldownTitle) {
			return;
	}
	if ($('panel-visible')) {
	
		if( getSliding() == 1 ){
				return false;
		}

		var elup = getNextSibling($('panel-visible'));
		eldown = document.getElementById(target);
		eldownTitle = getPreviousSibling(eldown);
		
		//set z-index for down to be infront of up
		eldown.style.zIndex=20;
		elup.style.zIndex=10;
		
		setSliding( 1 );
		
		new Effect.Parallel(
				[
						new Effect.SlideUp(elup),
						new Effect.SlideDown(eldown)
				], {
						duration: 0.3,
						afterFinish: function() { 
											 setSliding( 0 ); 
											if (thisObj != menuElem) {
												//closeAllDropDowns();
												disapear(thisObj);
											}
						}
				});

	
				$('panel-visible').id = '';
	}
	else {
			if( getSliding() == 1 ){
					return false;
			}
		
			//var eldown = getNextSibling(el);
			
			setSliding( 1 );
			
			new Effect.Parallel(
			[
					new Effect.SlideDown(eldown)
			], {
					duration: 0.3,
					afterFinish: function() { 
										 setSliding( 0 );
									}
			});

	}
	eldownTitle.id = 'panel-visible';
}

// hide visible (if exist)
function disapear(thisObj, targetContainer)
{
	if ($('panel-visible')) {
	
		if( getSliding() == 1 ){
				return false;
		}

		var elup = getNextSibling($('panel-visible'));
		
		//set z-index for down to be infront of up
		//elup.style.zIndex=10;
		
		setSliding( 1 );
		
		new Effect.Parallel(
				[
						new Effect.SlideUp(elup)
				], {
						duration: 0.3,
						afterFinish: function() { 
											setSliding( 0 ); 
											if (thisObj != menuElem) {
												closeAllDropDowns();
											}
											
											if ($(targetContainer)) {
												$(targetContainer).style.zIndex = -1;
											}
						}
				});
	
				$('panel-visible').id = '';
	}
}

// Next sibling method to work around firefox issues
function getNextSibling(startBrother){
  endBrother=startBrother.nextSibling;
  while(endBrother.nodeType!=1){
    endBrother = endBrother.nextSibling;
  }
  return endBrother;
}

// Previous sibling method to work around firefox issues
function getPreviousSibling(startBrother){
  endBrother=startBrother.previousSibling;
  while(endBrother.nodeType!=1){
    endBrother = endBrother.previousSibling;
  }
  return endBrother;
}

// Close all dropdowns on mouse out
function closeAllDropDowns(){
	var bodyPanels = document.getElementsByClassName('panel_body');
		
	for (i = 0; i < bodyPanels.length; i++) {
		bodyPanels[i].hide();
	}
	
	removePanelVisibleId();
}

// remove 'panel-visible' id
function removePanelVisibleId(){
	var visibleElement = document.getElementById('panel-visible');
	 if (visibleElement != null) {	
		visibleElement.id='';
	 }
}
//Event.observe(window, 'load', init, false);