// JavaScript Document
RunNewsTicker = function (width, maxLength) {

    var current_item = 0;

    $("#baby_feed div:first").clone().appendTo("#baby_feed");
    //just increments 'current_item'
    incrementItem = function () {
        if (current_item < maxLength) {
            current_item++;
        }
        else { current_item = 0; }
    }

    decrementItem = function () {
        if (current_item > 0) {
            current_item--;
        }
        else { current_item = maxLength; }
    }


    //array for news items.. the text that will be displayed dynamically
    //	var feed_items = [];
    //	feed_items.push("Amer Networks launches new 8 port, Layer 2, PoE switch with Synnex USA");
    //	feed_items.push("Visit Amer Networks at Interop 2011 in  Las Vegas. Booth number 907");
    //	feed_items.push("Amer Networks announces new family of wireless N products.");

    //counter for looping through array


    //	//set the text to the first item in the array
    //	$("#feed").text(feed_items[ current_item-1]);


    //start the timer
    var newsTimer = setInterval("nextItem()", 7000);


    //a variable that controls whether the "next" a "previous" buttons 
    //will allow a user to surf through news items 
    var interactive = true;

    //previous button clears the old timer
    //subtracts one from 'current_item'
    //calls the  'nextItem' function 
    //them resets the timer 
    $("#previous").bind("click", function (e) {
        e.preventDefault();
        if (interactive) {
            clearInterval(newsTimer);
            prevItem();
            newsTimer = setInterval("prevItem()", 7000);
        }
    });

    //next is the same as previous except that it
    //adds to 'current_item' rather than subtract 
    $("#next").bind("click", function (e) {
        e.preventDefault();
        if (interactive) {
            clearInterval(newsTimer);
            nextItem();
            newsTimer = setInterval("nextItem()", 7000);
        }
    });

    GetStartLocation = function () {
        return current_item * width;
    }

    VerifyPrevMax = function () {
        if (current_item == 0) {
            current_item = maxLength;
            $("#baby_feed").css("margin-left", -GetStartLocation());
        }
    }

    VerifyNextMax = function () {
        if (current_item == maxLength) {
            current_item = 0;
            $("#baby_feed").css("margin-left", -GetStartLocation());
        }
    }
    nextItem = function () {
        //de-activate buttons
        interactive = false;
        //animate the text out of the div

        VerifyNextMax();
        incrementItem();
        $("#baby_feed").animate({ marginLeft: -GetStartLocation() }, 1200, function () {

            //on complete...
            VerifyNextMax();
            interactive = true;
        });
    }

    prevItem = function () {
        //de-activate buttons
        interactive = false;
        //animate the text out of the div

        VerifyPrevMax();

        decrementItem();

        $("#baby_feed").animate({ marginLeft: -GetStartLocation() }, 1200, function () {

            //on complete...
            VerifyPrevMax();
            interactive = true;
        });
    }
}
