jQuery(function($) { 
  advanceSlide();

  $(window).resize(function() { 
    resizeContainersYo();
  });
});

function advanceSlide() {
  $(".box").each(function() { 

   var thisBox = $(this),
      thisSlides = $(thisBox).find(".slide"),
      totalSlide = $(thisSlides).size();
        
    $(thisBox).click(function() { 
      var nextslide = parseFloat($(thisBox).attr("data-active")) + 1;
          thisBoxHeight = $(thisBox).height();
          
      
      if(nextslide > totalSlide) {
        var nextslide = 1;
      }
      
        var newSlideBox = $(thisBox).find($(".slide[data-order=" + nextslide +"]"));

        //Resize
        if($(newSlideBox).hasClass("text")) {
            if($(newSlideBox).hasClass("double")) {
              var newSlideBoxHeigth = 405; 
            } else {
              var newSlideBoxHeigth = $(newSlideBox).height();            
            }

              var newMarginTop =  (thisBoxHeight -  newSlideBoxHeigth) / 2;      
              

            if(newMarginTop > 0) {
              if($(newSlideBox).hasClass("double")) {
                $(newSlideBox).find("h3").css({'paddingTop' : newMarginTop});
              }
                        
              else {
                   
              if($(thisBox).hasClass("bottom")) {
               $(newSlideBox).css({'paddingTop' : newMarginTop});
              } else {
                $(newSlideBox).css({'paddingBottom' : newMarginTop});
              }
            
             }
            }        
        }          
                   
      if($(".active").hasClass("double") && ($(thisBox).attr("id") === "collection")) {          
                              
      } else {
      
        $(thisSlides).hide().removeClass("active");
        $(newSlideBox).show().addClass("active");    
        $(thisBox).attr('data-active', nextslide);

        var hashed = "#" + $(thisBox).attr("id") + "-" + nextslide;
        document.location.replace(hashed);

      }    
    });
  });


      $(".sbox1").click(function() { 
         var thisBox2 = $(this),
          thisSlides2 = $(thisBox2).find(".slide2"),
          totalSlide2 = $(thisSlides2).size();
        
            
        var nextslide2 = parseFloat($(thisBox2).attr("data-sactive")) + 1;      
        if(nextslide2 > totalSlide2) {
          var nextslide2 = 1;
        }    
        var newSlideBox2 = $(thisBox2).find($(".slide2[data-order=" + nextslide2 +"]"));

        $(thisSlides2).hide().removeClass("active");
        $(newSlideBox2).show().addClass("active");    
        $(thisBox2).attr('data-sactive', nextslide2);
      
      });

      $(".sbox2").click(function() { 
         var thisBox2 = $(this),
          thisSlides2 = $(thisBox2).find(".slide2"),
          totalSlide2 = $(thisSlides2).size();
        
            
        var nextslide2 = parseFloat($(thisBox2).attr("data-sactive")) + 1;      
        if(nextslide2 > totalSlide2) {
          var nextslide2 = 1;
        }    
        var newSlideBox2 = $(thisBox2).find($(".slide2[data-order=" + nextslide2 +"]"));

        $(thisSlides2).hide().removeClass("active");
        $(newSlideBox2).show().addClass("active");    
        $(thisBox2).attr('data-sactive', nextslide2);
      
      });

}


function resizeContainersYo() {
$(".box").each(function() { 
    
    var thisBox = $(this),
        thisSlides = $(this).find(".slides"),
        thisBoxHeight = $(thisBox).height();

        if($(thisSlides).hasClass("text")) {
            var newSlideBoxHeigth = $(thisSlides).height(),
                newMarginTop =  (thisBoxHeight -  newSlideBoxHeigth) / 2;      

            if(newMarginTop > 0) {
              if($(thisBox).hasClass("bottom")) {
               $(newSlideBox).css({'paddingTop' : newMarginTop});
              } else {
                $(newSlideBox).css({'paddingBottom' : newMarginTop});
              }
            }        
        }          
});
}
