(function($){
	$.fn.extend({
		slideviewer : function(jumpURL, jsonURL, photoWindowSelector, captionWindowSelector, duration)
		{
			var self = this;
			if(jumpURL != ''){
				self
				    .css({cursor:'pointer'})
				    .click(function(){
					   document.location.href = jumpURL;
				    });
			}
			$.ajax({
				url: jsonURL,
				dataType : 'jsonp',
				error : function(xhr, textStatus, errorThrown)
				{
					
				},
				complete : function(xhr, testStatus)
				{
					
				}
			});
			
			window.callback = function(data)
			{
				//attaching data
				self.data('slides',data);
				self.data('counter',0);
				
				var img = $('<img/>')
				        .appendTo(photoWindowSelector),
				    span = $('<span/>')
				        .appendTo(captionWindowSelector),
					existDateNow = !!Date.now,
					currentId = existDateNow ? Date.now() : +new Date(),
					ruler = $('<span id="' + currentId + '" style="position:absolute;visibility:hidden;z-index:-9999;font-size:' + span.css('font-size') + ';font-weight:' + span.css('font-weight') + ';letter-spacing:' + span.css('font-spacing') + ';word-spacing:' + span.css('word-spacing') + ';"></span>').appendTo('body');
				
				img.load(function(){
					img
						.css({
							top : (self.data('slides')[self.data('counter')]['transition'] == 'up')? '0px' : (self.data('slides')[self.data('counter')]['transition'] == 'down')? 150 - img.height() + 'px' : (150 - img.height())/2 + 'px',
							left : (220 - img.width())/2 
						})
						.fadeTo(duration/10,1)
						.animate({
							top: (self.data('slides')[self.data('counter')]['transition'] == 'up')? 150 - img.height() + 'px' : (self.data('slides')[self.data('counter')]['transition'] == 'down')? '0px' : (150 - img.height())/2 + 'px'
						},
						{
							complete : function()
							{
								self.data('counter', (self.data('slides').length == self.data('counter')+1)? 0 : self.data('counter') + 1);
								img
								.fadeTo(duration/10,0,function(){
									img.attr({
										src : self.data('slides')[self.data('counter')]['image']
									});
									span.text(self.data('slides')[self.data('counter')]['caption']).width(ruler.text(self.data('slides')[self.data('counter')]['caption']).width()).css({left:0});
								});
							},
							duration : duration
						});
					
					span
					.animate({left:0},duration * 0.2)
					.animate({
						left : (span.width() > 220)? (220 - span.width()) + 'px' : 0
					},{duration:duration*0.6,easing:'linear'});
					
				}).fadeTo(1,0).attr({src:self.data('slides')[self.data('counter')]['image']});
				
				span.text(self.data('slides')[self.data('counter')]['caption']).width(ruler.text(self.data('slides')[self.data('counter')]['caption']).width());
			};
			
			return self;
		}
	});
	
})(jQuery);
