// SimpleSlideShow CLASS :: JanK_, Rendez
var SimpleSlideShow = new Class({
	
	Implements: Options,
	
	options: {
		fadeTime: 800,
		stayTime: 3000
	},
	
	initialize: function(wrapper, images, options) {
		this.setOptions(options);
		this.wrapper = $('main_image');
		this.images = images.map(function(image, index){
			return new Asset.image(image, {
				/*width   : '100%',
				height  : '100%',*/
				tween   : {duration: this.options.fadeTime},
				onload  : function(){
					this.images[index].store('loaded', true);
					if (index == 0) {
						this.topImage = this.images[0].inject(this.wrapper).setStyle('opacity', 0).get('tween', 'opacity', {duration: 250}).start(1);
						$('gallery_loading').retrieve('loading_fx').start(0);
					}
				}.bind(this),
				onerror : function(){ this.images.splice(index, 1); }.bind(this),
				onabort : function(){ this.images.splice(index, 1); }.bind(this)
			});
		}, this);
		this.index = 0;
		this.fade.periodical(this.options.stayTime + this.options.fadeTime, this);
	},
	
	fade: function() {
		if (this.images[this.index].retrieve('loaded') && this.images[(this.index + 1) % this.images.length].retrieve('loaded')) {
			if (Browser.Engine.webkit420) $('gallery_loading').retrieve('loading_fx').start(0); // fix Safari 3 cache issue
			this.index = (this.index + 1) % this.images.length;
			this.bottomImage = this.topImage;
			this.topImage = this.images[this.index]
				.fade('hide')
				.inject(this.wrapper)
				.fade('in');
		}
	}

});

// FADE IN IMAGES :: Rendez
window.addEvent('load', function() {
	// Fading in handling the tween of every image
	if ($('profile') || $('contact') || $('jobs')) $$('img').fade('in');
});

window.addEvent('domready', function(){
	
	// LOADER HOME :: Rendez
	var spinner = $('gallery_loading');
	if (spinner) {	
		spinner.store(
			'loading_fx',
			spinner.get('tween', 'opacity', { duration: 250 }))
		.set(0.8);
	}
	
	// MENU EFFECTS :: nfq
	var menu = $$('.menu li');
	menu.each(function(element) {

		var fx = new Fx.Styles(element, {duration:300, wait:false});
		fx.set({'background-color': '#F5F5F5'})
		element.addEvent('mouseenter', function(){
			fx.start({
				'background-color': '#DDD',
				'color': '#FFFFFF'
			});
		});

		element.addEvent('mouseleave', function(){
			fx.start({
				'background-color': '#F5F5F5',
				'color': '#FFFFFF'
			});
		});
	});
	
	// GALLERY CONTROLS :: nfq
	var links = $$('#gallery a');
	var gallery = new QD.Gallery(categories, {
		prevPage:  links[2],
		nextPage:  links[3],
		prevImage: links[0],
		nextImage: links[1],
		initialize: function(){
			this.caption = $('gallery_caption');
			this.loadingFx = $('gallery_loading').get('tween', 'opacity', { wait: false, duration: 250 }).set(0.8);
		
			this.playing = false;
			this.playingDiv = $('gallery_play').addEvent('click', function(){
				this.playing = !this.playing;
				this[(this.playing ? 'start' : 'stop') + 'Slideshow']();
				this.playingDiv[(this.playing ? 'add' : 'remove') + 'Class']('pause');
			}.bind(this));
		
			$('gallery').show();
		
			if(Browser.Engine.trident) $$('a').addEvent('focus', function(){ this.hideFocus = true; });
		}
	});

	gallery.addEvents({
		onImageLoading: function(){
			this.loadingFx.start(0.7);
		},
		onImageLoaded: function(imageData){
			this.caption.set('html', imageData.caption);
			this.loadingFx.start(0);
		}
	});
	
});

