var picasaView = Class.create({
	
	/* poorly commented, I know :(
	*/
	numberOfImages: 0,
	startIndex: 1,
	imagesPerPage: 20,
	imagesPreLoaded: 0,
	daddy: null,
	id: null,

	initialize: function(id, count) {
	
		if(!$('picasaViewBlock-' + id + '-' + this.startIndex)) {
			return;
		}
		
		this.id = id;	
		this.daddy = 'picasaViewBlock-daddy-' + this.id;
		
		this.options = Object.extend({
					nextPage: $$('#' + this.daddy + ' .picasaViewNextPage > p'),
					previousPage: $$('#' + this.daddy + ' .picasaViewPreviousPage > p'),
					showingResults: $$('#' + this.daddy + ' .picasaViewShowingResults'),
					totalResults: $$('#' + this.daddy + ' .picasaViewTotalResults')
				}, arguments[0] || { });

		this.imagesPerPage = $('picasaViewBlock-' + id + '-' + this.startIndex).select('img').size();
		this.numberOfImages = this.options.totalResults[0].innerHTML;

		if(this.numberOfImages <= this.imagesPerPage) {
			this.options.nextPage.invoke('hide');
		}

		this.options.previousPage.invoke('hide');

		this.options.nextPage.each(function(s) {
			s.down('a').observe('click', this.nextPage.bind(this));
		}.bind(this));

		this.options.previousPage.each(function(s) {
			s.down('a').observe('click', this.previousPage.bind(this));
		}.bind(this));
		
		this.adjustBlockHeight();
	},

	nextPage: function(e) {
		e.stop();
		
		this.adjustBlockHeight();

		$('picasaViewBlock-' + this.id + '-' + this.startIndex).fade({duration: 0.3, queue: { position: 'end', scope: 'picasaview' }});

		this.startIndex += this.imagesPerPage;

		endIndex = this.startIndex + this.imagesPerPage - 1;
		if(endIndex >= this.numberOfImages) {
			endIndex = this.numberOfImages;
			this.options.nextPage.invoke('hide');
		}

		this.options.showingResults.invoke('update', this.startIndex + '-' + endIndex);
		this.options.previousPage.invoke('show');

		// show images on next page
		$('picasaViewBlock-' + this.id + '-' + this.startIndex).appear({duration: 0.6, afterFinish: this.adjustBlockHeight.bind(this), queue: { position: 'end', scope: 'picasaview' }});
		return false;

	},

	previousPage: function(e) {
		e.stop();

		this.adjustBlockHeight();
		
		$('picasaViewBlock-' + this.id + '-' + this.startIndex).fade({duration: 0.3, queue: { position: 'end', scope: 'picasaview' }});

		this.startIndex -= this.imagesPerPage;

		endIndex = this.startIndex + this.imagesPerPage - 1;
		if(this.startIndex <= 1) {
			this.startIndex = 1;
			this.options.previousPage.invoke('hide');
		}

		this.options.showingResults.invoke('update', this.startIndex + '-' + endIndex);
		this.options.nextPage.invoke('show');

		// show images on previous page
		$('picasaViewBlock-' + this.id + '-' + this.startIndex).appear({duration: 0.6, afterFinish: this.adjustBlockHeight.bind(this), queue: { position: 'end', scope: 'picasaview' }});
	
		return false;
	},

	adjustBlockHeight: function() {
		// adjust size of parent container to avoid flickering during fade/appear effects
		var inner = $('picasaViewBlock-' + this.id + '-' + this.startIndex);
		
		var son = $(this.daddy).down('.picasaViewBlock-son');
		
		son.setStyle({minHeight: inner.getHeight() + 'px'});
		
		
	},

	checkThumbnailCropping: function(e) {
		if($F('picasaView_thumbnailSize') >= 200) {
			$('picasaView_cropThumbnails').checked = false;
			$('picasaView_cropThumbnails').disabled = true;
		} else {
			$('picasaView_cropThumbnails').disabled = false;
		}
	}

});

document.observe("dom:loaded", function() {
	var i = 0;
	$$('.picasaViewBlock-daddy').each(function(s) {
		new picasaView(s.readAttribute('id').replace('picasaViewBlock-daddy-', ''), i++);
	});
	
	if($('picasaView_thumbnailSize')) {
		myPicasaView = new picasaView();
	}
});