$(document).ready(function() {

	// Auto-position second-level navigation links
	if($('#nav').length && $('#nav li.active').length && $('#subnav').length) {

		// Gather nav and subnav widths
		var totalWidth = $('#nav').width();
		var subnavWidth = 0;
		$('#subnav li').each(function() {
			el = $(this);
			subnavWidth += el.width() + 24; // 24 is margin on li
		});

		// Determine position to center under active tab
		var activeTab = $('#nav li.active');
		var activeTabX = activeTab.offset().left - $('#subnav').offset().left;
		var xPos = activeTabX - (subnavWidth/2) + (activeTab.width() / 2);

		// Right or left flush to avoid overflow
		if (totalWidth < (xPos + subnavWidth)) {
			xPos = totalWidth-subnavWidth-12;
		}
		if (xPos < 60) {
			xPos = 0;
		}

		// Set the left margin with our final adjustment value
		$('#subnav li:first').css('margin-left', xPos);
	}


	// Email quick-signup form
	var e = $('#email-input');
	// Toggle text in email field
	e.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	// Don't pass default text to magnetmail
	$('#email-signup').submit(function(){
		if(e.val() == e.attr('defaultValue')) {
			e.val('');
		}
	});


	// Embed flash movies
	if($('#home-banner').length) {
		swfobject.embedSWF("/images/site/home_banner_2010_loader.swf", "home-banner", "790", "256", "7.0.0");
	}
	if($('#dayton-banner').length) {
		swfobject.embedSWF("/images/site/dayton_banner.swf", "dayton-banner", "790", "256", "7.0.0");
	}


	// Accordion lists
	var accordionOpts = { header: 'dt', selectedClass: 'active', autoHeight: false, active: false, alwaysOpen: false };
	$('#employment-list').accordion(accordionOpts); 
	$('#directions-list').accordion(accordionOpts);
	$('.faq-list').accordion(accordionOpts);


	// Chef's Corner Recipe Show-Hide
	$("#recipe-nav li a").each(function(i){
		var anchor = $(this);
		var target = $(this).attr('href');
		if(i > 0) $(target).hide();
	
		$(this).click(function(){
			$("#recipe-nav li a").each(function(){
				var t = $(this).attr('href');
				$(t).hide();
				$(target).slideDown('slow');
			});
		});
	});


	// Automatic photo captions
	$("img.hero[title]").each(function() {	
		var title = $(this).attr('title');
		var wrapper = document.createElement('div');
		wrapper.className = "photo-wrap";
		wrapper.style.width = ($(this).width() + 14) + 'px'; // 14px padding+border
		$(this).wrap(wrapper);
		$(this).after($('<div class="caption">'+title+'</div>'));
	});
	$(".dining-hero img[title]").each(function() {	
		if(window.console) window.console.log("title: "+$(this).attr('title'));
		var title = $(this).attr('title');
		$(this).after($('<div class="caption">'+title+'</div>'));
		
	});
	

	// Rounded corners overlay (dining section)
	if($('img.rounded').length) {
  		$('img.rounded').each(function() {
			var wrapper = document.createElement('div');
			wrapper.className = 'rounded';
			$(this).wrap(wrapper);
			$(this).after($('<div class="corners"></div>'));
  		});
	}


	// Add form validation
	if($('form.chform').length) {
		$('form.chform').validate();
	}


	// Open external links in new window
	$("a[href^=http]").each(function() {
		if(this.href.indexOf(location.hostname.replace(/www./gi, "")) == -1) {
			$(this).click(function() { window.open(this.href, "_blank"); return false; });
		}
	})
});

