TweetWall = new Class({
	options: {
		animation: {
			transition: Fx.Transitions.Cubic.easeOut,
			duration: 1000,
			frames: 30,
			rotate: {
				interval: 10000
			}
		},
		minimumTwits: 400,
		doFade: false
	},
	
	initialize: function(el, url, options){
		this.setOptions(options);
		this.el = el;
		this.tweets = [];
		this.tweetsToCompose = [];
		this.compositionDelay = 100;
		this.currentBubble = null;
		
		var me = this;
		// preload bubble bg
		this.preloadImage("img/tweet_bubble." + (this.needsIEFix() ? "gif" : "png"));
		setTimeout(function () {
			var req = new Request({
				method: 'get',
				url: url,
				onRequest: function(){
					me.log('Request made. Please wait...');
				},
				onComplete: function(response){
					me.onTweetsLoaded(response);
				}
			}).send();
		}, 1000);
	},
	
	preloadImage: function (url)
	{
		var img = new Image();
		img.src = url;
	},
	
	onTweetsLoaded: function(response){
		var object = JSON.decode(response);
		if (object == null || object.results == null || object.results.length == 0) {
			this.log("Couldn't load tweets");
			return;
		}
		var arr = object.results;
		var len = Math.max(arr.length, this.options.minimumTwits);
		this.log("amount: " + len);
		for(var i=0; i<len; i++){
			this.tweets.push(arr[i%arr.length]);
		}
		this.addTweets();
		if(this.options.doFade)
			this.startComposition();
	},
	
	addTweets: function (){
		for(var i=0; i<this.tweets.length; i++){
			this.addTweet(this.tweets[i]);
		}
	},
	
	addTweet: function (obj){
		var li = new Element("li");
		li.id = "tweet-" + this.tweetsToCompose.length;
		var a = new Element("a");
		var img = new Element("img");
		img.setStyle("width", 48);
		img.setStyle("height", 48);
		img.src = obj.profile_image_url;
		a.appendChild(img);
		li.appendChild(a);
		this.el.appendChild(li);
		this.tweetsToCompose.push(li);
		var index = Math.floor(Math.random()*4);
		if(this.options.doFade){
			li.setStyle("background-image", "url(img/placeholders/" + index + ".png)"); // if it is IE, use gif
			img.setStyle("opacity", 0);
		}else{
			this.addMouseInteractivity(li);
		}
	},
	
	needsIEFix: function (){
		var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		return ie55 || ie6;
	},
	
	showTweetBubble: function(tweet, c){
		if(this.currentBubble != null){
			$(document.body).removeChild(this.currentBubble);
			this.currentBubble = null;
		}
		var div = new Element("div");
		div.className = "tweet_bubble";
		div.setStyle("left", (c.left-93) + "px");
		div.setStyle("top", (c.top-210 + 18) + "px");
		var a = new Element("a");
		a.className = "bubble_link";
		a.innerHTML = "@" + tweet.from_user;
		a.href="http://twitter.com/" + tweet.from_user;
		div.appendChild(a);
		var p = new Element("p");
		p.innerHTML = unescape(tweet.text);
		div.appendChild(p);
		this.currentBubble = div;
		// reply
		a = new Element("a");
		a.className = "bubble_reply_link";
		a.innerHTML = "reply";
		a.target = "_blank";
		a.href = "http://twitter.com/home?status=@" + tweet.from_user + "+";
		div.appendChild(a);
		// user icon
		var img = new Element("img");
		img.setStyle("width", 48);
		img.setStyle("height", 48);
		img.src = tweet.profile_image_url;
		div.appendChild(img);
		$(document.body).appendChild(div);
	},
	
	hideTweetBubble: function(e){
		this.log("hide bubble");
		if(this.currentBubble != null){
			$(document.body).removeChild(this.currentBubble);
			this.currentBubble = null;
		}
	},
	
	startComposition: function (){
		this.compositionIntervalId = this.composeNextTweet.periodical(this.compositionDelay, this);
	},
	
	composeNextTweet: function (){
		var index = Math.floor(Math.random()*this.tweetsToCompose.length);
		var li = this.tweetsToCompose[index];
		this.addMouseInteractivity(li);
		var img = li.getElement("img");
		img.tween("opacity", 1);
		this.tweetsToCompose.splice(index,1);
		if (this.tweetsToCompose.length == 0) {
			$clear(this.compositionIntervalId);
		}
	},
	
	addMouseInteractivity: function (li){
		var me = this;
		var index = parseInt(li.id.split("-")[1]);
		var tweet = this.tweets[index % this.tweets.length];
		li.addEvent('mouseover', function(e){
			var c = li.getCoordinates();
			//me.log("left: " + c.left + " top: " + c.top);
			me.showTweetBubble(tweet, c);
		});
	},
	
	log: function (msg) {
		if(window.console)
			window.console.log(msg);
	}
});

TweetWall.implement(new Events); // Implements addEvent(type, fn), fireEvent(type, [args], delay) and removeEvent(type, fn)
TweetWall.implement(new Options);// Implements setOptions(defaults, options)
