$(document).ready(function() {
   
   latest_popular();
   infinite_rotator_crossfade('.large-preview', 6);
   infinite_rotator('.small-preview', 6);
   $('.small-preview').each(function(index) {
      $(this).click(function() {
         $('.small-preview').removeClass('selected');
         $(this).addClass('selected');
         $('.large-preview').each(function(large_index) {
            if(large_index == index) {
               $('.large-preview').hide();
               $('.large-preview').eq(large_index).fadeIn(500);
            }
         });
      });
   });
   category_rotators();
   news_humor_ticker();
   
   alert_box();
   
   mailchimp();
   
});

function mailchimp() {
   $('div#mc_signup').slideUp(0);
   $('a.mailchimp').click(function() {
      $('div#mc_signup').slideToggle();
      return false;
   });
}

function alert_box() {
   $('div.alert-box').click(function() {
      document.location.href = "/sunset-rearview-2-0-features/";
   });
}

function news_humor_ticker() {
   infinite_rotator_crossfade('div.news-humor-content', 8);
}

function category_rotators() {
   $('div.category-box').each(function() {
      $(this).children().children('div.cb-recent-thumb').each(function(index) {
         $(this).attr('id', index);
         $(this).click(function() {
            var curitem = $(this).attr('id');
            $(this).parent().parent().children('div.category-content').hide();
            $(this).parent().parent().children('div.category-content').eq(curitem).fadeIn('slow');
         });
      });
   });
}

function infinite_rotator_crossfade(selector, seconds) {
   var InfiniteRotator = {
       
       init: function() {
           var itemInterval = seconds * 1000;
           var fadeTime = 1 * 500;
           var numberOfItems = $(selector).length;
           var currentItem = 0;
           
           $(selector).eq(currentItem).fadeIn(0);
          
           var infiniteLoop = setInterval( function() {
              
              
               $(selector).hide();
               
               if(currentItem == numberOfItems -1){    
                   currentItem = 0;
               } else {
                   currentItem++;
               }
               
               $(selector).eq(currentItem).fadeIn(fadeTime);
           
           }, itemInterval);
       }
   };
   InfiniteRotator.init();
}

function infinite_rotator(selector, seconds) {
   var InfiniteRotator = {
       
       init: function() {
           var itemInterval = seconds * 1000;
           var numberOfItems = $(selector).length;
           var currentItem = 0;
           
           $(selector).eq(currentItem).fadeIn(0);
          
           var infiniteLoop = setInterval( function() {
               
               $(selector).eq(currentItem).removeClass('selected');
               
               if(currentItem == numberOfItems -1){    
                   currentItem = 0;
               } else {
                   currentItem++;
               }
               $('.small-preview').removeClass('selected');
               $(selector).eq(currentItem).addClass('selected');
           
           }, itemInterval);
       }
   };
   InfiniteRotator.init();
}

function latest_popular() {
   
   $('.l-title').click(function() {
      $('.popular_posts').hide();
      $('.latest_posts').fadeIn();
      $(this).css('background-color', '#373035');
      $(this).css('color', '#dadada');
      $('.p-title').css('background-color', '#dadada');
      $('.p-title').css('color', '#373035');
   });
   
   $('.p-title').click(function() {
      $(this).css('background-color', '#373035');
      $(this).css('color', '#dadada');
      $('.l-title').css('background-color', '#dadada');
      $('.l-title').css('color', '#373035');
      $('.latest_posts').hide();
      $('.popular_posts').fadeIn();
   });
   
   $('.popular_posts').hide();
   
}
