function carousels() {
	try {
		$j("#billboards").jcarousel({
			auto: 8,
			scroll: 1,
			animation: 0,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel){
				$j('#billboards .controls a').each(function(index, link){
					$j(link).attr('rel', index+1).on('click', function(){
						carousel.scroll($j.jcarousel.intval(index+1));
						carousel.stopAuto();
						return false;
					});
				});
			},
			itemVisibleInCallback: function(carousel, li, index, state){
				$j('#billboards .controls a').removeClass('active');
				$j('#billboards .controls a[rel="' + index + '"]').addClass('active');
			}
		});
	} catch(e) {}
	
	try {
		$j("#features").jcarousel({
			scroll: 1,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel){
				$j('#features .controls a').each(function(index, link){
					$j(link).attr('rel', index+1).on('click', function(){
						carousel.scroll($j.jcarousel.intval(index+1));
						return false;
					});
				});
			},
			itemVisibleInCallback: function(carousel, li, index, state){
				$j('#features .controls a').removeClass('active');
				$j('#features .controls a[rel="' + index + '"]').addClass('active');
				$j('#features .starbox').each(function(index, element) {
					spafinder.Document.stars(element, parseFloat($j(element).text()), {
						ratedUrl: '/assets/images/common/star-orange-10x9.gif'
					});
				});
			}
		});
	} catch(e) {}
	
	$j(["#top-spa-deals","#guide","#top-ten-spas","#featured-spas"]).each(function(i, div){
		try {
			$j(div).jcarousel({
				scroll: 1,
				buttonNextHTML: null,
				buttonPrevHTML: null,
				initCallback: function(carousel){
					$j(div + ' .steps a').each(function(index, link){
						var rel = $j(link).attr('rel').toLowerCase();
						if (rel === 'next') {
							$j(link).bind('click', function() {
								carousel.next();
								return false;
							});
						} else if (rel === 'prev') {
							$j(link).bind('click', function() {
								carousel.prev();
								return false;
							});
						} else {
							$j(link).attr('rel', index).bind('click', function() {
								carousel.scroll($j.jcarousel.intval($j(this).attr('rel')));
								return false;
							});
						}
					});
				},
				itemVisibleInCallback: function(carousel, li, index, state){
					$j(div + ' .steps a').removeClass('active');
					$j(div + ' .steps a[rel="' + index + '"]').addClass('active');
					$j(div + ' .card img').each(function(index, img){
						img.src = img.getAttribute('rel');
					});
				}
			});
		} catch(e) {}
	});
	
	$j('#buy-book-widget .control a').on('click', function(){
		if ($j(this).attr('rel') === 'buy-gift-certificates') {
			$j('#online-booking').hide();
			$j('#buy-gift-certificates').show();
		} else {
			$j('#buy-gift-certificates').hide();
			$j('#online-booking').show();
		}
	});
}

function spaRahRahSegments(allSegments) {
	$j(document).ready(function(){
		var key = "travel";	
		var geolocation = $j('#spafinder').data('location');	
		if (geolocation !== '' && allSegments.data !== 'undefined') {	
			if(isNaN(geolocation)) {
				geolocation = geolocation.toLowerCase().replaceAll(',', '').replaceAll(' ', '_');
			} else {
				geolocation = geolocation.toString();
			}		
			for (var i in allSegments.data) {				
				if (geolocation.indexOf(allSegments.data[i]['key']) !== -1) {
					key = allSegments.data[i]['key'];
					break;
				}
			}
		}
		var headID = document.getElementsByTagName("head")[0];         
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.async = true;
		newScript.src =	'http://api.groupcommerce.com/api/v2/syndication/offers/current/' + key	+ '?callback=spaRahRahOffers&apiKey=7a423674-d496-4a96-bf73-f24038f88512';
		headID.appendChild(newScript);
	});
}

function spaRahRahOffers(offers) {
	try {
		var random = Math.floor(Math.random() * offers.data.length),
		url = offers.data[random].url,
		headline = offers.data[random].headline,
		image = offers.data[random].images[0].url;
		$j('#sparahrah-widget').attr('href', url).css({
			'display': 'block',
			'background-image': 'url('+image+')'
		}).find('.title').text(headline);
		$j('#sparahrah-widget .opacity').css('height', $j('#sparahrah-widget .title').outerHeight(true));
	} catch(e) {}
}

$j(document).ready(function(){
	carousels();
});
