function loadTweet() {
    $.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=DCITCorporation&count=1&callback=?", function(data) {
        var tweet = data[0].text;
        tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
            return '<a href="'+url+'">'+url+'</a>';
        }).replace(/B@([_a-z0-9]+)/ig, function(reply) {
            return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
        $("#lastTweetLbl").html(tweet);
    });
}

function animateSlide(curr, next) {
    var panel1 = $("#slide"+curr);
    var panel2 = $("#slide"+next);
    var panel1Width = panel1.outerWidth() * -1;
    panel1.animate({marginLeft: panel1Width}, 1200,
        function() {
            $("#ledImg"+curr).attr("src", "rsc/button_disabled.png");
            $("#ledImg"+next).attr("src", "rsc/button_enabled.png");
            panel1.css("display", "none");
            panel2.css("marginLeft", panel2.outerWidth());
            panel2.css("display", "block");
            panel2.animate({marginLeft: 0}, 1200);
            if(next == 1) {
                fireworks();
            }
        }
    );
}

function setUpAnimation() {
    var curr = 1;
    var next = 2;
    fireworks();
    var ref = setInterval(function() {
        animateSlide(curr, next);
        curr = next;
        next++;
        if(next == 8) {
            next = 1;
        }
    }, 1000 * 21);
    
    function jumpAnimation(event) {
        clearInterval(ref);
        next = parseInt($(this).attr("id").replace("slideLink", ""));
        animateSlide(curr, next);
        curr = next;
        next++;
        if(next == 8) {
            next = 1;
        }
        
        ref = setInterval(function() {
            animateSlide(curr, next);
            curr = next;
            next++;
            if(next == 8) {
                next = 1;
            }
        }, 1000 * 21);
    }
    
    $("#slideLink1").click(jumpAnimation);
    $("#slideLink2").click(jumpAnimation);
    $("#slideLink3").click(jumpAnimation);
    $("#slideLink4").click(jumpAnimation);
    $("#slideLink5").click(jumpAnimation);
    $("#slideLink6").click(jumpAnimation);
    $("#slideLink7").click(jumpAnimation);
}

$(document).ready(function() 
{
    setUpAnimation();
    loadTweet();
});
