(function($) {
	
	$.fn.quickview = function(){
		
		var quickview = $(this);
		// var form = quickview.find('form'); don't use this, ajax calls may replace form content
		
		quickview.find('.tabs').tabs(); //the product description tabs
		addthis.toolbox('.addthis_toolbox');
		
		// cart form submission
		quickview.find('form.order-form').livequery('submit', function() {
			quickview.toggleSpinner({hide: 'form'});
		});
		quickview.find('form.order-form').ajaxForm({
			complete: function(html) {
				if($(html.responseText).find('.errorList, .error-message, .formError, .error').length){
					var validatedForm = $(html.responseText).find('.form-fields');
					quickview.find('form').find('.form-fields').html(validatedForm.html());
					quickview.toggleSpinner({show: 'form'});
					$.fn.colorbox.resize();
				}else{
					if ($('div.mainarea-OrderItemDisplayViewShiptoAssoc').length) {
						// if this is the cart page
						location.reload();
					} else {
						$.fn.colorbox.close()
						$('html, body').animate({scrollTop: 0}, 'slow');
						refreshAndDisplayMiniCart();
					}
				}
			}
		});
		
		// defining attribute selection box option
		quickview.find('.attribute select').livequery('change', function(e) {
			e.preventDefault();
			$.fn.inventory(quickview);
			$(quickview).togglePrice();
			$(quickview).togglePhoto();
		});
		
		// email a friend
		quickview.find('.email-a-friend-link').livequery('click', function(e) {
			e.preventDefault();
			var emailAFriendLink = $(this);
			emailAFriendLink.hide()			
			quickview.find('.email-a-friend-disabled').show();
			quickview.find('.product-info').slideUp(function() {
				var productInfoContainer = $(this);
				if (quickview.find('.email-a-friend').length) {
					quickview.find('.email-a-friend').remove();
				}
				//pageTracker._trackPageview(emailAFriendLink.attr('href'));
				$.ajax({
					url: emailAFriendLink.attr('href')
					, complete: function(html) {
						var emailAFriendForm = $(html.responseText).hide();
						quickview.find('.product-info').before(emailAFriendForm);
						emailAFriendForm.slideDown(function() {
							$.fn.colorbox.resize();								
							//$.fn.colorbox.resize({width:'650px'});
						});
					}
				});
			});
		});
		
		$.fn.inventory(quickview);
		
		$('#emailAFriendForm').livequery('submit', function(e) {
			e.preventDefault();
			var form = $(this);
			form.hide();
			var loadingMsg = form.next('.loading-image');
			loadingMsg.css('height', form.height());
			loadingMsg.css('width', form.width());
			loadingMsg.show();			
			$.ajax({
				url: form.attr('action'),
				data: form.serialize(),
				complete: function(html) {
					loadingMsg.hide();
					var validatedForm = $(html.responseText).find('form');
					form.after(validatedForm);
					form.remove();
					$.fn.colorbox.resize();
				}
			});
		});
		
		$('.email-a-friend button.cancel').livequery('click', function(e) {
			e.preventDefault();
			quickview.find('.email-a-friend-disabled').hide();
			quickview.find('.email-a-friend-link').show();
			$('.email-a-friend').slideUp(function() {
				quickview.find('.product-info').slideDown($.fn.colorbox.resize);
			});
		});
	};
	
	//ajax refresh and display of mini cart
	var refreshAndDisplayMiniCart = function() {
	   var miniCartUrl = $("#ajaxMiniCartURL").attr('href');
	   $("#miniCart").load(miniCartUrl, function() {
		   $("#miniCart").addClass('miniCartExpanded');
	   });
	};
	
})(jQuery)

