 $(document).ready(function(){
  var currentView = $(".tabcontent:visible").attr("class").split(" ").slice(-1); // Get the current className
  $('.tabcontent').each(function(i){
   if(currentView == 'imgview') {
    $('div:odd',this).after('<div class="hr"><hr /></div>'); // wrap every 2nd element
    $('div.hr:last-child',this).remove();
   } else if(currentView == 'textview') {
    $('.textview div',this).filter(function(index) { // wrap every 3rd element
     return index % 3 == 2;
    }).after('<br class="clearer" />');
   }
  });
  $("a.switch-view").toggle(function(){
   $(".tabcontent:visible").fadeOut("fast", function() {
    $('a.switch-view').addClass("swap");
    $('.tabcontent').removeClass("imgview").addClass('textview');
    $('.tabcontent').children('div').remove('.hr').removeAttr("class");
    $('.tabcontent').each(function(i){
     $(this).children('div').filter(function(index) { // wrap every 3rd element
      return index % 3 == 2;
     }).after('<br class="clearer" />');
    });
   }).fadeIn("fast");
  }, function () {
   $(".tabcontent:visible").fadeOut("fast", function() {
    $('a.switch-view').removeClass("swap");
    $('.tabcontent').removeClass("textview").addClass('imgview');
    $('.tabcontent').children().removeAttr("class").remove('br');
    $('.tabcontent').each(function(i){
     $(this).children('div:odd').after('<div class="hr"><hr /></div>');
     $('div.hr:last-child',this).remove();
    });
   }).fadeIn("fast");
  });
 });

