/*
  Image popup function
*/
function popupImage(path, width, height)
{
  if (height > window.screen.height)
  {
    height = window.screen.height;
    // Make room for vertical scrollbar.
    width += 25;
  }
  width = (width > window.screen.width) ? window.screen.width : width;
  window.open(path, "popupImage", "width=" + width + ",height=" + height + ",status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1");
  return false;
}

/*
	Class for displaying section sub navigation.
*/
var Nav = Class.create();

Nav.prototype =
{
	initialize: function(section, subNav)
	{
		/* Get the section and sub nav DOM elements */
		this.section = $(section);
		this.subNav = $(subNav);
		this.active = !Element.Methods.hasClassName(this.section, "selected");

		/* Don't bother with behaviors if the section is already selected */
		if (this.active)
		{
			this.section.onmouseover = this.mouseover.bindAsEventListener(this);
			this.section.onmouseout = this.mouseout.bindAsEventListener(this);
			this.subNav.onmouseover = this.mouseover.bindAsEventListener(this);
			this.subNav.onmouseout = this.mouseout.bindAsEventListener(this);
		}
	},

	/* Display the sub nav on mouseover */
	mouseover: function(e)
	{
		this.show();
	},

	/* Hide the sub nav on mouseout */
	/* Use a timeout to allow the user to hover from the section to the sub nav and back */
	mouseout: function(e)
	{
		this.timer = setTimeout(this.hide.bind(this), 500);
	},

	/* Hides the sub nav */
	hide: function()
	{
		this.subNav.style.visibility = "hidden";
	},

	/* Shows the sub nav. Cancels any attempt to hide it */
	show: function()
	{
		this.subNav.style.visibility = "visible";
		clearTimeout(this.timer);
	}
}

/*
  Class for case study galleries.
*/

var CaseStudyGallery = Class.create();

CaseStudyGallery.prototype =
{
  initialize: function(linkClass, linkSelectedClass, galleryClass, galleryShownClass)
  {
    this.linkSelectedClass = linkSelectedClass;
    this.galleryShownClass = galleryShownClass;
  
    this.links = $$('.' + linkClass);
    this.galleries = $$('.' + galleryClass);
    
    this.selectedIndex = 0;
    for (var i = 0; i < this.links.length; i++)
    {
      var currLink = this.links[i];
      currLink.onclick = this.click.bindAsEventListener(this, i);
      if (currLink.hasClassName(this.linkSelectedClass))
      {
        this.selectedIndex = i;
      }
    }
  },
  
  click: function(e, index)
  {
    this.showGallery(index);
  },
  
  showGallery: function(index)
  {
    this.links[this.selectedIndex].removeClassName(this.linkSelectedClass);
    this.galleries[this.selectedIndex].removeClassName(this.galleryShownClass);

    this.links[index].addClassName(this.linkSelectedClass);
    this.galleries[index].addClassName(this.galleryShownClass);

    this.selectedIndex = index;
  }
}