//addLoadEvent(readLinks);
addLoadEvent(readImgContainers);

function addLoadEvent(func) {
	var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
			oldonload();
			func();
		}
	}
}

function readLinks(){
	var bodyTag = document.getElementsByTagName("body");
	for(i=0; i<bodyTag.length; i++){
		bodyTag[i].onLoad= function(){
			readImgContainers();
			return false;
		}
	}
}

/*******************************************************
*						*						*
*	kører læsningen af "containers", 		*
*	igennem og initialisere slideshow objektet	*
*	HUSK af lave et array med billeder,		*
*	det skal hedde billedArray, eller ryger	*
*	dynamiken, hvis det skal kører dynamisk	*
*						*						*
********************************************************/

function readImgContainers(){
	//kigger efter className for hvor mange "containers" der kører billeder
	var container = document.getElementsByClassName("dias");

	preLoad = new Array();
	containerElement = new Array();
		
	//læser igennem billederne og cacher billederne samt smider dem i billedArray
	for(i=0; i<container.length; i++){
		containerElement[i] = $('img'+i);
	}
	var slideshow = new rotateImg(billedArray, containerElement, linkArray);
}

var thisObj;

var rotateImg = Class.create();
rotateImg.prototype = {
	
	initialize: function(imgArray, imgContainer, linkArray){
		thisObj = this;
		this.imgArray = imgArray;
		this.imgContainer = imgContainer;
		this.imgRotate = 0;
		this.elementIncrement = imgContainer.length-1;
		this.imgLength = imgArray.length;
		this.elementLength = imgContainer.length;
		this.preLoaded = this.preLoad();
		this.linkArray = linkArray;
		
		this.imgCircle();
	}, 
	
	imgCircle: function(){
		if(this.imgRotate < this.elementLength){
			var troggleImg = this.imgContainer;
			this.imgLink(this.imgRotate,troggleImg[this.imgRotate].parentNode);
			
			new Effect.Appear(
				troggleImg[this.imgRotate].parentNode.parentNode.id, 
				{
					duration:2.0, 
					queue:'front',
					scope:'makeImg', 
					afterFinish:this.continueCircle
				}
			);
		}else{
			var queue = Effect.Queues.get('makeImg');
				queue.each(function(e) { e.cancel() });
			this.imgRotate = 0;
			this.runCarousel();
		}
	}, 
	
	continueCircle: function(){
		thisObj.imgRotate+=1;
		thisObj.imgCircle();
	}, 
	
	runCarousel: function(){
		if(this.imgRotate < this.elementLength){
			
			new Effect.Fade(
			this.imgContainer[this.imgRotate].parentNode.parentNode.id,
				{
					duration:2.0,
					queue:
						{
							position:'end', 
							scope:'slideshowRotate', 
							limit:2
						},
					afterFinish:this.incrementCarousel
				}
			);
		}else{
			this.imgRotate = 0;
			var queue = Effect.Queues.get('slideshowRotate');
			    queue.each(function(e) { e.cancel() });
			this.runCarousel();
		}
	},
	
	incrementCarousel: function(){
		var step=thisObj.elementIncrement+=1;
		if(thisObj.elementIncrement >= thisObj.imgLength){
			var step=thisObj.elementIncrement=0;
		}
		thisObj.imgLink(step,thisObj.imgContainer[thisObj.imgRotate].parentNode);
		thisObj.imgContainer[thisObj.imgRotate].src = thisObj.preLoaded[step].src;
		
		new Effect.Appear(
			thisObj.imgContainer[thisObj.imgRotate].parentNode.parentNode.id,
				{
					duration:2.0,
					queue:
						{
							position:'end', 
							scope:'slideshowRotate', 
							limit:2
						}
				}
			);
			
		thisObj.imgRotate+=1;
		thisObj.runCarousel();
	},

	preLoad: function(){
		var cache = new Array();
		for(i=0; i<this.imgLength; i++){
			cache[i] = new Image();
			cache[i].src = this.imgArray[i];
		}
		return cache;
	},
	
	imgLink: function(arrayPlace, url){
		var a = this.linkArray[arrayPlace];
		url.href=a;
	}, 
	
	test: function(test){
		alert(test);
		return false;
	}
}
