var sliding = 0;


function setSliding(a_ISliding){
	sliding = a_ISliding;
}


function getSliding(){
	return sliding;

}


function accordion(evt) {
	el = Event.element(evt);
	
	if ($('visible') == el) {
			//alert($('visible').className);
			
			$('visible').style.background = "url(img/nymag_" + $('visible').className + "_on.gif) no-repeat";
			
			return;
	}
	if ($('visible')) {
			
			$('visible').style.background = "url(img/nymag_" + $('visible').className + "_off.gif) no-repeat";
			
			//alert("1) " + document.getElementById("panel1-body").style.display + " 2) " + document.getElementById("panel2-body").style.display + " 3) " + document.getElementById("panel3-body").style.display + " 4) " + document.getElementById("panel4-body").style.display);

			if( getSliding() == 1 ){
					return false;
			}

			var eldown = getNextSibling(el);
			var elup = getNextSibling($('visible'));	
			
			setSliding( 1 );
			
			//alert($('visible').className);			
			//elup.style.background = "url(img/nymag_shop_on.gif) no-repeat";
			
			new Effect.Parallel(
			[
					new Effect.SlideUp(elup),
					new Effect.SlideDown(eldown)
			], {
					duration: 0.2,
					afterFinish: function() { 					
					setSliding( 0 );
					}
			});
			
			
			
			$('visible').id = '';
			
			
	}
	el.id = 'visible';

}


// Setup accordian initial state
function init() {
		
		var bodyPanels = document.getElementsByClassName('panel_body');
		var panels = document.getElementsByClassName('panel');
		var noPanels = panels.length;
		var percentageWidth = 100 / noPanels;
		var position = 0;
		

    for (i = 0; i < bodyPanels.length; i++) {
			bodyPanels[i].hide();
			panels[i].style.display = 'block'; 	
			Event.observe(panels[i].getElementsByTagName('h3')[0], 'mouseover', accordion, false);
			Event.observe(panels[i].getElementsByTagName('h3')[0], 'mousemove', accordion, false);
			
			position += percentageWidth;
    }
		

    var vis = $('visible').parentNode.id+'-body';
    $(vis).show();
	
	
	
}


function getNextSibling(startBrother){
	
  endBrother=startBrother.nextSibling;
  while(endBrother.nodeType!=1){
    endBrother = endBrother.nextSibling;
  }
  return endBrother;  

  
}


Event.observe(window, 'load', init, false);



