﻿/* SimpleSlide v 1.1 Developed by Gravita, www.gravita.se */
var SimpleSlide = new Class({
	initialize: function(container,options) {
		this.container = container;
		this.options = options;
		if(this.options.auto == "loop" || this.options.auto == "once") {
			var automated;
			this.automated = this.slider.periodical(this.options.time,this,$(this.container));
		} else {
			this.slider($(this.container))
		}
	},
	slider: function(container) {
		var direction;
		if(this.options) direction = this.options.direction;
		else direction = "forward";
		var child;
		if(this.options.goTo) {
			var goTo = this.options.goTo.toInt();
				goTo -= 1;
		}
		var children = container.getChildren().getChildren()[0];
		children.each(function(e) {
			if(e.id == "currentChild") {
				child = e;
			}
		});
		if(goTo || goTo == 0) {
			if(container.getChildren()[0].getChildren()[goTo]) child = container.getChildren()[0].getChildren()[goTo];
			else alert("Slide "+goTo+" does not exist");
		} else {
			if(!child) {
				if(direction == "forward") {
					child = children[0].getNext();
				}
				else if(direction == "back") {
					child = container.getChildren()[0].getLast();
				}
			} else {
				if(direction == "forward") {
					var lastElement = container.getChildren()[0].getLast();
					if(lastElement == child.getNext() && this.options.auto == "once") $clear(this.automated);
					if(lastElement == child) child = children[0];
					else child = child.getNext();
				}
				else if(direction == "back") {
					var firstElement = container.getChildren()[0].getFirst();
					if(firstElement == child.getPrevious() && this.options.auto == "once") $clear(this.automated);
					if(firstElement == child) child = container.getChildren()[0].getLast();
					else child = child.getPrevious();			
				}
			
			}
		}
		if(child) {
			if(this.options.type == "scroll") this.scroll(container,children,child);
			else if(this.options.type == "fade") this.fade(container,children,child);
			else if(this.options.type == "scrollfade") this.scrollfade(container,children,child);
		}
	},
	scroll: function(container,children,child) {
		var scroll = new Fx.Scroll(container,{duration: this.options.duration, onComplete: function() {
			children.each(function(e) {
				e.id = "";
			});
			child.id = "currentChild";
		}}).toElement(child);
		
	},
	fade: function(container,children,child) {
		var fade = new Fx.Style(container,'opacity',{duration: this.options.duration, onComplete: function() {
			new Fx.Scroll(container,{duration: 1,onComplete: function() {
				children.each(function(e) {
					e.id = "";
				});
				child.id = "currentChild";
				new Fx.Style(container,'opacity').start(0.01,1);
			}}).toElement(child);
		}})
		fade.start(1,0.01);
	},
	scrollfade: function(container,children,child) {
		var durationInt = this.options.duration.toInt();
		var fade = new Fx.Style(container,'opacity',{duration: (durationInt/2)})
		fade.start(1,0.01).chain(function() {
			fade.start(0.01,1);
		});
		new Fx.Scroll(container,{duration: durationInt, onComplete: function() {
			children.each(function(e) {
				e.id = "";
			});
			child.id = "currentChild";
		}}).toElement(child);
	}
});

window.addEvent('domready', function(){
	new SimpleSlide("SimpleSlide",{type: "fade", direction: "forward", auto: "loop", duration: 1000, time: 5000});
});
