//////////////////////////////////////////////////////////
//
//  File: Global Javascript
//  Author: Craig Nelson
//
//

  // external links
  function setLinks(anchors) {
    anchors.each(function (e) {
      if (e.getAttribute("rel") == "external") {
        e.target = "_blank";
      }
    });
    return;
  }
  // --external links
  
  // header slideshows
	function setSlideShow(slides, time) {
	  var currentSlide = 0;
  	var nextSlide = 1;
  	var slideShow = function () {
  	  if (slides.length === 0) { // does the current page have a slideshow?
  	    return;
  	  }
  	  Effect.Fade(slides[currentSlide], { duration: .5 });
      Effect.Appear(slides[nextSlide], { duration: .5 });
      currentSlide++;
      nextSlide++;
      if (nextSlide == slides.length) {
        nextSlide = 0;
      }
      if (currentSlide == slides.length) {
        currentSlide = 0;
      }
      setTimeout(function () {
        slideShow();
      }, time);
  	};
  	return slideShow;
	}
	// --header slideshows
	
	// gallery slideshows
  var currSlide = 1;
  var moveInProgress;
  
  function move(element, slides) {
    var strip = $('gallery-slideshow');
    var pixelsToMove;
    
    if (element.hasClassName("next")) {
      if (currSlide == slides || moveInProgress == true) {
        return;
      }
      new Effect.Move(strip, {
        x: -488,
        y: 0,
        duration: .4,
        transition: Effect.Transitions.easing,
        beforeStart: function () {
          moveInProgress = true;
        },
        afterFinish: function () {
          currSlide++;
          moveInProgress = false;
        }
      });
    }
    
    if (element.hasClassName("back")) {
      if (currSlide == 1 || moveInProgress == true) {
        return;
      }
      new Effect.Move(strip, {
        x: 488,
        y: 0,
        duration: .4,
        transition: Effect.Transitions.easing,
        beforeStart: function () {
          moveInProgress = true;
        },
        afterFinish: function () {
          currSlide--;
          moveInProgress = false;
        }
      });
    }
  }
  // --gallery slideshows
	
	// events
  Event.observe(window, "load", function () {
    // external links
    setLinks($$("a"));
    
    // header slideshows
    setTimeout(function () {
      setSlideShow($$("#header-slideshow .slide"), 7000)();
    }, 7000);
    
    // gallery slideshows
    var gallerySlideshow = $('gallery-slideshow');
    if (gallerySlideshow) { // check to see if we are on a page that has a gallery slideshow
      var gallerySlides = $$('#gallery-slideshow li');
      var slides = Math.floor(gallerySlides.length / 7);
      var overflowSlides = (gallerySlides.length % 7);
      if (overflowSlides < 7 && overflowSlides != 0) {
        slides++;
      }
      var controls = $$('li.control');
      Effect.Transitions.easing = function (pos) {
        return (pos==1) ? 1 : -Math.pow(2, -10 * pos) + 1;
      }
      controls.each(function (e) {
        if (slides > 1) {
          e.style.display = "block";
        }
        Event.observe(e, 'click', function (event) {
          this.blur();
          move(e, slides);
        });
      });
    }
  }); // window load
  // --events