/*
	Author: Hector ' The Ecma/Java Script Killah' Vergara 007 Bond p0w4h Superscripter
*/

var Slider = new Class({
	links: [],
	posts: [],
	current: -1,
	options: {
		duration: 3000,
		autoStart: true
	},

	initialize: function(posts, links, options) {	

		this.links = $$(links);
		this.posts = $$(posts);
		this.options = $merge(this.options, options || {});
		this.timer = this.next.periodical(this.options.duration, this);

		this.links.each(function(link, index) {
			link.getElement('A').addEvent('click', this.show.bind(this, index));
		
		}, this);

		this.reset();

		this.show(0, !this.options.autoStart);

	},

	reset: function() {

		this.links.each(function(link) {
			link.removeClass('active');
		});

		this.posts.each(function(post) {
			post.info = post.getElement('div');

			post.set('tween', {duration: 750});
			post.info.set('tween', {duration: 750});

			post.fade('hide');
			post.info.setStyle('bottom', -1 * post.info.getStyle('height').toInt());
		});

	},

	show: function(index, pause) {
		
		this.links[index].addClass('active');
	
		if(this.current >= 0) {
			this.links[this.current].removeClass('active');

			this.posts[this.current].info.tween('bottom', -1 * this.posts[this.current].info.getStyle('height').toInt());
			this.posts[this.current].fade('out');
		}

		this.posts[index].fade('in');
		this.posts[index].getElement('div').tween('bottom', 0);
		this.current = index;

		if(pause != false) {
			clearTimeout(this.timer);
		}

	},

	next: function() {
		var nextIndex = this.current + 1;

		if(nextIndex >= this.links.length)
			nextIndex = 0;
		
		this.show(nextIndex, false);
	}
	
});


