function PopupMenu(popup_id, anchor_id)
{
	this.anchorID = anchor_id;
	this.popupID = popup_id;
	this.onMenu = false;
	
	this.anchor_hovered = function(ev)
	{
		this.onMenu = true;
		this.show_menu();
	};
	this.anchor_exited = function(ev)
	{		
		var relTarg = $(ev.relatedTarget || ev.toElement);
		if (relTarg.descendantOf($(this.anchorID)) ||  relTarg.identify() == this.popupID)
			return;
		
		this.onMenu = false;
		setTimeout(this.deferred_hide_menu.bind(this), 150);
	}
	
	this.menu_hovered = function(ev)
	{
		this.onMenu = true;
	};
	this.menu_exited = function(ev)
	{
		var relTarg = $(ev.relatedTarget || ev.toElement);
		if (relTarg.descendantOf($(this.popupID)) ||  relTarg.identify() == this.anchorID)
			return;
			
		this.hide_menu();
	};
	
	
	this.show_menu = function()
	{
		if (!$($(this.anchorID).parentNode).hasClassName("selected"))
		{
			$($(this.anchorID).parentNode).addClassName("selected");
			$($(this.anchorID).parentNode).addClassName("temporarily");
		}
		
		Effect.Appear(this.popupID, {duration: 0.2});
	};
	
	this.deferred_hide_menu = function() 
	{
		if (this.onMenu)
			return;
			
		this.hide_menu();
	};
	
	this.hide_menu = function() 
	{
		if ($($(this.anchorID).parentNode).hasClassName("temporarily"))
			$($(this.anchorID).parentNode).removeClassName("selected");
		
		this.onMenu = false;
		
		Effect.Fade(this.popupID, {duration: 0.15});
	};
	
	$(this.popupID).style.display = "none";
	
	Event.observe(this.anchorID, "mouseover", this.anchor_hovered.bind(this));
	Event.observe(this.anchorID, "mouseout", this.anchor_exited.bind(this));
	Event.observe(this.anchorID, "mouseenter", this.anchor_hovered.bind(this));
	
	Event.observe(this.popupID, "mouseout", this.menu_exited.bind(this));
	Event.observe(this.popupID, "mouseover", this.menu_hovered.bind(this));
}

function PhotoGallery(gallery_id, velocityPixels) 
{
	this.velocity = velocityPixels ? velocityPixels : 5;
	this.speed = 1000/35; // updates per second
	this.timeout = null;
	this.galleryID = gallery_id;
	this.itemsDiv = null;
		
	this.scroll_right = function()
	{
		this.advance_scroll_position(this.velocity);	
		this.timeout = setTimeout(this.scroll_right.bind(this),this.speed);
	};
	
	this.scroll_left = function()
	{
		this.advance_scroll_position(-this.velocity);	
		this.timeout = setTimeout(this.scroll_left.bind(this),this.speed);
	};
	
	this.jump_right = function()
	{
		this.advance_scroll_position(this.itemsContainer().getWidth()*0.75);
	}
	this.jump_left = function()
	{
		this.advance_scroll_position(-this.itemsContainer().getWidth()*0.75);
	}
	
	this.clear_scroll = function()
	{
		clearTimeout(this.timeout);
		this.timeout = null;
	};
	
	this.advance_scroll_position = function(positionDelta)
	{
		var el = this.itemsContainer();
		el.scrollLeft = Math.min(el.scrollLeft + positionDelta, el.scrollWidth);
	};
	
	this.itemsContainer = function()
	{
		return $$("#"+this.galleryID + " .content").first();
	}
	
	this.init = function()
	{
		var descendants = $(this.galleryID).descendants();
		for (var i = 0; i < descendants.size(); i++)
		{
			if (!(el = descendants[i]) || (el.tagName != "A"))
				continue;
			
			if ($(el.parentNode).hasClassName("arrow"))
				Event.observe(el, "mouseout", this.clear_scroll.bind(this));
			
			if ($(el.parentNode).hasClassName("right"))
			{
				Event.observe(el, "click", this.jump_right.bind(this));
				Event.observe(el, "mouseover", this.scroll_right.bind(this));
			}
			else if ($(el.parentNode).hasClassName("left"))
			{
				Event.observe(el, "click", this.jump_left.bind(this));
				Event.observe(el, "mouseover", this.scroll_left.bind(this));
			}
		}
	}
	
	this.init();
}

function FAQController(faq_container)
{
	this.containerID = faq_container;
	
	this.init = function() 
	{
		$(this.containerID).descendants().each(function(el)
		{
			if (el.hasClassName("question"))
			{
				el.observe("click", function(ev){ Effect.toggle(ev.element().next(".answer"), 'appear', {duration: 0.4}) });
			}
			else if (el.hasClassName("answer"))
			{			
				el.style.display = "none";
				el.addClassName("narration");
			}			
		});
		
	}
	
	this.init();
}