document.observe('dom:loaded', function()
{
    l1 = new Slider('index_offers','offer_prev','offer_next','div.special_offer',2);
	hs = new SimpleSlider('index_middle','img',3);
});

function Slider(parent,left,right,elems,freq)
{
    this.current = 0;
    this.direction = 'fw';
    this.timer = false;
    this.effect = false;
    this.items = (elems != undefined) ? $(parent).select(elems) : $(parent).getElementsByTagName('li');
    this.count = this.items.length;
  
    for(i=0;i<this.count;i++)
        if(i!=0) this.items[i].style.display = 'none';

    this.__slide = function(stop)
    {
		if(stop != false) this.timer.stop();
    	if(this.effect) return;
    	this.effect = true;
        Effect.Fade(this.items[this.current], { duration: 0.4 });
        if(this.direction == 'fw')
            if (this.current == (this.count-1)) { this.current = 0; } else { this.current++; }
        else
            if (this.current == 0) { this.current = (this.count-1); } else { this.current--; }
        Effect.Appear(this.items[this.current], { duration: 0.4 });
        this.effect = false;
    };

    if(freq)
    	this.timer = new PeriodicalExecuter(this.__slide.bind(this,false),freq);
    
    $(right).observe('click',this.__slide.bind(this,function(){ this.direction = 'fw'; }));
    $(left).observe('click',this.__slide.bind(this,function(){ this.direction = 'prev'; }));
}

function SimpleSlider(parent,elems,freq)
{
	this.current = 0;
	this.timer = false;
	this.effect = false;
	this.items = (elems != undefined) ? $(parent).select(elems) : $(parent).getElementsByTagName('li');
	this.count = this.items.length;
	
	for(i=0;i<this.count;i++)
		if(i!=0) this.items[i].style.display = 'none';
		
	this.__slide = function()
	{
		if(this.effect) return;
		this.effect = true;
		Effect.Fade(this.items[this.current], { duration: 0.4 });
		if (this.current == (this.count-1)) { this.current = 0; } else { this.current++; }
		Effect.Appear(this.items[this.current], { duration: 0.4 });
		this.effect = false;
	};
		
	this.timer = new PeriodicalExecuter(this.__slide.bind(this),freq);		
}


