(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();
				}
			}
		});
	};

	var form = {
		"Internet":[
			"Search via Google",
			"Search via another search engine",
			"eBay",
			"Link from another website"
		],
		"Magazine advert":[
			"RREC Advertiser",
			"BDC Advertiser",
			"RREC Regional Newsletter",
			"The Flying Lady",
			"Classic Car Mart",
			"Classic Cars For Sale",
			"Practical Classics",
			"Classic Cars",
			"Octane",
			"Classic Car Weekly",
			"Autocar",
			"Classics Monthly",
			"Classic, Veteran & Vintage Car Directory",
			"Hemmings Motor News",
			"Other publication"
		],
		"RREC Annual Rally":[],
		"Existing Customer":[],
		"Recommendation\/Word of Mouth":[],
		"Other":[]
	};


	$(window).load(function() {
	
		//Image Hover
		$('.listing-item .product-image a img, .listing-type-grid .product-image a img').each(function(i, e) {
			var scale = 2;
			var w = $(e).width();
			var h = $(e).height();
			var z = $(e).css('z-index');
			var new_w = w * scale;
			var new_h = h * scale;
			var new_l = - ((new_w - w) / 2);
			var new_t = - ((new_h - h) / 2);
			
			$(e).hover(function() {
				$(this).addClass('enlarged').css({
					zIndex: 1000
				}).animate({
					height: new_h,
					left: new_l,
					top: new_t,
					width: new_w
				});
			}, function() {
				$(this).animate({
					height: h,
					left: 0,
					top :0,
					width: w
				}, function() {
					$(e).removeClass('enlarged').css({
						zIndex: z
					});
				});
			}).parent('a').css({
				height: h,
				width: w
			});
		});
	
	});


	$(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>';
	    };
	    
	  $('.category1').cycle({
			fx: 'scrollLeft' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category2').cycle({
			fx: 'scrollRight' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category3').cycle({
			fx: 'scrollUp' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category4').cycle({
			fx: 'scrollDown' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category5').cycle({
			fx: 'scrollLeft' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category6').cycle({
			fx: 'scrollRight' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category7').cycle({
			fx: 'scrollUp' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category8').cycle({
			fx: 'scrollDown' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category9').cycle({
			fx: 'scrollLeft' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		$('.category10').cycle({
			fx: 'scrollRight' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		
		$('.header .primary-nav li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
		
		$('.header .primary-nav li .menuparent').hover(function() {
			$(this).siblings('ul').show();
		}, function() {
			$(this).siblings('ul').hide();
		});
		$('.header .primary-nav li ul').hover(function() {
			$(this).show();
		}, function() {
			$(this).hide();
		});
		
		var catalogsearch = $('#catalogsearch');

		catalogsearch.hide();

		var catalogsearch_hover = false;

		catalogsearch.hover(function() {
			catalogsearch_hover = true;
		}, function() {
			catalogsearch_hover = false;
		});

		$('#catalogsearch-toggle a').click(function(event) {
			catalogsearch.show();
			$('#myInput').focus();
			event.stopPropagation();
			event.preventDefault();
		});
		
		$('body').delegate('.yui-ac-hd a', 'click', function(event) {
			$('#catalogsearch form').submit();
			event.preventDefault();
		})
		
		$('body').click(function(event) {
			if (!catalogsearch_hover) {
				catalogsearch.hide();
			}
		});

		$('.cart-container').load('/cart.php');
		
		if ($.fn.cycle) {
			$('.news-scroller').cycle({
				fx: 'scrollLeft',
				timeout: 3000
			});
			$('.gallery').cycle();
			$('.category-image').cycle({
				slideExpr: 'p'
			});
		}
		if ($.fn.imageOverlay) {
			$('.banner-box').imageOverlay({
				duration: 0,
				from:-5,
				to: 153,
				descriptionElement:'.description'
			});
		}		

		var select1 = $('#select1'),
		select2 = $('#select2'),
		option2 = select2.find('option');
		select2.hide();

		select1.change(function() {
			var val = $(this).val(),
			len = (val != 'select') ? form[val].length : 0,
			html = [];

			if (len > 0) {
				select2.show();
				html.push('<option value="">Please Choose...</option>');
				$.each(form[val], function(i, e) {
					html.push('<option value="' + e + '">' + e + '</option>');
				});
				option2.remove();
				select2.html(html.join(''));
			} else {
				option2.remove();
				html.push('<option value="">Please Choose...</option>');
				select2.html(html.join('')).hide();
			}
		});

		//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 (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();
		});

		//View image show/hide
		$('.category-head .view-image').click(function(e) {
			var self = $(this),
			button = self.find('img');
			$('#category-image').slideToggle(function() {
				if ($('#category-image img').is(':visible')) {
					button.attr('src', button.attr('src').replace('category-view-image', 'category-hide-image'));
				} else {
					button.attr('src', button.attr('src').replace('category-hide-image', 'category-view-image'));
				}
			});

			e.preventDefault();
		});

		$('.home .category-list li').equalHeight();
		$('.col-1-layout .listing-type-category li.item a').equalHeight();
		$('.col').equalHeight();

		//External Links

		$('a[rel="external"], #links a').live('click', function (event) {
			window.open(this.href);
			event.preventDefault();
		});

		// Back to top
		var body_html = $('body, html');
		$('#back-to-top a').click(function(e) {
			body_html.animate({
				scrollTop: 0
			});
			e.preventDefault();
		});

	});

}(this.jQuery));

Cufon('h1, h2, h3, .header h4, .banner-container h4, .middle-container h4, h5, h6, dt, .breadcrumbs');
