﻿$(
	function () {
		var safariFix = (($.browser.safari || $.browser.chrome) ? true : false);
		$('.home-carousel').data('safarifix', safariFix).jcarousel(
			{
				scroll: 5,
				visible: 5,
				auto: 0,
				wrap: 'both',
				isFixedSize: true
			}
		);
		$('.home-carousel-links li').click(
			function () {
				SwitchCarousel(this);
			}
		);
		SwitchCarousel();
	}
);

function SwitchCarousel(item) {
	var index;
	var $links = $('.home-carousel-links li');
	if (!item) {
		index = CurrentCarousel();
		item = $links.eq(index);
	}
	else index = $links.index(item);
	$links.removeClass('selected');
	$(item).addClass('selected');
	var $current = $('.home-carousel')
		.hide()
		.eq(index)
		.show();
	if ($current.data('safarifix')) {
		$current
			.data('safarifix', false)
			.data('jcarousel')
			.scroll(1, false);
	}
	CurrentCarousel(index);
}

function CurrentCarousel(index) {
	var cookieName = 'home-carousel-current';
	if (arguments.length > 0) Cookie(cookieName, index);
	else {
		var returnValue = Cookie(cookieName);
		if (!returnValue || isNaN(returnValue)) returnValue = 0;
		return returnValue;
	}
}
