$(document).ready(function() {
// Sitewide stuff

	// Show header sub-menus on mouse-over
	$('#menu-header li').hover(function() {
		$(this).children('.sub-menu').css({'visibility':'visible'});
	}, function() {
		$(this).children('.sub-menu').css({'visibility':'hidden'});	
	});
	
	// This sets the nav to the correct parent / child relationship for the News & Events section since News and Events now need to be "Custom Links"
	// under the News & Events item. This will maybe need to be updated when pushed live to change the hardcoded id for News & Events.
	//
	// Also doing this for Our Pinot Noir > Accolades and Our Chardonnay > Accolades since those are also now custom page links.	
	if ($('body').hasClass('custom-post-type-flowers_article-archive') || $('body').hasClass('custom-post-type-flowers_event-archive')) {
		$('#menu-item-592, #menu-item-830').addClass('current-menu-ancestor current-page-ancestor current-menu-parent current_page_parent current_page_ancestor');
		if ($('body').hasClass('custom-post-type-flowers_article-archive'))
			$('#menu-item-592 ul li:first-child, #menu-item-830 ul li:first-child').addClass('current-menu-item current_page_item');
		else if ($('body').hasClass('custom-post-type-flowers_event-archive'))
			$('#menu-item-592 	ul li:last-child, #menu-item-830 ul li:last-child').addClass('current-menu-item current_page_item');

	} else if ($('body').hasClass('custom-post-type-flowers_chardaccl-archive')) {
		$('#menu-item-63, #menu-item-290').addClass('current-menu-ancestor current-page-ancestor current-menu-parent current_page_parent current_page_ancestor');
		$('#menu-item-809, #menu-item-827').addClass('current-menu-item current_page_item');

	} else if ($('body').hasClass('custom-post-type-flowers_pinotaccl-archive')) {
		$('#menu-item-62, #menu-item-283').addClass('current-menu-ancestor current-page-ancestor current-menu-parent current_page_parent current_page_ancestor');
		$('#menu-item-808, #menu-item-828').addClass('current-menu-item current_page_item');
	}

	// The flourish for the top navigation bar, expands to meet the current page item on hover and gets set to the proper currently selected item or ancestor.
	var $el, leftPos,
			$mainNav = $('#header .menu-container');

		$mainNav.append('<div id="magic-line"></div>');
		var $magicLine = $('#magic-line');
		var $width = ($('.current-page-ancestor, .current-menu-item').width() / 2);

		if($('#menu-header .current-menu-ancestor.first').length != 0) {
			$magicLine
				.width($('#menu-header .current-menu-item, #menu-header .current-page-ancestor').position().left + 500 + $width - 56)
				.data("origLeft", $magicLine.position().left)
				.data("origWidth", $magicLine.width());
		}	else if($('#menu-header .current-menu-item.last').length != 0) {
			$magicLine
				.width($('.current-menu-item, .current-page-ancestor').position().left + 500 + $width - 10)
				.data("origLeft", $magicLine.position().left)
				.data("origWidth", $magicLine.width());
		} else if ($('#menu-header .current_page_item, #menu-header .current-page-ancestor').length != 0) {
			$magicLine
				.width($('#menu-header .current-page-ancestor').parents('#menu-header .current-page-ancestor:first').position().left + 500 + $width -32)
				.data("origLeft", $magicLine.position().left)
				.data("origWidth", $magicLine.width());
		} else {
			$magicLine
				.width(0)
				.data("origLeft", $magicLine.position().left)
				.data("origWidth", $magicLine.width());
		}

		$('#menu-header .sub-menu, #menu-header .last a').parent().hover(function() {
			$el = $(this);
			leftPos = $el.position().left;
			$newWidth = $(this).width() / 2;

			if($el.hasClass('first')) {
				$magicLine.stop().animate({
					width: leftPos + 504
				});
			} else if ($el.hasClass('last')) {
				$magicLine.stop().animate({
					width: leftPos + 543
				});
			} else {
				$magicLine.stop().animate({
					width: leftPos + 500 + $newWidth - 32
				});
			}
		}, function() {
			$magicLine.stop().animate({
				left: $magicLine.data("origLeft"),
				width: $magicLine.data("origWidth")
			});
		});


// Specific pages below

// home page
//---------------------------------------------
	
	// Slowly fade the B&W image out to reveal the color version
	$('div.fadehover img:last-child').stop().animate({'opacity': '0'}, 6400);


	// Gallery images light-box
	$('a[rel^="prettyPhoto"]').prettyPhoto();

	// Only execute this on the Stories section, so just check if timeline-container exists...
	if ($('.timeline-container').length) {
		if ($('.timeline-container .current-menu-item').length) {
			$('.timeline-container').scrollTo('.current-menu-item');
		}
		
		$('<div id="time-line"></div>').appendTo('.timeline-container');

		var $timeLine = $('#time-line');
		var $position = $('.current-menu-item').position();

		$timeLine
			.width($('#menu-stories-timeline .current-menu-item').position().left + 40)
			.data('origLeft', $timeLine.position().left)
			.data('origWidth', $timeLine.width());
		
		$('#menu-stories-timeline a').hover(function() {
			$timeLine.stop().animate({
				width: 90 + $(this).position().left + 'px'
			});
		}, function() {
			$timeLine.stop().animate({
				width: $timeLine.data('origWidth')
			});
		});
		
		$('#menu-stories-timeline li li a').hover(function() {
			$timeLine.stop().animate({
				width: 40 + $(this).position().left + 'px'
			});
		}, function() {
			$timeLine.stop().animate({
				width: $timeLine.data('origWidth')
			});
		});
	} // end if timeline-container

/************ USE THE SCROLLTO PLUGIN TO MAKE THE NEXT / PREV BUTTONS FOR THE TIMELINE, JUST SET THE SCROLL TO WIDTH PLUS OR MINUS THE CURRENT POSITION AND THE AMOUNT TO THE WIDTH OF THE SCREEN OR HALF IF YOU WANT TO DO SMALLER CHUNKS! ****/
});
