/************ slideshows *************/
var SlideShow = Class.create();
SlideShow.prototype = {
	initialize : function(initial, contents, buttons, delay){
		this.initial = initial;
		this.contents = contents;
		this.buttons = buttons;
		this.delay = delay;
		this.showOnly(this.contents[this.initial]);
		this.current = initial;
		this.buttons[this.current].addClassName('active');
		this.start();
		this.show = null, this.hide = null;
		this.buttons.each(function(el, i){
			el.observe('click', (function(ev){
				ev.stop();
				this.pause();
				this.goTo(i, .5);
			}).bind(this));
		}.bind(this));
		
		this.buttons[0].up().observe('mouseleave', function(ev){
			this.start();
		}.bind(this));
	},
	start : function(){
		if (this.timer) return;
		this.timer = setTimeout(this.next.bind(this), this.delay * 1000); //new PeriodicalExecuter(this.next.bind(this), this.delay);
	},
	showOnly : function(el){
		this.contents.each(Element.hide);
		el.show();
	},
	pause : function(){
		if (this.timer){
			clearTimeout(this.timer);
			this.timer = false;
		}
		this.hide ? this.hide.cancel() : 0; 
		this.show ? this.show.cancel() : 0;
	},
	next : function(){
		this.timer = false;
		if (this.current == this.contents.length-1)
			this.goTo(0);
		else
			this.goTo(this.current+1);
		this.start();
	},
	goTo : function(idx, dur){
		if (idx == this.current) return;
		this.hide = new Effect.Fade(this.contents[this.current], {duration : dur || 2});
		this.buttons[this.current].removeClassName('active');
		this.current = idx;
		this.show = new Effect.Appear(this.contents[this.current],{duration: dur || 2});
		this.buttons[this.current].addClassName('active');
	},
	getCurrent : function(){
		return this.current;
	}
	
};

var UpMenu = Class.create(HoverDelay, {
	initialize : function($super, trigger, options){
		this.height = trigger.getHeight();
		options['enterCb'] = function() {
			trigger.addClassName('active');
			new Effect.Move(trigger, { x: 0, y: -1 * this.height + 95, mode: 'absolute', duration: 0.2});
		}.bind(this);
		options['leaveCb'] = function() {
			trigger.removeClassName('active');
			new Effect.Move(trigger, { x: 0, y: 0, mode: 'absolute', duration: 0.2});
		}.bind(this);
		$super(trigger, options);
	}
});

document.observe('dom:loaded', function() {
	if ($('slideshow'))
		var show = new SlideShow(0, $$('#slideshow > img'), $$('#slideshow ul.controls li'), 6);
		
	if ($('watchVideo'))
		$('watchVideo').observe('click', function(){
			var videoIndex = show.getCurrent();
		});
	
	if ($('widgetSlideUp')) {
		$('widgetSlideUp').select('.tout').each(function(el){
			new UpMenu(el, {delay:0.25});
		});
	}
	
	$$('#getStarted li[id]').each(function(el){
		new DelayedHover(el, 'active', {delay:0.23});
	});
});