$jQ(document).ready(function() {
	
	// Dropdown Menus: Only Activate for Mega + Suckerfish + Simple
	if(!$jQ('#main-menu .no-dropdowns').length) {
		var menuSelector = "#main-menu ul > .level0";
		if($jQ('#main-menu .suckerfish-dropdowns').length) // Suckerfish
			menuSelector = "#main-menu ul li";
		$jQ(menuSelector).hoverIntent({
			interval: 20,
			over: function() { 
				$jQ(this).addClass('droplink-active').children('.dropdown').addClass('dropdown-active');
			}, 
			out: function() {
				$jQ(this).removeClass('droplink-active').children('.dropdown').removeClass('dropdown-active');
			}
		});
	}
		
	// jQuery Default Value HTML5 Placeholder Fallback
	$jQ('[placeholder]').defaultValue();
	
	// Search Focus
	$jQ('#search input').focus(function() { $jQ(this).addClass('focus'); })
						.blur(function() { $jQ(this).removeClass('focus'); });
	
	// Back to Top Button
	$jQ('.top').click(function(e) {
		$jQ('html, body').animate({ 'scrollTop': $jQ('#logo').scrollTop() }, 500);
		e.preventDefault();
	});
	$jQ.waypoints.settings.scrollThrottle = 30;
	$jQ('#wrapper').waypoint(function(event, direction) {
		$jQ('.top').toggleClass('hideTop', direction === "up");
	});
	
	// Homepage Features (IE6-8)
	if($jQ('.ie .features').length) {
		$jQ('.ie .features .feature').last().addClass('last-feature');
	}
	
	// Instacart
	initInstacart();
	
	// Slideshow
	if($jQ('.slideshow').length) {
		$jQ('.slideshow').slides({
			generateNextPrev: true,
			generatePagination: true,
			hoverPause: true,
			play: 3000
		});
		
		// Center Slideshow Pagination
		$jQ('.slideshow').each(function() {
			var pager = $jQ(this).find('.pagination');
			pager.css('left', (470 - ((pager.outerWidth()/2)+10)) + 'px');
		});
		
		$jQ('.slideshow').hoverIntent({
			over: function() { $jQ(this).find('.prev, .next').fadeTo(220, 0.4); },
			out: function() { $jQ(this).find('.prev, .next').fadeTo(220, 0); }
		});
		
		$jQ('.slideshow .prev, .slideshow .next').hoverIntent({
			interval: 20,
			over: function() { $jQ(this).fadeTo(10, 1); },
			out: function() { $jQ(this).fadeTo(220, 0.4); }			
		});
	}
	
	// Feature Overlay
	if($jQ('#overlay-features').length) {
		$jQ('.feature .feature-overlay').click(function(e) {
			window.location.href = $jQ(this).parent().find('a').attr('href');
			e.preventDefault();
		});
		$jQ('.feature').hoverIntent({
			interval: 20,
			over: function() { 
				$jQ(this).find('.feature-overlay').show();
			}, 
			out: function() {
				$jQ(this).find('.feature-overlay').hide();
			}			
		});
	}
	
	// Notifications Slide In & Close Behavior
	if($jQ('.messages').length) {
		if(!$jQ('.messages').parent().is('.global-messages, .page-messages').length) {
			$jQ('.messages').wrap('<div class="page-messages" />');
			$jQ('.messages-close').appendTo('.page-messages');
		}
		$jQ('.messages').parent().addClass('messages-active');
		
		// IE6-7 Z-Index & Zoom Fix
		if($jQ('.ie7 .messages, .ie6 .messages').length) {
			$jQ('.ie7 .messages, .ie6 .messages').closest('div[class*="grid_"]').css({
				position: 'relative',
				'zIndex': 9999
			});
		}
		
		var open = setTimeout(function() {
			$jQ('.messages-close a').css('display', 'block').click(function(e) {
				$jQ('.messages').parent().removeClass('messages-active');
				$jQ('.messages-close').remove();
				e.preventDefault();
			});
		}, 400);
	}
	
	// Compare Slide In & Behavior
	if($jQ('#compare-products-pane').length) {
		var shownToUser = false;
		
		if($jQ('.success-msg:contains("comparison list")').length) {
			// Show user the compare pane if recently added compare item
			$jQ('#compare-products-pane').addClass('compare-active');
			$jQ('body').addClass('push-it');			
		}
		
		var waitToInit = setTimeout(function() {
			$jQ('#compare-button').mouseenter(function(e) {
				$jQ('#compare-products-pane').addClass('compare-active');
				$jQ('body').addClass('push-it');
			
				if($jQ('.success-msg:contains("comparison list")').length)
					$jQ('#compare-button').removeClass('compare-fade-color');
			});
		}, 1000);

		$jQ('#compare-products-pane').mouseleave(function(e) {
			$jQ(this).removeClass('compare-active');
			$jQ('body').removeClass('push-it');
			
			if($jQ('.success-msg:contains("comparison list")').length && shownToUser === false) {
				$jQ('#compare-button').addClass('compare-fade-color');
				shownToUser = true;
			}
		});
	}
	
	// Language/Currency Switcher
	if($jQ('#cart-menu .flags').length) {
		$jQ('#cart-menu .flags').hoverIntent({
			interval: 20,
			over: function() { 
				$jQ('.currency-selector').appendTo('#cart-menu .flags');
				$jQ('.currency-selector').show();
			}, 
			out: function() {
				if(!$jQ('.currency-selector').hasClass('hasFocus'))
					$jQ('.currency-selector').hide();
			}
		});
		
		$jQ('.currency-selector').live('focus', function() {
			$jQ(this).addClass('hasFocus');
		}).live('blur', function() {
			$jQ(this).removeClass('hasFocus').hide();
		});
	}
});

function initInstacart() {
	if($jQ('.instacart').length && !$jQ('.no-items-in-cart').length) {
		$jQ('.cart').hoverIntent({
			interval: 20,
			over: function() { $jQ(this).addClass('cart-active').find('.instacart').show(); }, 
			out: function() { $jQ(this).removeClass('cart-active').find('.instacart').hide(); }			
		});
	}
}

(function($) {
    $.fn.center = function() {
	    this.css("position", "absolute");
	    this.css("top", (($jQ(window).height() - this.outerHeight()) / 2) + $jQ(window).scrollTop() + "px");
	    this.css("left", (($jQ(window).width() - this.outerWidth()) / 2) + $jQ(window).scrollLeft() + "px");
	    return this;
	}
})(jQuery);
