// JavaScript Document
// Observer for initialising galleries
document.observe("dom:loaded",function() {
	
	try {
    	if(galleryArray0) {
			var imageGallery = new Gallery(0,'0',galleryArray0);
		}
	} catch(e) {
      variablename=[];
	}
});

var Gallery = Class.create({
	initialize: function(id, defaultImage,json) {
		this.currentImage = 0;
		this.currentGallery = json.galleryImages[json.galleryDefaults.usedGalleryName];
		
		
		setTimeout(this.slideShow(),10000);
	},
	
	changeGalleryType: function(event,element) {
		this.currentGallery = galleryName;
		this.changeImage();
	},
	
	changeImage: function() {
		this.currentImage += 1;
		
		if(this.currentImage != this.currentGallery.length) {
			if(this.currentImage == (this.currentGallery.length+1)) { this.currentImage = 0; }
			imageObject = new Image();
			imageObject.src = this.currentGallery[this.currentImage]['image'];
			
			if(this.currentGallery[this.currentImage]['description']) {
				$('background-description').appear({duration:1.5});
				$('background-description').innerHTML = this.currentGallery[this.currentImage]['description'];
				
				if(this.currentGallery[this.currentImage]['link']) {
					$('background-description').innerHTML += "<hr /><a href=\""+this.currentGallery[this.currentImage]['link']+"\" />"+this.currentGallery[this.currentImage]['viewDetailsLabel']+"</a>";
				}
				
				$('background-description').innerHTML = "<div>"+$('background-description').innerHTML+"</div>";
				
			} else {
				if($('background-description').getStyle("opacity") == 1) {
					$('background-description').fade({duration:2.1});
					//$('background-description').innerHTML = "";
				}
			}
			
			imageObject.onload = function(e) {
				$('background-overlay').setStyle({backgroundImage:"url("+imageObject.src+")"});
				$('background-overlay').appear({duration:1.5});
				
				setTimeout("$('background').setStyle({backgroundImage:\"url("+imageObject.src+")\"})",2000);
				setTimeout("$('background-overlay').hide()",2100);				
			}
		}
	},
	
	slideShow: function() {		
		new PeriodicalExecuter(this.changeImage.bind(this),5);			
	}
});

