/*
  Simple slideshow using prototype and scriptaculous.
*/

function Shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function Slideshow(slideshow, timeout) {
  this.slides = [];
  this.slidescontainer = [];
	var nl = $(slideshow).getElementsByTagName('div');
  var zindex = 0;
	this.slideorder = [];
	for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {
			this.slideorder.push(zindex);
			this.slidescontainer.push(nl[i]);
			zindex++;
		}
	}
	this.slideorder = Shuffle(this.slideorder);
  
	var j = 0;
	var k = 0;
	
	j = this.slideorder.length;
	for (var i = 0; i < j; i++) {
			
			k = this.slideorder[i];
	    this.slides.push(this.slidescontainer[k]);
	}
	
  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 + 850);
}
Slideshow.prototype = {
  next: function() {
    for (var i = 0; i < this.slides.length-1; 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 + 850);
	
	// set active menu item
	urlIndex = this.slideorder[this.current]+1;
	this.myActiveItem = document.getElementById('decourl' + urlIndex).href;
	for(i=0;i<document.getElementById('mainnav').getElementsByTagName('a').length;i++) {
			
		if(document.getElementById('mainnav').getElementsByTagName('a')[i].href.toLowerCase() == this.myActiveItem.toLowerCase()) {
		document.getElementById('mainnav').getElementsByTagName('a')[i].id = "al";
		} else {
		document.getElementById('mainnav').getElementsByTagName('a')[i].id = "nl";
		}
	}
  }
 }

/*
Slideshow.prototype = {
	setActiveMenuItem: function(){
		
		this.myActiveItem = document.getElementById('decourl' + this.current).href;
	
		for(i=0;i<document.getElementById('mainnav').getElementsByTagName('a').length;i++) {
		 if(document.getElementById('mainnav').getElementsByTagName('a')[i].href.toLowerCase() == this.myActiveItem.toLowerCase()) {
			 document.getElementById('mainnav').getElementsByTagName('a')[i].className = "link active";
		 } else {
			 document.getElementById('mainnav').getElementsByTagName('a')[i].className = "link";
		 }	 
	
	}
}
*/
