/**
 *	Size functions
 *
 *	Developed for Greenflame
 *	October 2011
 *
 *	Version 1.0
 */
 
//-------------------------------------------------------------------

/**
 *	Select size
 *
 *	When a size is selected, this function changes some values
 *	in the form used when adding a product to the shopping cart.
 */
$(document).ready(function() {
	$('.size').change(function() {
		// Get id's
		id = $(this).attr('id');
		part_id = $(this).val();

		// Change shown price
		$.ajax({
			url: "/ajax/update_size_price",
			async: true,
			cache: false,
			type: 'post',
			data: {
				product_info_id: part_id
			},
			statusCode: {
		    	404: function() {
					$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
		    	}
		  	},
			error:function (xhr, ajaxOptions, thrownError) {
				$.jGrowl(xhr.status);
				$.jGrowl(thrownError);
			},
			success: function(data) {
				$('.price_container_'+id).html(data);
			}
		});
		
		// Change shown part number
		$('.part_number_container_'+id).html($('.part_number_'+part_id).val());
		
		// Change product form details
		$('.product_info_id_'+id).val(part_id);
		$('.part_number_'+id).val($('.part_number_'+part_id).val());
		$('.weight_'+id).val($('.weight_'+part_id).val());
		$('.price_'+id).val($('.price_'+part_id).val());
		$('.stock_'+id).val($('.stock_'+part_id).val());
	});
});


/**
 *	Error if there's not selected any size before add to cart
 */ 
$(document).ready(function () {
	// Grid
	$(".grid .addToBasket").click(function() {
		// Se if product has an size option
		if($(this).parent().parent().find('select.size').length > 0) {
			// Se if there is made an selection within the size option
			if($(this).parent().parent().find('select.size option:selected').val() == '') {
				// Inform the user of the missing size argument
				$(this).parent().parent().find('select.size').addClass('error');
				//$.jGrowl('Størrelse ikke valgt!<br />Du skal vælge en størrelse for at kunne lægge varen i kurven', { life:6000 });
			}else{
				// If everything is ok do this
				$(this).parent().parent().find('select.size').removeClass('error');
			};
		};
	});
	
	// Show - As on Pecani-Shop (select)
	$(".vis .addToBasket").click(function() {
		// Se if product has an size option
		if($(this).parent().parent().find('.valg').length > 0) {
			// Se if there is made an selection within the size option
			if($(this).parent().parent().find('select.size option:selected').val() == '') {
				// Inform the user of the missing size argument
				$(this).parent().parent().find('select.size').addClass('error');
				//$.jGrowl('Størrelse ikke valgt!<br />Du skal vælge en størrelse for at kunne lægge varen i kurven', { life:6000 });
			}else{
				// If everything is ok do this
				$(this).parent().parent().find('select.size').removeClass('error');
			};
		};
	});
	
	// Show - As on Shoesonline (radio)
	$(".vis .addToBasket").click(function() {
		// Se if product has an size option
		if($(this).parent().parent().find('.valg').length > 0) {
			// Se if there is made an selection within the size option
			if($(this).parent().parent().find('input.size:checked').val() == '') {
				// Inform the user of the missing size argument
				//$.jGrowl('Størrelse ikke valgt!<br />Du skal vælge en størrelse for at kunne lægge varen i kurven', { life:6000 });
			};
		};
	});
});
