function updateSlideshows()
{
/*
	var slideshows;

	if( slideshows==null )
	{
		slideshows = $$("div.slideshow-container");
	}
*/
	document.slideshows.each(function slideSwitch(slideshow)
	{
		try
		{
			//console.log('trying');
			var table = slideshow.getElement('table.active');
			var nextactive = table.getNext('table');

			//console.log(nextactive);
			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)
		{

		}
	});

	return;
}

function renderSlides(slideshow, feed, mode)
{
	var max_slides = 10; // Limit the slides to stop crazy slowness on loading.

	var images = [];
	var slides = [];

	feed.each(function(feed_item, index)
	{
		// Limit the number of slides.
		if( slides.length==max_slides )
		{
			return;
		}

		if ( (feed_item.codart.charAt(feed_item.codart.length-2) == 'E') && (feed_item.codart.charAt(feed_item.codart.length-1) == 'X') ) //If product is Export
		{
			feed_item.codart = feed_item.codart.slice(0 , feed_item.codart.length-2 );
		}

		var table = new Element('table'); // .setStyles({'width':'132px', 'margin':'auto'});
		var src = slideshow.get('site:server') + '/thumb/boxart//60x60/' + feed_item.codart + '.jpg';
		var viewmore = '['+slideshow.get('site:slideshow-viewmore')+']';

		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',viewmore)
						)
					)
				)
			).set('opacity',0)
		);

		images.push(src);
		slides.push(table);
	});

	slideshow.images = images;
	slideshow.slides = slides;

	// If there is lots of these it might be best to move
	// this into the load event.

	cacheSlides(slideshow);

	return;
}

function cacheSlides(slideshow)
{
	var first = true;
	var slideshow_size = slideshow.getSize();

	// Use Asset.images to wait for the images to
	// load before checking the size of each slide.

	new Asset.images(slideshow.images, {
		onProgress: function(index){
			if (index>=0 && index <= slideshow.images.length)
			{
				var table = slideshow.slides[index-1]; //for some reason it's starting from 1. Balls.

				// 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;
					}
				}
			}
		},
		onComplete: function(){
			slideshow.images=null;
			slideshow.slides=null;
		}
	});
 
	return;
}

window.addEvent('domready', function()
{
	document.slideshows = $$('div.slideshow-container');

	document.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);
});

/*
window.addEvent('load', function()
{
	document.slideshows.each(function(slideshow){
		cacheSlides(slideshow);
	});
});
*/

