//Create Object
var Headline = new Object();

//Create 3 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() {
    //Hover over news text
    jQuery('.headline_nav').hover(mousein, mouseout);

    function mousein() {
        //Stop the current animation
        Headline.arrHeadlines.stop(true, true);

        //Clear the timeout
        clearTimeout(Headline.timeout);
    }

    function mouseout() {
        Headline.fadeIn();
    }
});

//Main
jQuery(function() {
    //Fill array with newsitems
    Headline.arrHeadlines = jQuery('.headlinecontent');

    //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");

    //set all items in array attribute top
    //Headline.arrHeadlines.attr("style", "top:1em");

    //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();
    });
}

//fade In function
Headline.fadeIn = function() {
    CheckForRestart();

    //Set current element into var Elem
    var Elem = Headline.arrHeadlines[Headline.CurrentElem];


    jQuery(Elem).fadeTo("slow", 1.0, function() {
        Headline.timeout = setTimeout(function() {

            Headline.fadeOut();
        }, Headline.Delay)
    });
}

//fade Out function
Headline.fadeOut = function() {
    //Set current element into var Elem
    var Elem = Headline.arrHeadlines[Headline.CurrentElem];

    jQuery(Elem).fadeTo("slow", 0.0, function() {
        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;
    }
}
