﻿$(document).ready(function() {
    var currentItem = 0;
    var numberOfItems = $("#newsContent li").length;
    var paddingBottom = 10;
    var banimate = false;
    var itemInterval;
    var rotateDirection = 'up';
    var timeRotate = false;

    itemInterval = setInterval(headline_rotate, 5000); //time in milliseconds

    $("#newsArea").css("overflow", "hidden");

    $("#down").click(function() {
        rotateDown();
    });

    $("#up").click(function() {
        rotateUp();
    });

    function rotateDown() {
        if (currentItem != (numberOfItems - 2) && numberOfItems > 2 && banimate == false) {
            banimate = true;
            var height = $("#newsContent li:eq(" + currentItem + ")").height() + paddingBottom;
            window.status = height + ":" + currentItem;
            var marginTop = parseInt($("#newsContent").css("margin-top"), 10);
            height = height * -1;
            $("#newsContent").animate({
                marginTop: marginTop + height
            }, 500, "linear", animateComplete);
            currentItem++;
        }
    }

    function rotateUp() {
        if (currentItem != 0 && numberOfItems > 2 && banimate == false) {
            banimate = true;
            var height = $("#newsContent li:eq(" + (currentItem - 2) + ")").height() + paddingBottom;
            window.status = height + ":" + currentItem;
            var marginTop = parseInt($("#newsContent").css("margin-top"), 10);
            $("#newsContent").animate({
                marginTop: marginTop + height
            }, 500, "linear", animateComplete);
            currentItem--;
        }
    }

    function animateComplete() {
        banimate = false;
    }

    function headline_rotate() {
        if (currentItem == ((numberOfItems - 2) && numberOfItems > 2) || currentItem == 0) {
            if (rotateDirection == 'up') {
                rotateDirection = 'down'
            }
            else {
                rotateDirection = 'up'
            }
        }

        if (rotateDirection == 'down') {
            rotateDown();
        }
        else {
            rotateUp();
        }
    }
});
	
