// Class to Show Mini Preview
var Previewer = Class.create();
Previewer.prototype = {
	initialize: function(parent, images) {
		// Setup Properties if needed
		if(images.length > 1){
			var p = this;
			this.parent = parent;
			this.images = images;
			this.pos = 0;
			this.hide_images();
			this.controls = parent.select('.controls').first();
			this.prev_link = this.controls.select('.prev').first();
			this.next_link = this.controls.select('.next').first();
			this.update_controls();
			this.controls.appear();
			this.count_text = this.controls.select('.count_text').first();
		}
	},
	
	hide_images: function(){
		this.images.each(function(image){image.addClassName('hide_preview');});
		this.images.first().removeClassName('hide_preview');
	},
	
	// Display Next Image
	show_next: function(){
		this.hide_current();
		this.pos++;
		this.show_current();
	},
	
	// Display Prev Image
	show_prev: function(){
		this.hide_current();
		this.pos--;
		this.show_current();
	},
	
	// Updates controls based on available images existence
	// If the next or previous image exists, then attach the behaviour
	// Else remove the functionality
	// This is run after every preview attempt.
	update_controls: function(){
			this.update_count_text();
			Event.observe(this.prev_link, 'click', this.click_prev.bindAsEventListener(this), false);
			Event.observe(this.next_link, 'click', this.click_next.bindAsEventListener(this), false);
	},
	
	click_prev: function(event){
		if ( this.images[this.pos-1] ){ this.show_prev(); } 
		Event.stop(event);
	},
	
	click_next: function(event){
		if ( this.images[this.pos+1] ){ this.show_next(); } 
		Event.stop(event);
	},
	
	update_count_text: function(){
		var current_num = this.pos + 1;
		var total_num = this.images.length;
		this.controls.select('.count_text').first().innerHTML = current_num + " of " + total_num;
	},
	
	current: function(){
		return this.images[this.pos];
	},
	
	// Hide current - Animate?
	hide_current: function(){
		// this.current().hide();
		this.current().addClassName('hide_preview');
	},
	
	// Show current - Animate?	
	show_current: function(){
		this.update_count_text();
		var self = this;
		// alert(this.current().style.display)
		// setTimeout(function(){self.current().style.display = 'block';self.current().style.border = '1px solid red';self.current().style.height = '150px';}, 10)
		this.current().removeClassName('hide_preview');
	}
	
	
}