var sexAlbums = new Array();

function imageHandler(event)
{
	event.preventDefault();
	sexAlbums[$(this).parent().parent().parent().attr('id')].next();
}

function SexAlbum(id)
{
	if ($('div#' + id).length != 1)
		alert('error: album ' + id + ' not found');

	this.id = id;

	this.queue = new Array();

	this.first  = 0;
	this.second = 1;

	this.addItem = function (id, name, price, imgpath)
	{
		img = new Image();
		img.src = imgpath;
		$(img).hide();		
		this.queue.push( { 'id': id, 'name': name, 'price': price, 'img': img } );
	}

	this.next = function ()
	{
		if (this.queue.length % 2 != 0)
		{
			var el = { 'id': this.queue[0].id,
				       'name': this.queue[0].name,
					   'price': this.queue[0].price,
					   'img': $(this.queue[0].img).clone()
					   };
			this.queue.push(el);
		}
		
		if (this.second == this.queue.length - 1)
		{
			this.first  = 0;
			this.second = 1;
		}
		else
		{
			this.first = this.first + 2;
			this.second = this.second + 2;
		}

		$('div#' + this.id + ' img.sexAlbumPhoto').fadeOut('normal', function () {
			var id = $(this).parent().parent().parent().attr('id');
			var s = sexAlbums[id];
			var next = null;
			if ($(this).parent().parent().hasClass('sexAlbumPageLeft'))
				next = s.firstItem();
			else
				next = s.secondItem();
			var clone = $(next.img).clone();
			clone.addClass('sexAlbumPhoto');
			clone.click(imageHandler);
			$(this).replaceWith(clone);
			clone.fadeIn();
		});

		$('div#' + this.id + ' div.sexAlbumText').fadeOut('normal', function () {			
			var id = $(this).parent().parent().attr('id');
			var s = sexAlbums[id];
			var next = null;
			if ($(this).parent().hasClass('sexAlbumPageLeft'))
				next = s.firstItem();
			else
				next = s.secondItem();
			$(this).children('a.sexAlbumItemName')
				   .text(next.name);
			$(this).children('a.sexAlbumItemLink')
				   .attr('href', '/items/item?id=' + next.id);
			$(this).children('span.sexAlbumTextPrice').text(next.price + ' Ð.');
			$(this).fadeIn();
		});
	}

	this.firstItem = function ()
	{
		return this.queue[this.first];
	}

	this.secondItem = function ()
	{
		return this.queue[this.second];
	}

	sexAlbums[this.id] = this;
	$('img.sexAlbumPhoto').click(imageHandler);
}
