var homeImage;

// == Begin Custom plugin ==
;(function($){
	$.fn.slideShow = function(options){
		var config = {
			speed: 1000,
			childrenSelector : ">img",
			nextSelector : 'a.nextimage',
			prevSelector : 'a.previmage',
			onAfter : function(newImage){},
			onBefore: function(newImage){},
			onSetup : function(firstImage){}
		};
		
		if(options) $.extend(config, options);

		var onAfter  = config.onAfter,
			onBefore = config.onBefore,
			onSetup  = config.onSetup;

		var imageEffects = function(currentImage, newImage, callback) {
			if (!currentImage && !newImage) return;
			if (newImage.length == 0) return;
			onBefore(newImage);
			// Increment its z-index
			currentImage.stop().css('zIndex', 2);
			newImage.css('zIndex', 5);
			// Fade it in
			newImage.fadeIn(config.speed, function(){
				callback();
				onAfter(newImage);
			});
		};


		return this.each(function(){
			var container = $(this);
			var minHeight = container.height();
			var images = container.find(config.childrenSelector);

			// filter the first (currently visible)
			var current = images.filter(":visible");
			onSetup(current);

			// give all images position:absolute, and the container position:relative
			container.css({
				'position': 'relative',
				'minHeight': minHeight
			});
			images.css({
				'position': 'absolute',
				'zIndex':   1
			});

			images.not(current).hide();

			// Create the "show next" event
			container.bind("next", function(){
				// Grab the next image
				var newImage = current.next().not(":animated");
				imageEffects(current, newImage, function() {
					current.hide();
					current = newImage;	
				});
			});
			
			container.bind("prev", function(){
				// Grab the next image
				var newImage = current.prev().not(":animated");
				imageEffects(current, newImage, function() {
					current.hide();
					current = newImage;	
				});
			});

			$(config.nextSelector).click(function(){
				container.trigger("next");
				return false;
			});

			$(config.prevSelector).click(function(){
				container.trigger("prev");
				return false;
			});

		});

	};
}(jQuery));

// == End of Plugin ==


var caption = $("#gallery_right .details p"),
	links   = $("#gallery_right .image-viewmore"),
	gallery = $(".gallery");

gallery.slideShow({
	speed: 400,
	nextSelector : '.nextimage > a',
	prevSelector : '.previmage > a',
	onBefore : function(newImage){
		caption.html(newImage.attr('alt') + "<br/>" + newImage.attr('longdesc'));
		links.attr('href',(newImage.attr('rel')));
		homeImage = newImage
			.bind('print',function(){FOTOMOTO.API.addPhoto(this).showPrint();});
	},
	onSetup : function(firstImage){
		caption.html(firstImage.attr('alt') + "<br/>" + firstImage.attr('longdesc'));
		links.attr('href',(firstImage.attr('rel')));
	}
});

var fmImages = $("#key_gallery_image img")
	.bind('print', function(){FOTOMOTO.API.addPhoto(this).showPrint();})
	.bind('card', function(){FOTOMOTO.API.addPhoto(this).showCard();})
	.bind('ecard', function(){FOTOMOTO.API.addPhoto(this).showEcard();});

$(document).ready(function(){
	
	homeImage = $('#gallery_left img:first-child')
		.bind('print',function(){FOTOMOTO.API.addPhoto(this).showPrint();});
		
	$('a.fotomoto').click(function(){
		homeImage.trigger('print');
	})
	
	$(".purchase a").click(function(){
		action = $(this).attr('rel');
		if(!action) return;
		fmImages.trigger(action);
		return false;
	});

})


