/*
 * presbeton.IntroboxController
 */

var presbeton = window.presbeton = window.presbeton || {}; 
 
presbeton.IntroboxController = function(options)
{
	this.options = $.extend({
		timeout: 400000,
		speed: 500
	}, options);
	this.active = -1;
};

presbeton.IntroboxController.prototype.init = function()
{	
	this.$frames = $('.intro-frames .frame');
	this.$menu = $('.intro-menu li');
	this.$menu.find('a').bind('click', $.proxy(this.clickMenu, this));
	this.$frames.hide().find('.links').hide();
	this.$frames.bind('mouseenter', $.proxy(function(){ this.pause(); }, this));
	this.$frames.bind('mouseleave', $.proxy(function(){ this.restart(); }, this));
	this.$menu.bind('mouseenter', $.proxy(function(){ this.pause(); }, this));
	this.$menu.bind('mouseleave', $.proxy(function(){ this.restart(); }, this));
	this.transitionTo(0);
	this.restart();
};

presbeton.IntroboxController.prototype.transitionTo = function(i)
{
	i = i % this.$frames.size();
	if(i != this.active)
	{
		this.$frames.eq(this.active).stop().fadeTo(this.options.speed, 0).css({ zIndex: 1 });
		this.$frames.eq(this.active).find('.links').hide();
		this.$frames.eq(i).stop().fadeTo(this.options.speed, 1).css({ zIndex: 10 });
		this.$frames.eq(i).find('.links').show();
		this.$menu.removeClass('active').eq(i).addClass('active');
		this.active = i;
	}
};

presbeton.IntroboxController.prototype.clickMenu = function(event)
{
	var $li = $(event.currentTarget).closest('li');
	this.transitionTo(this.$menu.index($li.get(0)));
	return false;
};

presbeton.IntroboxController.prototype.automate = function()
{
	this.transitionTo(this.active + 1);
	this.restart();
};

presbeton.IntroboxController.prototype.restart = function()
{
	if(this.timer) window.clearTimeout(this.timer);
	this.timer = window.setTimeout($.proxy(this.automate, this), this.options.timeout);
};

presbeton.IntroboxController.prototype.pause = function()
{
	if(this.timer) window.clearTimeout(this.timer);
};

