(function () {
	var window = this,
		$ = window.jQuery;
	
	$.fn.extend({
		detailsSlideshow: function (settings) {
			settings = $.extend({
				speed: 500,
				interval: 4000
			}, settings);
			
			var images = [],
				slideShow,
				current = 0,
				last = 0,
				vImage = $('#vImage', this);
			
			// check interval must be at least twice the speed
			// we want to avoid simultaneous animations
			if (settings.interval < (settings.speed * 2)) {
				settings.interval = settings.speed * 2;
			}
			
			$('a[rel=vDealerThumbs], a[rel=vStockThumbs]', this).each(function (i, val) {
				// get each image from the thumb list, create an actual image element out of it
				// and add it to the DOM
				images.push(($(vImage).clone().addClass('slide').css({opacity: 0}).appendTo('#vLargePhoto').attr('src', $(this).attr('href'))));
			});
			
			// proceed only if there is more than one image
			if (images.length > 1) {
				$(images[0]).css({opacity: 1});
				$(vImage).hide();
			
				// compensate for unclickable anchor below slideshow (IE)
				if ($.browser.msie) {
					$('#vLargePhoto img').css({cursor: 'pointer'}).click(function () {
						$(this).parent().find('a').click();
						return false;
					});
				}
			}
			
			slideShow = function () {
				if (images.length > 1) {
					setInterval(function () {
						current = (++current <= (images.length - 1)) ? current : 0;
						$(images[current]).css({zIndex: 1}).animate({opacity: 1}, settings.speed, function () {
							$(images[last]).css({zIndex: 0, opacity: 0});
							last = current;
						});
					}, settings.interval);
				}
			};
			
			return $(this).each(slideShow);
		}
	});
}());
