

$(function(){
	var ask_qty=true; // Ask for quantity or submit 'qty=1' immediately?

	// Replace byMass 'buy' buttons with a script
	$('form.price[data-bymass*=]').unbind().submit(function(){
			// Collect vital data
			var product = $(this);
			var product_bymass = {
				min:    parseFloat(product.attr('data-bymass-min')),
				max:    parseFloat(product.attr('data-bymass-max')),
				step:   parseFloat(product.attr('data-bymass-step'))
				};
			// Create a smartcart object
			new smartcart(this, ask_qty, 'mass', product_bymass);
			return false; // Prevent the form from reloading the page
			});

	//SET: If we're in the product page, smartcart should appear immediately
	if (window.location.pathname.indexOf('/products/') === 0)
		$('form.price').submit();
	});

/** Quantity popup for the smartcart
 * @param smcart         Smartcart object
 * @param smpart         $(Subsmartcart) that belongs to this method
 * @param arg            An argument for the smartcart. {min: max: step: }
 */
smartcart.prototype.popup_mass = function(smcart, smpart, arg){
	var sliderval=smpart.find('.mass');
	// Set default value
	sliderval.val(arg.min);
	// Bind DisplayPrice function
	sliderval.keyup(function(){  smcart.upprice_mass(smcart, smpart, arg);  }).keyup();

	smpart
		// View the slider
		.find('.slider').slider({
				value:  arg.min,
				min:    arg.min,
				max:    arg.max,
				step:   arg.step,
				slide: function(event, ui) {  sliderval.val(ui.value).keyup();  }
			}).end()
		// add OK event
		.find('.ok').unbind().click(function(){
			// kill the slider
			smpart.find('.slider').slider('destroy');
			// Purchase
			var qty = Math.round( parseFloat(  smpart.find('.mass').val()  ) / arg.step  );
			smcart.purhase(qty);
			return false; // prevent! (default action)
			});
	};


/** Updates the price when something external is changed
 */
smartcart.prototype.upprice_mass = function(smcart, smpart, arg) {
	smpart.find('.price').text(
			(smcart.price * smpart.find('.mass').val() / arg.step) + ' ' + jscart.curr
			);
	};