;(function($) {
	$.fn.gallery = function(op) {
		var container = $(this);
		var image = container.children("img");
		var counter = 0;
		var timer;
		var images = new Array();
		var timestamp = new Date();  //we'll use a timestamp on the end of the images to trick IE's cache

		$.requestPhoto = function() {
			$.getJSON(op.file, function(data) {
				$.each(data.items, function(i, item) {
					images[images.length] = new Array(item.media.m);
				});
				$.nextPhoto();
			});
		}

		$.nextPhoto = function() {
			clearTimeout(timer);

			img_url =  images[counter][0];

			counter++;
			if(counter >= images.length)
				counter = 0;
			image.animate(op.animation,op.speed, function () {
				image.attr("src", img_url + "?t=" + timestamp.getTime());
				image.load(function () {
					//alert(img_url);
					image.animate(op.animation,op.speed, function () {
						if(images.length > 1) {
							timer = setTimeout(function () {
								$.nextPhoto();
							}, op.interval*1000);
						}
					});

					image.unbind();
				});
			});
		}

		if(!op.animation) op.animation = {opacity:'toggle'};
		if(!op.speed) op.speed = "slow";

		$.requestPhoto();
	}
})(jQuery);