$(document).ready(function() {
	
	$("#module-products").each(function() {
		
		// Products photos
		$(this).find('ul.thumbs').each(function() {
			$(this).find('a').each(function() {
				$(this).mouseenter(function() {
					$("#main-photo")
						.attr('href', $(this).attr('href'))
						.find('img').attr('src', $(this).find('img').attr('rel'));
				});
			});
			
			$(".product #photos ul a")
				.fancybox({
					'cyclic': true,
					'centerOnScroll': true,
					'padding': 0,
					'transitionIn': 'elastic'
				})
				.hover(function() {
					$(".product #photos #main-photo").attr('src', $(this).attr('href'));
				});

			$(".product #main-photo").click(function() {
				$(".product #photos ul a[href='" + $(this).attr('href') + "']").click();
				return false;
			});
		});
		
		$('select[name^="attributes_attribute"]').change(function() {
			$.post(
				base_url + 'shop/products/quantity_list', {
					'attribute_id': $(this).find('option:selected').val()
				},
				function(data) {
					$("#attributes_quantity").parent().html(data);
				}
			);
		});		
	});	
	
	$("form#live").each(function() {
		$(this).find("input[type=text]").keyup(function() {
			if ($(this).val().length > 2 || $(this).val().length == 0)
			{
				live_search();
			}
		});
	
		$(this).find("select").change(function() {
			live_search();
		});
	
		function live_search() {
			var query = ($("form#live input[type=text]").val() == $("form#live input[type=text]").prop('label')) ? '' : $("form#live input[type=text]").val();
			
			$("#live_search_results").fadeOut();
						
			$.post(
				$("form#live").attr('action'), {
					'live_search_query': query,
					'live_search_category': $("form#live #live_search_category").val(),
					'live_search_manufacturer': $("form#live #live_search_manufacturer").val()
				},
				function(data) {
					$("#live_search_results").html(data).fadeIn();
				}
			);
		}
	});
	
	$("form#customer-details").each(function() {
		$("input[name=client_address_save]").prop('disabled', true);
		
		$("#client_address_vatin").parent().hide();
		
		$(this).find('legend input').each(function() {
			if ( ! $(this).prop('checked')) {
				$(this).parent().parent().find('ul').hide();
			}
			
			$(this).change(function() {
				if ( ! $(this).prop('checked')) {
					$(this).parent().parent().find('ul').slideUp();
				}
				else {
					$(this).parent().parent().find('ul').slideDown();
				}
			});
		});
	});
	
	/* ~products searching
	 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
	
	$("#product-searching").each(function() {
		var form = $(this);
		
		$(this).find('select, input').change(function() {
			form.submit();
		});
	});
	
	/* ~shipping costs
	 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

	$("#shipping-country form select").change(function() {
		$("#shipping-country form").submit();
	});
	
	/* ~order confirmation
	 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

	$("#order-confirmation select[name$='_method']").change(function() {
		$("#order-confirmation select[name$='_method']").each(function() {
			$(this).after('<strong>' + $(this).find('option:selected').text() + '</strong>').hide();
		});
		
		$("button[name='action_submit']").prop('disabled','disabled');
		
		$("form#order-confirmation").submit();
	});
	
	/* ~installments
	 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

	$("a.calculate-installment").fancybox({
		height: 500,
		type: 'iframe',
		width: 630
	});
	
	$("a.zagiel-terms").fancybox({
		height: 600,
		type: 'iframe',
		width: 720
	});
	
	$("#order-confirmation button[name='action_submit']").click(function() {
		$("input[name='zagiel_terms']").each(function() {
			if ( ! $("input[name='zagiel_terms']").prop('checked'))
			{
				alert('Zanim złożysz zamówienie, zapoznaj się z procedurą udzielenia kredytu ratalnego eRaty Żagiel.');
				$("input[name='zagiel_terms']").parent().css('background-color', '#FEFFEA');
				return false;
			}
		});
	});
	
	/* ~product details
	 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
	
	$("body#module-products #details").each(function() {
		// Set active tab
		$(this).find('> li').first().addClass('active');
		
		// Do links
		$(this).find('> li h3').each(function() {
			$(this).wrapInner('<a href="' + window.location.href + '#' + $(this).parent().attr('id') + '" />');
		});
		
		// Hide inactive content
		$(this).find('> li:not(.active) .content').hide().parent().addClass('non-active');
		
		// Make tabs
		var tab_left = 0;
		$(this).css('position', 'relative').css('padding', '34px 0 0')
			   .find('> li').css('position', 'static').css('padding', '0')
			   .find('h3').css('position', 'absolute').css('top', 0).each(function() {
					$(this).css('left', tab_left);
					tab_left += $(this).outerWidth() + 4;
					
					$(this).find('a').click(function() {
						$("#details li.active")
							.removeClass('active').addClass('non-active')
							.find('.content').hide();
							
						$(this)
							.parent().parent().addClass('active').removeClass('non-active')
							.find('.content').show();
						
						return false;
					});
				});
				
		$(this).find('a[href="#manufacturer"]').click(function() {
			$("#details li.active")
				.removeClass('active').addClass('non-active')
				.find('.content').hide();
				
			$("#details #manufacturer")
				.addClass('active').removeClass('non-active')
				.find('.content').show();
			
			return false;
		});
		
		// Sets
		$(this).find('#sets li').each(function() {
			var set = $(this);
			var chosen = set.find('.chosen-set-product');
			
			set.find('select[name="product_id"]').change(function() {
				var set_id = set.find('input[name="set_id"]').val();
				var main_id = set.find('input[name="main_id"]').val();
				var second_id = $(this).find('option:selected').val();
				
				if (chosen.css('display') == 'none') {
					chosen.slideDown('slow');
				}
				
				$.ajax({
					url: base_url + 'shop/products/set_product',
					type: 'post',
					data: 'main_id=' + main_id + '&second_id=' + second_id + '&set_id=' + set_id,
					success: function(data) {						
						chosen.html(data).find('table').fadeIn('slow');
					}
				});
			});
		});
	});	
	
	// Packets
	$(this).find('#product-packets > li').each(function() {
		var packet = $(this);
		
		packet.find('select').each(function() {
			change_packet_product($(this));
			
			$(this).change(function() {
				change_packet_product($(this));
			});
		});
		
		function change_packet_product(select) {
			var group = select.parent().parent();
			var product_id = select.find('option:selected').val();
			var packet_id = packet.find('input[name="packet_id"]').val();
			
			$.ajax({
				url: base_url + 'shop/products/packet_product',
				type: 'post',
				data: 'product_id=' + product_id + '&packet_id=' + packet_id,
				success: function(data) {						
					group.find('.chosen-packet-product').first().html(data).find('table').fadeIn('slow');
				}
			});
		}
	});
	
	// Cart sets
	$(this).find('#cart-sets .cart-set').each(function() {
		var set = $(this);
				
		set.find('select').each(function() {
			change_set_product($(this));
			
			$(this).change(function() {
				change_set_product($(this));
			});
		});
		
		function change_set_product(select) {
			var chosen = select.parent().parent().find('.chosen-set-product');
		
			var set_id = select.parent().parent().find('input[name="set_id"]').val();
			var product_id = select.find('option:selected').val();
		
			if (chosen.css('display') == 'none') {
				chosen.slideDown('slow');
			}
		
			$.ajax({
				url: base_url + 'shop/cart/set_product',
				type: 'post',
				data: 'product_id=' + product_id + '&set_id=' + set_id,
				success: function(data) {						
					chosen.html(data).find('table').fadeIn('slow');
				}
			});
		}
	});
	
});

