var pmwGoo = new Class({

	options: {
		photoArray: [],
		photoNum: 0,
		counter: 0,
		currentheight: 0,
		currentwidth: 0,
		currentcaption: "",
		speed: 500,
		transition_busy: 0
	},

	initialize: function(el, options) {
		this.setOptions(options);
		this.start(el);
	},

	start: function(el){
		
		this.options.className = el;
		
		this.options.photoNum = this.options.photoArray.length;
		this.options.currentheight = this.options.photoArray[this.options.counter][2];
		this.options.currentwidth = this.options.photoArray[this.options.counter][1];
		this.options.currentcaption = this.options.photoArray[this.options.counter][3];
		
		this.options.transition_busy = 1;
		this.resizeContatinerHeight = new Fx.Style($(this.options.imageContainer), 'height', {'duration': this.options.speed, 'wait': false, onComplete:function() {}});

		this.switchPhoto();
		this.options.transition_busy = 0;
		
		this.resizeContatinerWidth = new Fx.Style($(this.options.container), 'width', {'duration': this.options.speed, 'wait': false});
		this.options.transition_busy = 0;
		this.nextPhoto(1);
	},

	prevPhoto: function(number) {
		if (this.options.transition_busy == 0) {
			if (number == null) {this.options.counter --} else {this.options.counter = (number-1)};
			if (this.options.counter < 0) { this.options.counter = this.options.photoNum-1 };
			this.loadPhoto();
		}
	},

	nextPhoto: function(number) {
		if (this.options.transition_busy == 0) {
			if (number == null) {this.options.counter ++} else {this.options.counter = (number-1)};
			if (this.options.counter == this.options.photoNum) { this.options.counter = 0 };
			this.loadPhoto();
		}
	},

	loadPhoto: function() {
		
		if($(this.options.imageNavigation)){
			this.navigation();
		}

		this.hide_elements();
		$(this.options.imageLoading).style.display = 'inline';
		
		imgPreloader = new Image();

		new Fx.Style(this.options.imageImage, 'opacity').set(0);
		this.resize();
		imgPreloader.src = this.options.photoArray[this.options.counter][0];
		this.switchPhoto();
		
		$(this.options.imageLoading).style.display = 'none';
	},

	resize: function() {
		currentwidth = $(this.options.imageImage).width;
		currentheight = $(this.options.imageImage).height;
		this.resizeContatinerHeight.start(currentheight,this.options.photoArray[this.options.counter][2]);
		this.resizeContatinerWidth.start(currentwidth,this.options.photoArray[this.options.counter][1]);
	},

	hide_elements: function() {
		if ($(this.options.imageCaption)){
			$(this.options.imageCaption).innerHTML = '&nbsp;';
		}
		$(this.options.imagePrev).style.display = 'none';
		$(this.options.imageNext).style.display = 'none';
	},
	
	switchPhoto: function() {
		//alert(this.options.counter);
		$(this.options.imageImage).src    = this.options.photoArray[this.options.counter][0];
		$(this.options.imageImage).alt    = this.options.photoArray[this.options.counter][3];
		$(this.options.imageImage).width  = this.options.photoArray[this.options.counter][1];
		$(this.options.imageImage).height = this.options.photoArray[this.options.counter][2];
		$(this.options.imageImage).name   = this.options.counter;
		if($(this.options.imageCaption)){
			this.caption();
		}
		this.show_elements();
	},

	show_elements: function() {
		this.fade_in(this.options.imageImage,this.options.speed);
		if($(this.options.imageCaption)){
			this.fade_in(this.options.imageCaption,this.options.speed);
		}
	},

	show_nav_overlay: function() {
		$(this.options.imagePrev).style.display = 'inline';
		$(this.options.imageNext).style.display = 'inline';
	},

	navigation: function(){
		$(this.options.imageNavigation).innerHTML = '';
		var i=0
		for (i=0;i < this.options.photoNum;i++) {
			var content = $(this.options.imageNavigation).innerHTML;
			
			if(i==this.options.counter) $(this.options.imageNavigation).innerHTML = content + ' <span id="'+this.options.imageSelected+'">'+(i+1)+'</span> ';
			else $(this.options.imageNavigation).innerHTML = content + ' <a href=\'#' +(i+1)+ '\' onclick=\"'+this.options.className+'.nextPhoto('+(i+1)+');\">'+(i+1)+'</a> ';

			if(i < this.options.photoNum-1) {
				var content = $(this.options.imageNavigation).innerHTML;
				$(this.options.imageNavigation).innerHTML = content;
			}
		}
		if (this.options.counter == this.options.photoNum-1) $(this.options.imageNext).href = '#1';
		else $(this.options.imageNext).href = '#' + (this.options.counter+2);
		
		if (this.options.counter == 0) $(this.options.imagePrev).href = '#' + this.options.photoNum;
		else $(this.options.imagePrev).href = '#' + (this.options.counter);
	},
	
	caption: function(){
		if(this.options.photoArray[this.options.counter][3]=='') $(this.options.imageCaption).innerHTML = '&nbsp;';
		else $(this.options.imageCaption).innerHTML = this.options.photoArray[this.options.counter][3];
	},

	fade_in: function(element,speed) {
		new Fx.Style(element, 'opacity', {'duration': this.options.speed, 'wait': false, onComplete:function() {}}).start(0,1);
		this.show_nav_overlay();
	}
});

pmwGoo.implement(new Events, new Options);