$(document).ready(function() {
    
var imgs = $('.tpl-slideshow img');
                
  if(imgs.length > 1) {
  
  var i = 0;
  var no = 1;
  var zIndex = 10;
  var first = $(imgs.get(0));
  var next = first.next();
  var tmp = null;
  var timeoutThread = null;
  var effectThread = null;
  
  var switchRadio = function() {
    
    var currentSwitcher = $('.tpl-slideshow .tpl-switcher a.active');
    var nextSwitcher = currentSwitcher.next();
    
    if(nextSwitcher.length == 0)
      nextSwitcher = $('.tpl-slideshow .tpl-switcher').children().next().next();
    
    $(currentSwitcher[0]).removeClass('active');
    $(nextSwitcher[0]).addClass('active');
    //currentSwitcher[0].className = '';
    //nextSwitcher[0].className = 'active';
    
  };
  var func = function() {
    
    timeoutThread = window.setTimeout(function() {
      
      switchRadio();
      
      next.hide();
      next.css('zIndex', ++zIndex);
      next.fadeIn(2000, function() {
        
        tmp = next.next();
        
        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.appendChild(document.createTextNode(i));
    
    switcher.swoImg = img;
    
    $(switcher).click(function() {
      
      window.clearTimeout(timeoutThread);
      imgs.stop();
      imgs.hide();
      this.swoImg.show();
      this.swoImg.css('opacity', '1');
      $(switchers).removeClass('active');
      $(this).addClass('active');
      
    });
    switchers.push(switcher);
    $('.tpl-slideshow .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();
  });
  
  }
  
});

