// <![CDATA[
// jQuery for product and sub-category image carousels.
$('.container').hide();

$(document).ready(function() {
	// Hold existing height to reapply after carousel transformations for IE6.
	var ieHeight = $('#container').css('height');

	// Get the start page if not forcing carousel to another position.
	if (typeof forceStart !== 'number') {
		var results = document.cookie.match('(^|;) ?' + cookiePrefix + 'StartPage=([^;]*)(;|$)');
		var startPage = (results ? parseInt(unescape(results[2])) : 0);
	} else {
		startPage = forceStart;
	}

	// Calculate and select the start page control
	$('.previewControls a.pageControl').removeClass('selected');
	$('.previewControls a.pageControl').eq(startPage).addClass('selected');

	// Enable the paging controls
	$('.previewControls').show();

	// Fade in the images.
	$('.container').fadeIn(1600);

	// Initialise the carousel
	$('#previewPane .container').jCarouselLite({
		speed: 700,
		circular: false,
	    btnNext: '.next',
	    btnPrev: '.prev',
	    btnGo: pageControls,
	    scroll: 1,
	    visible: 1,
	    start: startPage,
	    beforeStart: updateCarousel,
	    afterEnd: updateCarousel
	});

	// Fixes container explanding problems in IE6 by forcing CSS to refresh.
	if ($.browser.msie && $.browser.version <= 6) {
		$('#container').css('height', ieHeight);
		$('.container').css('width', '592px');
 	}
});

// Updates the selected page and set the cookie
function updateCarousel(item) {
	// Get the class of the current list element (which is the page number minus one)
	var page = parseInt(item.attr('class'));

	//Set the cookie
    document.cookie = cookiePrefix + "StartPage=" + escape(page);

	// Update the selected page
	$('.previewControls a.pageControl').removeClass('selected');
	$('.previewControls a#paging_' + (page + 1)).addClass('selected');
}
// ]]>
