var tweets = null;
var lum = null;
var color = null;

$(document).ready(function () {
    //var colors = ['#ba3232', '#30ba30', '#ba315f', '#cee100'];
    //$("b").css('color', colors[Math.floor(Math.random() * colors.length)]);
		//$("b").css('color', '#'+Math.floor(Math.random()*16777215).toString(16));
		
		
		while(lum < 128) {
			var r = Math.floor(256*Math.random());
			var g = Math.floor(256*Math.random());
			var b = Math.floor(256*Math.random());
			lum = (0.2126*r) + (0.7152*g) + (0.0722*b);
			color = 'rgb(' + r + ',' + g + ',' + b + ')';

		}
		
		$("b").css('color', color);
		
    $.ajax({
        url: "http://twitter.com/statuses/user_timeline/20499110.json",
        //url: "20499110.json",
        dataType: "jsonp",
        success: function (data) {
            tweets = data;
            if (tweets != null) {
                getTweet();
                setInterval("getTweet()", 6000);
            }
        }
    });
	
	snow();
	
});

function getTweet() {
    var randomTweet = tweets[Math.floor(Math.random() * tweets.length)];
    var status = twttr.txt.autoLink(randomTweet.text);

    $("#twitter").hide();
    $("#twitter").html("<p>" + status + "</p>");
    $("#twitter").fadeIn("slow");
}

	var no = 10;
	var dx, xp, yp;
	var am, stx, sty;
  
	var doc_width = $(window).width();
	var doc_height = $(window).height();

	dx = new Array();
	xp = new Array();
	yp = new Array();
	am = new Array();
	stx = new Array();
	sty = new Array();
	for (var i = 0; i < no; ++ i){  
		dx[i] = 0;
		xp[i] = Math.random()*(doc_width-50);
		yp[i] = Math.random()*doc_height;
		am[i] = Math.random()*10;
		stx[i] = 0.02 + Math.random()/10;
		sty[i] = 0.7 + Math.random();
		$("body").append('<div id="dot'+ i + '" style="position: absolute; z-index: '+ i +';"><img src="img/snow.gif" border="0"></div>');
	}

	function snow(){
		for (var i = 0; i < no; ++ i){
			yp[i] += sty[i];
			if (yp[i] > doc_height-50){
				xp[i] = Math.random()*(doc_width-am[i]-30);
				yp[i] = 0;
				stx[i] = 0.02 + Math.random()/10;
				sty[i] = 0.7 + Math.random();
			}
			dx[i] += stx[i];
			document.getElementById("dot"+i).style.top=yp[i]+"px";
			document.getElementById("dot"+i).style.left=xp[i] + am[i]*Math.sin(dx[i])+"px";  
		}
		snowtimer=setTimeout("snow()", 10);
	}

	function hidesnow(){
		if (window.snowtimer) clearTimeout(snowtimer)
		for (i=0; i<no; i++) document.getElementById("dot"+i).style.visibility="hidden"
	}
	
	


