/* set up diagonal image galleries*/
$(document).ready(function() {
	if(!$('.imageGallery').length) {
		return;
	}
	$('.imageGallery').imagesLoaded(function() {
		$('.imageGallery').each(function() {
			var $gallery = $(this),
				moveDiag = function(elem,i) {
					var $elem = $(elem),
						$li = $elem.find('li').eq(i),
						$img = $li.find('img'),
						heightDiff = $img.height()-containerHeight,
						widthDiff = $img.width()-containerWidth,
						changes = {
							top: parseInt($li.css('bottom'),10)+heightDiff/2,
							left: imagesLeft-parseInt($li.css('left'),10)-widthDiff/2
						};
					$elem.stop(true,true).animate(changes);
				},
				i=0,
				imgCount = $gallery.find('.images img').length,
				leftOffset = 0,
				bottomOffset = 0,
				$firstImg = $gallery.find('li img:eq(0)'),
				containerWidth = $firstImg.width(),
				containerHeight = $firstImg.height(),
				maskWidth = $gallery.find('.mask').width(),
				maskHeight = $gallery.find('.mask').height(),
				imagesLeft = (maskWidth-$firstImg.width())/2,
				prevWidth,
				prevHeight;
			$gallery
				.height(maskHeight)
				.width(maskWidth)
				.find('.images')
				.height(containerHeight)
				.width(containerWidth)
				.css('left', imagesLeft)
				.each(function() {
					$(this).find('li').each(function(i) {
						var $img = $(this).find('img'),
							width = $img.width(),
							height = $img.height();
						if(i!==0) {
							leftOffset += 1.5*prevWidth - 0.5*width;
							bottomOffset += height;
							$(this).css({
								left: leftOffset,
								bottom: bottomOffset
							});
						}
						prevWidth = width;
						prevHeight = height;
					});
				});
			$gallery.find('.previous, .next').click(function(e) {
				e.preventDefault();
				$(this).attr('class')==="previous" ? i-- : i++;
				if(i<0) {
					i=imgCount-1;
				} else if(i>=imgCount) {
					i=0;
				}
				moveDiag($(this).siblings('.images'),i);
			});
		});
	});
});

/* this inspired by parts of crossSlide by Tobia Conforto <tobia.conforto@gmail.com> */
$.fn.fadeTrans = function(sleep) {
	var $container = this,
		$imgSrcs = $container.find('.img'),
		imgCount = $imgSrcs.length,
		imgs = [],
		$imgs = $();
	if(!$imgSrcs.length) {
		return;
	}

	// pre-load imgs
	$container.empty();
	(function(proceed) {

		var n_loaded = 0;
		function loop(i, img) {
			// this loop is a for (i = 0; i < imgs.length; i++)
			// with independent var i, img (for the onload closures)
			img.src = $imgSrcs.eq(i).text();
			$imgs = $imgs.add($('<img src="'+img.src+'" />').css({
					position: 'absolute',
					visibility: 'hidden',
					marginTop: "0",
					left: 0,
					border: 0
				}).appendTo($container).imagesLoaded(function() {
					n_loaded++;
					if (n_loaded == $imgSrcs.length)
						proceed();
				}));
			if (i + 1 < $imgSrcs.length)
				loop(i + 1, new Image());
		}
		loop(0, new Image());

	})(function() { // then proceed
		var animationTimeout,
			trigger = function() {
				$('#imageNav .next').click();
			},
			animation = function(i,i2) {
				$imgs.eq(i).animate({
					opacity: 0
				}, 'linear', function() {
					$(this).css('visibility','hidden');
				});
				$imgs.eq(i2).css({
					visibility: 'visible',
					opacity: 0
				}).animate({
					opacity: 1
				}, 'linear', function() {
					i = i2;
					i2 = ++i2 % imgCount;
					animationTimeout = window.setTimeout(trigger, sleep);
				});
			};

		$imgs.each(function() {
			var height = $(this).height();
			if(height) {
				$(this).css({
					top: "50%",
					marginTop: "-"+height/2+"px"
				});
			}			
		});

		// show first image
		$imgs.eq(0).css({
			visibility: 'visible',
			opacity: 0
		}).animate({
			opacity: 1
		});
		
		$('#imageNav .dot').click(function(e) {
			e.preventDefault();
			var i = $('#imageNav .dot').filter('.active').index()-1,
				i2 = $(this).index()-1;
			if(i2===i) {
				return;
			}
			$(this).addClass('active').siblings().removeClass('active');
			animationTimeout = window.clearTimeout(animationTimeout);
			$imgs.stop();
			animation(i,i2);
		}).eq(0).addClass('active');
		$('#imageNav .previous, #imageNav .next').click(function() {
			var direction = $(this).hasClass('previous') ? -1 : 1,
				$dots = $('#imageNav .dot'),
				toClick = ($dots.filter('.active').index()-1+direction) % imgCount;
			$dots.eq(toClick).click();
		});
		
		// ok, let's go
		animationTimeout = window.setTimeout(trigger, sleep);
	});
};

$(document).ready(function() {
	/* set up booking system for featured properties */
	var origHeight = $('.bookingForm').height();
	$('.bookingForm h2.arrow').click(function(e) {
		e.preventDefault();
		var $arrow = $(this),
			$container = $(this).closest('.bookingForm'),
			currentHeight = $container.height(),
			targetHeight = $arrow.height()+$arrow.siblings('#bookingSystem').height(),
			opened = currentHeight === targetHeight;
		$container.animate({
			height: opened ? origHeight : targetHeight
		}, function() {
			// handily, the animation sets overflow to hidden during the animation
			$container.css('overflow', opened ? 'hidden' : 'visible');
		});
	});

	/* start the crossfade of the large home page images */
	$('.imagePanel').fadeTrans(3000);
	
	/* set the in-page links in the homepage copy */
	$('#intro a, #content .aboutPanel a').click(function(e){	
		var place =  $(this).attr('href'),
			$toPlace;
		if(!place) {
			return true;
		}
		$toPlace = $('a[name='+place.substring(1)+'], #'+place.substring(1)).eq(0);
		if($toPlace.length && $toPlace.is(':visible')) {
			e.preventDefault();
			$.scrollTo($toPlace, 300);
			$(this).blur();
			return false;
		} else {
			return true;
		}
	});
	
	/* add testimonial nav */
	$('#testimonials').each(function() {
		var $controls = $(this).find('#testimonialsControls span'),
			$wrap = $(this).children('.jbasewrap').eq(1),
			$testP = $wrap.find('p:eq(0)'),
			testHeight = $testP.height()+parseInt($testP.css('paddingTop'),10)+parseInt($testP.css('paddingBottom'),10),
			heightLimit = testHeight*($wrap.children('p').length-1);
			
		$testimonialPara = $(this).find('.testimonial p').each(function() {
			$(this).css({
					'marginTop': '-'+$(this).height()/2+'px',
					'visibility': 'visible'
				});
		});
				
		$('#testimonialsControls .dot').click(function(e) {
			e.preventDefault();
			var testimonialsHeight = $('#testimonials').height(),
				i = $('#testimonialsControls .dot').filter('.active').index()-1,
				i2 = $(this).index()-1;
			if(i2===i || $wrap.is(":animated")) {
				return;
			}
			$(this).addClass('active').siblings().removeClass('active');
			
			$wrap.animate({
				top: "-"+(i2*testimonialsHeight)+"px"
			});
		}).eq(0).addClass('active');
		
		$('#testimonialsControls .previous, #testimonialsControls .next').click(function() {
			var direction = $(this).hasClass('previous') ? -1 : 1,
				$dots = $('#testimonialsControls .dot'),
				testimonialsCount = $('#testimonials .testimonial').length,
				toClick = ($dots.filter('.active').index()-1+direction) % testimonialsCount;
			$dots.eq(toClick).click();
		});

	});
});
