headings_scaled = false;
/**
 * jQuery DOM ready handler.
 **/
$(document).ready(function() {
	
	// Defuscate any hrefs in format you(replace+with+at+symbol)domain.com. Replaces the mailto and the link text.
	// Note that emails are being encrypted in the CMS via the cleanup_rich_text function
	//$('p').defuscate({link: true});
	
	$('.default-value').each(function() {
	    var default_value = this.value;
	    //$(this).css('color', '#999'); // this could be in the style sheet instead
	    $(this).focus(function() {
	        if(this.value == default_value) {
	            this.value = '';
	            $(this).css('color', '#76797b');
	        }
	    });
	    $(this).blur(function() {
	        if(this.value == '') {
	            $(this).css('color', '#999');
	            this.value = default_value;
	        }
	    });
	});
	
	/*
	SUBMENUS
	*/
	// Add div for subnav bg to DOM.
	$('nav#primary').append('<div class="subnav-bg">&nbsp;</div>');
	if ($('nav#primary li.has-menu.active').length){
		// Add identifying class to primary nav if a section with a submenu is active.
		$('nav#primary li.has-menu.active').parent().parent().addClass('open');
		// Set height of subnav bg to match the height of active submenu.
		var subnav_active_menu_len = $('nav#primary li.active li').length;
		$('.subnav-bg').css('height',((17*subnav_active_menu_len)+16)+'px');
	}
	// Hide all submenus except the currently active one.
	$('nav#primary>ul>li').each(function() {
		if ($(this).hasClass('active')){
			$(this).find('ul').css({
				'display': 'block'
			});
		} else {
			$(this).find('ul').css({
				'display': 'none'
			});
		}
	});
	// Prepare each subnav menu item for animating, except the active submenu.
	$('nav#primary li:not(.active) ul').each(function() {
		$(this).css({
			'opacity': 0
		});
	});
	// Mouse enter/leave handlers for top-level menu buttons.
	$('nav#primary>ul>li').mouseenter(function(){
		$this = $(this);
		if ($this.hasClass('has-menu')){
			$this.find('ul').css({
				'display': 'block'
			});
			// Default height to animate subnav bg to.
			var targ_subnav_height = 0;
			var subnav_active_menu_len = 0;
			var subnav_menu_len = $this.find('li').length;
			if ($('nav#primary').hasClass('open')){
				subnav_active_len = $('nav#primary li.active li').length;
			}
			targ_subnav_height = Math.max((17*subnav_menu_len)+16,(17*subnav_active_menu_len)+16);
			$('.subnav-bg').stop();
			$('.subnav-bg').animate({
				height: targ_subnav_height+'px'
			}, 200, function(){
				// anim complete
				$this.find('ul').each(function(){
					if(!$this.hasClass('active')){
						// Fade in submenu items.
						$(this).stop();
						$(this).css('opacity',0);
						$(this).animate({
							opacity: 0.99
						}, 200);
					}
				});
			});
		}
	}).mouseleave(function(){
		$this = $(this);
		// Should the subnav bg close?
		// Default: close it.
		var targ_subnav_height = 0; 
		if ($('nav#primary').hasClass('open')){
			// This page has an active submenu. Leave it open.
			var subnav_active_menu_len = $('nav#primary li.active li').length;
			targ_subnav_height = (17*subnav_active_menu_len)+16;
		}
		// Should the submenu be hidden?
		// Default: hide it.
		var targ_opacity = 0;
		var targ_display = 'none';
		if ($this.hasClass('active')){
			// This is an active submenu. Don't hide it.
			targ_opacity = 0.99;
			targ_display = 'block';
		}
		$this.find('ul').css({
			'display': targ_display
		});
		// Set subnav menu to desired opacity.
		$this.find('ul').each(function(){
			$(this).css({
				opacity: targ_opacity
			});
		});
		// Animate the subnav bg to desired height.
		$('.subnav-bg').stop();
		$('.subnav-bg').animate({
			height: targ_subnav_height
		}, 200);
		
    });
    
    /*
	Home page
	Animation performed on docuemnt load to ensure all required images are loaded.
	*/
	$('.bg-text').css('opacity',0);
	$('.bg-text').css('display','block');
	var taglineReducedDimensions = {width: '0px', height: '0px', "margin-top": '58px'};
	$('.home-hero .tagline img').css(taglineReducedDimensions);
	
    /*
	Lettering.js
	*/
	if( jQuery.isFunction(jQuery.fn.lettering) ){
		$('.page-about h1').lettering('lines');
	}
    
    /*
	FAQ/Sauces Accordian 
	*/
	// IE7 messes up the scaled headings when adding an <a> to them,
	// so only continue if !IE7
	if (!$.browser.msie  || parseInt($.browser.version) > 7) {
		$('.answer, .product-desc').hide();
		$('.faq h2, .sauces h2').wrapInner('<a href="#"></a>');
		$('.faq h2 a, .sauces h2 a').live('click', function(e){
			e.preventDefault();
			$h2 = $(this).closest('h2');
			if(!$h2.hasClass('open')){
				$('.faq h2, .sauces h2').each(function(){
					var $h2 = $(this).closest('h2');
					if($h2.hasClass('open')){
						$h2.removeClass('open');
						$h2.next().slideUp(500);
					}
				});
				$h2.addClass('open');
				$h2.next().slideDown(500);
			} else {
				$h2.removeClass('open');
				$h2.next().slideUp(500);
			}
		});
	}
	
	/*
	Restaurant menus: be nice to IE7
	*/
	$('.single-item').parent().parent().parent().css('padding-right','0px');
	/*
	After a timeout we will reveal all webfonts and scale headings.
	This is just a safety measure, in case fonts don't load for some reason.
	*/ 
	setTimeout("revealWebfonts()", 5000);
	setTimeout("scaleHeadings()", 5000);
	
});
$(window).load(function() {
	/*
	Home page
	Animation performed on docuemnt load to ensure all required images are loaded.
	*/
	
	var text_anim_delay = 4500;
	var text_anim_dur = 1500;
	
	$('.bg-text.r1-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r1-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r1-3').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r2-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r2-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r3-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r3-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r3-3').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r4-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r4-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r5-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r5-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r6-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r6-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r7-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r7-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r7-3').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r8-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r8-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r8-3').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r9-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r9-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r10-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r10-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r11-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r11-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r11-3').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r12-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r12-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r13-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r13-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	$('.bg-text.r14-1').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	$('.bg-text.r14-2').delay(Math.floor(Math.random()*text_anim_delay)).animate({opacity: 0.99}, text_anim_dur);
	
	var taglineDimensions = {width: '594px', height: '117px', "margin-top": '0px'};
	$('.home-hero .tagline img').delay(5500).animate(taglineDimensions, 1000, 'easeOutBounce');
});
/*
Scale heads so they are of equal width.
See: http://stackoverflow.com/questions/4165836/javascript-scale-text-to-fit-in-fixed-div
See: http://vidasp.net/tinydemos/adjust-font-size.html
Note: Secondary nav items are no longer scaled, font sizes are hard coded.
*/
function scaleHeadings(){
	if (!headings_scaled){
		headings_scaled = true;
		//$('.faq h2, .sauces h2, nav#secondary li, .sauces h1').each(function() {
		$('.faq h2, .sauces h2, .sauces h1').each(function() {
	
			var $this = $(this);
			//var boxWidth = $(this).css('width');
			var boxWidth = $(this).width();
			var fontSize = parseInt($(this).css('fontSize'), 10);
			var $line;
			var lineWidth;
			
			$this.html(function(i,v) {
				return $('<span>').html(v).css('whiteSpace', 'nowrap');
			});
			
			$line = $this.children('span').first();
			//lineWidth = $line.css('width');
			lineWidth = $line.width();
			
			while ( lineWidth < boxWidth ) {
				fontSize += 1;
				$line.css('fontSize', fontSize + 'px');
				//lineWidth = $line.css('width');
				lineWidth = $line.width();
			}
			
			fontSize -= 1;
			$line.css('fontSize', fontSize + 'px');
			// Add a little padding to the bottom of shorter lines, to keep leading even-ish.
			$line.parent().css('padding-bottom', Math.floor(((150/fontSize)/2)-1) + 'px');
		});
		//$('.faq, .sauces, nav#secondary').css('visibility','visible');
		$('.faq, .sauces').css('visibility','visible');
	}		
}
/*
After a timeout we will reveal all webfonts.
This is just a safety measure, in case fonts don't load for some reason.
*/
function revealWebfonts(){
	$('nav#primary li, .page-planet #content h1, nav#secondary').css('visibility','visible');
}
/*
Load fonts.com web fonts via Google web font loader
See: http://code.google.com/apis/webfonts/docs/webfont_loader.html
*/
WebFontConfig = {
    monotype: {
        projectId: 'a72c478a-0fd7-4236-8586-8928b3fdfa40'//this is your Fonts.com Web Fonts projectId
    },
    active: function() {
    	// A brief delay to ensure web fonts have been applied 
    	// before we proceed with scale calculations.
	    setTimeout("scaleHeadings()", 250);
	    // Sometimes google font loader will remain in wf-loading state, even after this function executes.
	    // This ensure fonts are revealed in a timely fashion.
	    revealWebfonts();
	}
};
(function() {
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
	    '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
})();




