//Create Object
var Headline = new Object();

//Create 4 attributes
Headline.arrHeadlines;      //array for all the news
Headline.CurrentElem = 0;   //Set current element on 0
Headline.Timeout;           //Create Timeout
Headline.Delay = 2250;      //Delay

jQuery(document).ready(function()
{
    //Fill array with newsitems
    Headline.arrHeadlines = jQuery('.headlinecontent').children("ul").children("li");

    //Stop if there are no items
    if (Headline.arrHeadlines.length == 0)
    {
        return;
    }

    //Set hover functions
    jQuery('.headline_nav').hover(MouseIn, MouseOut);

    //Set news tapeticker top position into var
    var top = jQuery('.headline_nav').position().top - 50;

    //Set as attribute
    jQuery('.headline_nav').attr("style", "top:" + top + "px");

    //Hide all newsitems
    Headline.HideAllNews();

    //Start with fade in
    Headline.FadeIn();
});

//Hide all Elements
Headline.HideAllNews = function()
{
    //Loop through collection
    jQuery.each(Headline.arrHeadlines, function()
    {
        //Hide current news item
        jQuery(this).hide();
    });
}

//MouseIn
function MouseIn()
{
    //Stop the current animation
    Headline.arrHeadlines.stop(true, true);

    //Clear the timeout
    clearTimeout(Headline.Timeout);
}

//MouseOut
function MouseOut()
{
    Headline.FadeIn();
}

//FadeIn function
Headline.FadeIn = function()
{
    CheckForRestart();

    //Set current element into var Elem
    var Elem = Headline.arrHeadlines[Headline.CurrentElem];

    //Fade the element 
    jQuery(Elem).fadeTo("slow", 1.0, function()
    {
        Headline.Timeout = setTimeout(function()
        {
            Headline.FadeOut();
        }, Headline.Delay)
    });
}

//FadeOut function
Headline.FadeOut = function()
{
    //Set current element into var Elem
    var Elem = Headline.arrHeadlines[Headline.CurrentElem];

    //Fade the element
    jQuery(Elem).fadeTo("slow", 0.0, function()
    {
        //Hide the element otherwise it will still be clickable if the old 
        jQuery(Elem).hide();

        //Higher the currentElement and call FadeIn
        Headline.CurrentElem++;
        Headline.FadeIn();
    });
}

function CheckForRestart()
{
    //If element is higher of same as array length = Restart
    if (Headline.CurrentElem >= Headline.arrHeadlines.length)
    {
        //Set current Elem on 0
        Headline.CurrentElem = 0;
    }
}
