(function ($) {

	// cart hover
	var isOverWritten = false;
	jQuery.fn.cartHover = function (cartItems, duration) {
		var cartLink = $(this),
		t;
		duration = duration || 500;

		//$(cartItems).hide();

		cartLink.live('mouseenter mouseleave', function (e) {
			if (!isOverWritten) {
				if (e.type === 'mouseover') {
					$(cartItems).fadeIn();
				} else {
					t = setTimeout(function () {
						$(cartItems).fadeOut();
					}, duration);
				}
			}
		});

		$(cartItems).live('mouseenter mouseleave', function (e) {
			if (!isOverWritten) {
				if (e.type === 'mouseover') {
					clearTimeout(t);
					$(cartItems).show();
				} else {
					$(cartItems).fadeOut();
				}
			}
		});
	};

	$(function () {

		$('#tabs a').click(function(event) {
			var index = $('#tabs a').index(this);
			$('#tab-content li').hide().eq(index).show();
			$(this).parent('li').siblings().removeClass('selected').end().addClass('selected');
			event.preventDefault();
		});

		$('#tab-content li').hide().eq(0).show();

		$('#category-navigation li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});


		$('#cart-container').load('/cart.php', {
			cache: false
		});

		//Ajax Cart
		$('.add-to-cart-form').submit(function(e) {
			/*
			if ($('.catalog-product-view').length) {
				if (!productAddToCartForm.validator.validate()) {
					return;
				}
			}
			*/
			var form = $(this),
			action = form.attr('action'),
			button = form.find('.form-button'),
			body = $('html, body'),
			button_text = button.find('span').text();
			$.ajax({
				beforeSend: function() {
					button.attr('disabled','disabled').addClass('disabled').find('span').text('Loading...');
				},
				data: form.serialize(),
				error: function(xhr, e) {
					button.find('span span').text(e + ' (' + xhr.status + ': ' + xhr.statusText + ')');
					setTimeout(function() {
						button.attr('disabled','').removeClass('disabled').find('span').text(button_text);
					}, 1000);
				},
				url: action,
				success: function() {
					button.attr('disabled','').removeClass('disabled').find('span').text(button_text);
					$.ajax({
						url: '/cart.php',
						success: function(data) {
							$('#cart-container').html(data);
							body.animate({
								scrollTop: 0
							}, function() {
								$('.cart-items').fadeIn();
								setTimeout(function() {
									$('.cart-items').fadeOut();
								}, 2000);
							});
						}
					});
				}
			});
			e.preventDefault();
		});


		//Layered Navigation
		$('#narrow-by-list dd').hide();
		$('#narrow-by-list dt a').prepend('<span>&darr;</span>').bind('click', function(e) {
			var self = $(this).parents('dt'),
			span = self.find('span');
			if (self.next('dd').is(':visible')) {
				self.removeClass('selected');
				span.html('&darr;');
			} else {
				self.addClass('selected');
				span.html('&uarr;');
			}
			self.next('dd').slideToggle('fast');
			e.preventDefault();
		});

		//Best Sellers/Latest Additions
		var $homeProducts = $('.home-products');
		$homeProducts.not(':first').hide();
		$('.home-products-container h1 a').each(function (i, e) {
			$(e).click(function (event) {
				$('.selected', '.home-products-container').removeClass('selected');
				$(e).addClass('selected');
				$homeProducts.filter(':visible').hide()
				$homeProducts.eq(i).show();
				event.preventDefault();
			});
		});


		//Products drop down
		var t;
		$('.nav .products a').bind({
			'click': function(e) {
				e.preventDefault();
			},
			'mouseenter': function() {
				$('.sub-menu').hide();
				clearTimeout(t);
				$('#pets-menu').show();
			},
			'mouseleave': function() {
				t = setTimeout(function() {
					$('#pets-menu').hide();
				}, 500);
			}
		});

		$('.nav .brand a').bind({
			'click': function(e) {
				e.preventDefault();
			},
			'mouseenter': function() {
				$('.sub-menu').hide();
				clearTimeout(t);
				$('#brands-menu').show();
			},
			'mouseleave': function() {
				t = setTimeout(function() {
					$('#brands-menu').hide();
				}, 500);
			}
		});

		$('.sub-menu').bind({
			'mouseenter': function() {
				clearTimeout(t);
				$(this).show();
			},
			'mouseleave': function() {
				var self = $(this);
				t = setTimeout(function() {
					self.hide();
				}, 500);
			}
		});

		//Home page scroller
		if ($('.banner-slides').length) {
			$('.banner-slides').cycle();
		}

		$('#search_mini_form').delegate('#view-all', 'click', function(event) {
			$('#search_mini_form').submit();
			event.preventDefault();
		});

		$('.mini-cart .content .view-basket').cartHover('.cart-items');

		// Search by condition dropdown
		$('#search-by-condition').change(function () {
			var $val = $(this).val();
			if ($val !== '') {
				window.location = $val;
			}
		});

		//external links
		$('a[rel="external"]').click(function (e) {
			window.open($(this).attr('href'));
			e.preventDefault();
		});
	});
}(this.jQuery));
