$(document).ready(function() {
		
	$('.external').live('click', function(e) {
		$(this).attr('target', '_blank');
	});

	$('.pagination .pagination-previous').parent().css('border-right', 'none');
	$('.pagination .page:last').parent().css('border-right', 'none');

	var newHeight = 0;
	$('#content-left ul li').each(function() {
		if ($(this).height() > newHeight) newHeight = $(this).height();
	});

	$('#content-left ul').height(newHeight + 20).cycle({ timeout: 0, prev: '#prev', next: '#next', fx: 'scrollHorz', speed: 800, timeout: 5000, pause: 1 });
	
	$('#subForm input').live('focus', function() {
		if ($.inArray($(this).val(), ['Name', 'Email Address']) > -1) $(this).val('');
	});

	var currentPage = $('body').attr('id');
	currentPage = currentPage.substr(0, currentPage.indexOf('-'));
	var $contentInner = $('#content-inner');

	// $('#navigation a, #homebtn, #emailsignup a').click(function(e) {
	var referer = window.location.pathname;
	
	function getDomain(url) {
		try {
			var host = url.match(/:\/\/(.[^/]+)/);
			if (!host) return false;
	   		else return host[1];
		} catch (err) {
			return false;
		}
	}
		
	//$('#navigation a, #homebtn, #emailsignup a, #content-wrapper a[class!="external"]').live('click', function(e) {
	$('a[class!="external"]').live('click', function(e) {

		$(this).blur();

		var url = $(this).attr('href');
		var host = getDomain(url);

		// if the URL is external, or the link is an email address
		if ((host && (host != getDomain(window.location.href))) || (url.indexOf('@') > -1)) {
			$(this).attr('target', '_blank');
			return true;
		}
		e.preventDefault();
		
		var newPage = url.replace(/\//g, '');
		var lastPage = currentPage;

		if (currentPage == newPage) return false;

		$('#navigation a').removeClass('down');
		if ($.inArray(newPage, ['food-and-drink', 'events', 'gallery', 'contact']) > -1) $(this).addClass('down');

		currentPage = newPage;

		// get and set the height of content-inner
		$contentInner.animate({opacity: 0}, 'fast', function() {

			$(this).css({ height: $contentInner.height() + 'px' });

			$('#loader').fadeIn('fast', function() {
				$contentInner.load(url + ' #content-wrapper', function(data) {

					$('.back', $contentInner).attr('href', referer);
					referer = url;
					
					$('#loader').fadeOut('fast', function() {

						var newHeight = $('#content-wrapper', $contentInner).height();
						
						$contentInner.animate({ height: $('#content-wrapper', $contentInner).height() + 'px' }, 'fast', function() {
							$contentInner.animate({ opacity: 1 });
						});
					});
				});
			});
		});
	});

});
