// load typekit

try{Typekit.load();}catch(e){}

// when ready

$(document).ready(function() {
	});

// when finished loading

$(window).load(function () {

	setTimeout(function() {

	// pop hotspot 1 up for a second

		var width = ( $('#facts div.hotspot-01').children('summary').width() + 20 );
		$('#facts div.hotspot-01').children('summary').removeAttr('style').css( { 'marginLeft': width / 2 - width } );
		$('#facts div.hotspot-01').children('summary').fadeIn(150).animate({ marginTop: -70 }, 300);
		setTimeout(function() {
			var width = ( $('#facts div.hotspot-01').children('summary').width() + 20 );
			$('#facts div.hotspot-01').children('summary').css( { 'marginLeft': width / 2 - width } );
			$('#facts div.hotspot-01').children('summary').animate({ marginTop: -60, opacity: 0 }, 300, function(){ $('#facts div.hotspot-01').removeAttr('style'); });
			}, 1000);

	// then 4

		setTimeout(function() {
			var width = ( $('#facts div.hotspot-03').children('summary').width() + 20 );
			$('#facts div.hotspot-03').children('summary').removeAttr('style').css( { 'marginLeft': width / 2 - width } );
			$('#facts div.hotspot-03').children('summary').fadeIn(150).animate({ marginTop: -70 }, 300);
			setTimeout(function() {
				var width = ( $('#facts div.hotspot-03').children('summary').width() + 20 );
				$('#facts div.hotspot-03').children('summary').css( { 'marginLeft': width / 2 - width } );
				$('#facts div.hotspot-03').children('summary').animate({ marginTop: -60, opacity: 0 }, 300, function(){ $('#facts div.hotspot-03').removeAttr('style'); });
				}, 1000);
			}, 500);

	// then 5

		setTimeout(function() {
			var width = ( $('#facts div.hotspot-05').children('summary').width() + 20 );
			$('#facts div.hotspot-05').children('summary').removeAttr('style').css( { 'marginLeft': width / 2 - width } );
			$('#facts div.hotspot-05').children('summary').fadeIn(150).animate({ marginTop: -70 }, 300);
			setTimeout(function() {
				var width = ( $('#facts div.hotspot-05').children('summary').width() + 20 );
				$('#facts div.hotspot-05').children('summary').css( { 'marginLeft': width / 2 - width } );
				$('#facts div.hotspot-05').children('summary').animate({ marginTop: -60, opacity: 0 }, 300, function(){ $('#facts div.hotspot-05').removeAttr('style'); });
				}, 1000);
			}, 750);

	// then 6

		setTimeout(function() {
			var width = ( $('#facts div.hotspot-06').children('summary').width() + 20 );
			$('#facts div.hotspot-06').children('summary').removeAttr('style').css( { 'marginLeft': width / 2 - width } );
			$('#facts div.hotspot-06').children('summary').fadeIn(150).animate({ marginTop: -70 }, 300);
			setTimeout(function() {
				var width = ( $('#facts div.hotspot-06').children('summary').width() + 20 );
				$('#facts div.hotspot-06').children('summary').css( { 'marginLeft': width / 2 - width } );
				$('#facts div.hotspot-06').children('summary').animate({ marginTop: -60, opacity: 0 }, 300, function(){ $('#facts div.hotspot-06').removeAttr('style'); });
				}, 1000);
			}, 1000);

		}, 5800);

	// navigation slider

	var pos = $('header ul li.current a').position();  
	var width = $('header ul li.current a').width();
	$('header mark').css( { 'left': ( pos.left - 19 ) + ( ( width + 20 ) / 2 ) } ).animate({ bottom: 0 }, 150);

	$('header ul li a').mouseenter(function(navHover) {
		var pos = $(this).position();  
		var width = $(this).width();
		$('header mark').animate({ left: ( pos.left - 19 ) + ( ( width + 20 ) / 2 ) }, 100);
		});
	
	$('header ul').mouseleave(function(navExit) {
	
		setTimeout(function() {
			var pos = $('header ul li.current a').position();  
			var width = $('header ul li.current a').width();
			$('header mark').animate({ left: ( pos.left - 19 ) + ( ( width + 20 ) / 2 ) }, 100);
			}, 300);
		
		});

	$('header ul li a').click(function(navHover) {
		$('header ul li').removeAttr('class');
		$(this).parent().addClass('current');
		});
	
	// form validation
	
	$("form#quality-club").validate({
		rules: {
			'cm-name': "required", //first name
			'cm-f-btiujd': "required", //last name
			'cm-qehij-qehij': { //email
				required: true,
				email: true
			}
			}
		});
	
	// tvc fancybox
	
	$("#tvc a").fancybox({
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.5
		});

	// menu
	
	$('#sidebar ul li .mask').click(function(menuSlider) {
		
		var menuTarget = $(this).parent();

		$('#sidebar ul li').animate({ height: 80 }, 100, function() {
			$(menuTarget).animate({ height: '100%' }, 100);
			});

		});

	$('#menu-slideshow').addClass('finished');

	$('#menu-slideshow div').fadeIn(300);

	$('#menu-slideshow div').cycle({
		fx: 'scrollLeft',
		easing:  'backout',
		timeout: 0, 
		pager:  '#sidebar ul',
		pagerAnchorBuilder: function(idx, slide) { 
			return '#sidebar ul li:eq(' + idx + ') .mask'; 
			} 
		});

	// form effects
	
	$('input[type=text], input[type=email], textarea').each(function() {

		var field = $(this);
		var default_value = field.val();

		field.focus(function() {
			if (field.val() == default_value) {
				field.val('').removeClass('inactive');
				}
			});

		field.blur(function() {
			if (field.val() == '') {
				field.val(default_value).addClass('inactive');
				}
			});

		});

	// fact block

	$('#facts div').mouseenter(function(hotspotHover) {
		$('#facts div summary').fadeOut(50);
		var width = ( $(this).children('summary').width() + 20 );
		$(this).children('summary').removeAttr('style').css( { 'marginLeft': width / 2 - width } );
		$(this).children('summary').fadeIn(150).animate({ marginTop: -70 }, 300);
		});

	$('#facts div').mouseleave(function(hotspotExit) {
		var width = ( $(this).children('summary').width() + 20 );
		$(this).children('summary').css( { 'marginLeft': width / 2 - width } );
		$(this).children('summary').animate({ marginTop: -60, opacity: 0 }, 300, function(){ $(this).removeAttr('style'); });
		});

	$('#facts div').click(function(hotspotHit) {

		var pos = $(this).position();  
		var width = $(this).width();
		var height = $(this).height();
		var details = $(this).children('details');
		var $target = $(hotspotHit.target);

		$('#glow').css( { 'left': pos.left + width / 2 , 'top': pos.top + height / 2 } ).fadeIn(300, function() {
			
			$('#facts h1, #facts h2').stop().animate({ opacity: 0 }, 120, function() {

				if ( $target.is(':nth-child(odd)') ) {
					$('#facts h1, #facts h2').addClass('toplevel');
					} else {
					$('#facts h1, #facts h2').removeClass('toplevel');
					}

				$('#facts h1, #facts h2').removeAttr('style');
				$('#facts h2').empty();
				$(details).clone().appendTo('#facts h2');
				$('#facts h1').animate({ marginTop: -20, opacity: 1 }, 200);
				$('#facts h2').animate({ marginTop: -20, opacity: 1 }, 400);

				});

			});

		});

	$('#facts').mouseleave(function(factsLeave) {

		setTimeout(function() {

			$('#glow, #facts h1, #facts h2').fadeOut(300, function() {
				$(this).removeAttr('style').removeAttr('class');
				});

			}, 500);

		});

	// home page element animations
	
	setTimeout(function() {
		$('#canvas:not(.locked) .lettuce').animate({ left: -154, top: 293 }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1600
				});
		}, 4000);
			
	setTimeout(function() {
		$('#facts, .index footer').addClass('finished');
		}, 5500);

	setTimeout(function() {
		$('#canvas:not(.locked) .wrap-01').animate({ left: '-168', top: '150' }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1600
			});
		}, 2000);

	setTimeout(function() {
		$('#canvas:not(.locked) .wrap-02').animate({ left: '567', top: '-16' }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1600
			});
		}, 3600);

	setTimeout(function() {
		$('#canvas:not(.locked) .spoon').animate({ left: '-164', top: '36' }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1600
			});
		}, 1000);

	setTimeout(function() {
		$('#canvas:not(.locked) .tomato').animate({ left: '-126', top: '73' }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1600
			});
		}, 1500);

	setTimeout(function() {
		$('#canvas:not(.locked) .avocado').animate({ left: '871', top: '282' }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1600
			});
		}, 3000);

	setTimeout(function() {
		$('#canvas:not(.locked) .fork').animate({ left: '875', top: '115' }, 1000).not('.ie').rotate({
				angle: 390,
				animateTo: 360,
				duration: 1600
			});
		}, 1000);

	setTimeout(function() {
		$('#canvas:not(.locked) .lemon').animate({ left: '800', top: '402' }, 1000).not('.ie').rotate({
				angle: 180,
				animateTo: 360,
				duration: 1900
			});
		}, 800);

	});
