
var curr_quick_look;

$(function() {
	
	/*
	 * Image rollovers.  Any image with class "rollover" will have _on 
	appended to the filename (excluding extension) on over, and removed 
	on blur.
	 *
	 */

	$('img.rollover, input.rollover').livequery(function() {
		$(this).hover(
			function () {
				$(this).attr('src', $(this).attr('src').replace(/\.([^\.]*)$/, '_on.$1'));
			},
			function() {
				$(this).attr('src', $(this).attr('src').replace(/_on\.([^\.]*)$/, '.$1'));
			}
			);
		});
	
	/*
	 * Quicklook
	 *
	 */
	
	$('#productgrid li, #productlist li, #productwindow li, #productselling li, #crosssell li').livequery(function() {
		$(this).hover(
			function() {
				$(this).find('.quicklookbutton').fadeIn(250);
			},
			function() {
				$(this).find('.quicklookbutton').fadeOut(250);
			}
		);
	});


	$('.quicklookbutton').click(function(e) {
		e.preventDefault();
		// quick look behaviour goes here
	});

	$('.closeminibasket').livequery('click', function(e) {
		e.preventDefault();
		$('#minibasketadded').fadeOut(200, function() {
			$('#mini_basket').hide("slide", { direction: "up" }, 1000);
			$('#minibasketdefault').show();
		});

	});
	

	$('a.quicklook').livequery('click', function(e) { 
		e.preventDefault(); 

		if ($('#mini_basket').is(':visible')) {
			$('#mini_basket').hide("slide", { direction: "up" }, 1000);
			$('#minibasketadded').hide();
			$('#minibasketdefault').show();
		}
		curr_quick_look = $(this).attr('rel');
		$.get('/products/', { ajax: true, quick_view: $(this).attr('rel') }, function (responseJson) {
			$('#popupquicklook').html(responseJson[0]);
			initPopup('#popupquicklook');
			$('.tooltip').tooltip({ showURL: false });
			if (initAttributes) initAttributes();
			$('#tabs').tabs();
			$('#quicklook_addtobasket').submit(function(f) {

				f.preventDefault();

				$('#minibasketdefault').hide();
				$('#minibasketadded').show();

				$.post('/basket', { ajax: true, quicklook: true, product_id: $('#ql_product_id').val(), product_quantity: $('#ql_product_quantity').val(), attributes: $('#popupquicklook').find('input:radio:checked').serialize() }, function (responseJson) {

					disablePopup('#popupquicklook');
					$('#mini_basket').html(responseJson[0]);
					$('#mini_basket').show("slide", { direction: "up" }, 1000);
					$('#minibasketitems').html(responseJson[1]);
					$('.checkoutbutton').show();
					window.location = '#toplinks';

				}, 'json');
				
			});
			
			setTimeout("$('#attributes .image a:first').each(function() { if ($(this).parent().find('.selected').length == 0) { $(this).click(); } });", 500);
			
		}, 'json');
		
	});
	
	/*
	 * .fielddescriptionlink tooltips
	 *
	 */
	$('.tooltip').livequery(function() {
		$(this).tooltip({ showURL: false });
	})
	
		$('#email_signup_form, .email_signup_form').submit(function(e) {
			e.preventDefault();

			$('#popupemail_signup_content').removeClass('errorbox, correctbox');

			if (!$('#email_signup').val().match(/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i)) {
				$('#popupemail_signup_content').addClass('errorbox');
				$('#popupemail_signup_content').html('Your e-mail address is invalid');
				initPopup('#popupemail_signup');
			} else {

			$.post('/newsletter', { ajax: true, submit_email_signup: true, email_signup: $('#email_signup').val(), opt_out: $('#opt_out').attr('checked') }, function (responseJson) {
				initPopup('#popupemail_signup');
				if (responseJson.errors) {
					$('#popupemail_signup_content').addClass('errorbox');

					errors = '';

					for (x in responseJson.errors) {
						errors = responseJson.errors[x];
					}

					$('#popupemail_signup_content').html(errors);

				} else {
					$('#popupemail_signup_content').addClass('correctbox');
					$('#popupemail_signup_content').html(responseJson.correct);
				}
				}, 'json');

			}

		});	
	
});



function updateReviewForm () {
	$('#review_form').submit(function(e) {
			e.preventDefault();

			allFields = '';

			$('#review_form input[type=text], #review_form textarea, #review_form input[type=radio]:checked').each(function(i) {
				allFields += $(this).attr('name') + "=" + $(this).val() + "&";
			});

			$.get('/products/reviews', { ajax: true, all_fields: allFields, product_id: $('#product_id').val(), review_id: $('#hidden_review_id').val() }, function (responseJson) {

				$('.error').removeClass('error');
				$('.infobubble').hide();
				$('.infobubble .error-msg').html('').hide();
				$('#review_errors').hide();
				$('#review_correct').hide();
				$('#review_info').hide();

				if (responseJson.errors) {
					$('#review_errors').show();

					for (x in responseJson.errors) {
						$('#' + x).parent().addClass('error');
						$('#' + x).parent().find('.infobubble .error-msg').html(responseJson.errors[x]);
						$('#' + x).parent().find('.infobubble .error-msg').show();
					if (x.match(/duplicate/)) {	
						$('#review_errors_list').html(responseJson.errors[x]);
					}
				}

				} else if (responseJson.info) {
					$('#review_info').html(responseJson.info);
					$('#review_info').show();

				} else {
					$('#review_correct').html(responseJson.correct);
					$('#review_correct').show();
				}

			}, 'json');
	});
}



function showReviewForm (reviewId) {
	$.get('/products/reviews', { ajax_popup: true, product_id: $('#product_id').val(), review_id: reviewId }, function (data) {
		$('#popupwritereview_content').html(data);
		initPopup('#popupwritereview'); 
		updateReviewForm();
	});
}




	

