Effect.Move2 = Class.create();
Object.extend(Object.extend(Effect.Move2.prototype, Effect.Base.prototype),
{
	initialize: function(element)
	{
		this.element = $(element);
		if (!this.element) throw(Effect._elementDoesNotExistError);
		var options = Object.extend({ "y": 0, "units": "px", "mode": "relative" }, arguments[1] || {});
		this.start(options);
	},
	setup: function()
	{
		this.element.makePositioned();
		this.originalTop = parseFloat(this.element.getStyle('top') || '0');

		if (this.options.mode == 'absolute')
		{
			this.options.y = this.options.y - this.originalTop;
		}
	},
	update: function(position)
	{
		this.element.setStyle({ "top": (Math.round(this.options.y * position + this.originalTop ) + this.options.units) });
	}
});

Effect.MoveBy2 = function(element, y, units)
{
	return new Effect.Move2(element, Object.extend({ "y": y, "units": units }, arguments[2] || {}));
};

var previous_menu_item = null;
var previous_item = null;
var popup_wrapper_height = null;
var first_run = true;
var anim_lock = false;

function animate_popup(current_menu_item, id)
{
	if (current_menu_item == previous_menu_item) return;
	if (anim_lock) return;
	anim_lock = true;
	// underline the current menu link
	//if (previous_menu_item) previous_menu_item.className = 'gn';
	previous_menu_item = current_menu_item;
	//current_menu_item.className = 'gna';
	// get the popup wrapper and its height
	var popup_wrapper = $('popup_wrapper');
	if (!popup_wrapper_height) popup_wrapper_height = Element.getHeight(popup_wrapper);
	// get the popup content and overflow
	var popup_content = $('popup');
	// hide the wrapper, and then bring it in again
	new Effect.Opacity(popup_wrapper,
	{
		"duration": (first_run ? 0.0 : 1.0),
		"from": 1.0,
		"to": 0.0,
		//"transition": Effect.Transitions.sinoidal,
		"afterFinish": function()
		{
			first_run = false;
			new Effect.Parallel(
				[
					// move the wrapper up by its own height (to show the whole thing)
					new Effect.MoveBy2(popup_wrapper, -popup_wrapper_height, 'px',
					{
						"sync": true
					}),
					// fade the wrapper in as it slides up
					new Effect.Opacity(popup_wrapper, { "sync": true, "from": 0.0, "to": 1.0 })
				],
				{
					//"transition": Effect.Transitions.sinoidal,
					"duration": 0.5,
					"beforeStart": function()
					{
						// get the current item from the id.href
						// hide the previous item
						// remember the current as previous
						// show the current item
						var current_item = $('popup_item_'+id);
						if (previous_item) previous_item.style.display = 'none';
						previous_item = current_item;
						current_item.style.display = 'block';
						// reset the top (i.e.: hide it)
						popup_wrapper.style.top = popup_wrapper_height+'px';
						// temporarily set overflow to hidden; otherwise, the zindex is wrong :\
						popup_content.style.overflow = 'hidden';
					},
					"afterFinish": function()
					{
						// reset the overflow once we're done
						popup_content.style.overflow = 'auto';
						anim_lock = false;
					}
				}
			);
		}
	});
}
