function disp(id) {
	id = '#'+id;
	if ($(id).is(":hidden")) {
		$(id).slideDown("fast");
	}
}
function hide(id) {
	id = '#'+id;
	if ($(id).is(":visible")) {
		$(id).slideUp("fast");
	}
}
function issel(el,val) {
	ret = false;
	$("#"+el+" option:selected").each(function () {
		if ($(this).val() == val) ret = true;
	});
	return ret;
}

$(document).ready(function(){
	// Reset Font Size
	var originalFontSize = $('html').css('font-size');
	$(".fres").click(function(){
		$('html').css('font-size', originalFontSize);
	});
	// Increase Font Size
	$(".finc").click(function(){
		var currentFontSizeNum = parseFloat($('html').css('font-size'), 10);
		if (currentFontSizeNum < 20)
			$('html').css('font-size', currentFontSizeNum*1.1);
		return false;
	});
	// Decrease Font Size
	$(".fdec").click(function(){
		var currentFontSizeNum = parseFloat($('html').css('font-size'), 10);
		if (currentFontSizeNum > 13)
			$('html').css('font-size', currentFontSizeNum*0.9);
		return false;
	});

	
	// CAROUSEL - INTERIOR PAGES
	$('#gallery-list').jcarousel({ scroll: 1 });
	
	function center_image ( el )
	{
		// center the image
		// get image dimensions
		_parent_width = $('#gallery-image').width();
		_parent_height = $('#gallery-image').height();

		_width = $(el).width();
		_height = $(el).height();

		if ( _width > _parent_width )
			_left = ((_width - _parent_width) / 2) * -1;
		else
			_left = (_parent_width-_width) / 2;

		$(el).css('left', _left + 'px');

		if ( _height > _parent_height )
			_top = ((_height - _parent_height) / 2) * -1;
		else
			_top = (_parent_height-_height) / 2;

		$(el).css('top', _top + 'px');
		
	}
	

	/* Set up my elements */
	$('div.jcarousel-skin-gallery,div.jcarousel-skin-gallery-large').before ( '<div id="gallery-main" />');
	$('#gallery-main').prepend ( '<div id="preloader" />')
	$('#gallery-main').prepend ( '<ul id="gallery-image" />');
	$('#gallery-image').prepend ('<li><img src="" /></li>');
	$('#gallery-image img').attr ('src', $('#gallery-list li:eq(0) img').attr('src') );
	
	$('#gallery-list li:eq(0) img').load ( function() {
		center_image('#gallery-image img');
	})

	/* Set up the events */
	$('#gallery-list img').click ( function(e) {
		// kill event
		e.preventDefault();

		// show preloader
		$("#preloader").fadeIn("fast");

		// add image
		$('#gallery-image').append ('<li><img src="' + $(this).attr('src') + '" class="hidden" /></li>');
		
		
		
		$('#gallery-image li').last().find('img').load(function() {
			// remove the preloader
			$("#preloader").fadeOut("fast");

			// center the image
			center_image ( this );
			
			// display the clicked image
			$(this).fadeIn("slow");
			
			// remove all list elements except the last one (which was just added)
			$('#gallery-image li:not(:last)').fadeOut("fast");
		});

	});
	

	
	/*
	3 MAIN IMAGES ON LANDING PAGES.
	Script to resize the images
	*/
	$('#page-image li').each( function() {
		img_src = $(this).find('img').attr('src');
		
		// begin the preload of the large background image.
	  	var img = new Image();

	  	// wrap our new image in jQuery, then:
	  	$(img)
	    // once the image has loaded, execute this code
	    .load(function () {
			pic_real_width = $(this).attr('width');
			pic_real_height = $(this).attr('height'); 
			pic_src = $(this).attr('src');
			
			if ( pic_src == $('#page-image li:eq(1) img').attr('src') )
				bound_width = 372; // then the image dimensions are 372 x 210
			else
				bound_width = 300; // the image dimensions are 300x210
			
			bound_height = 210;
			
			if ( pic_real_width < bound_width )
			{
				// set width of image to bound_width
				final_width = bound_width;
				
				// set height of image proportionally
				final_height = (bound_width * pic_real_height) / pic_real_width;
			}
			else 
			{
				final_width = pic_real_width;
				final_height = pic_real_height;
			}
			
			if ( final_height < bound_height )
			{
				// set height of image to bound_width
				final_height = bound_height;
				
				// set width of image proportionally
				final_width = (bound_height * pic_real_width) / pic_real_height;
			}
			
			if ( final_width == final_height )
			{
				final_width = 0;
				final_height = 0;
			}				
			
			// set the image dimensions
			if ( final_width != 0 && final_height != 0 )
			{
				// update the image dimensions
				$('#page-image li img').each( function(){
					if ( pic_src == $(this).attr('src') )
					{
						$(this).attr('width', Math.round(final_width));
						$(this).attr('height', Math.round(final_height));
					}
				});

				
			}

			$('#output').append(pic_real_width + ' - ' + pic_real_height + ' - final: ' + final_width + 'x' + Math.round(final_height) + '<br />');
	    })
	    .error(function () { // if there was an error loading the image, react accordingly
			
	    })
	    .attr('src', img_src); // *finally*, set the src attribute of the new image to our image
	});

	$('html').css('font-size', originalFontSize); // for IE7, for some reason


	$('.conditional').css('display','block').hide();
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf('safari') + 1) $('body').addClass('safari');

	/* Add in snazzy background color for focused elements' parent paragraphs */
	$('form :input').each(function() {
		$(this).focus(function() {
			if ($(this).parent().is("p")) $(this).parent().addClass('focused');
			else $(this).parent().parent().addClass('focused');
		});
		$(this).blur(function() {
			if ($(this).parent().is("p")) $(this).parent().removeClass('focused');
			else $(this).parent().parent().removeClass('focused');
		});
	});

	/* Search Form */
	$("#searchfield").focus(function(){
		if ( $(this).val() == "Search" ) $(this).val('');
	}).blur(function(){
		if ( $(this).val() == "" ) $(this).val('Search');
	});

	// Modalify all links to images
	var ireg = /jpe?g|gif|png/i;
	$('a').each(function(){
		if (ireg.test($(this).attr('href'))) {
			$(this).colorbox({maxHeight:'100%', maxWidth:'100%', opacity:0.6});
		}
	});

	/*
	Homepage main slideshow
	*/
	if ($('#slideshow').length) {
		$('#slideshow').cycle({
			fx:         'fade',
			timeout:     5000,
			fastOnEvent: false,
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#" class="sprite">' + idx + '</a></li>'; 
			}
		});
	}

	/*
	Navigation to show active state
	*/
	pageURL = (location.pathname + location.search).replace(/'/g,"%27").replace(/ /g,"%20");  // encode single quotes and spaces
	$('#navbar li a,#secnav ul a').each(function() {
		linkURL = $(this).attr('href').replace(/'/g,"%27").replace(/ /g,"%20");  // encode single quotes and spaces
		if ( linkURL == "http://lodgeatottercreek.unionstmedia.com" + pageURL || linkURL == "http://www.lodgeatottercreek.com" + pageURL || linkURL == "http://lodgeatottercreek.unionstmedia.com.livetest" + pageURL ) {
			$(this).parent('li').addClass('current');
		}
	});
	// if in 3rd level, then add current class to 2nd level list item
	$('li.current').parents('li').addClass('current');

	// Livetest
	pageURL = document.location + '';
	if (pageURL.indexOf('livetest') > -1) {
		$('a').each(function(){
			$(this).attr('href', $(this).attr('href').replace(/http:\/\/lodgeatottercreek\.unionstmedia\.com(\.livetest)?/ig,''));
		});
	}
});

var offset;

$(window).load(function(){
	// Left navigation buttons: move with scroll

	// figure out the initial offset
	offset = $('#leftnav').offset().top;

	$(window).scroll(function() {
		// if possible, we want to move the box to 15px below the window's top
		var t = $(window).scrollTop() + 15 - offset;
		if (t < 0) t = 0; // don't overlap the header, so set the ceiling
		else { // we don't want to overlap the footer, so we set the floor
			var lnh = $('#leftnav').outerHeight(false);
			var f = $('#footer').offset().top - 30;
			t = (lnh + t + offset > f) ? f - lnh - offset : t;
		}
		$('#leftnav').animate({top:t+"px" },{queue: false, duration: 350});
	}).scroll();
});
