jQuery.extend({

	/*
	 * parse twitter autolinks
	 */
	twitterAutolinks: function( str ) {
		var linkexp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		var userexp = /\S?[@]([A-Z0-9_]+)\S?/ig;
		var hashexp = /\S?[#]([A-Z0-9_]+)\S?/ig;
		return str.replace( linkexp, '<a href="$1" target="_blank">$1</a>' )
			.replace( userexp, '<a class="twitteruser" href="http://twitter.com/$1" target="_blank">@$1</a>' )
			.replace( hashexp, '<a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>' );
	}

});

jQuery.fn.extend({

	/*
	 * display last twitter status using twitter search api
	 */
	tweet: function( options ) {
		// --------
		// settings
		//
		this.settings = {
			username: 'tvoff'
		}
		if(options) {
			jQuery.extend(this.settings, options);
		}
		
		var url = 'http://search.twitter.com/search.json';
		var elmt = this;

		var profile = 'http://a2.twimg.com/profile_images/753882300/TVOFF-PROMO-01_Web_normal.jpg';
		var userurl = 'http://twitter.com/' + this.settings.username;
		var userlink = '<a class="twitteruser" href="' + userurl + '" target="_blank">' + this.settings.username + '</a>';

		/* TEST
		var str = 'This Thursday at Siltanen: EELS IN MY PANTS by TV OFF with @EMPERATRON - http://www.facebook.com/event.php?eid=136269746441754';		
		elmt.html( '<span>' + userlink + ': ' + jQuery.twitterAutolinks( str ) + '</span>' );
		*/

		jQuery.ajax({
			url: url,
			data: 'q=from:'+this.settings.username+'&rpp=1',
			dataType: 'jsonp',
			type: 'GET',
			success: function( data, textStatus, jqXHR ) {
			
				if( data.results && data.results[0] ) {
					var str = data.results[0].text;
					elmt.html( '<span>' + userlink + ': ' + jQuery.twitterAutolinks( str ) + '</span>' );
				}
				
			}			
		});

	},
	

	/* -------------------------------------------------------------------------
	 * TWITPICS
	 */
	_displayPhotostream: function( settings, elmt, photoArray ) {
		var buf = '';
		var icnt = 0;
		
		photoArray.sort( function( a, b ) {
			return b.Time - a.Time;
		});
		
		jQuery.each( photoArray, function( i, item ) {
			var t = '';
			if( item['Type'] == 'tumblr-photo' ) {
				t = settings.tumblrItemTemplate;
			}
			else if( item['Type'] == 'twitpic' ) {
				t = settings.twitItemTemplate;
			}
			for( var key in item ) {
				var rgx = new RegExp( '{{' + key + '}}', 'g' );
				t = t.replace( rgx, item[key] );
			}
				
			if( i < 8 ) {
				jQuery(elmt).find('div a:nth-child('+(i+1)+')').replaceWith( t );
			}
			else {
				buf = buf + t;
				if( ++icnt == 8 ) {
					jQuery(elmt).append( '<div>'+buf+'</div>' );
					buf = '';
					icnt = 0;
				}
			}
		});
				
		if( buf != '' ) {
			jQuery(elmt).append( '<div>'+buf+'</div>' );
		}
	},
	 
	photostream: function( options ) {	
		// --------
		// settings
		//
		this.settings = {
			username: null,
			tumblr: null,
			limit: 32,
			twitItemTemplate: '<a class="twitpic" href="{{MediumImageUrl}}"><img src="{{ThumbnailUrl}}" title="{{Message}}" style="top:0px" width="60" height="60"/></a>',
			tumblrItemTemplate: '<a href="{{Url}}" target="_blank"><img src="{{ThumbnailUrl}}" title="{{Message}}" style="top:0px" width="60" height="60"></a>',
			complete: null
		}
		if(options) {
			jQuery.extend(this.settings, options);
		}

		var elmt = jQuery(this);
		var settings = this.settings;

		var photoArray = new Array();

		// make request to plixi API
		var feedUrl = 'http://api.plixi.com/api/tpapi.svc/jsonp/users/' + settings.username + '/photos';
	
		jQuery.ajax({
			url: feedUrl,
			data: 'ps=' + settings.limit,
			dataType: 'jsonp',
			type: 'GET',
			success: function( data, textStatus, jqXHR ) {
				jQuery.each( data.List, function( i, item ) {
					var convItem = {
						MediumImageUrl: item['MediumImageUrl'],
						ThumbnailUrl: item['ThumbnailUrl'],
						Message: item['Message'],
						Time: item['UploadDate'],
						Type: 'twitpic'
					};
					photoArray.push( convItem );
				});
				
				// make tumblr api request
				var feedUrl = 'http://' + settings.tumblr + '/api/read/json';
				
				jQuery.ajax({
					url: feedUrl,
					data: 'type=photo',
					dataType: 'jsonp',
					type: 'GET',
					success: function( data, textStatus, jqXHR ) {					
						jQuery.each( data.posts, function( i, item ) {
							var convItem = {
								MediumImageUrl: item['photo-url-500'],
								ThumbnailUrl: item['photo-url-75'],
								Message: '',
								Time: item['unix-timestamp'],
								Url: item['url-with-slug'],
								Type: 'tumblr-photo'
							};
							photoArray.push( convItem );
						});
						
						$(this)._displayPhotostream( settings, elmt, photoArray );
		
						// complete
						if( settings.complete != null ) {
							settings.complete();
						}
					}
				}); // --end ajax req to tumblr
			
			 }
			 
		});	// --end ajax req to plixi
	}

});
