/* prototype extensions */
/* -- DOM READY http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype  -- */
/* Event.onDOMReady(function() { //stuff here! }); */

var ACC_Slider= Class.create();

ACC_Slider.prototype = {
  clickItems: null,
  newsList: null,
  itemIndex: 0,
  onEffect:false,
  isInfinite:false,
  toTop: false,
  timer: 4,
  delay: 0,
  duration: 0,
    totalWidth: 0,
    width: 0,
    initSize: 0,
  initialize: function(param){
    this.newsList = param.listItem;
        this.initSize = this.calculate();
        this.newsList.setStyle({width: + this.initSize + 'px'});
    this.isInfinite = param.isInfinite;
        this.clickItems = new Array();
    this.clickItems.clear();
    this.clickItems[0] = param.clickItemUp;
    if(param.clickItemDown)
      this.clickItems[1]= param.clickItemDown;
    if(param.delay)
      this.delay = param.delay;
    if(param.duration)
      this.duration = param.duration;
    this.initEvents();
    if(param.doIt)
            this.doIt();
        this.arrowVisible();
  },
    calculate: function(){
        this.newsList.immediateDescendants().each(function(item){
            this.totalWidth += item.getWidth();
        }.bind(this));
        return this.totalWidth;
    },
  initEvents: function(){
    this.clickItems.each(function(item){
      Event.observe(item, 'click', function(event){
        var direction = '';
        if(Event.element(event).up('div').hasClassName('up')) direction = 'up';
        this.move(direction, false);
        this.restartPeriodicalUpdater();
      }.bind(this));
    }.bind(this));
  },
  move: function(direction, isAuto){
    
    this.width = this.newsList.immediateDescendants()[0].getWidth();
    this.width = (direction == 'up') ? 0 - this.width : this.width;
    
        if( !this.onEffect &&
            (!this.isInfinite && 
            (this.itemIndex > 0 && direction != 'up' || this.itemIndex < this.newsList.immediateDescendants().length-1 && direction == 'up'))){
//           alert('toto'); 
/*        new Effect.Appear(this.newsList, {duration:0.1, fps:25, from:1.0, to:0.0,            */
//      new Effect.MoveBy(this.newsList, 0, this.width, {duration: this.duration, delay: (isAuto ? this.delay : 0), transition: Effect.Transitions.sinoidal, 
/*
            
      beforeStart:
        function(){
          this.onEffect = true;
        }.bind(this),
                
      afterFinish: 
        function(){
                
          if(direction == 'up'){
                    
            this.itemIndex++;
                        
          } else if(!this.isInfinite){
                    
            this.itemIndex = this.itemIndex == 0 ? 0 : this.itemIndex - 1;
                        
          }
                    
          this.onEffect = false;
                    this.arrowVisible();
                    
        }.bind(this)*/
//      });
    }else{
                        
            myList=this.newsList.immediateDescendants();
            countMyList=myList.size()-1;
        
            if(this.isInfinite){
            
                if(direction == 'up'){
                    var clonedItem = myList.first().cloneNode(true);
                    this.newsList.appendChild(clonedItem);
                }else{
                    var clonedItem = myList[countMyList].cloneNode(true);
					clonedItem.setStyle({display:"none"})
                    Element.insert(this.newsList, {top: clonedItem}); 
                    Element.remove(myList[countMyList]);
                    myList=this.newsList.immediateDescendants();
                }
            
                
        new Effect.Appear(this.newsList, {duration:0.5, from:1.0, to:0.0,            

                beforeStart:
                    function(){
                        this.onEffect = true;
                    }.bind(this),
                afterFinish: 
                    function(){
                     if(direction == 'up'){                
							Element.remove(myList.first());
							}else{
							myList.first().setStyle({display:"block"});
							}	

                       // }
                        //this.onEffect = false;
          //              this.arrowVisible();
            new Effect.Appear(this.newsList, 0.5, {duration:0.5,  from:0.0, to:1.0});
                        
                    }.bind(this)
                });
                
            }
            //this.arrowVisible();
        }
        
  },
    
  doIt: function(){
      this.pe=new PeriodicalExecuter(function(pe){
      var direction = '';
      
      if(this.isInfinite || !this.toTop && !this.isInfinite && this.itemIndex < this.newsList.immediateDescendants().length-1){
        direction = 'up';
      }else if(!this.toTop){
                this.toTop = true;
      }else if(this.itemIndex <= 1){
                this.toTop = false;
            }
      
      this.move(direction, true);
    }.bind(this), 4);
  },
  restartPeriodicalUpdater: function(){
    this.pe.stop();
    this.doIt(); 
  },   
    arrowVisible: function(){
        if(!this.isInfinite){
            if(this.newsList.getStyle('left') == '0px' || !this.newsList.getStyle('left')){
                this.clickItems[0].setStyle({visibility: "hidden"});
            } else if (this.newsList.getStyle('left') == 0-(this.totalWidth+this.width)+'px'){
                this.clickItems[1].setStyle({visibility: "hidden"});
            } else {  
                this.clickItems[0].setStyle({visibility: "visible"});
                this.clickItems[1].setStyle({visibility: "visible"});
            }
        }
    }
}

Event.observe(window, 'load', function(){
  if($('home-news')){

    try {
          new ACC_Slider({listItem: $$('#slider_2 .actush ul')[0], 
            clickItemUp: $$('#slider_2 .arrowLeft img')[0], 
            clickItemDown: $$('#slider_2 .arrowRight img')[0], 
            isInfinite: true,
            doIt: true,
            duration: 4});
    } catch (e) {
            alert(e);
    }
  }
   
  
});
