jQuery.noConflict();
(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 () {

		var $body = $('html, body'),
		    hasBeenClicked = false,
		    tabs = $('.tabs a'),
		    createMessage = function (text, type) {
		        return '<ul class="messages"><li class="' + type + '-msg"><ul><li>' + text + '</li></ul></li></ul>';
		    };

		//category page

		$('.more').click(function (e) {
			$(this).hide().nextAll().show();
			e.preventDefault();
		});

		$('.less').click(function (e) {
			$(this).hide().prevUntil('.more').hide();
			$('.more', $(this).parent()).show();
			e.preventDefault();
		});


		$('.cart-container .enquiry').cartHover('.cart-items');

		$('#IsDeliverySame').change(function (event) {
			if (document.customerform.IsDeliverySame.checked) {
				$('.billing-details .input-text, .billing-details select').attr('disabled', 'disabled');
			} else {
				$('.billing-details .input-text, .billing-details select').removeAttr('disabled');
			}
			event.preventDefault();
		});

		//category navigation
		var nav = $('#nav'),
		nav_timeout,
		pagerSelect = $('.pager select');
		
		$('#nav ul:not(#nav ul ul)').css({
			top: $('.header-nav-container').innerHeight() - 4
		});
		
		if (nav.length) {

			nav.find('> li').hoverIntent(function() {
				var self = $(this);
				clearTimeout(nav_timeout);
				hasBeenClicked = false;
				pagerSelect.hide();
				self.siblings('li').removeClass('over').children('ul').hide();
				self.addClass('over').children('ul').show();		
			}, function() {
				var self = $(this);
				pagerSelect.show();
				if (!hasBeenClicked) {
					nav_timeout = setTimeout(function() {
						self.removeClass('over').children('ul').hide();
					}, 750);
				}
			});
			
			nav.find('li li').bind('click', function (e) {
				var self = $(this),
				visibility = self.children('ul').css('display');
				hasBeenClicked = true;
				if (visibility === 'block') {
					self.children('ul').hide();
					return false;
				}
				if (self.children('ul').length > 0) {
					self.siblings().removeClass('over');
					self.addClass('over');
					if (self.siblings('li').children('ul:visible').length > 0) {
						self.siblings('li').children('ul:visible').hide(function () {
							self.children('ul').show();
						});
					} else {
						self.children('ul').show();
					}
					return false;
				}
				e.stopPropagation();
			});
			
			$body.bind('click', function () {
				hasBeenClicked = true;
				$('#nav li').removeClass('over');
				$('#nav li ul').hide();
			});
		}


		/*if ($('#nav').length) {
			$('#nav > li').bind('mouseenter', function (event) {
				hasBeenClicked = false;
				$('.pager select').hide();
				$(this).siblings('li').removeClass('over').children('ul').hide();
				$(this).addClass('over').children('ul').show();
			}).bind('mouseleave', function (event) {
				$('.pager select').show();
				if (hasBeenClicked === false) {
					$(this).removeClass('over').children('ul').hide();
				}
			}).bind('click', function (event) {
				if ($(this).children('ul').length) {
					hasBeenClicked = true;
					$('.pager select').show();
					$(this).addClass('over').children('ul').show();
					event.preventDefault();
					event.stopPropagation();
				}
			});

			$('#nav li li').bind('click', function (event) {
				var el = this,
				visibility = $(el).children('ul').css('display');
				hasBeenClicked = true;
				if (visibility === 'block') {
					$(el).children('ul').fadeOut();
					return false;
				}
				if ($(el).children('ul').length > 0) {
					$(el).siblings().removeClass('over');
					$(el).addClass('over');
					if ($(el).siblings('li').children('ul:visible').length > 0) {
						$(el).siblings('li').children('ul:visible').fadeOut(function () {
							$(el).children('ul').fadeIn();
						});
					} else {
						$(el).children('ul').fadeIn();
					}
					return false;
				}
				event.stopPropagation();
			});

			$body.bind('click', function (event) {
				hasBeenClicked = true;
				$('#nav li').removeClass('over');
				$('#nav li ul').hide();
			});
		}*/

		if (tabs.length) {
			tabs.click(function (event) {
				tabs.removeClass('selected').filter(this).addClass('selected');
				$('#tab-content').html(createMessage('Loading&hellip;', 'loading')).load(this.href + ' #content-box', function (response, status, xhr) {
					if (status === 'error') {
						$('#tab-content').html(createMessage('An error has occured. ' + xhr.status + ' ' + xhr.statusText, 'error'));
					}
				});
				event.preventDefault();
			}).eq(0).addClass('selected').click();
		}

		//AJAX Cart
		$('.add-to-cart-form, #product_addtocart_form').submit(function(e) {
			var self = this,
			button = $('button', self);
			button.hide().after('<div class="loading">Loading...</div>').attr('disabled', 'disabled');
			isOverWritten = true;
			$.post(self.action, $(self).serialize(), function () {
				$('.cart-container').load('/cart.php', function () {
					$('html, body').animate({
						scrollTop: 0
					});
					$('.cart-items').fadeIn();
					setTimeout(function() {
						$('.cart-items').fadeOut();
						isOverWritten = false;
					}, 3000);
				});
				$('.loading', self).remove();
				button.show().attr('disabled', '');
			});
			e.preventDefault();
		});


		//External Links

		$('a[rel="external"], #links a').live('click', function (event) {
			window.open(this.href);
			event.preventDefault();
		});

	});

}(jQuery));

Cufon('h1, h2, h3, h4, h5, h6, dt');
