TwitterFeed = Class.create({
	initialize: function(element, options){
		Object.extend(this, pH8.Mixin.needsTemplates);
		this.element = $(element);
		this.options = options || {};
		this.ajax = {};
		this.slice = 1;
		this.slices = 1;
		this.waitForTemplates(this.options.templates, this.onTemplatesLoaded.bind(this));
	},
	delegates: {
		click: {
			'a.less': function(e){
				this.showPreviousTwits();
			},
			'a.more': function(e){
				e.stop();
				this.showNextTwits();
			},
			'a.less img': function(e){
				e.stop();
				this.showPreviousTwits();
			},
			'a.more img': function(e){
				e.stop();
				this.showNextTwits();
			},
			'a.external': function(e){
				e.stop();
				window.open(e.element().getAttribute('href'));
			}
		}
	},
	addEventDelegates: function(){
		this.element.observe('click', Event.delegate(this.delegates.click).bindAsEventListener(this));
	},
	showNextTwits: function(){
		this.slice++;
		this.update();
	},
	showPreviousTwits: function(){
		this.slice--;
		this.update();
	},
	getData: function(){
		var headers = {};
		
		if(this.twits && this.twits.size() > 0){
			var today = new Date();
			today.setTime(this.twits.first().timestamp * 1000);
			headers = {
				'If-Modified-Since': today.toUTCString()
			}
		}
		
		this.ajax = new Ajax.Request('/ajax/twitter.php', {
			method: 'get',
			requestHeaders: headers,
			onSuccess: this.parseJSONData.bind(this),
			onFailure: this.update.bind(this)
		});
	},
	parseJSONData: function(transport){
		this.twits = $A(transport.responseJSON);
		this.slices = Math.ceil(this.twits.size() / this.options.twitsPerSlice);
		this.update();
	},
	update: function(){
		var html = this.templates.twitterfeed.process({
			twits: this.twits.eachSlice(this.options.twitsPerSlice)[this.slice - 1],
			nice_difference: this.niceDifference,
			slice: this.slice,
			slices: this.slices,
			formatText: this.formatText
		});
		this.element.down('ul').update(html);
		
		if(this.slice < this.slices){
			this.element.down('a.more').show();
		}else{
			this.element.down('a.more').hide();
		}
		if(this.slice > 1){
			this.element.down('a.less').show();
		}else{
			this.element.down('a.less').hide();
		}
	},
	formatText: function(text){
		return text.gsub(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/, function(match){
				return '<a href="'+match[0]+'" class="external">'+match[0]+'</a>';
		});
	},
	onTemplatesLoaded: function(){
		this.templatesLoaded = true;
		this.onReady();
	},
	niceDifference: function(timestamp){
		var today = new Date();
		var difference = Math.floor(today.getTime() / 1000) - parseInt(timestamp);
		
		if(difference >= 86400){
			var formatted = Math.floor(difference / 86400);
			if(formatted == 1){
				return formatted + ' day ago'; 
			}else{
				return formatted + ' days ago'; 
			}
		}else if(difference >= 3600){
			var formatted = Math.floor(difference / 3600);
			if(formatted == 1){
				return formatted + ' hour ago'; 
			}else{
				return formatted + ' hours ago'; 
			}
		} else if(difference >= 60){
			var formatted = Math.floor(difference / 60);
			if(formatted == 1){
				return formatted + ' minute ago'; 
			}else{
				return formatted + ' minutes ago'; 
			}
		}else{
			return 'less then 1 minute ago'; 
		}
	},
	setRefreshInterval: function(){
		this.interval = setInterval(this.getData.bind(this), (this.options.refreshAfterSeconds * 1000));
	},
	onReady: function(){
		if(this.templatesLoaded){
			this.addEventDelegates();
			this.setRefreshInterval();
			this.getData();
		}
	}
});