$(function(){
	
	$.fn.productDetail = function(){
		
		var productDetail = $(this);
		// var form = productDetail.find('form'); don't use this, ajax calls may replace form content
		
		productDetail.find('.tabs').tabs(); //the product description tabs
		
		// cart form submission
		productDetail.find('form').livequery('submit', function() {
			productDetail.find('.email-a-friend-link').hide();
			productDetail.toggleSpinner({hide: '.form-fields'});
		});
		productDetail.find('form').ajaxForm({
			complete: function(html) {
				if($(html.responseText).find('.errorList, .error-message, .formError, .error').length){
					var errors = $(html.responseText).find('.error-message');
					var validatedForm = $(html.responseText).find('.form-fields');
			
					productDetail.toggleSpinner({show: '.form-fields'});
					productDetail.find('.form-fields').html(validatedForm.html());
					productDetail.find('.email-a-friend-link').show();
					
					//show error messages above form
					productDetail.find('.product-detail-form div.error-message').remove();
					errors.each(function() {
						productDetail.find('.product-links').before(this);
					});
				}else{
					window.location = $('#cartUrl').val();
				}
			}
		});
		
		// defining attribute selection box option
		productDetail.find('.attribute select').livequery('change', function(e) {
			e.preventDefault();
			$.fn.inventory(productDetail);
			$(productDetail).togglePrice();
			$(productDetail).togglePhoto();
		});
		
		// email a friend lightbox and form initialization
		var emailAFriendLink = productDetail.find('.email-a-friend-link');
		emailAFriendLink.click(function(e) {
			e.preventDefault();
		});
		emailAFriendLink.colorbox({
			opacity: .5
			, scrolling: false
			, onComplete: function() {
				$.fn.colorbox.resize();
				//pageTracker._trackPageview($(this).val('action'));
				$('#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();
							var closeBtn = $('.email-a-friend button.cancel');
							closeBtn.remove();
							$.fn.colorbox.resize();
						}
					});
				});
			}
			, maxWidth: '100%'
		});
		
		$.fn.inventory(productDetail);

		// add to wishlist form submission
		productDetail.find('.add-to-wishlist-link').livequery('click', function(e) {
			e.preventDefault();
			productDetail.find('.email-a-friend-link').hide();
			productDetail.toggleSpinner({hide: '.form-fields'});
			$.ajax({
				url: $(this).attr('href'),
				data: productDetail.find('form').serialize(),
				complete: function(html) {
					if($(html.responseText).find('.errorList, .error-message, .formError, .error').length){
						var validatedForm = $(html.responseText).find('.form-fields');
						productDetail.toggleSpinner({show: '.form-fields'});
						productDetail.find('.form-fields').html(validatedForm.html());
						productDetail.find('.email-a-friend-link').show();
						//show error messages above form
						$(html.responseText).find('div.error-message').each(function() {
							productDetail.find('form').before($(this));
							alert($(this).html());
						});
						//productDetail.find('form').before(errors);
						
					}else{
						window.location = $('#wishListURL').val();
					}
				}
			});
		});
	};
})

