// Bind events
window.addEvent('domready', function(){
	var slideNewsletter = new Fx.Slide('newsopen', {duration: 200}).hide();
	$('newsopen').setStyle('display', 'block');

	$('newsslide').addEvent('click', function(e) {
		e = new Event(e);
		slideNewsletter.toggle();
		e.stop();
	});
	$$('div#newsopen a.close').addEvent('click', function(e) {
		e = new Event(e);
		slideNewsletter.toggle();
		e.stop();
	});
});

// The Rotator -- Original ideas by Merix.
var Rotator = new Class({
	disabled : false,
	spinner : null,
	active : null,
	holder : null,
	rotator : null,
	newPath : null,
	base : null,

	initialize: function(project, base) {
		// Get some handles on the situation
		this.active = $('rotactive');
		this.rotator = $('rotator');

		this.base = base;

		// Setup the holder
		this.holder = this.active.clone(true);
		this.holder.setAttribute('id','rotholder');
		this.rotator.appendChild(this.holder);
		var hideHolder = new Fx.Style(this.holder,'opacity').set(0);

		// Setup the spinner
		this.spinner = new Element('div', {'id' : 'rotspinner'});
		this.rotator.appendChild(this.spinner);
		var hideSpinner = new Fx.Style(this.spinner,'opacity').set(0);

		// Bind events to the pager
		$$('ul#cyclenav li.one a').addEvent('click', function(e){
			new Event(e).stop();
			this.switchImage(project, 'one', '1');
		}.bind(this));
		$$('ul#cyclenav li.two a').addEvent('click', function(e){
			new Event(e).stop();
			this.switchImage(project, 'two', '2');
		}.bind(this));
		$$('ul#cyclenav li.three a').addEvent('click', function(e){
			new Event(e).stop();
			this.switchImage(project, 'three', '3');
		}.bind(this));
	},

	switchImage: function(project, s, i) {
		// Avoid problems with quick trigger fingers
		if(this.disabled) {
			return;
		}
		this.disabled = true;

		// Setup the path
		var newPath = this.base + "/images/featured/rotator/" + project + i + ".jpg";

		// Setup the onload script
		var self = this;
		var onLoad = function(event) {
			// Need a copy of the image
			self.holder.src = newPath;

			// Remove highlight from selected number
			$$( 'ul#cyclenav li a' ).removeClass( 'active' );
			// Add highlight to newly selected number
			$$( 'ul#cyclenav li.' + s + ' a' ).addClass( 'active' );

			// Crossfade in the holder
			var fade = new Fx.Style(self.holder, 'opacity', {
				onComplete : function() {
					var hideSpinner = new Fx.Style(self.spinner, 'opacity').set(0);
					self.active.src = newPath;
					fade.set(0);
					self.disabled = false;
				}
			}).start(0, 1);
		}

		// Attempt to load the new image
		var img = new Image();
		img.onload = onLoad;
		img.src = newPath;

		// The load wasn't instant, so bring in the spinner
		if(img.complete === false) {
			var showSpinner = new Fx.Style(this.spinner, 'opacity', {duration: 200}).start(0, 1);
		}
	}
});

function startRotator(project, base) {
	var myRotator = new Rotator(project, base);
}
