window.addEvent('domready', function() {
	$$('.pg-sw').each(function(wrapper) {
		if(!wrapper.hasClass('pg-init')) new pgSlideshowWide(wrapper);
		wrapper.addClass('pg-init');
	});
});

var pgSlideshowWide = new Class({

	Implements: [Options],

	options: {
		showThumbnails: false,
		showCaption: false,
		showController: false,
		showChrome: false,
		delay: 5000 //ms
	},

	initialize: function(el, options) {
		//options
		this.setOptions(options);
		
		//element references
		this.wrapper = document.id(el);
		this.pg_outerWrapper = document.id('pg_outerWrapper');
		this.fullsizeWrapper = this.wrapper.getElement('.pg-sw-images');
		this.thumbWrapper = this.wrapper.getElement('.pg-sw-thumbnails');
		
		//-----
		
		this.maxImageWidth = 0;
		this.maxImageHeight = 0;
		
		//determine max image size to make sure all fit ok
		this.fullsizeWrapper.getElements('img').each(function(image) {
			var w = image.getSize().x;
			if(w > this.maxImageWidth) {
				this.maxImageWidth = w;
			}
			var h = image.getSize().y;
			if(h > this.maxImageHeight) {
				this.maxImageHeight = h;
			}
		}.bind(this));
		//adjust dimensions
		this.wrapper.setStyles({
			width: this.maxImageWidth + 'px',
			height: (this.maxImageHeight) + 'px'
		});
		this.fullsizeWrapper.setStyles({
			width: this.maxImageWidth + 'px',
			height: this.maxImageHeight + 'px'
		});
		if(this.pg_outerWrapper) {
			this.pg_outerWrapper.setStyle('width', this.wrapper.getSize().x + 'px');
		}

		//--
		
		//remove thumbnails
		if(this.thumbWrapper && !this.options.showThumbnails) {
			this.thumbWrapper.setStyle('display', 'none');
			this.wrapper.setStyle('height', this.maxImageHeight + 'px');
		}
		
		//remove chrome (this also disables the caption)
		if(!this.options.showChrome) {
			this.options.showCaption = false;
			this.wrapper.setStyle('border', '0');
		}
		
		//-----
		
		//slideshow
		this.slideshow = new Slideshow(this.wrapper, false, {
			controller: this.options.showController,
			thumbnails: this.options.showThumbnails,
			captions: this.options.showCaption,
			delay: this.options.delay,
			duration: 500,
			center: true,
			overlap: false,
			resize: false,
			classes: ['pg-sw'],
			titles: false,
			preload: true
		});
		
		//-----
		
		//adjust caption
		if(this.options.showCaption && !this.options.showThumbnails) {
			this.captionWrapper = this.wrapper.getElement('.pg-sw-captions');
			if(this.captionWrapper) {
				this.captionWrapper.setStyle('bottom', '0');
			}
		}
		
		//adjust controller
		if(this.options.showController) {
			this.controllerWrapper = this.wrapper.getElement('.pg-sw-controller');
			if(this.controllerWrapper) {
				this.controllerWrapper.setStyle('margin-top', '-' + ((this.options.showThumbnails ? 40 : 0) + 21) + 'px');
			}
		}
		
	}

});
