$(document).ready(function() {
	
	// drag n scroll scrolling thumbs
	//$('#thumbs_holder_wrapper').scrollview();
	
	// If there are many images then this prevents scrolling all the way
	// Expand the width of the holding div to cope with extra images
	var images = $('#thumbs_holder > a.fancy').size();
	var image_limit = 6;
	if(images > image_limit) {
		var diff = images - image_limit;
		
		// Don't know why .width() method doesn't work here?
		var width = $('#thumbs_holder').css('width');
		width = width.substring(0, width.length - 2);
		
		var new_width = parseInt(width) + 270 * diff;
		$('#thumbs_holder').width(new_width);
	}
	
	//$('#thumbs_tip').hide();

	
	// click thumbnail activate fancybox
	$('.fancy').fancybox({
		'titleShow' : false
	});
	// double click thumbnail activate fancybox
	/*
	$('.fancy').dblclick(function() {
		$('.fancy').fancybox({
      'titleShow' : false,
			// on close remove fancybox
			'onClosed' : function() {
				$('.fancy').unbind('click.fb');
			}
		});
		// on dbl click trigger click event
		$(this).trigger('click');
	}).click(function() {
		// stop fancybox triggering on drop
		return false;	
	});
	*/
	
	// start thumbnail scrolling animation
	setTimeout(function() {
		animateThumbs("forward");
	}, 750);
	
	// pause thumnbnail scrolling on hover
	$('#thumbs_holder_wrapper').hover(function() {
		//$('#thumbs_tip').fadeIn();			
		$(this).stop(true, false);
	}, function() {
		//$('#thumbs_tip').hide();			
		animateThumbs("forward");
	});
	
	
	// regular fancybox links
	$('.reg_fancy').fancybox();
	
	
	// show/hide footer sitemap
	$('#sitemap_handle a').toggle(function() {
		
		$(this).html("Hide Sitemap");
		$('#footer ul').slideDown(500);
		
		return false;
	}, function() {
		
		$(this).html("Sitemap");
		$('#footer ul').slideUp(500);
		
		return false;
	});
	
	
	// slide header down and init thumbs starting pos
	if($('#head_nav_holder').length > 0) {
		setTimeout(function() {
			$('#head_nav_holder').slideDown();
			$('#thumbs_holder_wrapper').scrollLeft(310);
			
		}, 500);
	}
	
	// redirect all links back to home page init page query string
	$('#nav ul li a').click(function() {
		
		// find page link
		var href = $(this).attr('href').split("/");
		var query = href[href.length - 1];
		
		/*
		 * redirect all pages other than testimonials
		 * back to home with query string init animation
		 */
		if(query !== "testimonials" && query !== "morzine-news" && query != "") {
			window.location = HOME+"?page="+query;
			return false;
		}
		
		return true;
	});
	
});


/*
 * Animate Thumbs
 * ----------------------------------------
 *
 * Recursive function to scroll thumbs
 * back and forth across page
 *
 * @param String direction to scroll thumbs
 */
function animateThumbs(dir) {
	
	var scrollLeft = "";
	var newDir = "";
	
	if(dir == "forward") {
		scrollLeft = "1500";
		newDir = "back";
	} else if(dir == "back") {
		scrollLeft = "310";
		newDir = "forward";
	}
	
	
	$('#thumbs_holder_wrapper').animate({
		
		scrollLeft : scrollLeft+'px'
	
	}, {
		
		duration : 50000,
		easing   : 'linear',
		complete : function() {
			animateThumbs(newDir);		
		} 
	
	});
}

