(function ($) {
	$.fn.nivoZoom = function (options) {
		var settings = $.extend({},
		$.fn.nivoZoom.defaults, options);
		if (settings.overlay) {
			if (! ($.browser.msie && $.browser.version.substr(0, 1) < 8)) {
				$('body').prepend('<div id="nivoOverlay" />');
				$('#nivoOverlay').css({
					position: 'fixed',
					top: 0,
					left: 0,
					width: '100%',
					height: '100%',
					background: settings.overlayColor,
					opacity: settings.overlayOpacity,
					'z-index': 90,
					display: 'none'
				})
			}
		}
		return this.each(function () {
			var context = $(this);
			var nivoZooms = $('a.nivoZoom', context);
			nivoZooms.each(function () {
				var link = $(this);
				if (link.is('a')) {
					var img = $(this).find('img:first');
					link.css({
						position: 'relative',
						display: 'inline-block'
					});
					link.attr('title', 'Click to zoom');
					link.append('<div class="nivoZoomHover" />');
					var nivoZoomHover = $('.nivoZoomHover', link);
					nivoZoomHover.css('opacity', '0');
					link.hover(function () {
						if (!link.hasClass('zoomed')) {
							nivoZoomHover.stop().animate({
								opacity: settings.zoomHoverOpacity
							},
							300)
						}
					},
					function () {
						if (!nivoZoomHover.hasClass('loading')) {
							nivoZoomHover.stop().animate({
								opacity: 0
							},
							300)
						}
					});
					link.bind('click', function () {
						if ($('img.nivoLarge', link).length == 0) {
							nivoZoomHover.addClass('loading');
							loadImg(img, link, function () {
								nivoZoomHover.removeClass('loading');
								doZoom(img, link, nivoZoomHover)
							})
						} else {
							doZoom(img, link, nivoZoomHover)
						}
						return false
					})
				}
			})
		});
		function doZoom(img, link, nivoZoomHover) {
			var imgLarge = $('img.nivoLarge', link);
			if (link.hasClass('zoomed')) {
				if (settings.overlay) $('#nivoOverlay').fadeOut(settings.speed / 2);
				if ($('.nivoCaption', link).length > 0) {
					$('.nivoCaption', link).fadeOut(settings.speed / 2)
				}
				imgLarge.fadeOut(settings.speed / 2, function () {
					img.animate({
						opacity: 1
					},
					settings.speed / 2)
				});
				link.removeClass('zoomed')
			} else {
				if (settings.overlay) $('#nivoOverlay').fadeIn(settings.speed / 2);
				nivoZoomHover.css('opacity', '0');
				img.animate({
					opacity: 0
				},
				settings.speed / 2, function () {
					imgLarge.fadeIn(settings.speed / 2, function () {
						showCaption(img, imgLarge, link)
					})
				});
				link.addClass('zoomed')
			}
		}
		function showCaption(img, imgLarge, link) {
			if ($('.nivoCaption', link).length > 0) {
				var nivoCaption = $('.nivoCaption:first', link);
				if (!nivoCaption.hasClass('nivo-processed')) {
					var imgWidth = img.width();
					if (imgWidth == 0) imgWidth = img.attr('width');
					var imgHeight = img.height();
					if (imgHeight == 0) imgHeight = img.attr('height');
					var bigImgWidth = imgLarge.width();
					if (bigImgWidth == 0) bigImgWidth = imgLarge.attr('width');
					var bigImgHeight = imgLarge.height();
					if (bigImgHeight == 0) bigImgHeight = imgLarge.attr('height');
					nivoCaption.css({
						width: bigImgWidth,
						opacity: settings.captionOpacity
					});
					if (link.hasClass('topRight')) {
						nivoCaption.css({
							top: (bigImgHeight - nivoCaption.outerHeight()) + 'px',
							right: '0px'
						})
					} else if (link.hasClass('bottomRight')) {
						nivoCaption.css({
							bottom: '0px',
							right: '0px'
						})
					} else if (link.hasClass('bottomLeft')) {
						nivoCaption.css({
							bottom: '0px',
							left: '0px'
						})
					} else if (link.hasClass('center')) {
						nivoCaption.css({
							top: Math.ceil(imgHeight / 2 - bigImgHeight / 2) + (bigImgHeight - nivoCaption.outerHeight()) + 'px',
							left: (imgWidth / 2 - bigImgWidth / 2) + 'px'
						})
					} else {
						nivoCaption.css({
							top: (bigImgHeight - nivoCaption.outerHeight()) + 'px',
							left: '0px'
						})
					}
					nivoCaption.addClass('nivo-processed')
				}
				nivoCaption.fadeIn(settings.speed / 2)
			}
		}
		function loadImg(img, link, callback) {
			var newImg = new Image();
			$(newImg).load(function () {
				$(this).addClass('nivoLarge');
				$(this).css({
					position: 'absolute',
					display: 'none',
					'z-index': 99
				});
				if (navigator.userAgent.match(/MSIE \d\.\d+/)) {
					link.css('z-index', '100')
				}
				if (link.hasClass('topRight')) {
					$(this).css({
						top: '0px',
						right: '0px'
					})
				} else if (link.hasClass('bottomRight')) {
					$(this).css({
						bottom: '0px',
						right: '0px'
					})
				} else if (link.hasClass('bottomLeft')) {
					$(this).css({
						bottom: '0px',
						left: '0px'
					})
				} else if (link.hasClass('center')) {
					var imgWidth = img.width();
					if (imgWidth == 0) imgWidth = img.attr('width');
					var imgHeight = img.height();
					if (imgHeight == 0) imgHeight = img.attr('height');
					var bigImgWidth = $(this).width();
					if (bigImgWidth == 0) bigImgWidth = $(this).attr('width');
					var bigImgHeight = $(this).height();
					if (bigImgHeight == 0) bigImgHeight = $(this).attr('height');
					$(this).css({
						top: (imgHeight / 2 - bigImgHeight / 2) + 'px',
						left: (imgWidth / 2 - bigImgWidth / 2) + 'px'
					})
				} else {
					$(this).css({
						top: '0px',
						left: '0px'
					})
				}
				$(this).attr('title', 'Click to close');
				link.append($(this));
				callback.call(this)
			}).attr('src', link.attr('href'))
		}
	};
	$.fn.nivoZoom.defaults = {
		speed: 500,
		zoomHoverOpacity: 0.8,
		overlay: false,
		overlayColor: '#333',
		overlayOpacity: 0.5,
		captionOpacity: 0.8
	}
})(jQuery);
