
(function($){$.fn.photoviewer = function(data, opts)
{
	this.each(function()
	{
	
	var obj				= this;
	var index			= 0;
	var busy			= false;
	var pause			= false;
	var	imgcache		= [];
	var captioncache	= [];
	var queue			= [];
	
	var speed			= opts.speed || 1000;
	var tmpspeed		= speed;
	var easing			= opts.easing || 'linear';
	var noimagepreload	= opts.noimagepreload || false;

	var newphoto;
	var direction;
	var unlocktimer;
	
	// Prepare loading message
	$('.pv-loading', obj).css({
		opacity: 0,
		visibility: 'visible'		
	});
	
	// Find previous/next index
	var findindex = function(calc)
	{
		myindex		= index + calc;
		if (myindex < 0) myindex = data.length + calc;
		if (myindex > data.length - 1) myindex = -1 + calc;
		return myindex;
	}

	// Previous photo
	var previous = function()
	{
		index = findindex(-1);
		direction = 0;
		setphoto(data[index]);
		return false;
	};

	// Next photo
	var next = function()
	{
		index = findindex(1);
		direction = 1;
		setphoto(data[index]);
		return false;
	};
	
	// Load new photo html
	var loadphoto = function(fig_id, preload)
	{
		preload = preload || false;
		if (typeof imgcache[fig_id] == 'undefined')
		{
			var data, html;
			data = {
				name	: 'photoviewer_photo',
				fig_id	: fig_id,
				width	: opts.width,
				height	: opts.height
			};
			anyRest.html.scomp(data, function(xml)
			{
				if (anyRest.aux.error(xml))
				{
					$.log(anyRest.aux.errorMsg(xml));
					return;
				}
				html = $('rsp', xml).text();
				imgcache[fig_id] = html;
				if (!preload) changephoto(fig_id, html);
			});
		} else
		{
			if (!preload) changephoto(fig_id, imgcache[fig_id]);
		}
	};
	
	// Change photo
	var changephoto = function(fig_id, html)
	{
		width = (direction ? '' : '-') + opts.width;
		newphoto = $('<div>')
			.addClass('pv-photo')
			.css({opacity: 0})
//			.attr('style', 'left:' + 0 + 'px') // for some reason it ignores css(left:500) ...
			.html(html)
			.appendTo($('.pv-photo-wrapper', obj))
			.find('.fig_image, .dv_image')
				.img_annotations()
				.img_zoomer();

		loadcaptiondone(fig_id);
	};
	
	// Loading caption done, check if image is already loaded
	var loadcaptiondone = function()
	{
		img = $('.pv-photo', obj).eq(1).find('img:eq(0)');
		if (img.attr('complete') || noimagepreload)
		{
			slideinphoto();
		} else
		{
			img.load(function(){ slideinphoto(); });
		}
	};
	
	// Slide in new photo
	var slideinphoto = function()
	{		
		tmpspeed = (queue.length) ? 1 : speed;

		$('.pv-photo', obj).eq(0).animate({opacity:0}, tmpspeed, easing, function()
		{
				$(this).remove();
				preload(index);
				busy = false;
				if (queue.length)
				{
					setphoto(queue.shift());
				}
		});
		
		$('.pv-photo', obj).eq(1).animate({opacity: 1 }, tmpspeed, easing);
	}
		
	// Slide in new picture
	var setphoto = function(fig_id)
	{
		if (!busy) loadphoto(fig_id);
		else queue.push(fig_id);
		busy = true;
	};
	
	// Preload ajax
	var preload = function()
	{
		loadphoto(data[findindex(1)],  1);
	};
	
	
	// Clicking behaviours
	/*
$('.pv-previous', obj).click(previous);
	$('.pv-next', obj).click(next);
*/
	
	
	// Preload previous and next
	preload(0);
	
	// Reload every X seconds
	var switchInterval = setInterval(function()
	{
		if (!pause) next();
	}, 7000);
	
	});
}})(jQuery);

