$(document).ready(function() {
	
	/**
	 * 	Set main menu hover on tab(s)
	 */
	$('.navigation li').hover(
		function() {
			$(this).addClass('selected_hover');
		},
		function() {
			$(this).removeClass('selected_hover');
		}
	);
	
	/**
	 * 	Check for valid input into search field (div class search_box
	 */
	$('#search_button').click(function() {
		var form = $('form[name="search_product"]');
		if ($('#search').val() == "Zoeken") {
			$(form).find('input[type="text"]').css('color', 'red');
			return false;
		}
		return form.submit();
	});
	
	
	/**
	 * 	Only show back / next buttons if focus on intro image
	 */
	$('.content.intro').mouseenter(function() {
		$('#intro_prev').show();
		$('#intro_next').show();
	});
	$('.content.intro').mouseleave(function() {
		$('#intro_prev').hide();
		$('#intro_next').hide();
	});
	
	/**
	 * 	Magnify product image(s) in product listing
	 */
	$('.product_image').hover(
		function() {
			var img_src = $(this).find('img').attr('src');			
			$(this).parent().parent().append('<div class="magnify"><img src="' +img_src+ '" border="0" /></div>');
			
			$(this).parent().parent().addClass('to_front');
		},
		function() {
			$('.magnify').remove();
			$(this).parent().parent().removeClass('to_front');
		}
	);
	
	/**
	 * 	Magnify product image into big image placeholder
	 */
	$('.product_images img.small').hover(
		function() {

			var img_src = $(this).attr('src');
			$('.main_product_image').attr('src', img_src);

			var img_src = img_src.replace(new RegExp("[0-9]{3}/[0-9]{3}", "g"), "600/600");			
			$('.main_product_image_full').attr('href', img_src);

			$('.product_images img').removeClass('active');
			$('.product_images .shade').removeClass('hidden');
			
			$('.main_product_image').attr('id', $(this).attr('id'));

			$(this).addClass('active');
			$(this).parent().parent().find('.shade').addClass('hidden');
		},
		function() {
			return false;
		}
	);
	
	/**
	 * 	If large image is clicked -> click small image 
	 * 	for lightbox plugin
	 */
	$('.main_image a img').click(function() {
		$('.small_images').find('img#' +$(this).attr('id')).parent().click();
		return false;
	});
	
	/**
	 * 	Lightbox plugin (magnify product image(s))
	 */
	$('.small_images a').lightBox({
		fixedNavigation: true,
		imageBtnClose: image_url+"lightbox-btn-close.gif",
		imageBtnNext: image_url+"lightbox-btn-next.gif",
		imageBtnPrev: image_url+"lightbox-btn-prev.gif",
		imageLoading: image_url+"lightbox-ico-loading.gif"
	});

	/**
	 * 	Calculate price by amount
	 */
	$('input[name="productAantal2"]').keyup(function() {
		
		var value = $(this).val();

		$.ajax({
			type: "POST",
			url: ajax_url + "getProductPriceByAmount",
			data: { 
				product_id: $('input[name="productId"]').val(),
				amount: $(this).val() 
			},
			dataType: "json",
			success: function(data) {		
				$('#selling_price').html(data.selling_box_price);
				$('#retail_price').html(data.retail_box_price);
				$('#discount_price').html(data.discount_box_price);				
			}
		});
		
	});

	/**
	 * 	Calculate price by surface meters
	 */
	$('input[name="productOppervlakte"]').keyup(function() {

		if ($('input#saw_loss').is(':checked')) {
			getSumBySurfaceCutting($(this).val());
		} else {
			getSumBySurface($(this).val());
		}

	});

	/**
	 * 	Calculate price by number of boxes
	 */
	$('input[name="productAantal"]').keyup(function() {
		
		var value = $(this).val();

		$.ajax({
			type: "POST",
			url: ajax_url + "getProductPriceByBoxes",
			data: { 
				product_id: $('input[name="productId"]').val(),
				amount_of_boxes: $(this).val() 
			},
			dataType: "json",
			success: function(data) {		
				$('#total_surface_meters').val(data.surface_meters);
				$('#selling_price').html(data.selling_box_price);
				$('#retail_price').html(data.retail_box_price);
				$('#discount_price').html(data.discount_box_price);				
			}
		});
		
	});

	/**
	 * 	Show or hide cutting loss box
	 */
	$('input#saw_loss').click(function() {
		
		var surface = $('#total_surface_meters').val();
		
		if ($(this).is(':checked')) {
			$('.zaag').removeClass('hidden');
			getSumBySurfaceCutting(surface);
		} else {
			$('.zaag').addClass('hidden');
			getSumBySurface(surface);
		}
	});

	/**
	 * 	Toggle (extra) product information
	 */
	$('#toggle_info').toggle(
		function() {
			$('.product_information .extra').slideDown('slow');
			$(this).html('Minder productinformatie');
		},
		function() {
			$('.product_information .extra').slideUp('slow');
			$(this).html('Meer productinformatie');
		}
	);
	
	/**
	 * 	
	 */
	$('.search_bar .head').toggle(
		function() {
			$('.search_bar li').removeClass('selected');
			$('.search_bar .items').hide();
			
			$(this).parent().parent().addClass('selected');
			$(this).parent().find('.items').show();
		},
		function() {
			$(this).parent().parent().removeClass('selected');
			$(this).parent().find('.items').hide();
		}
	);
	
	/**
	 * 	Apply CSS styles for specific list items (remove border from first
	 * 	element)
	 */
	$('.listing :first').css('border-top', '0');
	$('.nav_menu div :first').css('border-top', '0');
	$('.steps li:first').css('border-left', '1px solid #BBBBBB');

	/**
	 * 	Set hover on element hover
	 */
	$('.hover').hover(
		function() {
			$(this).addClass('active_hover');
		},
		function() {
			$(this).removeClass('active_hover');
		}
	);
	
	/**
	 * 	Redirect user to first "a" element found in children element(s)
	 */
	$('.hover').click(function() {
		var first_link = $(this).find('a:first-child').attr('href');
		
		if (first_link) {
			window.location = first_link;
		}

		return false;
	});
	
	/**
	 * 	Set hover on element hover
	 */
	$('.logo_bar img').hover(
		function() {
			$(this).addClass('focus');
		},
		function() {
			$(this).removeClass('focus');
		}
	);
	
	/**
	 * 	Remove keyword "Zoeken" from search box on focus
	 */
	$('#keyword').focus(function() {
		if ($(this).val() == "Zoeken") {
			$(this).css('color', 'black');
			$(this).val('');
		}
	});
	
	/**
	 * 	Set keyword "Zoeken" into search box on blur
	 */
	$('#keyword').blur(function() {
		if ($(this).val() == "") {
			$(this).val('Zoeken');
		}
	});

	/**
	 * 	Remove keyword "Zoeken" from search box on focus
	 */
	$('#newsletter').focus(function() {
		if ($(this).val() == "E-mailadres") {
			$(this).css('color', 'black');
			$(this).val('');
		}
	});
	
	/**
	 * 	Set keyword "Zoeken" into search box on blur
	 */
	$('#newsletter').blur(function() {
		if ($(this).val() == "") {
			$(this).val('E-mailadres');
		}
	});

	/**
	 * 	Submit offer form
	 */
	$('#btn_offer_submit').click(function() {
		$('form[name="order"]').submit();
	});

	/**
	 * 	Check for product amount when adding to cart
	 */
	$('form[name="order"]').submit(function() {
		
		if ($('input[name="productAantal2"]').length) {
			if ($('input[name="productAantal2"]').val()) {
				return true;
			}
		} else {
			if ($('input[name="productOppervlakte"]').val()) {
				return true;
			}
			if ($('input[name="productAantal"]').val()) {
				return true;
			}
		}
		alert('Er is geen correct productaantal ingevuld!');
		return false;

	});

	$('input[name="delivery"]').live('click', function() {
		if ($(this).is(':checked')) {
			$('.delivery_cost').hide();
			$('input[name="verzenden"]').val('false');
		} else {
			$('.delivery_cost').show();
			$('input[name="verzenden"]').val('true');
		}
	});
	

	$('form[name="intro_search"] select').live('change', function() {		
		$.ajax({
			url: ajax_url + "setintrosearch",
			data: {
				categorie_id: $('select[name="categorie_id"]').val(),
				merk_id: $('select[name="merk_id"]').val(),
				prijs: $('select[name="prijs"]').val(),
				prijs_m2: $('select[name="prijs_m2"]').val(),
				kleur: $('select[name="kleur"]').val()
			},
			dataType: "html",
			success: function(data) {
				$('.intro_search').html(data);
			}
		});
	});
	
	
	$('form[name="intro_search"]').submit(function() {
		
		var submit = false;
		
		$('form[name="intro_search"] select').each(function(index, value) {
			if ($(value).val()) {
				submit = true;
			}
		});
		
		if (submit) {
			return true;
		}
		
		alert('Er is geen categorie categorie en/of kleur geselecteerd');
		return false;
	});


	/**
	 * 	Shopping cart functions
	 */
	$('input[name="artikel_aantal"]').live('blur', function() {
		$.ajax({
			url: ajax_url + "updateitemfromcart",
			data: {
				articleId: $(this).attr('id'),
				articleAmount: $(this).val(),
				no_delivery: $('input[name="delivery"]').is(':checked')
			},
			dataType: "html",
			success: function(data) {
				$('.cart_contents').html(data);
				updateCart();
			}
		});

		return false;
	});
	
	
	$('.remove_from_cart').live('click', function() {

		if (confirm('Weet u het zeker?')) {
			$.ajax({
				url: ajax_url + "removeitemfromcart",
				data: {
					articleId: $(this).attr('id')
				},
				dataType: "html",
				success: function(data) {
					$('.cart_contents').html(data);
					updateCart();				
				}
			});
		}

		return false;
	});

	$('#delivery').live('click', function() {
		var shipment = $('.shipment');
		if ($(shipment).is(':hidden')) {
			$(shipment).removeClass('hidden');
		} else {
			$(shipment).addClass('hidden');
		}
		$.ajax({
			url: ajax_url + "setdeliver",
			data: {
				no_delivery: $('input[name="delivery"]').is(':checked')
			},
			dataType: "html",
			success: function(data) {
				$('.cart_contents').html(data);
				updateCart();
			}
		});
		return true;
	});
	
	// Als klant order wil afhalen dan de verzendkosten uit de order gooien
	$("input[name=verzendmethode]").live('click', function(){
		if($(this).is(':checked')) {
			var no_delivery = ($(this).val() == 'afhalen'); 
			$.ajax({
				url: ajax_url + "setdeliver",
				data: {
					no_delivery: no_delivery
				},
				dataType: "html",
				success: function(data) {
					$('.cart_contents').html(data);
					updateCart();
					if($('input[name=betaalmethode]:checked').val() == 'handmatig') {
						$($('input[name=betaalmethode]:first').attr('checked', true));
					}
					if(!$('ul.payment_methods li:last').hasClass('hidden') && !no_delivery) {
						$('ul.payment_methods li:last').addClass('hidden');
					} else {
						$('ul.payment_methods li:last').removeClass('hidden');
					}
					
				}
			});
			return true;
		}
	});
	
	$('#request_offer').toggle(
		function() {
			$('.request_offer').removeClass('hidden');
			$('html, body').animate({ scrollTop: $(".request_offer").offset().top }, 500);
			
			return false;
		},
		function() {
			$('.request_offer').addClass('hidden');
			return false;
		}
	);
	
	$('input[name="verzendmethode"]').click(function() {
		if ($(this).val() == "afhalen") {
			$('.payment_methods li.hidden').show();
		} else {
			$('.payment_methods li.hidden').hide();
		}
	});

	$('.product_complementary a.no_link').live('click', function() {
		
		var category_id = $(this).attr('id').substring(4,6);
		var product_id = $(this).parent().parent().attr('id').substring(5);

		$.ajax({
			url: ajax_url + "getcomplementaryproducts",
			dataType: "html",
			data: {
				product_id: product_id,
				category_id: category_id
			},
			success: function (data) {
				
				$('body').append('<div class="shadow"></div>');

				var html = '<div class="focus_box">';
				html += data;
				html += '</div>';
				$('body .container').prepend(html).fadeIn("slow");
			}
		});

		return false;
	});
	
	$('#close_focus').live("click", function() {
		closeFocusBox();
	});
	
	$('.shadow').live("click", function() {
		closeFocusBox();
	});
	
	function closeFocusBox() {
		$('.shadow').removeClass('shadow');
		$('.focus_box').remove();
	}

	function updateCart() {

		$.ajax({
			url: ajax_url + "updatecart",
			dataType: "html",
			success: function(data) {
				$('.shopping_cart').html(data);
			}
		});

	}

	function getSumBySurfaceCutting(surface) {

		$.ajax({
			type: "POST",
			url: ajax_url + "getProductPriceCuttingLoss",
			data: { 
				product_id: $('input[name="productId"]').val(),
				surface_meters: surface 
			},
			dataType: "json",
			success: function(data) {
				$('#surface_cutting_loss').val(data.surface_meters);
				$('#total_boxes').val(data.boxes);
				$('#selling_price').html(data.selling_box_price);
				$('#retail_price').html(data.retail_box_price);
				$('#discount_price').html(data.discount_box_price);				
			}
		});

	}

	function getSumBySurface(surface) {

		$.ajax({
			type: "POST",
			url: ajax_url + "getProductPriceBySurface",
			data: { 
				product_id: $('input[name="productId"]').val(),
				surface_meters: surface 
			},
			dataType: "json",
			success: function(data) {		
				$('#total_boxes').val(data.boxes);
				$('#selling_price').html(data.selling_box_price);
				$('#retail_price').html(data.retail_box_price);
				$('#discount_price').html(data.discount_box_price);				
			}
		});

	}

});

