/**
 *	These function require the DOM to be loaded
 *
 */
window.addEvent('domready', function() {
	getTweets();
	getPhotoStream();
	initWallpaper();
});

/**
 *	Interact with the wallpaper
 *
 */
function initWallpaper() {
	if ( !$chk( $('wallpaper') ) )
		return false;
	
	// get start height
	var wallpaperHeight = $('wallpaper').getStyle('height');
	
	// set animation
	var slide = new Fx.Morph('wallpaper', {duration: '2000', transition: Fx.Transitions.Back.easeOut});
		
	$('wallpaper').addEvent('click', function(e) {
		// cancel previous slide
		slide.cancel();

		if ( $('wallpaper').getStyle('height') == wallpaperHeight ) {
			slide.start({
				'height': $('wallpaper').getElement('img').getScrollSize().y
			});
		} else {
			slide.start({
				'height': wallpaperHeight
			});
		}
	});
}

/**
 *	Gets a tweet :)
 *
 */
function getTweets() {
	// Twitter JSONP request
	var r = new Request.JSONP({
		url: 'http://twitter.com/statuses/user_timeline/werockthestreet.json',
		method: 'get',
		data: {
			count: 5
		},
		noCache: true,
		onComplete: function(myTweets) {
			var tweets = '';

			myTweets.each(function(tweet) {
				var date = new Date().parse(tweet.created_at).format('%Y %B %d %H:%M');
				var text = tweet.text;
				
				// concatenate tweets
				tweets += '<p>' + parseTweet(text) + '<br /><em>' + date + '</em></p>' 
				
			});

			$('tweet').set('html', tweets);
		}
	}).send();
}

/**
 * make the Twitter @, www and https?, s?ftp and ssh links clickable
 *
 * Patterns inspired from Michele Costantino Soccio (http://www.soccio.it/michelinux/) 
 * and Alan Hogan (http://alanhogan.com/contact) and their clickable twitter @http://www.soccio.it/code/twitter/blogger.js 
 *
 */
function parseTweet(tweet) {
	// the patterns
	var dynamicLinkPattern	= /\bwww\.\w.\w/ig;
	var staticLinkPattern	= /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g;
	var userLinkPattern		= /\B@([_a-z0-9]+)/ig;
	
	// concatenate
	var parsedTweet			= '';

	parsedTweet = tweet.replace(dynamicLinkPattern, 'http://$&');
	parsedTweet = parsedTweet.replace(staticLinkPattern, '<a target="_blank" href="$1">$1</a>');
	parsedTweet = parsedTweet.replace(userLinkPattern, '@<a target="_blank" href="http://twitter.com/$1">$1</a>');
		
	return parsedTweet;
}

/** 
 *	Gets photostream from flickr
 *	key: 985914e925b7cc6bb3d1bd1cef9d8525
 *	NSID: 41373034@N08
 *
 */
function getPhotoStream() {
	// Flickr JSONP request
	var r = new Request.JSONP({
		url: 'http://api.flickr.com/services/rest',
		method: 'get',
		data: {
			per_page: 15,
			page: 1,
			format: 'json',
			api_key: '985914e925b7cc6bb3d1bd1cef9d8525',
			user_id: '41373034@N08',
			method: 'flickr.people.getPublicPhotos',
			jsoncallback: 'processPhotoStream'
		},
		noCache: true
	}).send();
}

function processPhotoStream(rsp) {
	if (rsp.stat != "ok"){
		// something broke!
		return;
	}
	
	// the photos
	var photos = rsp.photos.photo;

	// the html
	var flickr = '';

	photos.each(function(photo) {
		var src = 'http://farm'+photo.farm+'.static.flickr.com/'+photo.server+'/'+photo.id+'_'+photo.secret+'_s.jpg';

		flickr += '<div> \
			<a href="http://www.flickr.com/photos/41373034@N08"> \
				<img src="'+src+'" alt="" /> \
			</a> \
		</div>';
	});

	$('flickr').set('html', flickr);
}