/************ Sliders *************/
// GENERIC HOVER DELAY TRIGGER
var HoverDelay = Class.create({
    initialize : function(trigger, options){
        this.options = Object.extend({enterCb : function(){},   leaveCb : function(){}, delay : 0.5}, options || {});
        this.trigger = $(trigger);
        this.timeout = null; this.active = false;
        this.setup();
    },
    setup : function(){
        var eEvt = this.open.bindAsEventListener(this);
        var lEvt = this.close.bindAsEventListener(this);
        this.trigger.observe('mouseenter', eEvt);
        this.trigger.observe('mouseleave', lEvt);
        this.trigger.observe('hoverdelay:stop', function(){
            this.trigger.stopObserving('mouseenter', eEvt);
            this.trigger.stopObserving('mouseleave', lEvt);
        }.bind(this));
        document.observe('pop:active', function(){this.inactive=true;}.bind(this));
        document.observe('pop:inactive', function(){this.inactive=false;}.bind(this));
    }, 
    open : function(event){
        if (this.inactive) return;
        this.timeout = (function(){
            this.options.enterCb();
            this.active = true;
        }).bind(this).delay(this.options.delay);
    },
    close : function(){
        if (this.inactive) return;
        if (this.timeout) {
            window.clearTimeout(this.timeout);
            this.timeout = null;
        }
        if (this.active){
            this.options.leaveCb();
            this.active = false;
        }
    }
});

Effect.Transitions.EaseFromTo = function(pos) {
    if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,4);
    return -0.5 * ((pos-=2)*Math.pow(pos,3) - 2);   
}; 
var Slider = Class.create({
    initialize : function(initial, contents, fades, buttons, delay, width){
        this.initial = initial;
        this.contents = contents;
        this.buttons = buttons;
        this.fades = fades;
        this.delay = delay;
        this.width = width;
        this.current = initial;
        this.buttons[this.current].addClassName('active');
        this.start();
        this.show = null, this.hide = null;
        this.buttons.each(function(el, i){
        	new HoverDelay(el, {
	            enterCb : function(){
	            	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);
    },
    
    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 == 2)
            this.goTo(0);
        else
            this.goTo(this.current+1);
        this.start();
    },
    goTo : function(idx, dur){
        if (idx == this.current) return;
		if (Prototype.Browser.IE)
			this.hide = new Effect.Fade(this.fades[this.current],{duration: .2});
        else
			this.hide = new Effect.Fade(this.fades[this.current],{duration: .4});
		
		this.buttons[this.current].removeClassName('active');
        this.current = idx;
        this.slide = new Effect.Move(this.contents, {mode:'absolute', 'x':-idx*this.width, duration: .8}, {transition:Effect.Transitions.EaseFromTo});
        this.show = new Effect.Appear(this.fades[this.current],{duration: .4, queue:'end'});
        this.buttons[this.current].addClassName('active');
    },
    getCurrent : function(){
        return this.current;
    }
    
});
document.observe("dom:loaded", function(){
	new Slider(0, $('feature_slide'), $$('#main .feature_content'), $$('#touts .tout'), 5, 960);
	if ($('steps')){
		new Slider(0, $('steps'), null, $);
	}
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	
});