
var od_slideshow = new Class({

  /**
	* Inicializace
	*/ 
  initialize: function(options) {
    // Povinne nastaveni
    this.cont = $(options['idSlideshow']); 
    
    // Volitelne nastaveni
    this.containerSlideButton = $(options['containerSlideButton']); 
    this.durationTime = $defined(options['durationTime']) ? options['durationTime'].toInt() : 1000; 
    this.delayTime = $defined(options['delayTime']) ? options['delayTime'].toInt() : 5000; 
    this.delayTimeAfterClick = $defined(options['delayTimeAfterClick']) ? options['delayTimeAfterClick'].toInt() : 20000; 
    this.transitionEffect = $defined(options['transitionEffect']) ? options['transitionEffect'] : 'pushDown'; 
    this.slideButton = $defined(options['slideButton']) ? options['slideButton'] : false;
    this.autoplay = $defined(options['autoplay']) ? options['autoplay'] : true;
    this.slideContentSource = $defined(options['slideContent']) ? options['slideContent'] : 'div';
    // Nastavuje se pokud je nastaveno slideButton 
    this.pathBtnImg = options['pathBtnImg'];
    this.pathBtnImgSelect = options['pathBtnImgSelect'];
    this.pathBtnImgHover = options['pathBtnImgHover'];

    if(this.cont === null)
        return;    
    
    this.main();
  },
  
  
  /**
	* Hlavni ridici funkce
	*/ 
  main: function() {
      this.initSlideshow();
      this.initButtons();
      this.eventActionSlide();
      this.initPrevNextBt();
  }, 
  
  
  /**
   * Inicializace slideshow
   */     
  initSlideshow: function() {
      this.slideShow = new SlideShow(this.cont,{		
          delay: this.delayTime,
          duration: this.durationTime,
          transition: this.transitionEffect,
    	    autoplay: this.autoplay
      });        
  },
  
  
  /**
   * Inicializace tlacitek
   */     
  initButtons: function() {
      if(this.containerSlideButton === null)
          return;
      
      if(this.slideButton) {   
          var slideContents = this.cont.getElements(this.slideContentSource);
          slideContents.each(function(item, index) {
              var slideBtn = new Element('img', {
                  src: (index == 0 ? this.pathBtnImgSelect : this.pathBtnImg),
                  alt: ''
              });    
              
              slideBtn.inject(this.containerSlideButton, 'bottom');
          }.bind(this));
      
          this.btns = this.containerSlideButton.getElements('img');
      } else {
          this.btns = this.containerSlideButton.getChildren();
          this.btns[0].addClass('selected');      
      }         
  },
  
  initPrevNextBt : function() {
    var timer = '';
    var con = this.cont.getParent();
    var prevNextContainer = new Element ('div', {'id': 'prevNextContainer'});
    var prevBtn = new Element('span',{'class': 'prevBtn'});
    
    prevBtn.addEvent('click', function(){
        if(timer)
          $clear(timer);
    
        this.slideShow.pause();
      	     	
      	
        this.slideShow.show(this.slideShow.previousSlide(), {
              		transition: 'pushRight',
                  duration: this.durationTime
              		
            	});
        this.slideShow.play();
        
    }.bind(this));
    
    var nextBtn = new Element('span',{'class': 'nextBtn'});
    nextBtn.addEvent('click', function(){
        
        if(timer)
          $clear(timer);
        
        this.slideShow.pause();
        //timer = (function(){this.slideShow.play(); }.bind(this)).delay(this.delayTimeAfterClick - this.delayTime);
        this.slideShow.show(this.slideShow.nextSlide(), {
              		transition: 'pushLeft',
                  duration: this.durationTime
              		
            	});
        this.slideShow.play();    	
    }.bind(this));
    
    
    
    prevBtn.inject(prevNextContainer,'bottom');
    nextBtn.inject(prevNextContainer,'bottom');
    prevNextContainer.inject(con,'bottom');
  },
  
    
  
  
  /**
   * Zobrazeni slidu
   */ 
  eventActionSlide: function() {
      var timer = '';
      
      this.btns.each(function(item, index) {
          
          // Click
          item.addEvent('click', function(){
              if(timer)
                  $clear(timer);
    
              this.slideShow.pause();
            	this.slideShow.show(this.slideShow.slides[index], {
              		duration: this.durationTime
            	});
            	
            	timer = (function(){this.slideShow.play(); }.bind(this)).delay(this.delayTimeAfterClick - this.delayTime);
              if(this.slideButton) {
            	   this.btns.set('src', this.pathBtnImg);
            	   item.set('src', this.pathBtnImgSelect);
            	}
          }.bind(this));  
          
          if(this.slideButton) {
              // MouseOver
              item.addEvent('mouseover', function() {  
                  select = item.get('src').slice(-10); 
                  if(select != 'select.png')
                      item.set('src', this.pathBtnImgHover);
        
              }.bind(this)); 
              
              // MouseOut
              item.addEvent('mouseout', function(){
                  select = item.get('src').slice(-10); 
                  if(select != 'select.png')    
                      item.set('src', this.pathBtnImg);       
              }.bind(this));  
          }
                 
      }.bind(this));
  
    	this.slideShow.addEvents({
    	     
      		onShowComplete: function(obj) {
      		    if(this.slideButton) {
            	    this.btns.set('src', this.pathBtnImg);
            	    this.btns[obj.next.index].set('src', this.pathBtnImgSelect); 
              } else {
                  elm = $('containerSlideButton').getChildren();
                  elm.removeClass('selected');
                  elm[obj.next.index].addClass('selected');       
              }  
          }.bind(this)
    	}); 
  }  
 
})

function d(str) {
    console.log(str);
}

 window.addEvent("domready", function() {
 if ($("slideshow")) {  

          new od_slideshow({
            idSlideshow: "slideshow",
            slideContent: "div[class=csc-default]",
            containerSlideButton: "slideshow_tlacitka",
            delayTimeAfterClick: 5000,
            slideButton: true,
            pathBtnImg: "fileadmin/paxton/template/img/slideshow/btn.gif",
            pathBtnImgSelect: "fileadmin/paxton/template/img/slideshow/btn_show.gif",
            pathBtnImgHover: "fileadmin/paxton/template/img/slideshow/btn_show.gif",
            transitionEffect: "crossFade"    //transitionEffect: "crossFade"
          });
          
        }
});



