$(document).ready(function(){

	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'dark_rounded'});

	$('#wishlist').click(function(e) { 
		e.preventDefault(); 
		initPopup('#popupwishlist'); 

		allFields = '';
		$('#attributes select, #attributes input[type=radio]').each(function(i) {
			allFields += $(this).attr('name') + "=" + $(this).val() + "&";
		});

		$.get('/wishlist', { ajax: true, product_id: $('#product_id').val(), attributes: allFields } , function (responseJson) {
		});
	});

	$('#emailtofriend').click(function(e) { 
		e.preventDefault(); 

		$.get('/products/emailtofriend', { ajax_popup: true }, function (data) {
			$('#popupemailtofriend_content').html(data);
			initPopup('#popupemailtofriend'); 

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

				allFields = '';

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

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

					$('.errorbox').hide();
					$('.correctbox').hide();
					$('.infobubble .error-msg').hide();
					$('.error').removeClass('error');

					if (responseJson.errors) {
						$('#emailtofriend_error').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();
						}
					} else {
						$('#emailtofriend_correct').show();
					}

					}, 'json');
			});
		});
	})

	$('#writereview').click(function(e) { 
		e.preventDefault(); 
		showReviewForm();
	});

	$('#leaveamessage').click(function(e) { 
		e.preventDefault(); 

		$.get('/products/leaveamessage', { ajax_popup: true }, function (data) {
			$('#popupleaveamessage_content').html(data);
			initPopup('#popupleaveamessage'); 

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

				allFields = '';

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

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

					$('.errorbox').hide();
					$('.correctbox').hide();
					$('.infobubble .error-msg').hide();
					$('.error').removeClass('error');

					if (responseJson.errors) {
						$('#leaveamessage_error').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();
						}
					} else {
						$('#leaveamessage_correct').show();
					}

					}, 'json');
			});
		});
	})

	$('#printpage').click(function(e) { e.preventDefault(); window.print(); })
	
	$('#review_title').livequery('keydown', function() {
		remainingLength = 60 - $('#review_title').val().length;
		$('#review_title_characters').html(remainingLength);
	});
	
	$('img[alt=Review Helpful]').parent().click(function(e) {
		e.preventDefault();
		$('.thanksnothelpful, .thankshelpful').hide();
		$(this).parent().parent().find('.thankshelpful').fadeIn();
		
		ratingId = $(this).parent().parent().attr('id').replace("reviewid", "");
		
		var info = $(this).parent().parent().find('.user_ratings');
		var info_parent = $(this).parent().parent().find('.user_ratings_info');
		
		$.get('/products/reviews', { ajax: true, review_id: ratingId, user_review_rating: '1' }, function (responseJson)  {
			info.html(responseJson);
			info_parent.fadeIn();
		}, 'json');
		
	});
	
	$('img[alt=Review Not Helpful]').parent().click(function(e) {
		e.preventDefault();
		$('.thanksnothelpful, .thankshelpful').hide();
		$(this).parent().parent().find('.thanksnothelpful').fadeIn();
		
		var info = $(this).parent().parent().find('.user_ratings');
		var info_parent = $(this).parent().parent().find('.user_ratings_info');
		
		ratingId = $(this).parent().parent().attr('id').replace("reviewid", "");
		$.get('/products/reviews', { ajax: true, review_id: ratingId, user_review_rating: '0' }, function (responseJson)  {
			info.html(responseJson);
			info_parent.fadeIn();
			
		}, 'json');
	});
	
	
	$('p input, p select, p textarea').focus(function() {
		$(this).parent().find('.infobubble .error-msg').not(':regex("^$")').parent().fadeIn(250);
	});
	$('p input, p select, p textarea').blur(function() {
		$(this).parent().find('.infobubble .error-msg').not(':regex("^$")').parent().fadeOut(100);
	});
	


	$('p input, p select, p textarea').focus(function() {
		$(this).parent().find('.infobubble').fadeIn(250);
	});
	$('p input, p select, p textarea').blur(function() {
		$(this).parent().find('.infobubble').fadeOut(100);
	});
	
	$('a.freegiftbutton').click(function(e) {
		e.preventDefault();
		$('#freegifts').slideDown();
	});
	$('#freegifts a').click(function(e) {
		e.preventDefault();
		$('#freegiftchosen span').html($(this).find('img').attr('alt'));
		$('#freegiftselector').hide();
		$('#freegiftchosen').show();
		$('#freegifts').slideUp();
		
	});
	
	initAttributes = function() {
		$('#attributes input[type=radio]').click(function() {

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


			params = { ajax: true, attributes: allFields, product_id: $('#product_id').val() }

			var response = $.ajax({type: 'post', url: '/ajax/Ajax_Products/getVariationData',  data: params, async: false, dataType: 'json'}).responseText;
			response = JSON.parse(response);

			$('.details_thumbs_container').html(response.images);
			$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'dark_rounded'});
		});
		
		$('#attributes .image').each(function() {
			$(this).append('<span class="image_text">' + $(this).find('input[type=radio]:checked').parent().text() + '</span>');
			var attribute_id = $(this).attr('id').replace(/[^\d]/g, '');
			$(this).find('label').each(function() {
				var radioButtonLabel = $(this);
				$('<a href="#" class="tooltip" rel="nofollow" title="' + $(this).text() + '"><img src="/images/ac_product_attributes_data/attribute_image/' + $(this).find('input[type=radio]').val() + '" alt="' + $(this).text() + '" width="20" height="20" /></a>').insertAfter(radioButtonLabel).click(function(e) {
					e.preventDefault();
					radioButtonLabel.find('input[type=radio]').attr('checked', 'checked').click();
				}).addClass(radioButtonLabel.find('input[type=radio]').is(':checked') ? 'selected' : '');
				radioButtonLabel.hide();
			});
		});
		
		$('#attributes .image a:first').each(function() {
			if ($(this).parent().find('a.selected').length == 0) {
				$(this).click();
			}
		});
	}

	initAttributes();
	
});


