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 === 'mouseenter') {
					$(cartItems).fadeIn();
				} else {
					t = setTimeout(function () {
						$(cartItems).fadeOut();
					}, duration);
				}
			}
		});
		
		$(cartItems).live('mouseenter mouseleave', function (e) {
			if (!isOverWritten) {
				if (e.type === 'mouseenter') {
					clearTimeout(t);
					$(cartItems).show();
				} else {
					$(cartItems).fadeOut();
				}
			}
		});
	};

	$(function() {
		
		//Back Button
		$('.back').bind('click', function(e) {
			if (document.referrer) {
				window.open(document.referrer, '_self');
			} else {
				history.go(-1);
			} 
			e.preventDefault();
		});
		
		//Item hover
		$('.item').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
		
		//Cart hover
		$('.header .amount a').cartHover('.recent-items');
		
		//Ajax Cart
		
		$('.add-to-cart-form').submit(function(e) {
			var form = $(this),
			action = form.attr('action'),
			button = $(this).find('.button'),
			add_to_cart = button.parents('.add-to-cart'),
			block_cart = $('.block-cart'),
			body = $('html, body'),
			button_text = button.find('span span').text();
			
			if (action.indexOf('checkout') > 0) {
				if ($('.catalog-category-view').length > 0 || $('.cms-index-index').length > 0) {
					$.ajax({
						beforeSend: function() {
							button.attr('disabled','disabled').addClass('disabled').find('span span').text('Loading...');
						},
						cache: false,
						data: form.serialize(),
						url: action,
						complete: function(xhr) {
							
							button.attr('disabled','').removeClass('disabled').find('span span').text(button_text);
							
							$.ajax({
								url: '/ajax-cart.php',
								success: function(data) {
									block_cart.replaceWith(data);
									body.animate({
										scrollTop: 0
									}, function() {
										$('.recent-items').fadeIn().delay(2000).fadeOut();
									});
								}
							});
						}
					});
				} else {			
					$.ajax({
						beforeSend: function() {
							button.attr('disabled','disabled').addClass('disabled').find('span span').text('Loading...');
						},
						cache: false,
						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 span').text(button_text);
							}, 1000);
						},
						url: '/addtocart.php',
						success: function(xhr) {
							
							button.attr('disabled','').removeClass('disabled').find('span span').text(button_text);
							
							if (!xhr.result) {
								if ($('#advice-required-entry-qty').length > 0) {
									$('#advice-required-entry-qty').text(xhr.message);
								} else {
									$('<div id="advice-required-entry-qty" class="validation-advice">' + xhr.message + '</div>').insertAfter(add_to_cart);
								}
								return;
							}
							$.ajax({
								url: '/ajax-cart.php',
								success: function(data) {
									block_cart.replaceWith(data);
									body.animate({
										scrollTop: 0
									}, function() {
										$('.recent-items').fadeIn().delay(2000).fadeOut();
									});
								}
							});
						}
					});
				}
				e.preventDefault();
			}
		});
		
		/*$('.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 = $(this).find('.button'),
			block_cart = $('.block-cart'),
			body = $('html, body'),
			button_text = button.find('span span').text();

			if (action.indexOf('checkout') > 0) {
				$.ajax({
					beforeSend: function() {
						button.attr('disabled','disabled').addClass('disabled').find('span span').text('Loading...');
					},
					cache: false,
					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 span').text(button_text);
						}, 1000);
					},
					url: action,
					success: function() {
						button.attr('disabled','').removeClass('disabled').find('span span').text(button_text);
						$.ajax({
							url: '/ajax-cart.php',
							success: function(data) {
								block_cart.replaceWith(data);
								body.animate({
									scrollTop: 0
								}, function() {
									$('.recent-items').fadeIn().delay(2000).fadeOut();
								});
							}
						});
					}
				});
				e.preventDefault();
			}
		});*/
		
		
		// Product page image lightbox
		$('#image-colorbox').colorbox();
		
		
		// Home page banner
		$('#banner').cycle();
		
		//Sidebar banner
		$('#mini-banner').cycle({
			fx: 'all',
			timeout: 100
		});
		
		//Customer Service show/hide
		var show_hide_els = $('.cms-customer-service');
		
		show_hide_els.find('dd').hide();
		show_hide_els.find('dt').click(function(e) {
			$(this).next('dd').toggle();
			e.preventDefault();
		});
		
		// External Links
		$('a:external').click(function(e) {
			window.open($(this).attr('href'));
			e.preventDefault();
		});
		
		//CAPTCHA
		var captchacode = $('#captchacode');
		$('#refresh').click(function(e) {
			var generator = '/captcha/securimage_show_example2.php?' + Math.round(Math.random() * 100);
			captchacode.attr('src', generator);
			e.preventDefault();
		});
		
		var submit = $('#submit');
		
		submit.attr('disabled','disabled').addClass('disabled');
		
		$('<div class="validation-advice" id="advive-required-entry-captcha"></div>').insertAfter('#validate').hide();
		
		$('#validate').keyup(function() {
			var self = $(this),
			entry = self.val(),
			captcha_error = $('#advive-required-entry-captcha');
			$.ajax({
				type: 'post',
				url: '/validate.php',
				data: {
					text: entry
				},
				success: function(msg){
					if ($.trim(msg) === 'success') {
						submit.attr('disabled', '').removeClass('disabled');
						self.removeClass('validation-failed');	
						captcha_error.hide();
					} else {
						if (self.val().length >= 5) {
							self.addClass('validation-failed');
							captcha_error.text('The code you have entered is incorrect.').fadeIn();						
						}
					}
				}
			});
		});
		
	});
}(this.jQuery));


