﻿/**
Controls the behaviour of the scrolling news objects on the homepage
**/

 /*Global variables */
var stories;
var currentPosition = 0;
var timeout = null;  

/**
* Moves to the next news story and makes it visible
*/
function changeActiveStory(){
    var nextPosition = currentPosition + 1;
    $(stories[currentPosition]).fadeOut(250)
    setTimeout('$(stories[currentPosition]).addClass("hide")', 250);
    currentPosition++;       
    if (currentPosition == stories.length){
        currentPosition = 0;
    }
    setTimeout('$(stories[currentPosition]).removeClass("hide");$(stories[currentPosition]).fadeIn(250);', 260);
}

/**
* Fade out the news stories initially
*/
function FadeOutNewsStories(){
    stories = $('.newsstory');
    $('.newsstory').fadeOut(0);
    $('.newsstory').addClass('hide');
    $(stories[0]).removeClass('hide');
    $(stories[0]).fadeIn(250);
}

/**
* Set the news stories to fade in and out on timeout
*/
function SetNewsStoriesOnTimeout(){
    timeout = setInterval(changeActiveStory, 5000);
}

/**
* Set the news stories to pause 
*/
function PauseNewsStoriesOnHover(){
    $('.newsstory').hover(function(){
                            clearTimeout(timeout);
                          }, function(){
                            timeout = setInterval(changeActiveStory, 5000);
                          });
}

/**
* Set up the news stories to scroll and pause on mouse over
*/
function SetUpNewsStories(){
    FadeOutNewsStories();
    SetNewsStoriesOnTimeout();
    PauseNewsStoriesOnHover();
}

/**
* Run onload functions
*/
$(document).ready(function(){
    SetUpNewsStories()
});

