Array.prototype.random = function(){
	var rand = Math.floor(this.length * Math.random());
	return this[rand];
}

var Switcher = Class.create({
	initialize: function(options) {
		
		this.image_array = $A(options.image_array);
		this.container = $(options.container);
		this.images = this.image_array.pluck('src');
		this.urls = this.image_array.pluck('url');
		this.alts = this.image_array.pluck('alt');
		this.frequency = options.frequency || 5;
		this.fadeDuration = options.fadeDuration || 2; // seconds
		this.nextrand = (options.nextrand || false);
		this.started = false;
		this.soort = options.soort; // logos etc.
		this.groot = options.groot; // voor doorklikken

		this.initialized = false;
		// create html in container
		if (options.init) {
			this.initHTML();
		}
		if (options.start) {
			this.start();
		}
	},
	
	start: function() {
		if (!this.initialized) {
			this.initHTML();
		}
		if (this.image_array.length > 1) {
			if (!this.started) new PeriodicalExecuter(this.switchplaatje.bind(this), this.frequency);
			$('buttons').show();
		} else {
			$('buttons').hide();
		}
		this.started = true;
		current = this.soort;
	},
	
	stop: function() {
		this.initialized = false;
		this.started = false;
	},

	basename: function (path, suffix) {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: Ash Searle (http://hexmen.com/blog/)
		// +   improved by: Lincoln Ramsay
		// +   improved by: djmix
		// *     example 1: basename('/www/site/home.htm', '.htm');
		// *     returns 1: 'home'
	 
		var b = path.replace(/^.*[\/\\]/g, '');
		if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
			b = b.substr(0, b.length-suffix.length);
		}
		return b;
	},

	switchplaatje: function(pe) {
		if (this.started) this.blendImage(this.getNextImage());
		else pe.stop();
	},
	
	next: function() {
		if (this.started) this.stop();
		this.nextIndex();
		this.blendImage(this.getImage(), 0.5);
	},

	prev: function() {
		if (this.started) this.stop();
		this.prevIndex();
		this.blendImage(this.getImage(), 0.5);
	},
	
	getImage: function() {
		return this.image_array[this.imageIndex];
	},
	
	nextIndex: function() {
		if (this.imageIndex < this.image_array.length-1) {
			this.imageIndex++;
		} else {
			this.imageIndex = 0;
		}
		return this.imageIndex;
	},
	
	prevIndex: function() {
		if (this.imageIndex > 0) {
			this.imageIndex--;
		} else {
			this.imageIndex = this.image_array.length-1;
		}
		return this.imageIndex;
	},

	getNextImage : function() {
		if (this.nextrand) {
			var found = false;
			while (!found) {
				var randImg = this.image_array.random();
				if (randImg.src != this.basename(this.plaatje.src)) {
					found = true;
				}
			}
			return randImg;
		} else {
			this.nextIndex();
			return this.getImage();
		}
	},

	blendImage: function(image, fadeDuration) {
		this.container.display = "none";
		this.container.style.backgroundImage = "url(" + this.plaatje.src + ")";
		setTimeout(this.blendImageReal.bind(this, image, fadeDuration),1000)
	},

	blendImageReal: function(image, fadeDuration) {
		var speed = Math.round((fadeDuration||this.fadeDuration)*1000 / 100);
		this.changeOpac(0);

		//make new image
		this.plaatje.src = image.src;
		
		this.plaatje.style.bottom = 'auto';
		this.plaatje.style.right = 'auto';

		this.container.display = "block";
		this.plaatje.display = "block";

		//fade in image
		var timer = 0;
		for(i = 0; i <= 100; i++) {
			// setTimeout("changeOpac(" + i + ",'" + this.plaatje.id + "')",(timer * speed));
			setTimeout(this.changeOpac.bind(this, i),(timer * speed));
			timer++;
		}

		if (image.alt) {
			this.plaatje.alt = image.alt;
			this.plaatje.title = image.alt;
		}
		if (image.url) this.plaatje.up().href = image.url;
		
		this.setAltAndClick();

	},
	
	setAltAndClick: function() {
		if (this.groot) {
			// var num = this.imageIndex+1;
			this.altTekst = $H(teksten.get(this.soort)).get(this.imageIndex+1);
			this.plaatje.alt = this.plaatje.title = this.altTekst;
			this.plaatje.className = 'click';
			$(this.plaatje).observe('click', (function(event) {
				Lightview.show({
					href: 'img/'+this.soort+'-groot/'+(this.imageIndex+1)+'-groot.jpg',
					caption: this.altTekst,
					options: {
					  width: 640,
					  height: 480,
					  topclose: false
					}
				});
				this.stop();
			}).bind(this));
		}
	},

	changeOpac: function(opacity) {
		var style = this.plaatje.style;
		style.opacity = (opacity / 100);
		if (Prototype.Browser.Gecko) style.MozOpacity = (opacity / 100);
		if (navigator.userAgent.indexOf('KHTML') ) style.KhtmlOpacity = (opacity / 100);
		if (Prototype.Browser.IE) style.filter = "alpha(opacity=" + opacity + ")";
	},
	
	initHTML: function() {
		if (this.initialized) return;
		var disp = this.container.display;
		this.container.display = "none";
		this.imageIndex = 0;
		var imgStr = '<img src="'+this.image_array[this.imageIndex].src+'" id="'+this.container.id+'_image" alt="'+this.image_array[this.imageIndex].alt+'" title="'+this.image_array[this.imageIndex].alt+'" border="0">';
		if (this.image_array[this.imageIndex].url) imgStr = '<a href="#">'+imgStr+'</a>';
		this.container.update(imgStr);
		this.plaatje = $(this.container.id+'_image');
		if (this.image_array[this.imageIndex].url) this.plaatje.up().href = this.image_array[this.imageIndex].url;
		this.container.style.backgroundImage = "url(" + this.plaatje.src + ")";
		this.container.display = disp;
		this.initialized = true;
		this.setAltAndClick();
	}
	
});

