/*
 * Plixi.com jQuery plugin
 * Licensed under "do whatever you wish"
 * =====================
 * REQUIRES jQuery 1.5.x
 * =====================
 * http://twitter.com/markedman
 */

jQuery.fn.plixi = function(options) {
	// --------
	// settings
	//
	this.settings = {
		username: null,
		limit: 24,
		itemTemplate: '<li><a href="http://plixi.com/p/{{Id}}"><img src="{{ThumbnailUrl}}" title="{{Message}}"/></a></li>',
		complete: null
	}

	if(options) {
		jQuery.extend(this.settings, options);
	}

	var element = this;
	var settings = this.settings;
//	var feedUrl = 'http://api.plixi.com/api/tpapi.svc/jsonp/users/' + settings.username + '/photos';
	var feedUrl = 'http://api.twitpic.com/2/users/show.jsonp';

	// make request to plixi API

	jQuery.ajax({
		url: feedUrl,
		data: 'username='+settings.username,
		dataType: 'jsonp',
		type: 'GET',
		success: function( data, textStatus, jqXHR ) {
	
			var count = 0;
			var str = '';
			
			jQuery.each( data.images, function( i, item ) {
				var t = settings.itemTemplate;
				t = t.replace( '{{thumbnail}}', 'http://twitpic.com/show/thumb/'+item['short_id'] );
				t = t.replace( '{{large}}', 'http://twitpic.com/show/large/'+item['short_id'] );
				for( var key in item ) {
					var rgx = new RegExp( '{{' + key + '}}', 'g' );
					t = t.replace( rgx, item[key] );
				}
				
				str += t;

				if( ++count == 6 ) {
					count = 0;
					element.append('<div>' + str + '</div>');
					str = '';
				}
			});
			
			if( str != '' ) {
				element.append('<div>' + str + '</div>');
			}
			
			// complete
			if( settings.complete != null ) {
				settings.complete();
			}
			
		 }
	});	
	
};
