var TwitterGitter = new Class({
	
	//implements
	Implements: [Options,Events],

	//options
	options: {
		count: 3,
		sinceID: 1,
		link: true,
		onRequest: $empty,
		onComplete: $empty
	},
	
	//initialization
	initialize: function(username,options) {
		//set options
		this.setOptions(options);
		this.info = {};
		this.username = username;
	},
	
	//get it!
	retrieve: function() {
		new JsonP('http://twitter.com/statuses/user_timeline/' + this.username + '.json',{
			data: {
				count: this.options.count,
				since_id: this.options.sinceID
			},
			onRequest: this.fireEvent('request'),
			onComplete: function(data) {
				//linkify?
				if(this.options.link) {
					data.each(function(tweet) { tweet.text = this.linkify(tweet.text); },this);
				}
				//complete!
				this.fireEvent('complete',[data,data[0].user]);
			}.bind(this)
		}).request();
		return this;
	},
	
	//format
	linkify: function(text) {
		//courtesy of Jeremy Parrish (rrish.org)
		return text.replace(/(https?:\/\/\S+)/gi,'<a href="$1">$1</a>').replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>').replace(/(^|\s)#(\w+)/g,'$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
	}
});



window.addEvent('domready',function() {

		window.addEvent('domready', function() {		
	
			
			$$('.contactlink').removeProperty('href');
			$$('.close a').removeProperty('href');
			
			$$('.contactlink').addEvent('click', function(event) {
				$('contactinfo').set("tween", {duration: 400}).setOpacity(0).fade(1);
				$('curtain').set("tween", {duration: 400}).setOpacity(0).fade(1);
			});
			
			$$('.close a').addEvent('click', function(event) {
				$('contactinfo').set("tween", {duration: 400}).setOpacity(1).fade(0);
				$('curtain').set("tween", {duration: 400}).setOpacity(1).fade(0);
			});
			

		});


				$('tweets-here').set('html','');
				//get information
				var myTwitterGitter = new TwitterGitter('lkieksi',{
					count: 3,
					onComplete: function(tweets,user) {
						tweets.each(function(tweet,i) {
							new Element('div',{
								html: '<h3 class=\"title\"><a href=\"http://twitter.com/lkieksi/status/' + tweet.id + '\">'+ tweet.created_at.substr(0,11) +' 2010</a></h3><p>' + tweet.text + '</p>',
								'class': 'tweet clear article'
							}).inject('tweets-here');
							$('tweets-here').set('opacity','0').fade('in');

							
							$$('.article').addEvents({
								'mouseenter': function(){
											this.tween('background-color', '#363a3c');
									},
								 'click': function(){
											this.setStyles({'opacity' : 0.4, 'visibility' : 'visible'}).fade('in');
											var thelink = this.getElement('a').getProperty('href');
											window.location.href = thelink;
										},
										'mouseleave': function(){
											this.tween('background-color', '#25292b');
										}
							});
							
											
							
						});
					}
				}).retrieve();
			
		});
		