(function( $ ) {
	$.widget( "ui.imagefade", {
		options: {
			delay: 4000,
			zIndex: 1000,
			speed: 2000
		},
		_create: function() {
			var self = this;
			self.imgCurrent=0;
			self.imgCount=0;
			$('img',self.element).each(function(index){
				this.style.zIndex = (1000-self.options.zIndex);
				this.style.position = 'absolute';
				if (index > 1)
					this.style.display = 'none';
				self.imgCount++;
			});
			if (self.imgCount > 1) {
				setTimeout(function() {
					self.swap();
				},self.options.delay);
			}
		},
		swap: function() {
			var self = this;
			//alert($('img').eq(self.imgCurrent).attr('src'));
			$('img',self.element).eq(self.imgCurrent).fadeOut(self.options.speed,function(){
				setTimeout(function() {
					self.swap();
				},self.options.delay);
			});
			self.imgCurrent = (self.imgCurrent+1 > self.imgCount-1 ? 0 : self.imgCurrent+1);
			$('img',self.element).eq(self.imgCurrent).fadeIn(self.options.speed);
		}
	});
})(jQuery);
