/*
	Arguments:
		container - a container to hold the slideshow
		options - the options
		
	Options:
		type - "scroll/fade", Two different effects.
		direction - "forward/back", Defines the slide direction.
		goTo - number, Go to a specific slide.
		duration - milliseconds, Duration of the slide effect.
		auto - "loop/once", Makes the slide automated.
		time - milliseconds, Defines the amount of time an automated slide will stop at each element.
*/

var SweetSlider = new Class({
	options: {
		type: "none",
		direction: "none",
		duration: 400,
		auto: "none",
		time: 4000
	},
	initialize: function(container,options) {
		this.container = container;
		this.setOptions(options);

		//	Is it on autorun or manual?
		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;
		}
		// Get all child nodes to scroll between.
		var children = container.getChildren().getChildren()[0];
		// Run through all child nodes to see if there is a tagged one.
		children.each(function(e) {
			// If there is, make it current child.
			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 there isn't, make the first one current child.
				if(direction == "forward") {
					child = children[0].getNext();
				}
				else if(direction == "back") {
					child = container.getChildren()[0].getLast();
				}
			} else {
			// Are we going to the next or previous node?
				if(direction == "forward") {
					var lastElement = container.getChildren()[0].getLast();
					// Stops the loop at the last element.
					if(lastElement == child.getNext() && this.options.auto == "once") $clear(this.automated);
					// Is the current child the last node? Then set the first node as child, otherwise set the next node as child.
					if(lastElement == child) child = children[0];
					else child = child.getNext();
				}
				else if(direction == "back") {
					var firstElement = container.getChildren()[0].getFirst();
					// Stops the loop at the last element.
					if(firstElement == child.getPrevious() && this.options.auto == "once") $clear(this.automated);
					// Is the current child the last node? Then set the first node as child, otherwise set the next node as child.
					if(firstElement == child) child = container.getChildren()[0].getLast();
					else child = child.getPrevious();			
				}
			}
		}
		// Is the child defined?
		if(child) {

			// Which type of slider is defined?
			if(this.options.type == "scroll") this.scroll(container,children,child);
			else if(this.options.type == "fade") this.fade(container,children,child);
		}
	},
	scroll: function(container,children,child) {
		// Make it a scroll slide.
		var scroll = new Fx.Scroll(container,{duration: this.options.duration, transition: Fx.Transitions.Quart.easeInOut, onComplete: function() {
			// Remove tags from all child nodes.
			children.each(function(e) {
				e.id = "";
			});
			// Tag this child as current
			child.id = "currentChild";
		}}).toElement(child);
		
	},
	fade: function(container,children,child) {
		// Make it a fade slide
		var fade = new Fx.Style(container,'opacity',{duration: this.options.duration, transition: Fx.Transitions.Quart.easeInOut, onComplete: function() {
			new Fx.Scroll(container,{duration: 1,onComplete: function() {
				// Remove tags from all child nodes.
				children.each(function(e) {
					e.id = "";
				});
				// Tag this child as current
				child.id = "currentChild";
				new Fx.Style(container,'opacity').start(0.01,1);
			}}).toElement(child);
		}})
		fade.start(1,0.01);
	}
});

SweetSlider.implement(new Options);// Implements setOptions(defaults, options)
