

$(document).ready(function() {
     
$('span:empty').hide();
      

     $(".through").each(function(i) {
          var myDateEnd = $(this).parent().children(".rss-date-end").text();
          var myTimeEnd = $(this).parent().children(".rss-time-end").text();

          if ((myDateEnd == "") && (myTimeEnd == "")) {
               $(this).hide();
          }
     });
     

      
     $('#other-sites').slideUp('medium');
     $('#show-hide').attr("href", "#"); // disable the link if we have JavaScript

     $('#show-hide').click(function(){
          var myScrolly=window.scrollY;
          if ($('#other-sites').is(':hidden')) {
               $('#other-sites').slideDown('slow');
               $('#show-hide').addClass('down');
          } else {
               $('#other-sites').slideUp('slow');
               $('#show-hide').removeClass('down');
          }
          window.scroll(0,myScrolly);
     });
     
     
     $('ul.sf-menu').superfish({
          dropShadows: false,
          autoArrows: false
     });
     
     String.prototype.reverse = function() {
          var splitext = this.split("");
          var revertext = splitext.reverse();
          var reversed = revertext.join("");
          return reversed;
     }
     
     function rssShowMore(e) {
          if ($(this).parent().siblings('.rss-details:visible').length > 0) {
               var myEmail = $(this).parent().parent().find('.rss-email a').text();
               var myPhone = $(this).parent().parent().find('.rss-phone').text();
               
               $(this).parent().parent().find('.rss-email a').text(myEmail.reverse());
               $(this).parent().parent().find('.rss-email a').attr("href", '');
               $(this).parent().parent().find('.rss-phone').text(myPhone.reverse());
               
               $(this).parent().siblings('.rss-details').find('span:empty').show();
               $(this).parent().siblings('.rss-details').hide();
               $(this).text("show more info")
          } else {
               var myEmail = $(this).parent().parent().find('.rss-email a').text();
               var myPhone = $(this).parent().parent().find('.rss-phone').text();
               
               $(this).parent().parent().find('.rss-email a').text(myEmail.reverse());
               $(this).parent().parent().find('.rss-email a').attr("href", 'mailto:' + myEmail.reverse());
               $(this).parent().parent().find('.rss-phone').text(myPhone.reverse());
               
               $(this).parent().siblings('.rss-details').find('span:empty').hide();
               $(this).parent().siblings('.rss-details').slideDown('fast');
               /* $(this).text("hide more info") */
          }
     }

     $('#tabs div.rss-tab').hide(); // Hide all divs
     $('.rss-details').hide(); // Hide all the details
     $('.rss-more').bind("click", rssShowMore);
     $('#tabs div.rss-tab:first').show();
     $('#tab-1 .region-hub-website a').attr('href', 'https://hubs.apexcommunity.net/default.aspx');
     $('#tab-2 .region-hub-website a').attr('href', 'https://region1.apexcommunity.net/default.aspx');
     $('#tab-3 .region-hub-website a').attr('href', 'https://region2.apexcommunity.net/default.aspx');
     $('#tab-4 .region-hub-website a').attr('href', 'https://region3.apexcommunity.net/default.aspx');
     $('#tab-5 .region-hub-website a').attr('href', 'https://region4.apexcommunity.net/default.aspx');
     $('#tab-6 .region-hub-website a').attr('href', 'https://region5.apexcommunity.net/default.aspx'); 
     $('#tabs #tab-1').show();
     $('#tabs #tab-1 .rss-feed').show(); // Show the first div
     $('#tabs #subNavigation ul li:first').addClass('active'); // Set the class for active state
     $('#tabs #subNavigation ul li a').click(function(){ // When link is clicked
           
          $('#tabs #subNavigation ul li').removeClass('active'); // Remove active class from links
          $(this).parent().addClass('active'); //Set parent of clicked link class to active
          var currentTab = $(this).attr('href'); // Set currentTab to value of href attribute
          $('#tabs div.rss-tab').hide();
          
          $(currentTab).show(); // Show div with id equal to variable currentTab
          return false;
     });  
  




      window.sparkboxFadeSpeed = 300;
      window.sparkboxFadeDuration = 6000;
     
     
     // home page rss section
     if ($(".home .rss-group").length > 1) {
        window.sparkboxFaderInterval = setInterval("sparkboxNextItem('.home .rss-group')", 6000);
        
        $(".home #section-next").bind("click", function() {sparkboxNextItem(".home .rss-group"); sparkboxStop(); });
        $(".home #section-previous").bind("click", function() {sparkboxPreviousItem(".home .rss-group"); sparkboxStop(); });
     }
     
     
     // family ministry page
     $(".section-nav").show();

     if ($(".family-ministry .rss-item").length > 1) {

       window.sparkboxFaderInterval = setInterval("sparkboxNextItem('.family-ministry .rss-item')", window.sparkboxFadeDuration);

       $(".family-ministry #section-next").bind("click", function() { sparkboxNextItem(".family-ministry .rss-item"); sparkboxStop(); });
       $(".family-ministry #section-previous").bind("click", function() { sparkboxPreviousItem(".family-ministry .rss-item"); sparkboxStop(); });
     } else {
       $(".family-ministry #section-next").hide();
       $(".family-ministry #section-previous").hide();
     }

     if ($(".hero-family-ministry .hero-item").length > 1) {
        window.sparkboxImageFaderInterval = setInterval("sparkboxNextItem('.hero-family-ministry .hero-item')", 8000);
     }

});


function sparkboxUpdateTitle() {
  if ($(".home .rss-group").length) {
    if ($(".home .rss-group.current").hasClass("rss-network")) {
      $(".column-two-header span").text("Network Wide");
    } else if ($(".home .rss-group.current").hasClass("rss-region1")) {
      $(".column-two-header span").text("Region 1");
    } else if ($(".home .rss-group.current").hasClass("rss-region2")) {
      $(".column-two-header span").text("Region 2");
    } else if ($(".home .rss-group.current").hasClass("rss-region3")) {
      $(".column-two-header span").text("Region 3");
    } else if ($(".home .rss-group.current").hasClass("rss-region4")) {
      $(".column-two-header span").text("Region 4");
    } else if ($(".home .rss-group.current").hasClass("rss-region5")) {
      $(".column-two-header span").text("Region 5");
    }
    
    $(".column-two-header span").fadeIn(window.sparkboxFadeSpeed * .9   );
  }
}


function sparkboxStop() {
  clearInterval(window.sparkboxFaderInterval);
}


function sparkboxNextItem(theSelector) {
  if ($(".animating").length == 0) {
    if ($(".home .rss-group").length) {
      $(".column-two-header span").fadeOut("fast");
    }
  
    if ($(theSelector + ".current").next().length) {
  
      $(theSelector + ".current").addClass("animating").fadeOut(window.sparkboxFadeSpeed, function() {
        $(this).removeClass("current");
        $(this).next().addClass("current");
        sparkboxUpdateTitle();
        
        $(this).next().fadeIn(window.sparkboxFadeSpeed, function() {
          $(".animating").removeClass("animating");
        });
      });
  
    } else {

      $(theSelector + ".current").addClass("animating").fadeOut(window.sparkboxFadeSpeed, function() {
        $(this).removeClass("current");
        $(theSelector + ":first").addClass("current");
        sparkboxUpdateTitle();
      
        $(theSelector + ":first").fadeIn(window.sparkboxFadeSpeed, function() {
          $(".animating").removeClass("animating");
        });
      });
    }
  }
}

function sparkboxPreviousItem(theSelector) {
  if ($(".animating").length == 0) {
    if ($(".home .rss-group").length) {
      $(".column-two-header span").fadeOut("fast");
    }

    if ($(theSelector + ".current").prev().length) {
  
      $(theSelector + ".current").addClass("animating").fadeOut(window.sparkboxFadeSpeed, function() {
        $(this).removeClass("current");
        $(this).prev().addClass("current");
        sparkboxUpdateTitle();
      
        $(this).prev().fadeIn(window.sparkboxFadeSpeed, function() {
          $(".animating").removeClass("animating");
        });
      });
  
    } else {
  
      $(theSelector + ".current").addClass("animating").fadeOut(window.sparkboxFadeSpeed, function() {
        $(this).removeClass("current");
        $(theSelector + ":last").addClass("current");
        sparkboxUpdateTitle();
          
        $(theSelector + ":last").fadeIn(window.sparkboxFadeSpeed, function() {
          $(".animating").removeClass("animating");
        });
      });
    }
  }
}


