$(document).ready(function() {
  
  var imgs = $('.tpl-category-picture img');
  
  if(imgs.length > 1) {
  
    var i  = 0;
    var no = 1;
    var zIndex = 10;
    var first = $(imgs.get(0));
    var next = first.parent().next().children('img');
    var tmp = null;
    var timeoutThread = null;
    var effectThread = null;
    
    var switchRadio = function() {
      
      var currentSwitcher = $('.tpl-category-picture .tpl-switcher a.active');
      var nextSwitcher = currentSwitcher.next();
      
      if(nextSwitcher.length == 0)
        nextSwitcher = $('.tpl-category-picture .tpl-switcher').children().next().next();
      
      $(currentSwitcher[0]).removeClass('active');
      $(nextSwitcher[0]).addClass('active');
      
    };
    
    var func = function() {
      
      timeoutThread = window.setTimeout(function() {
        
        switchRadio();
        
        next.hide();
        next.css('zIndex', ++zIndex);
        
        $('.tpl-category-picture a.current').removeClass('current');
        next.parent().addClass('current');
        
        var last = next;
        
        next.fadeIn(2000, function() {
          
          tmp = next.parent().next().children('img');
          
          if(tmp.length != 0)
            next = tmp;
          else
            next = first;
          
          
          func();
          
        });
        
      }, 3000);
      
    };
    
    var switchers = [];
    
    imgs.each(function() {
      
      var img = $(this);
      
      img.css('zIndex', zIndex - i++);
      img.css('position', 'absolute');
      
      var switcher = document.createElement('a');
      $(switcher).text(no++);
      
      switcher.swoImg = img;
      
      
      $(switcher).click(function() {
        
        window.clearTimeout(timeoutThread);
        imgs.stop();
        imgs.hide();
        this.swoImg.show();
        this.swoImg.css('opacity', '1');
        $('.tpl-category-picture a.current').removeClass('current');
        //img.parent().siblings().removeClass('current');
        this.swoImg.parent().addClass('current');
        $(switchers).removeClass('active');
        $(this).addClass('active');
        
      });
      switchers.push(switcher);
      $('.tpl-category-picture .tpl-switcher').append(switcher);
      
    });
    
    $(switchers).addClass("number");
    
    if(switchers.length > 0)
      $(switchers[0]).addClass('active');
    
    func();
    
    $('a.next').click(function() {
      if($('.number.active').next('.number').length > 0)
        $('.number.active').next('.number').click();
      else
        $('.number.active').parent().children('.number').first().click();
    });
    
    $('a.prev').click(function() {
      if($('.number.active').prev('.number').length > 0)
        $('.number.active').prev('.number').click();
      else
        $('.number.active').parent().children('.number').last().click();
    });
    
  } else {
    $('.tpl-switcher .prev, .tpl-switcher .next').remove();
  }
  
  $('.tpl-switcher a.view').click(function() {
    $('.tpl-category-picture .highslide.current').trigger('click');
  });
  
});

