window.addEvent('domready', initBuyNowBanner);
window.addEvent('domready', initProductSlideshow);

function initBuyNowBanner()
{
	if($('buynowbanner'))
	{
		$$('#buynownav li').each(function(element, i)
		{
			var target = element.get('id').replace('bannernavi','') + 'tabcontent';

			element.addEvent('click', function(e){
				var e = new Event(e).stop();
				$(target).fireEvent('changeTab');
			});	 
		});
	}
}

var index = 0;
var slideshowInterval;
var slidewshowDelay = 5000;
var maxSlideshow = 0;
function initProductSlideshow()
{
	if($('buynowbanner'))
	{
		slideshowInterval = setInterval(advanceSlideshow, slidewshowDelay);
		
		maxSlideshow = $$('#buynowthumbs a').length;
		
		$$('#buynowthumbs a').each(function(element, i)
		{
			element.addEvent('click', function(e){
				var e = new Event(e).stop();
				index = i;
				hotThumb();
				updateSlideshow();
				clearTimeout(slideshowInterval);
				slideshowInterval = setInterval(advanceSlideshow, slidewshowDelay);
			});	
		});
	}
}
function advanceSlideshow()
{
	if((index + 1) >= maxSlideshow)
		index = 0;
	else
		index++;

	hotThumb();
	updateSlideshow();
}
function hotThumb()
{
	$$('#buynowthumbs a').removeClass('selected');
	$$('#buynowthumbs a')[index].addClass('selected');
}
function updateSlideshow()
{
	imgURL = $$('#buynowthumbs a')[index].get('href');
	$('buynowbanner').setStyle('backgroundImage', 'url('+imgURL+')');
}