/**
 * Find position method
 */
function findPos(id)
{
	var obj = document.getElementById(id);

	var curleft = 0;
	var curtop = 0;
	if(obj.offsetParent)
	  while(1) 
	  {
	  curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
		  break;
		obj = obj.offsetParent;
	  }
	else if(obj.x){
	curleft += obj.x;
		curtop  += obj.y;
	}

		curleft = curleft + 90;
		return [curleft,curtop];
}

/**
 * Slideshow v1.3
 * Author: Johan Arensman
 * Usage:
 * var mySlideshow = new Slideshow(myelement, { setting: mysetting });
 */
var Slideshow = Class.create({
							// Settings which can be updated using the settings parameter:
	interval: 10,			// - time between animations
	duration: 1.5,			// - duration of the fade-in or out animation
	random: false,			// - calculate next image randomly or linear
	syncFade: false,		// - will the fade-in occur after the fade-out (true) or during the fade-out (false)
	hiddenClassName: 'hide',// - the classname which hides the image before the script is loaded

	element: null,
	images: $A(),

	timer: null,
	busy: false,
	current: 0,
	scope: 'defaultscope',

	initialize: function(id, settings) {
		this.element = $(id);
		if(!this.element) {
			alert('Could not find element: '+id);
		}
		this.interval = (settings.interval) ? settings.interval : 3;
		this.syncFade = settings.syncFade;
		this.random  = settings.random;
		this.scope = this.element.identify()+'scope';
		this.images = this.element.select('img');

		if(this.images.size() > 1) {
			// Absolute position the image
			this.element.select('img').each(function(img) {
				img.setStyle({visibility: 'hidden'});
				img.removeClassName(this.hiddenClassName);
				(Prototype.Browser.IE) ? img.absolutize() : img.setStyle({position:'absolute'});
				img.setStyle({visibility: 'visible'});
				img.hide();
			}.bind(this));
			// show the first image
			this.images[0].show();
			// start the show
			this.start();
		}
	},

	start: function() {
		this.timer = new PeriodicalExecuter(this.next.bind(this), this.interval);
	},

	next: function(timer) {
		if(this.busy) { return; }
		// fade out:
		this.busy = true;
		new Effect.Fade(this.images[this.current], {
			duration: this.duration,
			afterFinish: (this.syncFade) ? this.fadein.bind(this) : Prototype.K
		});
		if(!this.syncFade) {
			this.fadein();
		}
	},

	fadein: function() {
		// calculate next image
		if(this.random) {
			oldcurrent = this.current;
			do {
				this.current = Math.floor(Math.random()*(this.images.length));
			} while(this.current == oldcurrent);
		} else {
			this.current = (this.images[this.current+1]) ? this.current+1 : 0;
		}

		// fade in:
		new Effect.Appear(this.images[this.current], {
			duration: this.duration,
			afterFinish: function() {
				this.busy = false;
			}.bind(this)
		});
	},

	stop: function() {
		this.timer.stop();
	}

});

Event.observe(window, 'load', function() {
	if(window.location.href.indexOf('noanimation') > 0) { return false; }
	var HeaderSlideshow = new Slideshow('header', {
		interval: 5,
		random: true
	});
});

