
function updateSlideshows()
{
	$$("div.slideshow-container").each(function slideSwitch(slideshow)
	{
		try
		{
			var table = slideshow.getElement('table.active');
			var nextactive = table.getNext('table');

			if(nextactive==null)
			{
				nextactive = slideshow.getFirst('table');
			}

			if(table!=nextactive && slideshow.mymouseover==null)
			{
				table.removeClass('active');
				table.fade('out');

				nextactive.addClass('active');
				nextactive.fade('in');
			}
		}
		catch(e)
		{

		}
	});
}

function renderSlides(slideshow, feed, mode)
{
	var slideshow_size = slideshow.getSize();

	var first = true;

	var images = [];
	var slides = [];

	feed.each(function(feed_item, index)
	{
		var table = new Element('table'); // .setStyles({'width':'132px', 'margin':'auto'});
		var src = slideshow.get('site:server') + '/thumb/boxart//60x60/' + feed_item.codart + '.jpg';

		switch( slideshow.get('site:slideshow-currency') )
		{
			default:
			case 'GBP':
				price = '&pound;' + feed_item.price.gbp;
				rrp = '&pound;' + feed_item.rrp.gbp;
				break;

			case 'EUR':
				price = feed_item.price.eur + '&euro;';
				rrp = feed_item.rrp.eur + '&euro;';
				break;
		}

		slideshow.adopt
		(
			table.adopt
			(
				new Element('tbody').adopt
				(
					new Element('tr').adopt
					(
						new Element('td').set('valign','middle').set('align','center').adopt
						(
							new Element ('a').set('href',feed_item.url).adopt
							(
								new Element('img').set('border','0').set('alt','boxart').set('src',src)
							)
						),
						new Element('td').adopt
						(
							new Element('div').set('class','product').set('valign','middle').set('align','center').adopt
							(
								new Element('a').set('target','_parent').set('href',feed_item.url).set('text',feed_item.name),
								new Element('p').set('class','multiprice').set('html', price),
								new Element('p').set('class','rrp').set('html', 'RRP: ' + rrp)
							)
						)
					),
					new Element('tr').adopt
					(
						new Element('td').set('align','center').set('colspan','3').adopt
						(
							new Element('a').set('href','/SEARCH/HTML/CATEGORY/' + mode).set('text','[view more]')
						)
					)
				)
			).set('opacity',0)
		);

		images.push(src);
		slides.push(table);
	});

	// Use Asset.images to wait for the images to
	// load before checking the size of each slide.
	new Asset.images(images, {
		onProgress: function(index){
			var table = slides[index];

			// If the slide is too big delete it.
			if( table.getSize().y>slideshow_size.y || table.getSize().x>slideshow_size.x )
			{
				slideshow.removeChild(table);
			} else {
				if( first )
				{
					table.set('opacity',1).set('class','active');
					first = false;
				}
			}
		}
	});

}

window.addEvent('domready', function()
{
	var slideshows = $$('div.slideshow-container');

	slideshows.each(function(slideshow)
	{
		slideshow.addEvent('mouseover',function(ev)
		{
			slideshow.mymouseover=true;
		});

		slideshow.addEvent('mouseout',function(ev)
		{
			slideshow.mymouseover=null;
		});

		var platform = (slideshow.get('site:slideshow-platform')==null ? '' : '/' + slideshow.get('site:slideshow-platform'));

		new Request.JSON(
		{
			method: 'get',
			url: '/SEARCH/JSON/CATEGORY/' + slideshow.get('site:slideshow-mode') + platform,
			onComplete: function(feed)
			{
				if( feed!=null && (feed.length>0 || platform=='') )
				{
					renderSlides(slideshow, feed, slideshow.get('site:slideshow-mode') + platform);
				} else {
					// If there are no items for this platform try without the platform.
					
					new Request.JSON(
					{
						method: 'get',
						url: '/SEARCH/JSON/CATEGORY/' + slideshow.get('site:slideshow-mode'),
						onComplete: function(feed)
						{
							if( feed!=null )
							{
								renderSlides(slideshow, feed, slideshow.get('site:slideshow-mode'));
							}
						}	
					}).send();
				}
			}
		}).send();
	});

	updateSlideshows.periodical(2000);
});

