var ANWGB = {}

ANWGB.Twitter = Class.create();
ANWGB.Twitter.prototype = {
	initialize: function(klass, feed, site) {
		this.cont = $$(klass)[0];
		this.feed = feed;
		this.site = site;

		this.start	= 1;
		
		this.loadFeed();
	},
	
	loadFeed: function() {
		this.options = {
			asynchronous: true,
			method: 'post',
			parameters: '?feed=' + this.feed,
			onSuccess: function(t) { this.parseTwitter(t, 0) }.bind(this),
			onCreate: function() { this.loadTwitter() }.bind(this)
		}
		//alert(this.options.parameters);
		new Ajax.Request('/75/twitter.php', this.options);
	},
	
	parseTwitter: function(t, s) {
		this.t = t.responseText;
		
		if(this.t.match(/Warning/) == null) {
			this.json = this.t.evalJSON(true);
			//this.json = eval('(' + this.t + ')');

			this.stop = (this.json.length - 1);

			this.cont.innerHTML = '';
			for(var i = 0; i <  3/*this.json.length*/; i++) {
				if(this.json[i].text != null) {
					this.cont.innerHTML += '<div class="' + this.site + '-twit-block"><div class="l"><img src="'+ this.json[i].img + '" /><div class="clear"></div></div><div class="r"><span><b>' + this.json[i].user.replace('@', '') + '</b> ' + this.json[i].name + '</span><br /><span class="' + this.site + '-twit-msg">' + this.json[i].text + '<br /><i>on ' + this.json[i].date + '</i></span></div><div class="clear"></div></div>';

					this.t_id = this.json[0].stid;
				} else {
					this.cont.innerHTML += '<span class="' + this.site + '-twit-msg">Twitter error</span>';
				}
			}
			
			this.length = this.cont.firstChild.getWidth();
			this.span = $(this.site + '-twit-feed');
		} else {
			this.cont.innerHTML = '<span>Kon de twitter feed niet laden...</span>';
		}
		
		/*setInterval(function() {
			this.twitPoll();
		}.bind(this), 60000);*/
	},
	
	loadTwitter: function() {
		this.cont.innerHTML = '<span>Twitter laden...</span>';
	},
	
	twitPoll: function(id) {
		this.options = {
			asynchronous: true,
			method: 'post',
			parameters: '?feed=' + this.feed,
			onSuccess: function(t) { this.addTwitter(t) }.bind(this)
		}

		new Ajax.Request('/75/twitter.php', this.options);
	},
	
	addTwitter: function(t) { 
		if(t.responseText != '') {
			this.t = t.responseText;
		
			this.json = t.responseText.evalJSON(true);
			this.stop = (this.json.length - 1);
			
			for(var i = 0; i <  this.json.length; i++) {
				if(this.json[i].text != null) {
					if(this.json[0].stid != this.t_id) {
						this.div = Builder.node('div', { 
							className: this.site + '-twit-block',
							style: 'display:none;'
						}).insert('<div class="l"><img src="'+ this.json[i].img + '" /><div class="clear"></div></div><div class="r"><span><b>' + this.json[i].user.replace('@', '') + '</b> ' + this.json[i].name + '</span><br /><span class="' + this.site + '-twit-msg">' + this.json[i].text + '<br /><i>on ' + this.json[i].date + '</i></span></div><div class="clear"></div>');
						
						this.cont.insertBefore(this.div, this.cont.firstChild);
						
						new Effect.Appear(this.div, { duration: 5.0 });
					}
				}
			}
		} else {
			console.log('No updates..');
		}
	}
}

document.observe('dom:loaded', function() {
	new ANWGB.Twitter('.anwbg-twitter', 'ANWBGolf', 'anwbg');
});
