var album = Class.create({
	options: {
		captions: false,
		center: true,
		classes: [],
		controller: false,
		delay: 5,
		duration: 2,
		fast: false,
		folder: '',
		height: false,
		href: '',
		linked: false,
		loader: {'animate': ['css/loader-#.png', 12]},
		loop: true,
		paused: false,
		thumbreplace: [/\.(.{3})$/, 't.$1'],
		resize: 'width',
		slide: 0,
		spacer: '/images/s.gif',
		thumbnails: false,
		transition: function(p){return -(Math.cos(Math.PI * p) - 1) / 2;},
		width: false
	},
	
	initialize: function(data,options) {  
		Object.extend(this.options, options || { }); //overload default options
		this.Container = $$('.slideshow-images');
		this.Thumbnails = $$('.slideshow-thumbnails');
		this.data = {'images': [], 'captions': [], 'hrefs': [], 'thumbnails': [], 'loaded': []};		
		//this.data = data;
		
		var count = 0;
		var tul = new Element('ul');

		//parse the data file	
		for (image in data){
				var obj = data[image] || {};
				var caption = (obj.caption) ? obj.caption : '';
				var href = (obj.href) ? obj.href : '';
				var thumbnail = (obj.thumbnail) ? obj.thumbnail : image.replace(this.options.thumbreplace[0], this.options.thumbreplace[1]);
				
				this.data.images.push(image);
				this.data.captions.push(caption);
				this.data.hrefs.push(href);
				this.data.thumbnails.push(thumbnail);
				//this.data.loaded.push(false);
					
			//create slides		
				/*var Eslide = new Element('div', { 'id': 'slide-'+count} );	
				var Eimage = new Element('img', { 'id': 'img-'+count, 'src': shim });
				var Elink = new Element('a', { 'id': 'link-'+count, href: href }).update(Eimage);
				var Ecaption = new Element('div', { 'class': 'caption'} ).update(caption);
				var EthumbImg = new Element('img', { 'src': this.folder + thumbnail, 'width': 50 });
				var Ethumb = new Element('a', { href: '#slide-'+count }).update(EthumbImg);*/
		
				var Ecaption = new Element('div', { 'class': 'caption'} ).update(caption);
				var Eimage = new Element('img', { 'id': 'img-'+count, 'src': this.options.spacer });
				var Elink = new Element('a', { 'id': 'link-'+count, href: href });
				Elink.appendChild(Ecaption);
				Elink.appendChild(Eimage);
				var Eslide = new Element('div', { 'id': 'slide-'+count} ).update(Elink);	
				//var EthumbImg = new Element('img', { 'src': this.options.folder + thumbnail, 'width': 50 });
				//var Ethumb = new Element('a', { href: '#slide-'+count }).update(EthumbImg);
				
				var li = new Element('li');
				//li.insert(Ethumb);
				
				//alert(thumbnail);
				//var a = new Element('div', { 'id': 'foo'+count, href: '/foo.html' }).update("Next page");
				//Eslide.appendChild(Elink);
				//Eslide.insert(Ecaption);
				//alert(Eslide);
				this.Container[0].appendChild(Eslide);
				
				tul.appendChild(li);
				count++;
		
		};
		
		if (this.options.thumbnails){
			this.Thumbnails[0].appendChild(tul);
		}
		this.SlideshowSize = count;
		this.startup();
	}, 

	startup: function() {  
		this.frame = 0;
		this.SlideshowAll = $('slideshow-images').childElements();
		this.SlideshowAll.each(Element.hide);
		
		//load the first image
		var Nimage = new Element('img', { 'id': 'img-'+this.frame, 'src': this.options.folder + this.data.images[this.frame] });
		//swap first image spacer with actual image
		$('img-'+this.frame).replace(Nimage);
		$('slide-'+this.frame).show();
		
		//$('slideshow-images').firstDescendant().show();
		this.SlideShowLength = this.SlideshowAll.length;
		new PeriodicalExecuter(this.cycle.bind(this), this.options.delay); // change image every 5 seconds 		
	}, 
	
	cycle: function() {
		current = (this.frame % (this.SlideShowLength-1));
		next = ((this.frame+1) % (this.SlideShowLength-1));
		
		currentSlide = $('slide-'+current);
		nextSlide = $('slide-'+next);
		
		//load the next image
		var Nimage = new Element('img', { 'id': 'img-'+next, 'src': this.options.folder + this.data.images[next] });
		//var Nlink = new Element('a', { href: this.data.hrefs[next] }).update(Nimage);
		
		//swap next image spacer with actual image
		$('img-'+next).replace(Nimage);
		
		//run the effects
		Effect.Fade(currentSlide, { duration: this.options.duration });
		Effect.Appear(nextSlide, { duration: this.options.duration });
		
		//advance the frame
		this.frame++;
	} 

});