MovieScroller=Class.create()

Object.extend(MovieScroller.prototype,{
  initialize:function(movie_container){
    this.scroller_container=movie_container
    this.movie_offset=0; 
    this.curr_tab_pos=0;
    this.scroll_delay=3; // timpul de autoscroll
    this.autoplay=true;  //true pentru auto scroll
    
    this.left_button=this.scroller_container.down('a.scroller-left');
    this.right_button=this.scroller_container.down('a.scroller-right');
	/*taburi cele mai ...*/
	this.selectare=this.scroller_container.down('.scroller-tabs').down('.scroller-tab')
	this.scroller_tabs=$A(this.scroller_container.down('.scroller-tabs').getElementsByTagName('b'));
	this.scroller_tabs2=$A(this.scroller_container.down('.scroller-tabs').getElementsByTagName('a'));
//	this.scroller_tabs=this.scroller_tabs.reject(function(el){return !$(el).hasClassName('scroller-tab')})
	/*taburi categorii*/
	
    this.scroller_movies=$A(this.scroller_container.down('.scroller-movie').getElementsByTagName('ul'));
    this.scroller_movies_container=this.scroller_container.down('.scroller-movie');
    this.scroller_period=null; // autoexecuterul pt scroll;
    this.scroller_movies_no=5; // numarul de poze pe slider
    this.scroll_step=this.scroller_container.down('.scroller-movie').getWidth()/this.scroller_movies_no;
    this.init();
    
  },
  
  init:function(){
    var self=this;
    self.scroller_tabs2.each(function(stab){
      Event.observe(stab,'click',self.showTab.bindAsEventListener(self));
    });
    
    Event.observe(self.left_button,'click',self.movieScroll.bindAsEventListener(self));
    Event.observe(self.right_button,'click',self.movieScroll.bindAsEventListener(self));
    
    if(this.autoplay)
      this.scroller_period=new PeriodicalExecuter(this.autoScroll.bind(this), this.scroll_delay);
  },
  
 showTab:function(event){
    Event.stop(event);
    var self=this;
   
    curr_tab=event.findElement('a');
     // opresc autoscrollul
    this.stopAutoscroll();
    if(curr_tab.hasClassName('scroller-categ')){
		
	}
    
    this.scroller_tabs2.invoke('removeClassName','selected');
    curr_tab.addClassName('selected');

    //$A(this.scroller_movies).each(function(sm){
    //  Element.hide(sm);
    //})
    last_pos=this.curr_tab_pos
    this.curr_tab_pos=this.scroller_tabs2.indexOf(curr_tab);
    
    new Effect.Fade(
    	$A(this.scroller_movies)[last_pos],
    	{
    		afterFinish:function(){
    			new Effect.Appear(
    				$A(self.scroller_movies)[self.curr_tab_pos],
    				{duration:0.5}
    			)
    		},
    		duration:0.5
    		
    	}
    )
    
    //Element.show($A(this.scroller_movies)[this.curr_tab_pos]);
    
    new Effect.ScrollHorizontal(this.scroller_movies_container,{to:0});
    this.movie_offset=0;
  },

  
  stopAutoscroll:function(){
    if(this.scroller_period !=null){
      this.scroller_period.stop();
      this.scroller_period=null;
    }
  },
  
  movieScroll:function(event){
    scroll_button=event.findElement('a');
    Event.stop(event);
    
    movies_no=$A(this.scroller_movies[this.curr_tab_pos].getElementsByTagName('li')).length;
    movie_max_scroll=movies_no*this.scroll_step;
    
    // opresc autoscrollul
    this.stopAutoscroll();
    
    // daca e butonul de scroll stanga
    if(scroll_button.hasClassName('scroller-left')){
      (this.movie_offset - this.scroll_step < 0) ? scroll_to = movie_max_scroll - this.scroller_movies_no*this.scroll_step : scroll_to = this.movie_offset - this.scroll_step  
    }
    
    // daca e butonul de scroll dreapata
     if(scroll_button.hasClassName('scroller-right')){
      (this.movie_offset + this.scroll_step*this.scroller_movies_no >= movie_max_scroll) ? scroll_to=0 : scroll_to = this.movie_offset + this.scroll_step
     }
     
     this.movie_offset=scroll_to;
     new Effect.ScrollHorizontal(this.scroller_movies_container,{to:scroll_to});
  },
  
  autoScroll:function(){
    scroll_to = this.movie_offset + this.scroll_step;
    
    movies_no=$A(this.scroller_movies[this.curr_tab_pos].getElementsByTagName('li')).length;
    movie_max_scroll=movies_no*this.scroll_step;
    
    if(scroll_to+this.scroll_step >= movie_max_scroll)
      scroll_to=0;
    
    this.movie_offset=scroll_to;
    new Effect.ScrollHorizontal(this.scroller_movies_container,{to:scroll_to});
  }
  
  
});

document.observe('dom:loaded',function(){
  $$('.scroller-container').each(function(movie_slider){
    new MovieScroller(movie_slider);
  })
})