var RSGallery = new Class({

  /**
	 * Inicializace
	 */ 
  initialize: function(options) {
  	this.container = $(options['imageContainer']);
    this.prevButton = $$(options['prevButton']);
    this.nextButton = $$(options['nextButton']);
    this.pagerTextWrap = $$(options['pagerTextWrap']);
    this.main();
  },


  /**
	 * Hlavni ridici funkce
	 */ 
  main: function() {
    if(this.container == null) return false;

    var allImgClean = [];
    this.container.setStyle('position', 'relative');
    var allImg = this.container.getElements('img').setStyles({visibility: 'hidden',position: 'absolute',left: 0,top: 0, opacity: 0});
    var size = this.container.getElement('img').setStyles({visibility: 'visible', opacity: 1}).getSize();  
    this.container.setStyles({width: size.x, height: size.y});
    
    allImg.each(function(item, index) { 
      if($(item).get('class')!='frontEndEditIcons') {
        allImgClean.push(item);
      }
    });
    
    // Strankovani text
    this.pagerText(allImgClean,this.pagerTextWrap,0,1);
    
    // Nastaveni stylu pro tlacitka
    this.setStyleBtn(allImgClean,'load','');
    
    // Dalsi
    this.nextButton.addEvent('click', this.next.bind(allImgClean,[this]));

  	// Predchozi
  	this.prevButton.addEvent('click', this.prev.bind(allImgClean,[this])); 	
  },
  
  
  /**
	 * Kliknuti na tlacitko dalsi
	 */
  next: function(obj) {
    var posImg = 0;
    var reg = /visible/;

    this.each(function(item, index) { 
      if(reg.test(item.get('style')) == true)  {
        posImg = index.toInt();
      } 
    });
    
    if(posImg+1 < this.length) {        
      this[posImg].fade(0);
      this[posImg+1].fade(1);
      
      // Strankovani text
      obj.pagerText(this,obj.pagerTextWrap,posImg,2);
      
      // Nastaveni stylu pro tlacitka      
      obj.setStyleBtn(this,'next',posImg);
    }
  },
  
  
  /**
	 * Kliknuti na tlacitko predchozi
	 */
  prev: function(obj) {
    var posImg = 0;
    var reg = /visible/;
    var regClassPrev = /prev_inactive/;
    var regClassNext = /next_inactive/;

    this.each(function(item, index) { 
      if(reg.test(item.get('style')) == true)  {
        posImg = index.toInt();
      } 
    });
    
    if(posImg > 0) {        
      this[posImg].fade(0);
      this[posImg-1].fade(1);
      
      // Strankovani text
      obj.pagerText(this,obj.pagerTextWrap,posImg,0);
      
      // Nastaveni stylu pro tlacitka
      obj.setStyleBtn(this,'prev',posImg);
    }
  },
  
  
  /**
	 * Text ve strankovani
	 */ 
  pagerText: function(img,obj,posImg,num) {
    var pagerText = '<span class="curent_pos">'+(posImg+num)+'</span>'+'<span class="separator">/</span>'+
    '<span class="all_images">'+img.length+'</span>'; 
    obj.set('html', pagerText);
  },


  /**
	 * Nastaveni stylu pro tlacitka
	 */  
  setStyleBtn: function(img,action,posImg) {
    var regClassPrev = /prev_inactive/;
    var regClassNext = /next_inactive/;

    if(action == 'next' || action == 'prev')  {
      if(regClassNext.test(this.nextButton.get('class')) == true) var removeNext = 'next_inactive';
      else var removeNext = 'next_active';
      
      if(regClassPrev.test(this.prevButton.get('class')) == true) var removePrev = 'prev_inactive';
      else var removePrev = 'prev_active';
      
      this.nextButton.removeClass(removeNext);
      this.prevButton.removeClass(removePrev);      
    }
    
    if(action == 'load')  {
      this.prevButton.addClass('prev_inactive');
      if(img.length>1) this.nextButton.addClass('next_active');
      else this.nextButton.addClass('next_inactive');
    }
    
    if(action == 'next')  {
      this.prevButton.addClass('prev_active');
      if(posImg+2 < img.length) this.nextButton.addClass('next_active');
      else this.nextButton.addClass('next_inactive');    
    }
    
    if(action == 'prev')  {
      this.nextButton.addClass('next_active');
      if(posImg > 1) this.prevButton.addClass('prev_active');
      else this.prevButton.addClass('prev_inactive');
    }
  }

});
