﻿/*
     Created by Sean Matheson
     Used for the image gallery, because the downloaded crossfader script was breaking in some browsers.  And had
     some funny bugs when larger pictures were used.
     
     Options:
     holdingElementId - element id of the element which contains the elements you want to crossfade
     childElementType - element types to crossfade. e.g. 'div'
*/

var SimpleCrossfader = Class.create();

SimpleCrossfader.prototype = {
	initialize : function(options) {
	    this.selector = '#' + options.holdingElementId + ' .' + options.childElementClass;
	    this.elements = $$(this.selector);
	    this.effectQueueScope = options.id + 'effectscope';
	    this.queue = Effect.Queues.get(this.effectQueueScope);
	    
	    if (this.elements.length > 0) {
	        this.elementIndex = null;
	        this.gotoSlide(0);
	    }
	},
	
	next : function(){
		slideNumber = this.elementIndex + 1;
		
		if (slideNumber == this.elements.length) slideNumber = 0;
		
		this.gotoSlide(slideNumber);
	},
	
	previous : function() {
		var slideNumber = this.elementIndex - 1;
		
		if (slideNumber < 0) slideNumber = (this.elements.length - 1);
		
		this.gotoSlide(slideNumber);
	},
	
	gotoSlide : function(slideNumber) {
	    if (this.elementIndex == slideNumber)
	        return;  // Already viewing this slide
	
	    if (this.queue.effects.length > 0) {
	        // Not done with previous effects
	        
	        this.queue.effects.clear();
	        
	        this.elements.each(function(element) {
	            element.setStyle({ display: 'none' });
	        });
	    }
	    else {
	        if (this.elementIndex != null)
	            new Effect.Fade(this.elements[this.elementIndex], { queue: { position: 'end', scope: this.effectQueueScope, limit: 2}, duration:1, from:1.0, to:0.0 });
	    }
	    
	    this.elementIndex = slideNumber;
	    new Effect.Appear(this.elements[slideNumber], { queue: { position: 'end', scope: this.effectQueueScope, limit: 2}, duration:1, from:0.0, to:1.0 });
	}
};