

$(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;
     });  
  
     
     
     //Clear the auto-rotation and goto the previous item type
     function clickPrevious() {
          window.clearInterval(theInterval);
          gotoPrevious();
     }

     //Clear the auto-rotation and goto the next item type
     function clickNext() {
          window.clearInterval(theInterval);
          gotoNext();
     }
     
     //decrement the current item global, hide all rss-items,
     //  show the current rss-item
     function gotoPrevious() {
          currentItemType--;
          
          if (currentItemType < 0) {
               currentItemType = itemArray.length-1;
          }
          
          $(".rss-item").hide();
          $("." + itemArray[currentItemType] + "-item").fadeIn();
     }

     //increment the current item global, hide all rss-items,
     //  show the current rss-item
     function gotoNext() {
          currentItemType++;
          
          if (currentItemType > itemArray.length - 1) {
               currentItemType = 0;
          }
          
          $(".rss-item").hide();
          $("." + itemArray[currentItemType] + "-item").fadeIn();
     }
     


     //Hide all the RSS Items except the Events
     $(".need-item").hide();
     $(".prayer-item").hide();
     $(".topic-item").hide();
     
     //Bind the "click" event on the next and previous arrows
     $("#section-previous").bind("click", clickPrevious);
     $("#section-next").bind("click", clickNext);


     //Setup globals for the items
     var currentItemType = 0;
     var itemArray = ["event", "topic", "prayer", "need"];
     
     
     //Start rotating through the RSS feeds from The City
     var theInterval = window.setInterval(gotoNext, 8000);
     
     $(".rss-item").each(function() {
          if ($(this).find("h5").length == 0) {
               $(this).append("<h5 class='rss'>Nothing right now.</h5>");
               $(this).find(".rss-more").remove();
          }
     });
});
