var SimpleSlideShowDemo = new Class({
  Implements: [Options, Events],
  options: {
    slides: [],
    startIndex: 0,
    wrap: true,
    onShow: $empty, //Mootools 1.2: $empty
    onRotate: $empty,
    onStop: $empty,
    onAutoPlay: $empty,
    onShowSlide: $empty,
    slideInterval: 2500,
    auto:false,
    transitionDuration: 700,
    splitter:"&middot;",
    prev:"&laquo; ",
    next:" &raquo",
    play:"&infin;",
    stop:"&phi;",
    bigNavigation : true,
    smallNavigation: true,
    browser:true,
    addBarInside:true,
    hideBar:false

  },
  initialize: function(container,options){

    if (container[0].getParent().get('tag')=='a') {
     this.containerNest = container[0].getParent().getParent();
    } else {
      this.containerNest = container[0].getParent();
    }
    this.container = container;
    this.setOptions(options);
    this.slides = [];
    this.title = [];
    this.descr = [];
    this.effects = [];
    this.browserArr = [];
    this.playStatus = false;
    this.slideNum = 0;
    if (this.options.auto) this.playStatus = true;

    this.createStructure();

    this.addSlides(this.container);
    if(this.slides.length) this.showSlide(this.options.startIndex);
    if (this.options.auto) {
      this.autoplay();
    }
  },
  createStructure: function() {

    var oldClass = this.containerNest.getProperty("class");
    this.containerNest.setProperty('class', '');
    this.containerNest.addClass('rgslideshow');
    
    this.test = new Element('div').addClass(oldClass).inject(this.containerNest, 'before');
    this.containerNest.inject(this.test);

    if (!this.options.hideBar) {
      if (this.options.addBarInside) {
        this.nestAll = new Element('div').setProperty('class', 'rgsnest').inject(this.containerNest,'top');    
      } else {
        this.nestAll = new Element('div').setProperty('class', 'rgsnest').inject(this.containerNest,'after');   
      }
      
      this.title = new Element('div').setProperty('class', 'rgstitle').inject(this.nestAll);
      this.description = new Element('div').setProperty('class', 'rgsdescription').inject(this.nestAll);
    }
    
    if(this.options.browser) {
    
      this.navigation = new Element('div').setProperty('class', 'rgslideshownav').inject(this.containerNest,'after');
      this.innerNavigation = new Element('div').setProperty('class', 'rgsnav2').inject(this.navigation);
      
      var containerWidth =  this.containerNest.getSize().x;
      this.navigation.setStyle('width',containerWidth-1+'px');

      if(this.options.smallNavigation) this.prevEl = new Element('span').setProperty('class', 'rgsprev').inject(this.innerNavigation).set('html',this.options.prev).addEvent('click', this.cycleBack.bind(this));
      this.browser = new Element('div').setProperty('class', 'rgsbrowser').inject(this.innerNavigation);
      if(this.options.smallNavigation) this.nextEl = new Element('span').setProperty('class', 'rgsnext').inject(this.innerNavigation).set('html',this.options.next).addEvent('click', this.cycleForward.bind(this));

      this.play = new Element('span').setProperty('class', 'rgsplay').inject(this.innerNavigation).addEvent('click', this.togglePlay.bind(this));    

      if (!this.playStatus) {
        this.play.set('html',this.options.play).setProperty('title',"Play");
      } else {
        this.play.set('html',this.options.stop).setProperty('title',"Stop");
      }
    } 


    if (this.options.bigNavigation) {
      this.bigPrev = new Element('a').setProperties({'class': 'rgsbigprev', 'href': "javascript:void(0);"}).inject(this.containerNest).addEvent('click', this.cycleBack.bind(this));
      this.bigNext = new Element('a').setProperties({'class': 'rgsbignext', 'href': "javascript:void(0);"}).inject(this.containerNest).addEvent('click', this.cycleForward.bind(this));
    }
  },
  addSlides: function(slides){

    slides.each(function(slide){
      this.slides.include($(slide));

      this.effects[this.slideNum] = new Fx.Tween(slide, {duration: this.options.transitionDuration, link: 'cancel'});

      slide.addClass('rgssimg');
      
      var title="&nbsp;";
      var descr="";
      
      if (slide.getProperty("title")) {
        split = slide.getProperty("title").split("|");
        if (split[0]) {
          title = split[0];
          slide.setProperty("title",title);
        }
        if (split[1]) descr = split[1];
      }

      this.descr[this.slideNum] = descr;      
      this.title[this.slideNum] = title;
      
      if(this.options.browser) {        
        this.browserEl = new Element('span').setProperties({'class': 'rgsbrowserEl', 'title': title}).set('html',this.slideNum+1).inject(this.browser);
        this.browserEl.addEvent('click', this.cycleTo.bind(this,this.slideNum));
        this.browserArr[this.slideNum] = this.browserEl;
      }

      if (this.slideNum>0) {
        if(this.options.browser) this.splitter = new Element('span').setProperty('class','rgssplitEl').set('html',this.options.splitter).inject(this.browserEl,'before');
        slide.fade('hide');
      } else {
        var slideHeight = slide.getSize().y;
        if(this.options.bigNavigation) {
          if(!this.options.hideBar && this.options.addBarInside) {
            var barHeight = this.nestAll.getSize().y;
            
            this.bigPrev.setStyle('height',(slideHeight-barHeight)+'px');
            this.bigNext.setStyle('height',(slideHeight-barHeight)+'px');
          } else {
            this.bigPrev.setStyle('height',slideHeight+'px');
            this.bigNext.setStyle('height',slideHeight+'px');
          }
        }

        if(this.containerNest.getSize().y <= 1) 
          this.containerNest.setStyle('height', slideHeight+'px');

        /*if(this.options.addBarInside) { 
          this.nestAll.setStyle('width',slide.getProperty("width")+'px');
        }*/
      }
    this.slideNum++;
    }, this);
  },
  addSlide: function(slide){
    this.addSlides([slide]);
  },
  cycleForward: function(){
    if(this.now < this.slides.length-1) this.showSlide(this.now+1);
    else if (this.options.wrap) this.showSlide(0);
  },
  cycleBack: function(){
    if(this.now > 0) this.showSlide(this.now-1);
    else if(this.options.wrap) this.showSlide(this.slides.length-1);
  },
  cycleTo: function(i){
    if(this.playStatus) {
      this.togglePlay();
    }  
    this.showSlide(i);
  },
  togglePlay: function() {
    if (this.playStatus) {
      this.stop();
      this.playStatus = false;
      this.play.set('html',this.options.play).setProperty('title',"Play");
    }
    else {
      this.autoplay();
      this.playStatus = true;
      this.play.set('html',this.options.stop).setProperty('title',"Stop");
    }
  },
  autoplay: function(){
    this.periodID = this.rotate.periodical(this.options.slideInterval, this);
    this.fireEvent('onAutoPlay');
  },
  stop: function(){
    $clear(this.periodID);
    this.fireEvent('onStop');
  },    
  rotate: function(){
    this.cycleForward();
    this.fireEvent('onRotate');
  },  
  showSlide: function(iToShow){

    var currentSlide = this.slides[this.now];
    var slide = this.slides[iToShow];

    this.fireEvent('onShowSlide', [slide, iToShow]);
    
    function fadeIn(){
      slide.setStyles({
        display:'block',
        visibility: 'visible',
        opacity: 0
      });
      this.effects[iToShow].start('opacity', 1);
      
      if (!this.options.hideBar) {
        this.description.set('html',this.descr[iToShow]);
        this.title.set('html',this.title[iToShow]);
      }
      if(this.options.browser) {
        this.browserArr[iToShow].addClass("rgsact");
      }
      
      this.fireEvent('onShow', [slide, iToShow]);
    };
    
    if(slide && this.now != iToShow) {
    
      if (!this.options.hideBar) {
        this.description.set('html', "&nbsp;");
        this.title.set('html', "&nbsp;");
      }

      if($chk(this.now)){
        if(this.options.browser)
          this.browserArr[this.now].removeClass("rgsact");
        this.effects[this.now].start('opacity', 0).chain(function(){
          this.slides[this.now].setStyle('display', 'none');
          fadeIn.apply(this);
        }.bind(this));
      } else {
        fadeIn.apply(this);
      }
      this.now = iToShow;
    }
    

  }
});

