$(document).ready(function() {
  $("#ticker ul").rss("http://ul.com/global/eng/pages/corporate/newsroom/rss.jsp", {
    limit: 5,
    ssl: false,
    template: '{entry}<li><a href="{url}">{title}</a></li>{/entry}'
  });
});

$(window).load(function() { home.init(); });

var home = {
  init: function () {
    this.slideshows();
    setTimeout(home.ticker, 100);
    // this.twitter();
    // helps maintain equal heights between sections
    this.equalHeight($('.bucket section, .consumers section:eq(1)'));
    
    this.equalHeight($('section.showcase.stories-two h4'));
    this.equalHeight($('section.showcase.stories-two .content'));
    
    this.equalHeight($('section.showcase.stories-three h4'));
    this.equalHeight($('section.showcase.stories-three .content'));
    
    this.equalHeight($('section.four-column section>p'));
    this.equalHeight($('section.four-column section>section'));
    this.rollovers();
    
  },
  slideshows: function () {
    var anything_config = {
      timing    : 1000,
      delay     : 8000,
      easing    : false
    };

    $('.masthead .slideshow').anythingSlider({
      // Appearance
      resizeContents      : false,      // If true, solitary images/objects in the panel will expand to fit the viewport
      easing              : anything_config.easing,   // Anything other than "linear" or "swing" requires the easing plugin or jQuery UI

      buildArrows         : false,      // If true, builds the forwards and backwards buttons
      buildNavigation     : true,      // If true, builds a list of anchor links to link to each panel
      buildStartStop      : false,      // If true, builds the start/stop button

      toggleArrows        : false,     // If true, side navigation arrows will slide out on hovering & hide @ other times
      toggleControls      : false,     // if true, slide in controls (navigation + play/stop button) on hover and slide change, hide @ other times

      // Function
      enableArrows        : true,      // if false, arrows will be visible, but not clickable.
      enableNavigation    : true,      // if false, navigation links will still be visible, but not clickable.
      enableStartStop     : false,      // if false, the play/stop button will still be visible, but not clickable. Previously "enablePlay"
      enableKeyboard      : true,      // if false, keyboard arrow keys will not work for this slider.

      // Navigation
      startPanel          : 1,         // This sets the initial panel
      changeBy            : 1,         // Amount to go forward or back when changing panels.
      hashTags            : false,      // Should links change the hashtag in the URL?
      infiniteSlides      : true,      // if false, the slider will not wrap & not clone any panels
      navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
      navigationSize      : false,     // Set this to the maximum number of visible navigation tabs; false to disable

      // Slideshow options
      autoPlay            : true,     // If true, the slideshow will start running; replaces "startStopped" option
      autoPlayLocked      : false,     // If true, user changing slides will not stop the slideshow
      autoPlayDelayed     : false,     // If true, starting a slideshow will delay advancing slides; if false, the slider will immediately advance to the next slide when slideshow starts
      pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
      stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page. This also stops the rewind effect when infiniteSlides is false.
      playRtl             : false,     // If true, the slideshow will move right-to-left

      // Times
      delay               : anything_config.delay,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
      resumeDelay         : 15000,     // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
      animationTime       : anything_config.timing, // How long the slideshow transition takes (in milliseconds)
      delayBeforeAnimate  : 0,         // How long to pause slide animation before going to the desired slide (used if you want your "out" FX to show).

      // Callbacks
      onInitialized       : function(e, slider) {  }, // Callback when the plugin finished initializing
      onSlideBegin        : function(e, slider) {  }, // Callback before slide animates

      // Interactivity
      clickForwardArrow   : "click",         // Event used to activate forward arrow functionality (e.g. add jQuery mobile's "swiperight")
      clickBackArrow      : "click",         // Event used to activate back arrow functionality (e.g. add jQuery mobile's "swipeleft")
      clickControls       : "click focusin", // Events used to activate navigation control functionality
      clickSlideshow      : "click"         // Event used to activate slideshow play/stop button
    }); 
  },
  ticker:function() { /* source of reference - http://webdesignfan.com/building-a-compact-jquery-news-ticker/ */
    $('#ticker').each(function() {
      var ticker = $(this);
      var fader = $('<span class="fader">&nbsp;</span>').css({display:'inline-block'});
      var links = ticker.find('ul>li>a');
      ticker.find('ul').replaceWith(fader);
        
      var counter = 0;
      var curLink;
      var fadeSpeed = 600;
      var showLink = function()
      {
        var newLinkIndex = (counter++) % links.length;
        var newLink = $(links[newLinkIndex]);
        var fadeInFunction = function()
        {
            curLink = newLink;
              fader.append(curLink).fadeIn(fadeSpeed); 
        };
        if (curLink)
        {
             fader.fadeOut(fadeSpeed, function(){
                 curLink.remove();
                 setTimeout(fadeInFunction, 0);
             });
         }
         else
         {
             fadeInFunction();
         }
      };
      
      var speed = 4500;
      var autoInterval;
      
      var startTimer = function()
      {
          autoInterval = setInterval(showLink, speed);
      };
      ticker.hover(function(){
          clearInterval(autoInterval);
      }, startTimer);

      fader.fadeOut(0, function(){
          fader.text('');
          showLink();
      });
      startTimer();
        
    });
  },
  twitter:function(){
    getTwitters('twt', {
      id: 'SafetyatHome',
      count: 1,
      enableLinks: true,
      ignoreReplies: true,
      clearContents: true,
      template: '"%text%" <br /><a class="time" href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a>'
    });
  },
  equalHeight:function(group){
    tallest = 0;
    group.each(function() {
      thisHeight = $(this).height();
      if(thisHeight > tallest) {
         tallest = thisHeight;
      }
    });
    group.height(tallest);
  },
  rollovers:function(){
    $('#homeContainer section.industries li li a').hover(function(){
      var img = $(this).find('img');
      var img_array = img.attr('src').split('.');
      var concat = '';
      $.each(img_array, function(index, value){
        switch (index) {
          case 0:
            concat += value; 
            break;
          
          case img_array.length - 1:
            concat += '_hover.' + value; 
            break;

          default:
            concat += '.' + value; 
            break;
        };
      }); 
      img.attr('src', concat);
    },function() {
      var img = $(this).find('img');
      img.attr('src', img.attr('src').replace('_hover', ''));
    });
  }
};

