/*
Author:
	Ryan Page
License:
	MIT License
 
Class
	gallery (v1)

Requires:
	mootools 1.2
*/
var gallery = new Class({
	
	Implements: Options,
	options: {
		loaderImg: '/_images/ajax-loader.gif'
	},
	
	initialize: function(container, options){
		this.setOptions(options);
		this.container = $(container);
		this.imageContainer = this.container.getElement('.fullimg');
		this.imageContainer.set('morph', {duration: '800'});
		
		this.imageContainer.addEvent('click', function(){ 
			this.showLargeImage();
		}.bind(this));
		
		this.links = this.container.getElements('.item');
		
		this.images = new Asset.images(this.options.images, {
			onComplete: function() {
				this.setLinks();
			}.bind(this)
		});

		this.showImage(0, true);          	
	},
	
	setLinks: function() {
		this.links.each(function(el, index){
			el.addEvent('click', function(e){ 
				e.stop();
				this.showImage(index); 
			}.bind(this));
			
		}.bind(this));    		
	},
	
	showImage: function(index, first) {
		if (this.active != index || first) {
			if (first) { this.active = 0; }
			
			this.links[this.active].setStyle('margin', '0');
		
			this.imageContainer.morph({display : 'none', opacity : [1, 0]});
			this.imageContainer.empty();
			
			var image = new Element('img', {
				'src': this.images[index].src, 
				'styles': {
					'visibility': 'visible'
				}
			}).inject(this.imageContainer);
			
			this.imageContainer.morph({display : 'block', opacity : [0, 1]});
			this.active = index; 
			
			this.links[this.active].setStyles({
				margin: '-22px 0px 0px 0px'
			});
			
			this.current = index;
		}
	},
	
	showLargeImage: function(){
		//alert(this.images[this.current].src);
		var largeImage = this.images[this.current].src.replace('.jpg', '_full.jpg');
		window.open(largeImage, '_blank', 'status=0, toolbar=0, location=0, menubar=0, resizable=0, height=500, width=600, scrollbars=0');
	},
	
	showLoader: function() {
	
	},
	
	hideLoader: function() {
	
	}

});