/***************************************
  Volunteer Centre Popup Script
****************************************
 This script is dependent on:
	prototype.js
	effects.js
***************************************/

var fAnimationDuration = 0.5;	// Duration of animations (in seconds)
var divCentre = null;		// stores current selected volunteer centre div
var divOpenedCentre = null;	// stores the current opened volunteer centre div
var divAnimating = false;	// used to determine if we are currently animating

// Show volunteer centre popup takes in: 
// el = the button element, centre = volunteer centre div to show
function showPopup(el, centre) {	

	// if we are not currently animating
	if (!divAnimating) {
	
		// populate divCentre variable
		divCentre = $('centre_' + centre);

		// if the selected centre is already open - close it
		if (divOpenedCentre == divCentre)
		{
			closePopup(false);
			return;
		}

		// get the position of the volunteer centre button
		var pos = findPosition(el);
		var iLeft = pos[0];
		var iTop = pos[1];

		// adjust the position of the volunteer centre div
		divCentre.style.top = (iTop - 250) + 'px';
		divCentre.style.left = iLeft + 'px'; 

		// if a volunteer centre div is already open, close it first
		if (divOpenedCentre) 
			closePopup(true);
		else
			openPopup();
	}
}

// does the actual openning of the popup
function openPopup() {
	divAnimating = true;
	new Effect.Appear(divCentre, { duration: fAnimationDuration, from:0, to:0.95, afterFinish: PopupOpened });
}

// does the actual closing of the popup, takes in:
// openNextPopup = should we open another popup after closing
function closePopup(openNextPopup) {
	divAnimating = true;
	new Effect.DropOut(divOpenedCentre, { duration: fAnimationDuration, afterFinish: PopupClosed(openNextPopup) });
}


// Called after open animation has finished
function PopupOpened() {
	divAnimating = false;
	divOpenedCentre = divCentre;
}

// Called after close animation has finished
function PopupClosed(a) {
	divOpenedCentre = null;
	divAnimating = false;
	if (a) openPopup();	
}

// routine to determine the actual position of an element
function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}
