﻿// by Brett Kromkamp, September 20, 2007

function Slideshow(slideshow, timeout) {
  this.slides = [];
  var nl = $(slideshow).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {
      this.slides.push(nl[i]);
    }
  }
  this.timeout = timeout;
  this.current = 0;

  for (var i = 0; i < this.slides.length; i++) {
    this.slides[i].style.zIndex = this.slides.length - i;
  }

  Element.show(slideshow);
  setTimeout((function() { this.next(); }).bind(this), this.timeout + 4000);
}

Slideshow.prototype = { 
  next: function() {
    for (var i = 0; i < this.slides.length; i++) {
      var slide = this.slides[(this.current + i) % this.slides.length];
      slide.style.zIndex = this.slides.length - i;
    }

    Effect.Fade(this.slides[this.current], {
      afterFinish: function(effect) {
        effect.element.style.zIndex = 0;
        Element.show(effect.element);
        Element.setOpacity(effect.element, 1);
      }
    });
    
    this.current = (this.current + 1) % this.slides.length;
    setTimeout((function() { this.next(); }).bind(this), this.timeout + 4000);
  }
}

// ======================================================================

function slideUp(el) {
	new Effect.Parallel(
	[
		new Effect.SlideUp(el, { sync: true }),
		new Effect.Fade(el, { sync: true })
	], {});
}

function slideDown(el) {		
	new Effect.Parallel(
	[
		Effect.SlideDown(el, { sync: true }),
		Effect.Appear(el, { sync: true })
	], {});
}

// ======================================================================

var timeArray = new Array();

function runMenu(name, height, direction) {
	var divNum = parseInt(name.substr(3, 1));
	if (direction == "down") {
		clearTimeout(timeArray[divNum]);
		timeArray[divNum] = setTimeout("openMenu('" + name + "','" + height + "','" + divNum + "')", 1);
	} else if (direction == "up") {
		clearTimeout(timeArray[divNum]);
		timeArray[divNum] = setTimeout("closeMenu('" + name + "','" + height + "','" + divNum + "')", 1);
	}
}

function openMenu(name, height, arrayNum) {
	obj = $(name);
	obj.style.overflow = "hidden";
	obj.style.zIndex = 2;
	
	var timer;
	var num = parseInt(arrayNum);
	var oHeight = parseInt(obj.style.height);

	if (parseInt(height) >= oHeight) {
		timeArray[num] = setTimeout("openMenu('" + name + "','" + height + "','" + num + "')", 1);
		var checkNum = parseInt(height) - oHeight;
		
		if (checkNum >= 25) {
			oHeight = oHeight + 7;
		} else if (checkNum <= 25){
			oHeight = oHeight + 1;
		}
	} 
	obj.style.height = oHeight + "px";
}

function closeMenu(name, height, arrayNum) {
	obj = $(name);
	obj.style.overflow = "hidden";
	obj.style.zIndex = 1;
	var timer;
	var num = parseInt(arrayNum);	
	var oHeight = parseInt(obj.style.height);
	
	if (parseInt(height) <= oHeight){
		timeArray[num] = setTimeout("closeMenu('" + name + "','" + height + "','" + num + "')", 1);
		var checkNum = oHeight - parseInt(height);
		
		if (checkNum >= 25){
			oHeight = oHeight - 7;
		} else if (checkNum <= 25) {
			oHeight = oHeight - 1;
		}
	} 
	obj.style.height = oHeight + "px";
}
