// JavaScript Document

var Status = new Class({

  status: {},
	
	is: function(key){
		return !!this.status[key];
	},
	
	isNot: function(key){
		return !this.is(key);
	},
	
	setStatus: function(key, value){
		this.status[key] = value;
	},
	
	getStatus: function(key){
		return this.status[key];
	}

});

var Touax = {
	
	init: function(){
		
		if (document.id('reference-image')) new Touax.Gallery();
		
		if (document.id('block-news-tools')) Touax.News.Tools.init();
	}
	
 };

Touax.Gallery = new Class({
													
	Implements: [Options, Status, Events],
	
	options: {
		observeDuration: 100,
		path: '/images/references/pictures/'
	},
	
	status: {
	},
	
	initialize: function(options){
		
		this.setOptions(options);
		
		this.container = document.id('reference-image');
		this.caption = this.container.getElement('p.description');
		this.image = this.container.getElement('div.image');
		this.wrapper = this.container.getElement('div.image-wrapper');
		this.maxWidth = this.wrapper.getSize().x.toInt();
		
		this.resizeFx = new Fx.Tween(this.image, { property: 'height', duration: 200, link: 'chain' });
		this.fadeFx = new Fx.Tween(this.image, { property: 'opacity', duration: 200, link: 'chain' });
		this.captionFx = new Fx.Tween(this.caption, { property: 'opacity', duration: 200, link: 'chain' });
	
		this.preload = this.container.getElement('img');
		this.resizeFx.set(this.preload.height);
  
	  this.items = document.id('reference-thumbnails').getElements('li');
		this.count = this.items.length;
		if (this.count < 2) return;
		this.index = 0;
		
		this.cache = new Hash();
		
		this.previous = new Element('div', {
		  'class': 'overlay prev',
			'events': {
				'click': function(){
					this.move('prev');
				}.bind(this)
			}
		}).fade('hide').inject(this.wrapper);
		
		this.next = new Element('div', {
		  'class': 'overlay next',
			'events': {
				'click': function(){
					this.move('next');
				}.bind(this)
			}
		}).fade('hide').inject(this.wrapper);
		
		this.wrapper.addEvents({
		  'mouseenter': function(){
				this.previous.fade(0.7);
				this.next.fade(0.7);
			}.bind(this),
		  'mouseleave': function(){
				this.previous.fade('out');
				this.next.fade('out');
			}.bind(this)
		});
		
		this.captions = new Array();
		this.items.each(function(item, index){
			var anchor = item.getElement('a');
			var img = item.getElement('img');
			var src = img.get('src').replace('_thumb', '');
			var caption = img.get('alt');
			anchor.addEvent('click', function(event){
			  event.stop();
				this.setActive(anchor.getParent('li'));
				this.show(src, caption, index);
				anchor.blur();
			}.bind(this));
		}, this);

	},
	
	show: function(src, caption, index){
		this.fadeFx.start(0);
		if (this.preload){
			this.preload.onload = null;
		}
		if (this.cache.has(src)){
			this.index = index;
			this.preload = this.cache.get(src);
			this.animate(caption, src);
		} else {
			this.preload = new Image();
			this.preload.src = src;
			this.preload.onload = function(){
				this.index = index;
				this.animate(caption, src);
			}.bind(this)
		}
	},
	
	move: function(dir){
	  var dir = (dir ? dir : 'next');
		var index = 0;
		switch (dir){
			case 'next':
			  index = (this.index + 1)%this.count;
			break;
			case 'prev':
			  index = this.index == 0 ? this.count - 1 : (this.index - 1)%this.count;
			break;
		}
		var img = this.items[index].getElement('img');
		var src = img.get('src').replace('_thumb', '');
		var caption = img.get('alt');
		this.show(src, caption, index);
		this.setActive(this.items[index]);
	},
	
	animate: function(caption, src){
		if (!this.cache.has(src)){
			this.cache.set(src, this.preload);
		}
		this.captionFx.start(0);
		this.resizeFx.start(this.preload.height).chain(
		  function(){
				this.image.empty();
				this.image.adopt(this.preload);
				this.fadeFx.start(1);
				var padding = 10 + ((this.maxWidth-this.preload.width)/2).toInt();
				this.caption.setStyles({'padding-left': padding, 'padding-right': padding});
				this.caption.set('html', caption ? caption : '&nbsp;');
				this.captionFx.start(1);
			}.bind(this)
		);
	},
	
	setActive: function(activeItem){
		this.items.each(function(item){
		  item == activeItem ? item.addClass('active') : item.removeClass('active');
		});
	}
	
});

Touax.News = {}

Touax.News.Tools = {
	
	init: function(){
		
		this.container = document.id('block-news-tools');
		
		this.body = document.id('body');
		
		this.textSize = {
			inc: this.container.getElement('li.font-size a.inc'),
			dec: this.container.getElement('li.font-size a.dec')
		}
		
		this.textSize.inc.addEvent('click', function(event){
		  event.preventDefault();
			if (!this.textSize.inc.hasClass('disabled')){
				this.body.addClass('large-text');
				this.textSize.inc.addClass('disabled');
				this.textSize.dec.removeClass('disabled');
			}
		}.bind(this));
		
		this.textSize.dec.addEvent('click', function(event){
		  event.preventDefault();
			if (!this.textSize.dec.hasClass('disabled')){
				this.body.removeClass('large-text');
				this.textSize.inc.removeClass('disabled');
				this.textSize.dec.addClass('disabled');
			}
		}.bind(this));
	}
};


window.addEvent('domready', function(){

  Touax.init();

});
