/* @author		JOOFORGE.com
 * @copyright	Copyright(C) 2010 Jooforge
 * @licence		GNU/GPL http://www.gnu.org/copyleft/gpl.html */

var GbSlider = new Class({
	options: {
		'width': 662,
		'time': 12000
	}, fx: {
		duration: 500,
		transition: Fx.Transitions.Sine.easeInOut
	}, active: null,
	initialize: function () {
		var a = this;
		this.main = $E('div.gbox_shw');
		this.descbox = this.main.getElement('div.descriptions');
		this.descriptions = this.main.getElements('div.description');
		this.games = this.main.getElements('ul.games li');
		this.rail = this.main.getElement('div.rail');
		this.corner = this.main.getElement('div.corner');
		this.railfx = this.rail.effect('margin-left', this.fx);
		this.descfx = this.descbox.effect('top', this.fx);
		this.games.addEvent('click', function () {
			a.slideTo(a.games.indexOf(this))
		});
		this.presetTop();
		this.slideTo(0)
	}, refreshTime: function () {
		$clear(this.auto);
		var a = this;
		this.auto = setTimeout(function () {
			a.slideTo(a.active < a.games.length - 1 ? a.active + 1 : 0);
			a.refreshTime()
		}, a.options.time)
	}, slideTo: function (a) {
		this.refreshTime();
		if (a != this.active) {
			this.games.removeClass('active');
			this.games[a].addClass('active').adopt(this.corner);
			if ($defined('Cufon')) {
				Cufon.replace('div.gbox_shw span');
			}
			this.railfx.start(-a * this.options.width)
		}
		if (this.active == null) {
			this.active = a;
			this.descriptions[this.active].setStyle('display', 'block')
		}
		var b = this.descriptions[this.active];
		var c = this.descriptions[a];
		this.descfx.start(-b.getCoordinates().height).chain(function () {
			b.setStyle('display', 'none');
			c.setStyle('display', 'block');
			this.start(0)
		});
		this.active = a
	}, presetTop: function () {
		this.descbox.setStyle('top', -this.descbox.getCoordinates().height)
	}
});
window.addEvent('domready', function () {
	new GbSlider()
});