$(function(){
// image-rotator
	if($('.image-rotator').length > 0) {
		$('.image-rotator').cycle({
			fx: 'fade',
			speed:   1000,
			timeout: 5000,
			pager: '#rotator-nav-items',
			slideExpr: 'img'
		});
	}

// show / hide map
	if($('.getting-here a').length > 0) {
	$('.getting-here a').click(function(){
		$(this).hide();
		$('.map-trans-wrp').animate({width: '100%'})
		return false;
	});

	$('.hide-map').click(function(){
		$('.getting-here a').show()
		$('.map-trans-wrp').animate({width: 0});
		return false;
	});
	}

// homepage promo scrollable
	hppInterval = 4000;
	hppItemW = 221;
	hppAnimating = false;
	if($('body').hasClass('home')){
		hppSetClones();
		$('.promo-wrapper .next').click(hppNext);
		$('.promo-wrapper .prev').click(hppPrev);
		hppTimer = setInterval(hppNext, hppInterval);
		$('.promo-wrapper').hover(
			function(){
				clearInterval(hppTimer);
			},
			function(){
				hppTimer = setInterval(hppNext, hppInterval);
			}
		);
	}
// feat scrollable
	$('.featured-scrollable').scrollable({
			circular: true,
			next:  	'.fs-next',
			prev:  	'.fs-prev'
	}).autoscroll({
		interval: 3000
	});

// section title animation
	stTimer = setTimeout(
		function(){
			$('.section-title').animate({top: 0}, {duration: 800});
		},
		400
	);
	$('.header-image-container, .section-title-wrap').hover(
		function(){
			$('.section-title').stop().animate({top: -160}, {duration: 600});
		},
		function(){
			$('.section-title').stop().animate({top: 0}, {duration: 600});
		}
	)

// category accordion
	$('.acc_container').hide();
//	$('.acc_trigger:first').addClass('active').next().show();
	$('.acc_trigger').click(function(){
		if( $(this).next().is(':hidden') ) {
			$('.acc_trigger').removeClass('active').next().slideUp();
			$(this).toggleClass('active').next().slideDown();
		} else {
			$('.acc_trigger').removeClass('active').next().slideUp();
		}
	return false;
	});
	$('.acc_container .close').click(function(){
		$('.acc_trigger').removeClass('active').next().slideUp();
	})

// subnav accordion
	$('.ddown').hide();
	$('.active .ddown').slideDown();
	$('.sn-expand').click(function(){
		if($(this).parent().hasClass('active')) {
			$('.sidenav .active').removeClass('active').children('.ddown').slideUp();
		} else {
			$('.sidenav .active').removeClass('active').children('.ddown').slideUp();
			$(this).parent().addClass('active').find('.ddown').slideDown();
		}
	});

// rollovers
	$('body.contact input.submit, .search-b, .so-form .submit-btn').hover(
		function(){
			src = $(this).attr('src').replace('.', '-over.')
			$(this).attr('src', src)
		},
		function(){
			src = $(this).attr('src').replace('-over', '')
			$(this).attr('src', src)
		}
	)

	// to do lists
	if($('body.home').length > 0) {
		tdDelay = 4000;
		tdFadeDuration = 1000;
		tdLoaded = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
		tdAnimating = false;
		tdAnimationWaiting = false;
		tdLoadSync = false;
		tdLoadSyncInd = 0;
		tdScreen = Math.floor(Math.random()*5);
		tdCurrent = 4*tdScreen;
		tdNext = tdCurrent+1;
		tdFirstImg = '<img alt="" src="'+tdImages[tdCurrent]+'" class="tdImg'+tdCurrent+' tdFirstImg" />';

		$('.navi-wrap .navi li:eq('+tdScreen+') a').addClass('current');
		$('.to-do-'+tdScreen+' li:first').addClass('active');
		$('.to-do-showroom').append(tdFirstImg);
		$('.tdFirstImg').load(function(){
			tdLoaded[tdCurrent] = true;
			tdNextToLoad = tdCurrent + 1;
			tdLoadImages();
			$(this).css({zIndex: 3}).fadeIn(tdFadeDuration);
			switchScreen();
		});

		tdLinks = $('.navi-wrap .navi a')
		tdLinks.click(tdLinksClick)
		tdAuto = setTimeout(tdAutoRotate, tdDelay);
	}

// gallery thumbs equal height and fancybox
	glMax = 0;
	$('.gallery li').each(function(){
		if( $(this).height() > glMax ) {
			glMax = $(this).height();
		}
	});
	$('.gallery li').each(function(){
		//$(this).css({height: glMax});
	});
	$(".gallery a").fancybox({
		padding: 0,
		autoScale: false,
		titlePosition: 'inside',
		titleFormat: formatTitle
	});
		// title is the clicked <a> title attribute
		// currentArray has references to all the links that form the current gallery
		// current index is zero based image index
		// use "console.log(currentOpts)" to examine it
		// for example "currentOpts.href" will hold the big image url
		// while "currentOpts.orig.context" will hold a reference to the link that
		// was clicked to launch the lightbox
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		fbTitle  = '<div class="gallery-popup-title">'+(title && title.length ? title : '')+'</div>';
		window.globalImageIndex = currentIndex;
		var page_toolbar = $( 'div.page_toolbar_code' );
		fbTitle += $( page_toolbar[currentIndex] ).html();
		return fbTitle;
	}

// tabs in last dropdown
$('ul.book-tabs').tabs('div.panes > div.pane');

// main nav dropdowns
	$('.nav li#book').mouseenter(function(){
		
		if( typeof window.book_trip_menu_lastopen !== "undefined" )
		{
			window.book_trip_menu_currentopen = new Date().getTime();
			if( ( window.book_trip_menu_currentopen - window.book_trip_menu_lastopen ) > 500 )
			{
				$('#book .drop-down-booking').show();
			}
		}
		else
		{
			$('#book .drop-down-booking').show();
		}
	});
	$('.dd-close').click(function(){
		$('#book .drop-down-booking').hide();
		window.book_trip_menu_lastopen = new Date().getTime();
	})
	$('.nav>li:not(#book)').mouseenter(function(){
		$('#book .drop-down-booking').hide();
	})

	// datepicker
$('.datepicker').datepicker( { 'minDate': 0 } );


});



function tdLoadImages() {
	firstNotLoaded = $.inArray(false, tdLoaded);
	if(firstNotLoaded >= 0) {
		if(tdLoadSync) {
			tdLoadSync = false;
			for (i=0; i<4; i++) {
				ind = tdLoadSyncInd+i
				if(!tdLoaded[ind]) {
					tdNextToLoad = ind;
					break;
				}
			}
		}
		if(tdLoaded[tdNextToLoad]) {
			tdNextToLoad = firstNotLoaded;
		}
		if (!tdLoaded[tdNextToLoad]) {
			$('.to-do-showroom').append('<img alt="" src="'+tdImages[tdNextToLoad]+'" class="tdImg'+tdNextToLoad+'" />');
			$('.tdImg'+tdNextToLoad).load(function(){
				tdLoaded[tdNextToLoad] = true;
				tdNextToLoad++;
				tdNextToLoad %= 20;
				tdLoadImages();
			})
		}
	}
}
function tdShowImg() {
	if (!tdAnimating) {
		tdAnimating = true;
		if(tdLoaded[tdNext]) {
			$('.to-do-'+tdScreen+' li:eq('+tdNext%4+')').addClass('active');
			$('.tdImg'+tdNext)
				.css({zIndex: 5})
				.fadeIn(tdFadeDuration, function(){
					$('.tdImg'+tdCurrent).css({display: 'none', zIndex: 1});
					$(this).css({zIndex: 3});
					tdCurrent = tdNext;
					tdNext++;
					tdNext %= 20;
					tdAnimating = false;
					tdAnimationWaiting = false;
					tdAuto = setTimeout(tdAutoRotate, tdDelay);
				});
		} else {
			tdAnimating = false;
			tdAnimationWaiting = true;
			tdshowTimeout = setTimeout(tdShowImg, 200)
		}
	}
}
function tdAutoRotate() {
	tdNextTemp = (tdCurrent+1)%20;
	if(!tdAnimating && !tdAnimationWaiting && tdLoaded[tdNextTemp]) {
		if(tdCurrent %4 == 3) { // the last image on a screen
			tdScreen = ++tdScreen % 5;
			switchScreen();
		}
		tdNext = tdNextTemp;
		tdShowImg();
	} else {
		tdAuto = setTimeout(tdAutoRotate, 200);
	}
}
function switchScreen(){
	$('.navi-wrap .navi a').removeClass('current');
	$('.navi-wrap .navi li:eq('+tdScreen+') a').addClass('current');
	$('.to-do-list li').removeClass('active');
	$('.to-do-'+tdScreen+' li:first').addClass('active');
	$('.header-image-container .item').fadeOut();
	$('.header-image-container .item:eq('+tdScreen+')').fadeIn();
}
function tdLinksClick(){
	if(!tdAnimating && !tdAnimationWaiting && !$(this).hasClass('current')) {
		clearTimeout(tdAuto);
		tdScreen = tdLinks.index($(this));
		switchScreen()
		tdLoadSync = true;
		tdLoadSyncInd = tdScreen*4;
		tdNext = tdScreen*4;
		tdShowImg();
	}
	return false;
}
// homepage promo scrollable fns
function hppSetClones() {
	hppTotal = $('.home-promo .promo-item').length;
	if(hppTotal <= 5) {
		$('.promo-items').append($('.promo-items').html());
		hppSetClones();
	} else {
		$('.home-promo .promo-item').each(function(i){
			l = i*hppItemW
			$(this).css({position: 'absolute', top: 0, left: l})
		})
	}
}
function hppPrev(){
	if(!hppAnimating) {
		hppAnimating = true;
		$('.home-promo .promo-item:last').css({left: -hppItemW}).prependTo('.promo-items');
		$('.home-promo .promo-item').animate({left: '+='+hppItemW}, function(){hppAnimating = false;})
	}
}
function hppNext(){
	if(!hppAnimating) {
		hppAnimating = true;
		lastMoved = false;
		$('.home-promo .promo-item').animate({left: '-='+hppItemW}, function(){
			l = (hppTotal-1)*hppItemW;
			if(!lastMoved) {
				lastMoved = true;
				$('.home-promo .promo-item:first').css({left: l}).appendTo('.promo-items');
			}
			hppAnimating = false;
		});
	}
}

// Cufon
Cufon.replace('h1');
Cufon.replace('.image-caption');
Cufon.replace('.section-title');
Cufon.replace('.left-col h2');
Cufon.replace('.touch-events-col h2');
Cufon.replace('.trip-calc h2');
Cufon.replace('.wide-sub-heading h2');


