// JavaScript Document
var Slideshow = new Class({

  Implements: [Options, Events],
	
	options: {
		toggles: '',
		slides: '',
		start: 0,
		toggleOff: 1,
		toggleOn: 0.01,
		slideOn: 1,
		slideOff: 0,
		duration: 500,
		wait: 5000,
		autoStart: true
	},
	
	initialize: function(container, options){
		
		this.container = document.id(container);
		
		this.setOptions(options);
		
		this.toggles = this.container.getElements(this.options.toggles);
		this.slides = this.container.getElements(this.options.slides);
		this.count = this.slides.length;
		
		this.isStill = true;
		
		this.toggleFx = new Array();
		
		this.toggles.each(function(toggle, index){
		  var anchor = toggle.getElement('a');
		  this.toggleFx[index] = new Fx.Tween(anchor, { property: 'opacity', duration: 100, link: 'cancel' });
			this.toggleFx[index].set(index == 0 ? this.options.toggleOn : this.options.toggleOff)
			anchor.addEvent('click', function(event){
			  event.preventDefault();
				this.stop();
				this.show(index);
				if (!this.isStill) this.start();
			}.bind(this));
		}, this);
		
		this.current = this.options.start;
		
		this.slideFx = new Array();
		this.slides.each(function(slide, index){
		  this.slideFx[index] = new Fx.Tween(slide, { property: 'opacity', duration: this.options.duration, link: 'cancel' }).set(index == this.current ? this.options.slideOn : this.options.slideOff);
		}, this);
		
		this.container.addEvents({
			'mouseenter': function(){
				if (!this.isStill) this.stop();
			}.bind(this),
			'mouseleave': function(){
			  if (this.isStill) this.start();
			}.bind(this)
		});
		this.container.addClass('ready');
		
		if (this.options.autoStart) this.start();
		
		
	},
	
	start: function(){
		this.isStill = false;
		this.repeater = this.show.periodical(this.options.wait, this);
	},
	
	stop: function(){
		this.isStill = true;
		$clear(this.repeater);
	},
	
	show: function(index){
		if (index == undefined) index = (this.current+1)%this.count;
		this.slideFx[this.current].start(this.options.slideOff);
		this.slideFx[index].start(this.options.slideOn);
		this.toggleFx[this.current].start(this.options.toggleOff);
		this.toggleFx[index].start(this.options.toggleOn);
		this.current = index;
	},
	
	toElement: function(){
		return this.container;
	}

});

var Giant = {
	
	init: function(){
		this.container = document.id('giant');
		this.img = this.container.getElement('img');
		this.link = this.container.getElement('a');
		
		this.img.set({
		  'styles': {
				'cursor': 'pointer'
			},
			'events': {
				'click': function(event){ window.location = this.link.get('href'); }.bind(this)
			}
		});
	}
};

var Main = {

  init: function(){
		
		// Slideshow
		if (document.id('slideshow')){
			new Slideshow('slideshow', {
				toggles: '#slideshow-tabs li',
				slides: '.slide'
			});
		}
		
		// Homepage overlay 
		if (document.id('graphic-overlay')){ Main.GraphicOverlay.init(); }
		if (document.id('giant')){ Giant.init(); }
		
		// Remooz
		ReMooz.assign('a.remooz', {
			'origin': 'img',
			'resizeFactor': 0.8, 
			'cutOut': false,
			'opacityResize': 0,
			'dragging': true,
			'centered': true
		});
		
		// link replacement
		$$('span.link').each(function(element){
		  var link = new Element('a', { 'href': element.get('title'), 'target': '_blank', 'text': element.get('text') });
			link.replaces(element);
		});
		
		// _blank
		$$('a._blank').set('target', '_blank');
		
		
	}
	
 };

Main.GraphicOverlay = {
	
	init: function(){
		this.container = document.id('graphic-overlay').inject('page-wrapper');
		this.content = document.id('graphic-overlay-content');
		this.content.hide().inject('page-wrapper').addClass('ready');
		
		this.container.addEvents({
		  'mouseenter': function(){
				this.content.show();
			}.bind(this),
			'mouseleave': function(){
				this.content.hide();
			}.bind(this)
		});
		
	}
 };





window.addEvent('domready', function(){

  Main.init();
	
});