(function($){  
  $.fn.imagePlayer = function(options) {  
    var defaults = {  
      xmlUrl: null,
      group: 0,
      stepSpeed: 4000,
      fadeSpeed: 1000
    };
    var options = $.extend(defaults, options);
    var imagesInfo = null;
    var $obj = this;
    $.get(options.xmlUrl, function(data){
      imagesInfo = $(data).find('obrazek');
      if (options.group > 0) {
        imagesInfo = imagesInfo.filter('[skupina="' + options.group + '"]');
      }
      imagesInfo.each(function(){
        $('<img />')
          .attr({
            src: $(this).attr('src')
          })
          .appendTo($obj)
          .addClass('ip-item')
          .hide()
        ;
      });
     
      var $navigation = $('<ul />').addClass('navigation').appendTo($obj);
      var $prev = $('<li />').addClass('prev').appendTo($navigation);
      var $pause = $('<li />').addClass('pause').appendTo($navigation);
      var $play = $('<li />').addClass('play').appendTo($navigation);
      var $next = $('<li />').addClass('next').appendTo($navigation);
      
      var imagePlayer = function () {
        var activeIndex = 0;
        var interval = null;
        var self = this;
        var isRunning = false;
        var firstRun = true;
        
        this.changeImage = function(step, speed) {
          activeIndex += step;
          if (activeIndex >= $('.ip-item').length) {
            activeIndex = 0;
          }
          if (activeIndex < 0) {
            activeIndex = $('.ip-item').length - 1;
          }
          
          if (speed == undefined || speed == 0) {
            $('.ip-item:visible').fadeOut('fast');
            $('.ip-item').eq(activeIndex).fadeIn('fast');
          } else {
            $('.ip-item:visible').fadeOut(speed);
            $('.ip-item').eq(activeIndex).fadeIn(speed);
          }
        }
        
        this.play = function(){
          if (!isRunning) {
            $play.addClass('active')
            $pause.removeClass('active');
            isRunning = true;
            if (firstRun) {
              this.changeImage(0, options.fadeSpeed);
              firstRun = false;
            } else {
              this.changeImage(1, options.fadeSpeed);
            }
            interval = setInterval(function(){self.changeImage(1, options.fadeSpeed)}, options.stepSpeed)
          }
        }
        
        this.pause = function(){
          $play.removeClass('active')
          $pause.addClass('active');
          isRunning = false;
          if (interval) {
            clearInterval(interval);
          }
        }
        
        this.navigateTo = function(step){
          this.pause();
          this.changeImage(step, 0);
        }
      }
      
      var ip = new imagePlayer();
      $('.ip-item').eq(0).load(function(){
        ip.play();
      });
      
      if ($('.ip-item').length <= 1) {
        ip.pause();
        $navigation.hide();
      } else {
        $navigation.find('li').hover(
          function(){
            $(this).addClass('hover')
          },
          function(){
            $(this).removeClass('hover')
          }
        );

        $play.click(function() {
          ip.play();
        });

        $pause.click(function() {
          ip.pause();
        });

        $next.click(function() {
          ip.navigateTo(1);
        });

        $prev.click(function() {
          ip.navigateTo(-1);
        });
      }
    });
    return this;
  };  
})(jQuery);  
