$(function(){
  var interval = null;
  $.fn.extend({
    slide: function(items){
      var obj = this;
      var showItem = function($link){
        if($link.hasClass('current') || $(".js_inTransact").length > 0){
          return false;
        }
          
        var currentItem = items[$link.attr('rel')];

        if(currentItem != null){
          $link.siblings("a").addClass("js_disabled");  
          $link.removeClass("js_disabled");  
          $link.addClass("js_inTransact");  
          $("a.current").removeClass("current");
          $link.addClass("current");

          var newDiv = $(document.createElement('div')).css({
            display: "none",
            position: "absolute",
            zIndex: 94,
            top: $(".slideshowImage > div > div").offset().top,
            left: $(".slideshowImage > div > div").offset().left,
            width: $(".slideshowImage > div > div").outerWidth(),
            height: $(".slideshowImage > div > div").outerHeight(),
            background: "url(" + currentItem.background + ") no-repeat"
          });  
          newDiv.attr("id", "slideshownew");
          
          $("body").append(newDiv);
          $(".slideshowText a").attr('href', currentItem.link);
          $(".slideshowText h2 > a").text(currentItem.title);
          $(".slideshowText h3 a").text(currentItem.text);  
          $(".slideshowText > a").text(currentItem.link_label);        
          
          $("#slideshownew").fadeIn(500, function(){
            $(".slideshowImage > div > div").css({
              background: "url(" + currentItem.background + ") no-repeat"
            });
           
            $("#slideshownew").fadeOut(100, function(){
              $(this).remove();
              $(".js_inTransact").removeClass("js_inTransact");  
              
            });
          });
        }        
      };
      
      var stopReproduction= function(){
        if(interval != null){
          clearInterval(interval);
          interval = null;
        }
      };      
      
      if(items != null && items.length > 0){
        $(".slideshowPagination").hide();
        
        for(var i = 0; i < items.length; i++){
          var link = $(document.createElement('a')).attr('rel', i)
                               .attr('href', 'javascript:void(0)')
                               .text(i + 1);
                               
          if(i == 0){
            link.addClass("current");
          }
          
          link.css({ marginRight: 5 });
          
          $(".slideshowPagination").append(link);
          $(".slideshowPagination").css({ zIndex: 99 });
        }
        
        $(".slideshowPagination a").click(function(e){  
          stopReproduction();
          showItem($(this));
        });  
        
        var adjustPagingPos = function(){
          $(".slideshowPagination").css({
            position: "absolute",
            top: $("#slideshow").position().top + $("#slideshow").outerHeight() - 30,
            left: $("#slideshow").position().left + $("#slideshow").outerWidth() - 250        
          });
        };
        
        adjustPagingPos();
        $(window).scroll(adjustPagingPos);
        $(window).resize(adjustPagingPos);  

        $(window).load(function(){
          $(".slideshowPagination").fadeIn(200);
          
          interval = setInterval(function(){
            var index = parseInt($("a.current").attr("rel"));
            
            
            
            if(index >= 0 && (index + 1) < items.length){
              index += 1;
            }else{
              index = 0;
            }
            
            var link = $('.slideshowPagination a[rel="' + index + '"]');
            showItem(link);
          }, 7000);
          
        });
      }
    }
  });
});
