$(document).ready(function () {

  $('.homepage-testimonials-thumbs .views-row a').click(function () {
    $row = $(this).parent().parent().parent();

    $row.parent().find('.views-row').removeClass('testimonial-thumb-selected');
    $row.addClass('testimonial-thumb-selected');
    var nodeid = $row.attr('id').substr(10);

    if (nodeid != undefined) {
      $('.vid-player:visible').hide();
      $('#vid-player-'+nodeid).show();
    }
    
    return false;
  });
  
  if ($('#content-content .view-product-landing').size() > 0) {
  
    var pids = Array();
    
    $('#content-content .view-product-landing .attachment .views-row')
      .each(function (i) {
        pids[i] = $(this).attr('id').substring(15);
      })
      .filter(':first').addClass('selected');
      
    var timer = setInterval(function () {
      // get current center
      var $curr = $('#content-content .view-product-landing .attachment').find('.selected');
      var $next = $curr.next();
      if ($next.size() == 0) {
        $next = $('#content-content .view-product-landing .attachment .views-row:first');
      }
      
      var currID = $curr.attr('id').substring(15);
      var nextID = $next.attr('id').substring(15);
      
      swapSelector(currID,nextID);
    },5000);
  }

  
  $('.homepage-testimonials-thumbs .views-row').hover(function () {
    $(this).find('.views-field-field-video-embed').css('opacity',0.35);
    $(this).find('.views-field-title').show();
  }, function () {
    $(this).find('.views-field-field-video-embed').css('opacity',1);
    $(this).find('.views-field-title').hide();
  });
  
  $('#content-content .view-product-landing .attachment .imagecache-product_icon_default').click(function (e) {
    clearInterval(timer); 
  
    var $clickedRow = $(this).parent().parent().parent();
    var newpid = $clickedRow.attr('id').substring(15);

    var $currentRow = $('#content-content .view-product-landing .attachment').find('.selected');
    var oldpid = $currentRow.attr('id').substring(15);
    
    swapSelector(oldpid, newpid);
  });

  function swapSelector(oldpid, newpid) {

    // Get the selector
    var $newThumbRow = $('#content-content .view-product-landing .attachment').find('#selector-thumb-'+newpid);
    var $newThumbArrow = $newThumbRow.find('.selector-arrow');
    
    // Get the selector
    var $oldThumbRow = $('#content-content .view-product-landing .attachment').find('#selector-thumb-'+oldpid);
    var $oldThumbArrow = $oldThumbRow.find('.selector-arrow');
    
    var $oldContent = $('#content-content .view-product-landing .selector-mainbody').find('#selector-main-'+oldpid);
    var $newContent = $oldContent.parent().find('#selector-main-'+newpid);
    
    $oldContent.fadeOut();
    $newContent.fadeIn();
    
    $oldThumbRow.removeClass('selected');
    $oldThumbArrow.hide();
    $newThumbRow.addClass('selected');
    //$newThumbArrow.show();
  }

});
