/**
 * Resizes floated image containers to the size of the image
 */
$.fn.imageWidth = function(threshold) {
	return this.each(function() {
		// Threshold is the maximum width an image plus its border and padding can be in relation to its parent container
		var threshold = (threshold) ? threshold : 2/3;
		
		// Determine the width of the image along with borders and padding
		var $image = $('img', $(this));
		var imageWidth = $image.width();
		var paddingLeft = parseInt($image.css('padding-left').replace('px', ''), 10);
		var paddingRight = parseInt($image.css('padding-right').replace('px', ''), 10);
		var borderLeft = parseInt($image.css('border-left-width').replace('px', ''), 10);
		var borderRight = parseInt($image.css('border-right-width').replace('px', ''), 10);
		
		// Calculate total edge (padding and border) width and the total width (edge and image)
		var edgeWidth = paddingLeft + paddingRight + borderLeft + borderRight;
		var totalWidth = imageWidth + edgeWidth;
		
		// Find parent and determine width
		var $parent = $(this).parent();
		var parentWidth = $parent.width();
		
		// If the image is greater then the threshold times the parent width resize the image and the container width
		// Otherwise set the image left's div to the size of the image plus the edge
		if ((threshold * parentWidth) <= totalWidth) {
			var revisedWidth = parentWidth * threshold;
			var revisedImageWidth = revisedWidth - edgeWidth;
			
			$image.width(revisedImageWidth);
			$(this).width(parseInt(revisedWidth, 10));
		} else {
			$(this).width(totalWidth);
		};
	});		
};

/**
 * Builds pull quote divs assuming you've wrappted your content with a span with the class: pullquote-left or pullquote-right
 */
$.fn.pullQuote = function() {
	return this.each(function() {
		var contents = $.trim($(this).html());
		var firstCharacterCode = contents.charCodeAt(0);
		if (firstCharacterCode < 65 || firstCharacterCode > 96) {
			contents = '&hellip; ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' &hellip;';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});		
};

/**
 * Clears a text form element when it has the style 'clear-default'
 */
$.fn.clickClear = function() {
	return this.each(function() {
		this.defaultValue = $(this).val();
		$(this).click(function() {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		}).focus(function() {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val(this.defaultValue);
			};
		});
		
		$('form').submit(function(event) {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		});
	});	
};

var quicklinks = function() {
	$('#nav-quicklinks').hover(function() {
		$('ul', $(this)).slideDown();
	}, function() {
		$('ul', $(this)).slideUp();
	});
};

/**
 * Adds various classes to generic items for use in CSS.
 */
var markupPrep = function() {
	var listPrep = function() {
		$('> li:last', 'ul, ol').addClass('last');
	};
	
	var	tablePrep = function() {
		$('table tr:odd').addClass('alt');
		$('table td:last, table th:last').addClass('last');
	};
	
	listPrep();
	tablePrep();
};

/**
 * Expands and contracts the lunch list on the Reservations page.
 * Automatically hides the list on load so folks without javascript can see it.
 */
function lunchInfo () {
	var $lunchInfo = $('.collection-form .lunch-info');
	
	if ($lunchInfo.size() > 0) {
		$lunchInfo.hide();
		
		$lunchInfo.prev().click(function(event) {
			event.preventDefault();
			$lunchInfo.slideToggle('normal');
		});
	};
}

function datePickers () {
	if ($('body.forms').size() > 0) {
		$('.forms #content-main-content input[type=text]').datepicker();
	};
	
	if ($('body.press-room.press-releases').size() > 0) {
		$('body.press-room #content-main-content input[type=text]').datepicker();
	};
}

function PhotoGallery() {
	slideshow = function() {
		if ($('body.park-attractions').size() > 0) {
			containerHeight = 382;
		} else {
			containerHeight = 460;
		};
		
		$('.gallery-slideshow').innerFade({
			containerheight: containerHeight,
			indexContainer: '.gallery-menu ul',
			speed: 'slow',
			timeout: '8000',
			prevLink: null,
			nextLink: null
		});
	};
		
	cycle = function() {
		$container = $('.gallery-menu-container ul');
		speed = 300;
		itemWidth = 53;
		containerWidth = $('.gallery-menu-container').width();
		
		$('a.next').click(function(event) {
			event.preventDefault();
			if (!$container.is(':animated')) {
				left = parseInt($container.css('left').replace('px', ''), 10);
				width = $('li', $container).size() * itemWidth;
				if (width + left > containerWidth) {
					$container.animate({left: (left - itemWidth) + 'px'}, speed);
				};
			};
		});
		
		$('a.previous').click(function(event) {
			event.preventDefault();
			if (!$container.is(":animated")) {
				left = parseInt($container.css('left').replace('px', ''), 10);
				if (left < 0) {
					$container.animate({left: (left + itemWidth) + 'px'}, speed);
				};
			};
		});
	};
	
	this.init = function() {
		if ($('body.photo-gallery, body.park-attractions .gallery-menu').size() > 0) {
			slideshow();
			cycle();
		};
	};
};

function homepage() {
	$('.home #content #photo-gallery ul').innerFade({
		speed: 'slow',
		timeout: '5000'
	});
}

var pageTools = function() {
	$('ul#page-tools a.email').click(function() {
		$('ul#page-tools li.email-form').animate({width: 'toggle'});
		return false;
	});
	
	$('ul#page-tools a.enewsletter').click(function() {
		$('ul#page-tools li.enewsletter-form').animate({width: 'toggle'});
		return false;
	});
};

var storeLinks = function() {
	$('ul.hover li').hover(
		function () {
			$(this).find('ul.subnav').show();
		},
		function () {
			$(this).find('ul.subnav').hide();
		}
	);
};

var resizeEmbed = function() {
	$('embed').each(function(index) {
		if ($(this).attr('width') > 540) {
			$(this).attr('width', 540);
		};
	});
};

$(document).ready(function() {
	$('input.clear-default').clickClear();
	$('div.image-left, div.image-right').imageWidth();
	$('span.pullquote-left, span.pullquote-right').pullQuote();
	
	// lunchInfo();
	markupPrep();
	quicklinks();

	resizeEmbed();
	
	datePickers();
	
	pageTools();
	
	storeLinks();
	
	var gallery = new PhotoGallery();
	gallery.init();
});
