﻿/*
* JqNews - JQuery NewsTicker
* Author: Gravagnola Saverio and Iuliano Renato
* Version: 1.0
*/

var newsVisual = 23; // Number of news to be displayed - News To Be Displayed
var intervallo = 3500; // >1500

$(document).ready(function() {
    // Totale news
    var numNews = $("#jqnews").children().length;
    
    //Overflow control
    if (newsVisual > numNews) {
        newsVisual = numNews;
    }

    // Hide news unnecessary initialization
    for (var i = newsVisual; i < numNews; i++) {
        $($("#jqnews").children()[i]).css("opacity", "0");
    }
    
    var gestInter = setInterval(jqNewsRotate, intervallo);

    // Management-mouseover mouseout
    $("#jqnews").mouseover(function() { clearInterval(gestInter) });
    $("#jqnews").mouseout(function() { gestInter = setInterval(jqNewsRotate, intervallo); });
});

function jqNewsRotate(_newsVisual) {

    //Enter the same value used to define the height and the margins of the div in the file css / style.css
    var altezzaDiv = -60; 
    var margineDiv = 5;

    // Hide the first news
    $($("#jqnews").children()[0]).animate({ opacity: 0 }, 1000, "linear", function() {
    // Upward movement
        $($("#jqnews").children()[0]).animate({ marginTop: altezzaDiv }, 1000, "linear", function() {
        // Restore hidden element position
            $($("#jqnews").children()[0]).css("margin", margineDiv);
            // Moving element hidden in the tail
            $("#jqnews").append($($("#jqnews").children()[0]));
            // Viewing the latest news
            $($("#jqnews").children()[(newsVisual - 1)]).animate({ opacity: 1 }, 1500);
        });
    });
}
