var sliding = 0;
var slideTime = '';

// Set is sliding value
function setSliding(a_ISliding){
	sliding = a_ISliding;
}

// Get is sliding value
function getSliding(){
	return sliding;
}

// Carry out accordian styled effect
function accordion(evt) {
	el = Event.element(evt);
	var eldown = getNextSibling(el);
	
	//  If element is visible do nothing
	if ($('visible') == el) {
			resetTabs();
			return;
	}
	if ($('visible')) {
	
			if( getSliding() == 1 ){
					return false;
			}
		
			var elup = getNextSibling($('visible'));

			setSliding( 1 );
			
			parellelSlide( elup, eldown );
			$('visible').id = '';
			
	}
	else{
			setSliding( 1 );
			singleSlide( eldown );
	}
	
	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 = 390 / noPanels;
		var position = 0;
		
		//  Loop through body panels and panels applying required styles and adding event listeners
    for (i = 0; i < bodyPanels.length; i++) {
			bodyPanels[i].hide();
			//panels[i].style.width = percentageWidth + '%';
			//panels[i].style.position = 'absolute';
			//panels[i].style.left = ( position + 100 ) + 'px';
			
			Event.observe(panels[i].getElementsByTagName('h3')[0], 'click', accordion, false);
			//Event.observe(panels[i].getElementsByTagName('h3')[0], 'mousemove', accordion, false);
			Event.observe(document.body, 'mousemove', resetIdle, false);
			
			position += percentageWidth;
    }
		
		if( $('visible') ){
		//  Set panel with id of visible to be initial displayed
			var vis = $('visible').parentNode.id+'-body';
			parentNode.id;
			$(vis).show();
		}

		setIdle();
}

// Next sibling method to work around firefox issues
function getNextSibling(startBrother)
{
	var endBrother=startBrother.nextSibling;

	while( endBrother.nodeType != 1 )
	{
		endBrother = endBrother.nextSibling;
  	}
  	
  	return endBrother;
}

function getPrevSibling(startBrother)
{
	var endBrother = startBrother.previousSibling;

	while( endBrother.nodeType != 1 )
	{
		endBrother = endBrother.previousSibling;
  	}
  	
  	return endBrother;
}

function parellelSlide(elup, eldown)
{
		new Effect.Parallel(
		[
				new Effect.SlideUp(elup),
				new Effect.SlideDown(eldown)
		], {
				duration: 0.2,
				afterFinish: function() { setSliding( 0 );}
		});
		
		oSibling = getPrevSibling(eldown);
		oSibling.innerHTML = "[&ndash;]" + oSibling.innerHTML.substr(3);
		
		oSibling = getPrevSibling(elup);
		oSibling.innerHTML = "[+]" + oSibling.innerHTML.substr(3);
}

function singleSlide(eldown)
{
		new Effect.Parallel(
		[
				new Effect.SlideDown(eldown)
		], {
				duration: 0.2,
				afterFinish: function() { setSliding( 0 );}
		});
		
		oSibling = getPrevSibling(eldown);
		oSibling.innerHTML = "[&ndash;]" + oSibling.innerHTML.substr(3);
}

function resetTabs(){
	
	var resetEl = getNextSibling($('visible'));
	
	setSliding( 1 );
	
	new Effect.Parallel(
	[
			new Effect.SlideUp( resetEl )
	], {
			duration: 0.2,
			afterFinish: function() { setSliding( 0 );}
	});
	
	$('visible').id = '';
	oSibling = getPrevSibling(resetEl);
	oSibling.innerHTML = "[+]" + oSibling.innerHTML.substr(3);
}

function resetIdle(){
		if( $('visible') ){
				window.clearTimeout( slideTime );
				slideTime = window.setTimeout( "resetTabs()", 3000 );
		}
}

function setIdle(){
	if( $('visible') ){
		slideTime = window.setTimeout( "resetTabs()", 3000 );
	}
}

Event.observe(window, 'load', init, false);
