if (typeof Z24 == 'undefined') {
	var Z24 = {}
}

Z24.imageSlideshow = {
	
	images : [],
	imageCount : 0,
	currentImage: 0,
	adRefreshFrequency: 10, /* 0 = aldrig, 10 alltid */
	
	init : function() {
		var container = $("#slideshowcontainer").get(0);
		var buttonPrevious = $("#arrow_prev").get(0);
		var buttonNext = $("#arrow_next").get(0);
		
		
		if (!container || !buttonPrevious || !buttonNext) return;
		if (Z24.imageSlideshow.imageCount < 1) return;
		
		buttonPrevious.onclick = function() { Z24.imageSlideshow.back(); return false; }
		buttonNext.onclick = function() { Z24.imageSlideshow.forward(); return false; }
	},
	
	add : function(imageURL, imageDescription, imgByline) {
		var image = [imageURL, imageDescription, imgByline];
		this.images.push(image);
		this.imageCount++;
	},
	
	adRefresh : function(frequency) {
		this.adRefreshFrequency = frequency;
	},
	
	forward : function() {
		this.currentImage++;
		if (this.currentImage >= this.imageCount) this.currentImage = 0;
		Z24.imageSlideshow.update();
	},
	
	back : function() {
		this.currentImage--;
		if (this.currentImage < 0) this.currentImage = (this.imageCount -1);
		Z24.imageSlideshow.update();
	},
	
	update : function() {
		var iframe = $("#imageframe").get(0);
		var adframe = $("#adframe").get(0);
		var description = $("#description").get(0);
		var count = $("#counter").get(0);
		var byline = $("#byline").get(0);
		var imageID = this.currentImage +1;
		var randomNumber = Math.round((Math.random()*9)+1);
		var url = iframe.src;		
			url = url.split('imageUrl=');
			url = url[0];
			url+= 'imageUrl=';
			url+= this.images[this.currentImage][0];
		var adurl = adframe.src;
		var countContent = 'Image ';
			countContent += imageID;
			countContent += "/";
			countContent += this.imageCount;
			
		iframe.src = url;
		if (this.adRefreshFrequency >= randomNumber) adframe.src = adurl;
		description.innerHTML = this.images[this.currentImage][1];
		byline.innerHTML = this.images[this.currentImage][2];
		count.innerHTML = countContent;
	}
}

$(document).ready(function() {
	Z24.imageSlideshow.init();
});
