//
// slideShow, v. 1.0.5
//
function Slider(newConfig) {

  //
  // parameters
  //
  var config = {
    ulClass: 'mySlideShow',
    showNavigation: true,
    showButtons: true,
    scrollSpeed: 800,
    presentationSpeed: 2000,
    initialTimeout: 1000
  };
  if (typeof(newConfig) == "object") {
    for (var name in newConfig ) {
      config[name] = newConfig[name];
    }
  }

  //
  // event functions
  //
  this._clickNavigation = function(event) {
    self.stopPresentation();
    self.move($(event.target).data('index'));
  }
  this._changeNavigation = function(previous,next) {
    this.objekt.find('.navigation li').eq(previous).css('background-position','center 0');
    this.objekt.find('.navigation li').eq(next).css('background-position','center -26px');
  }
  this._mouseoverNavigation = function(event) {
    if ($(event.target).data('index') != self.pozice) {
        $(event.target).css('background-position','center -13px');
    }
  };
  this._mouseoutNavigation = function(event) {
    if ($(event.target).data('index') != self.pozice) {
        $(event.target).css('background-position','center 0');
    }
  };
  this._mouseoverButton = function(event) {
    $(event.target).css('background-position','center center');
  };
  this._mouseoutButton = function(event) {
    $(event.target).css('background-position','-100% -100%');
  };
  this._clickPreviousButton = function(event) {
    self.stopPresentation();
    self.left();
  };
  this._clickNextButton = function(event) {
    self.stopPresentation();
    self.right();
  };

  //
  // class variables and html
  //
  var seznam = $('ul.' + config.ulClass);
  this._slides = seznam.children();
  this.pocet = this._slides.length;
  // end if not at least 2 objects to animate
  if (this.pocet<2)
    return;
  this.objekt = $('<div class="SlideShow ' + seznam.attr("class") + '"><div class="buttonsandscroll"><div class="scroll"></div></div></div>');
  seznam.before(this.objekt);
  seznam.removeClass();
  this.objekt.find('.scroll').append(seznam);
  this.scrollSpeed = config.scrollSpeed;
  this.presentationTime = config.presentationSpeed;
  var sirka = this.objekt.width();
  this.pozice = 0;
  this.block = false;
  var self = this;
  // bind buttons
  if (config.showButtons) {
    this.objekt.find('div.buttonsandscroll').prepend('<ul class="buttons"><li class="prev-button"></li><li class="next-button"></li></ul>');
    this.objekt.find('li.prev-button').bind('mouseover',this._mouseoverButton);
    this.objekt.find('li.prev-button').bind('mouseout',this._mouseoutButton);
    this.objekt.find('li.prev-button').bind('click',this._clickPreviousButton);
    this.objekt.find('li.next-button').bind('mouseover',this._mouseoverButton);
    this.objekt.find('li.next-button').bind('mouseout',this._mouseoutButton);
    this.objekt.find('li.next-button').bind('click',this._clickNextButton);
  }
  // container width
  this.objekt.find('.scroll ul').width(this.pocet*sirka);
  // navigation
  if (config.showNavigation) {
    var lista = $('<ul class="navigation"></ul>');
    this.objekt.append(lista);
    for (var i=0;i<this.pocet;i++) {
        var li = $('<li></li>');
        li.bind('click',this._clickNavigation);
        li.bind('mouseover',this._mouseoverNavigation);
        li.bind('mouseout',this._mouseoutNavigation);
        li.data('index',i);
        lista.append(li);
    }
    this._changeNavigation(0,0);
  }

  //
  // runtime functions
  //
  this._presentation = function() {
    var self=this;
    this.right();
    this._timer = setTimeout(function(){self._presentation()},this.presentationTime+this.scrollSpeed);
  }
  this.stopPresentation = function() {
    clearTimeout(this._timer);
  }
  this._timer = setTimeout(function(){self._presentation()},config.initialTimeout);

  this._finalize = function() {
    if (this.pozice == this.pocet) {

      this._slides.eq(1).before(this._slides.eq(0));
      this.objekt.find('.scroll').scrollTo(0);
      this.pozice = 0;
    }
    if (this.pozice == -1) {
      this._slides.eq(-2).after(this._slides.eq(-1));
      this.objekt.find('.scroll').scrollTo(this._slides.eq(-1));
      this.pozice = this.pocet-1;
    }
    this.block= false;
  }

  this.move = function(poradi) {
    if (this.block) {
      return;
    } else {
      this.block = true;
    }
    if (poradi==this.pozice) {
      this.block = false;
      return;
    }
    this.objekt.find('.navigation li').eq(this.pozice).css('background-position','center 0');
    this.objekt.find('.navigation li').eq(poradi).css('background-position','center -26px');
    this.pozice=poradi;
    this.objekt.find('.scroll').scrollTo(this._slides.eq(poradi),this.scrollSpeed,{onAfter:function(){self._finalize()}});

  }
  this.left = function() {
    if (this.block) {
      return;
    } else {
      this.block = true;
    }
    var self = this;

    this.pozice--;
    if (this.pozice==-1) {
      this._changeNavigation(0,this.pocet-1);
      this._slides.eq(0).before(this._slides.eq(-1));
      this.objekt.find('.scroll').scrollTo(this._slides.eq(0));
      this.objekt.find('.scroll').scrollTo(this._slides.eq(-1),this.scrollSpeed,{onAfter:function(){self._finalize()}});
    } else {
      this._changeNavigation(this.pozice+1,this.pozice);
      this.objekt.find('.scroll').scrollTo(this._slides.eq(this.pozice),this.scrollSpeed,{onAfter:function(){self._finalize()}});
    }
  }

  this.right = function(event) {
    if (this.block) {
      return;
    } else {
      this.block = true;
    }
    this.block = true;
    this.pozice++;
    var self = this;
    if (this.pozice==this.pocet) {
      this._changeNavigation(this.pocet-1,0);
      this._slides.eq(-1).after(this._slides.eq(0));
      this.objekt.find('.scroll').scrollTo(this._slides.eq(-1));
      this.objekt.find('.scroll').scrollTo(this._slides.eq(0),this.scrollSpeed,{onAfter:function(){self._finalize()}});
    } else {
      this._changeNavigation(this.pozice-1,this.pozice);
      this.objekt.find('.scroll').scrollTo(this._slides.eq(this.pozice),this.scrollSpeed,{onAfter:function(){self._finalize()}});
    }

  }
  
}
