var pptGallery = new Class({
  Implements: [Options],

	options: {
		container:        null,
		imageTarget:      null,
		clickTargets:     null,
		images:           null,
		repeat:           true,
		initOnFirstClick: false,
		preloadImages:    true
	},

	currentIndex:  0,
	currentPage:   0,
	previousIndex: 0,
	setupDone:     false,
	scroller:      null,

	thumbWidth:    120,
	pageWidth:     720,
	thumbsPerPage:   6,
	maxPages:        3,

	initialize: function(options) {
		this.setOptions(options);
		if (this.options.container && this.options.clickTargets && this.options.images && this.options.imageTarget) {
			if (this.options.preloadImages) {
				new Asset.images(this.options.images);
			}
			this.options.clickTargets.each(function(galleryItem, index){
				galleryItem.addEvent('click', this.imageClick.bindWithEvent(this, [index, galleryItem]));
			}.bind(this));
			if (!this.options.initOnFirstClick) {
				this.finalizeSetup();
				this.manageControls();
				this.showImage();
			}
			return true;
		} else {
			return false;
		}
	},

	// this method adds the next and previous buttons into the DOM
	finalizeSetup: function() {
//		this.options.container.empty();
		this.prevButton = new Element('div', {
			'class': 'gallery_prev_button',
			'events': {
				//'click': this.prevImage.bind(this)
				'click': this.prevPage.bind(this)
			}
		}).inject(this.options.container);
		this.nextButton = new Element('div', {
			'class': 'gallery_next_button',
			'events': {
				//'click': this.nextImage.bind(this)
				'click': this.nextPage.bind(this)
			}
		}).inject(this.options.container);

		this.scroller = new Fx.Tween(this.options.container.getElement('ul'), {link: 'cancel'});

		this.setupDone = true;
	},

	// controls the visibility of the next and previous buttons
	manageControls: function() {
//		if (this.options.repeat) {
//			return;
//		}
//		if (this.currentIndex > 0) {
//			this.prevButton.setStyle('visibility', 'visible');
//		} else {
//			this.prevButton.setStyle('visibility', 'hidden');
//		}
//		if (this.currentIndex < this.options.images.length - 1) {
//			this.nextButton.setStyle('visibility', 'visible');
//		} else {
//			this.nextButton.setStyle('visibility', 'hidden');
//		}
		if (this.currentPage > 0) {
			this.prevButton.setStyle('visibility', 'visible');
		} else {
			this.prevButton.setStyle('visibility', 'hidden');
		}
		if (this.currentPage < this.maxPages - 1) {
			this.nextButton.setStyle('visibility', 'visible');
		} else {
			this.nextButton.setStyle('visibility', 'hidden');
		}
	},

	showImage: function() {
		if (!this.setupDone) {
			this.finalizeSetup();
		}
//		this.manageControls();
		this.options.imageTarget.setStyle('background-image', 'url(' + this.options.images[this.currentIndex] + ')');
		this.options.clickTargets[this.previousIndex].removeClass('active_image');
		this.options.clickTargets[this.currentIndex].addClass('active_image');

//		var myFx = new Fx.Scroll(window).toElement(this.options.container);
		//this.scroller.start('left', -(this.currentIndex * 120));
		//console.log(this.options.clickTargets[this.currentIndex], this.currentIndex, this.previousIndex);
		var page = Math.floor(this.currentIndex / this.thumbsPerPage);
		this.gotoPage(page);

	},

	imageClick: function(evt, index, galleryItem) {
		evt.preventDefault();
		this.previousIndex = this.currentIndex;
		this.currentIndex = index;
		this.showImage();
	},

	nextImage: function() {
		this.previousIndex = this.currentIndex;
		this.currentIndex++;
		if (this.currentIndex >= this.options.images.length) {
			this.currentIndex = this.options.repeat ? 0 : this.options.images.length - 1;
		}
		this.showImage();
	},

	prevImage: function() {
		this.previousIndex = this.currentIndex;
		this.currentIndex--;
		if (this.currentIndex < 0) {
			this.currentIndex = this.options.repeat ? this.options.images.length - 1 : 0;
		}
		this.showImage();
	},

	nextPage: function() {
		this.currentPage++;
		if (this.currentPage > this.maxPages - 1) {
			this.currentPage = this.maxPages - 1;
		}
		this.showPage();
	},

	prevPage: function() {
		this.currentPage--;
		if (this.currentPage < 0) {
			this.currentPage = 0;
		}
		this.showPage();
	},

	gotoPage: function(index) {
		if (index < 0) {
			index = 0;
		} else if (index >= this.maxPages) {
			index = this.maxPages -1;
		}

		this.currentPage = index;
		this.showPage();
	},

	showPage: function() {
		this.scroller.start('left', -(this.currentPage * this.thumbsPerPage * this.thumbWidth));
		this.manageControls();
	}

});

window.addEvent('domready', function(){
	this.gallery = new pptGallery({
		container:    $('gallery_container'),
		imageTarget:  $('main'),
		clickTargets: $$('#gallery_container ul a'),
		images:       $$('#gallery_container ul a').get('href'),
		repeat:       false
	});
});
