var twitterAPISrc = "http://twitter.com/statuses/user_timeline/davebost.json?callback=twitterCallback&count=20";
var index = 0;
var MAX_TWITS = 20;
var twitData = null;

function twitterCallback(obj)
{
    twitData = obj;
}

if (!window.SilverTwitter)
	window.SilverTwitter = {};

SilverTwitter.SilverTwitter = function() 
{
}

SilverTwitter.SilverTwitter.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;

		this.twitterText = control.content.findName("TwitterText");
		this.twitterText.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.navigateToTwit));
        
        this.upArrow = control.content.findName("upArrowEnabled");
        this.upArrow.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.upArrow_mouseLeftButtonDown));
        
        this.downArrow = control.content.findName("downArrowEnabled");
        this.downArrow.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.downArrow_mouseLeftButtonDown));
        
        this.fadeOutAnim = control.content.findName("fadeTwitOut");
        this.fadeOutAnim.addEventListener("completed", Silverlight.createDelegate(this, this.refreshTwitter));
        
        this.timer = control.content.findName("updateTimer");
        this.timer.addEventListener("completed", Silverlight.createDelegate(this, this.fadeOut));
        
        this.refreshTwitter();
	},
	
	navigateToTwit:function(sender, eventArgs)
	{
	    var url = "http://www.twitter.com/davebost/statuses/" + twitData[index].id;
        window.open(url, "twitter");
	},
	
	upArrow_mouseLeftButtonDown: function(sender, eventArgs)
	{
	    if (index == 0) return;
        index--;
        this.refreshTwitter();
	},
	
	downArrow_mouseLeftButtonDown: function(sender, eventArgs)
	{
	    if (index == (MAX_TWITS - 1)) return;
        index++;
        this.refreshTwitter();
	},
	
	fadeOut: function(sender, eventArgs)
	{
	    sender.findName("fadeTwitOut").Begin();
        index++;
	},
	
	setButtons: function()
	{
	    var upEnable = this.control.content.findName("upArrowEnabled");
        var upDisable = this.control.content.findName("upArrowDisabled");
        var downEnable = this.control.content.findName("downArrowEnabled");
        var downDisable = this.control.content.findName("downArrowDisabled");
        
        if (twitData.length == 0)
        {
            downDisable.opacity = 1;
            downEnable.opacity = 0;
            upDisable.opacity = 1;
            upEnable.opacity = 0;
            return;
        }
        
        if (index > 0)
        {
            downEnable.opacity = 1;
            downDisable.opacity = 0;
        }
        if ((index < (MAX_TWITS - 1)))
        {
            upEnable.opacity = 1;
            upDisable.opacity = 0;
        }
        if (index == 0)
        {
            upEnable.opacity = 0
            upDisable.opacity = 1;
            downEnable.opacity = 1;
            downDisable.opacity = 0;
        }
        if (index == (MAX_TWITS - 1))
        {
            downEnable.opacity = 0;
            downDisable.opacity = 1;
            upEnable.opacity = 1;
            upDisable.opacity = 0;
        }
	},
	
	refreshTwitter: function()
	{
	    if (!this.control) return;
        
        if (index > (MAX_TWITS - 1) || index < 0) index = 0;
        if (twitData) {
            this.control.content.findName("postedTime").text = this.relative_time(twitData[index].created_at);
            this.control.content.findName("TwitterText").text = twitData[index].text;
            this.setButtons();
            this.control.content.findName("fadeTwitIn").Begin();
            this.control.content.findName("updateTimer").Begin();
	    } else
	        this.control.content.findName("TwitterText").text = "no data";
	},
	
	relative_time: function(time_value)
	{
	    var values = time_value.split(" ");
	    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	    var parsed_date = Date.parse(time_value);
	    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	    delta = delta + (relative_to.getTimezoneOffset() * 60);  

	    if (delta < 60) {
		    return 'less than a minute ago';
	    } 
	    else if(delta < 120) {
		    return 'about a minute ago';
	    } else if(delta < (60*60)) {
		    return (parseInt(delta / 60)).toString() + ' minutes ago';
	    } else if(delta < (120*60)) {
		    return 'about an hour ago';
	    } else if(delta < (24*60*60)) {
		    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	    } else if(delta < (48*60*60)) {
		    return '1 day ago';
	    } else {
		    return (parseInt(delta / 86400)).toString() + ' days ago';
	    }
	}
}