var currentSlide = 0;

$(document).ready(function()
{
    $('#homepagePromo').cycle({
                slideExpr: 'img',
                fx: 'fade',
                timeout: 5000,
                cleartypeNoBg: true, //IE
                startingSlide: (new Date().getSeconds() ) % 3 * 3,
                after:  onAfter
        });
    $('#siteBody').css('backgroundImage','url(' + './images/gradient.jpg' +')');
    
//     animatePromoBubbles();
     showTwitterFeed();
});


function onAfter(curr,next,opts)
{
    currentSlide = opts.currSlide;
}

function selectSlide(index)
{
    $('#homepagePromo').cycle('pause');
    $('#homepagePromo').cycle(index);
}

function openCurrentHref()
{
    window.location.href = $('#homepagePromoHrefs a:nth-child(' + (currentSlide + 1) + ')').attr('href');
}

function animatePromoBubbles()
{
    var leftPos = Math.floor(Math.random() * 6) * 100;
    var imgId = Math.floor(Math.random() * 3) + 1;
    $('#homepagePromoBubbles').html("<img src=\"images/bubbles_white" + imgId + ".png\" alt=\"bubbles\"/>");
    $('#homepagePromoBuffer').slideUp(15000, function()
    {
      $('#homepagePromoBuffer').show();
      $('#homepagePromoBubbles').css({ left:leftPos });
      animatePromoBubbles();
    });
}

var twitterHTML = "";

function getTwitterFeed(twitters)
{
    twitter = twitters[0]; // we only care about the most recent status;
//	    var username = twitter.user.screen_name;
    var status = twitter.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, 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>';
    });
    twitterHTML = '<div id=\"twitterContent\">' + status + '</div><div id=\"twitterContentFooter\"></div>';
}

function showTwitterFeed()
{
    if (location.href.indexOf('.html') == -1) //only on homepage
    {
        var pos = $("#social").position();
        $("#twitterContainer").css( { "left": (pos.left + 100) + "px", "top": (pos.top + 51) + "px" } );
        $("#twitterContainer").fadeIn(3500);
        $("#twitterContainer").html(twitterHTML);
    }
}
