			$(function () {

			var galleryShowing = false,
			orig_background = $('#bg table img').attr('src');
				$("#gallery-navigation").find("a").each (function () {
					j = new Image();
					j.src = $(this).attr('href');
				});

			$("#subnav").find ("a:not(.sorter)").click (function () {
				$("#subnav").find("a").removeClass("active");
				$(this).addClass('active');
				if ($(this).attr('id')!='open-gallery')
				{
//					$('#container').stop().scrollTo($(this).attr('href'), 1000, {easing:'easeOutExpo'});
					if (galleryShowing)
					{
						closeGallery();
					}
				}
				if ($(this).attr('href').substring(0,1) != '#')
				{
					return true;
				}
				return false;
			});


			var speed = $.browser.msie ? 2000 : 1000;
			$('a[href^=#]:not(.sorter)').click (function () {
				var frag = $(this).attr('href');
				if ($('#container').find(frag).length)
				{
					$('#container').stop().scrollTo(frag, speed, {easing:'easeOutExpo'/*, onAfter: function () {location.hash = frag;}*/});
					if ($('#subnav').find('a[href=' + frag + ']').length)
					{
						$('#subnav').find('a').removeClass('active');
						$('#subnav').find('a[href=' + frag + ']').addClass('active');
					}
				}
			});


			$('#open-gallery').click ( function () {
				if (!galleryShowing)
				{
					openGallery();
				}
				else
				{
					closeGallery();
				}
					return false;
			});

			function openGallery ()
			{
					$('#sidebar').animate({
						left:'-612px'
					}, 1001, 'easeOutExpo');
					$('#main').animate ({
						marginLeft: '-478px'
					}, 1001,'easeOutExpo');
					$('#subnav').animate ({
						left: 0
					}, 1001,'easeOutExpo');
					$('#gallery-navigation').fadeIn(1001, 'easeOutExpo').find('li:first-child a').trigger('click');
					galleryShowing = true;
					$('<div id="gallery-sticky"></div>').appendTo('body');
			}

			function closeGallery()
			{
					$('#sidebar').animate({
						left:'0'
					}, 1001,'easeOutExpo');
					$('#main').animate ({
						marginLeft: '164px'
					}, 1001,'easeOutExpo');
					$('#subnav').animate ({
						left: '615px'
					}, 1001,'easeOutExpo');
					$('#gallery-navigation').fadeOut(1001, 'easeOutExpo');
					$('#bg table img').attr('src', orig_background);
					galleryShowing = false;
					$('#gallery-sticky').remove();
			}

			$("body").keypress(function (e) {
				if (galleryShowing)
				{
					if (e.keyCode=='27')
					{
						closeGallery();
						e.preventDefault();
						return false;
					}
				}
			});


			var gallery_navs = $('#gallery-navigation>li'),
					gallery_length = gallery_navs.length;


			gallery_navs.each (function (idx) {
				$(this).data('idx', idx);
			});

			$(document).click (function (e) {
				if(galleryShowing && e.target.nodeName != 'A') {
					e.pageX <= $('body').width()/2 ? galleryPrevious() : galleryNext();
				}
			});

			$(document).mousemove (function (e) {
				if (galleryShowing)
				{
					var dir = e.pageX <= $('body').width() / 2 ? 'prev' : 'next';
					e.pageX = (dir == 'prev') ? e.pageX - 20 : e.pageX;
					$('#gallery-sticky').attr('class', dir).css({left:e.pageX, top:e.pageY});
				}
			});

			function galleryPrevious ()
			{
				var act = gallery_navs.filter('.active').prev().find('a');
				if (!act.length)
				{
					act = gallery_navs.filter(':last-child').find('a');
				}
				act.trigger('click');
			}

			function galleryNext()
			{
				var act = gallery_navs.filter('.active').next().find('a');
				if (!act.length)
				{
					act = gallery_navs.filter(':first-child').find('a');
				}
				act.trigger('click');
			}

			if (document.getElementById('all-cottages'))
			{
				$cottages = $("#all-cottages");
				$('#subnav').find('.sorter').click (function () {
					$cottages.find(">li").hide();
					$cottages.find('.' + this.hash.slice(1)).fadeIn();
					$('.sorter').removeClass('active');
					$(this).addClass('active');
					return false;
				});
			}



			$('#close-fullscreen-gallery').click (function () {
				closeGallery();
				return false;
			});
			$('#gallery-navigation li:not(#close-fullscreen-gallery) a').click (function () {
				$('#bg table img').attr('src', $(this).attr('href'));
				$(this).parent().addClass('active');
				$(this).parent().siblings('li').removeClass('active');
				return false;
			});
			$('#extra_information').autoResize({limit:1001});

			$("#overview").find('h1+p').addClass('first-of-type');


			$(".cottage-collection#main").find("#overview>ul>li").click (function () {
 				location.href = $(this).find('a').attr('href');
			});




			$("a[href^=http]").each(function() {
				if(this.href.indexOf(location.hostname) == -1) {
					$(this).click( function(){
						window.open(this.href);return false;}
					);
				}
			})
			$("a[href$=.pdf]").attr('target', '_blank');
/*			$(window).scroll(function () { // scrollwheel bug
				try{
					$.scrollTo('#container', 10);
				}
				catch (e)
				{

				}
			});*/
		});

