/*
YAY !!! YOU'RE CAUGHT CHECKING MY CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Hello I also use this plateform to entertain my Front End Coding habilities ;) 
You'll find here 
 ** how to Simulate a position Fix under IE6,
 ** how to handle with First Letter Issues
 ** how to create a few simple stuff with Mootools library. 
 It's not always uber clean, but it seems to work pretty well !!! 
 
 Remember that IT'S OKAY TO COPY BUT USE YOUR ROCKIN' BRAIN MAN */
	  
var Preloader = new Class({

	initialize: function() {
		this._currentlyLoading = '';
		this._loading = false;
		this._imgQueue = [];
		this._loadEvents = {};
		this._loadedImages = {};
	},
	
	addToQueue: function(src) {
		if (this.isLoaded(src) || this.isInQueue(src)) return;
		this._imgQueue.push(src);
		if (!this._loading) this._loadNext();
		return this;
	},

	addToFrontOfQueue: function(src) {
		if (this.isLoaded(src)) return false;
		if (this._currentlyLoading == src) return true;
		this.removeFromQueue(src);
		this._imgQueue.unshift(src);
		if (!this._loading) this._loadNext();
		return true;
	},

	removeFromQueue: function(src) {
		this._imgQueue.remove(src)
		return this;
	},
	
	addEventOnLoad: function(src, fn) {
		this._loadEvents[src] = fn;
		return this;
	},
	
	flushQueue: function() {
		this._imgQueue = [];
		return this;
	},
	
	stopAllEvents: function() {
		this._loadEvents = {};
		return this;
	},
	
	isLoaded: function(src) {
		return !!this._loadedImages[src];
	},
	
	isInQueue: function(src) {
		return (this._currentlyLoading == src || this._imgQueue.contains(src));
	},
	
	priorityLoadWithCallback: function(src, fn) {
		this.addEventOnLoad(src, fn);
		if (!this.addToFrontOfQueue(src)){
			this._fireLoadEvent(src); // Already loaded
			return true;
		}
		return false;
	},
	
	_fireLoadEvent: function(src) {
		if (this._loadEvents[src]) this._loadEvents[src].call(this._loadedImages[src]);
		this._loadEvents[src] = null;
	},
	
	_loadNext: function() {
		if (this._imgQueue.length == 0) {
			this._currentlyLoading = '';
			return this._loading = false;
		}
		this._loading = true;
		this._currentlyLoading = this._imgQueue.shift();
		var img = new Element('img');
		var preloader = this;
		img.addEvent('load', function() {
			preloader._loadedImages[this.src] = this;
			preloader._currentlyLoading = '';
			preloader._fireLoadEvent(this.src);
			setTimeout(function() {
				preloader._loadNext();
			}, 0); //setTimeout for Opera; stops this hogging the javascript thread.
			this.removeEvent('load', arguments.callee);
		});
		img.src = this._currentlyLoading;
	}

});

window.addEvent('domready', function(){
	new SmoothScroll(); 
	links = $$('.myArchivesList');
	toggles = $$('.detailArchives');
	
	togs = {};
	links.each(function(el, idx) {
		togs[idx] = new Fx.Styles(toggles[idx], {duration:100, wait:false});
		el.addEvent('mouseenter', function(){ togs[idx].start({ 'top': 0 }); });
		el.addEvent('mouseleave', function(){ togs[idx].start({ 'top': 156 }); });
	});	

	var p = new Preloader();

	$$('.preloadImage img').each(function(img) {
		p.addEventOnLoad(img.src, function() {
			img.getParent().setStyle('background', 'none');
			img.setStyle('opacity', 0)
			img.style.display = 'block';
			img.effect('opacity').start(0,1);
		});
		p.addToQueue(img.src);
	});
	});
