var price = 0;
var org_price = 0;
var in_stock = ;
var delivery_time = ;
var vat = ;
var special = ;
var to_order = ;




function number_format (number, decimals, dec_point, thousands_sep) {
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) { s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0'); }
    return s.join(dec); 
}

function easyPrice(price) {
	if(!price) return;
    // price_type 1: Incl VAT, 2: Excl VAT:
    if(2 == 1) price = (price/(1+(vat/100)));
    return number_format(price, 2, ',', ' ').replace(",00", "") + " kr";
}

function goUpdate() {

	var n_price = price;
	var n_org_price = org_price;
	var n_delivery_time = 0;
	var n_in_stock = 9000000;

	var chosen_ones = false;

	$$('.choicesel').each(function(el) {
    	var v = el.get('value');
		if(v > 0) {
			chosen_ones = true;
			if(prices[v] != 0) { // Increase or decrease total product price
				n_price += prices[v];
				n_org_price += prices[v];
			}
			if(in_stocks[v] < n_in_stock) n_in_stock = in_stocks[v]; // Get the least in-stock-value
			if(delivery_times[v] > n_delivery_time) n_delivery_time = delivery_times[v]; // Get the longest delivery time
		}
	});

	if(chosen_ones) {
        // Set in-stock-status-message depending on lowest quantity in stock:
		if(n_in_stock <= 0) n_in_stock = 'Beställningsvara';
       	else if(n_in_stock < 3) n_in_stock = 'Ett fåtal kvar';
        else n_in_stock = 'Finns i lager';
        
        // Set delivery time-message depending on the highest number of delivery days:
		if(to_order == 1 || n_in_stock == 'Beställningsvara') n_delivery_time = 'Beställningsvara';
        else {
			if(n_delivery_time <= 3) n_delivery_time = '1-3 dagar';
			else if(n_delivery_time > 3) n_delivery_time = '7-14 dagar';
			/*
            if(n_delivery_time == 0) n_delivery_time = 'Skickas samma dag';
            else if(n_delivery_time <= 2) n_delivery_time = '1-2 dagar';
            else if(n_delivery_time <= 3) n_delivery_time = '1-3 dagar';
            else if(n_delivery_time <= 4) n_delivery_time = 'Ca 2-4 dagar';
            else if(n_delivery_time <= 5) n_delivery_time = 'Ca 2-5 dagar';
            else if(n_delivery_time <= 7) n_delivery_time = 'Ca 1 vecka';
            else if(n_delivery_time <= 16) n_delivery_time = 'Ca 1-2 veckor';
            else if(n_delivery_time <= 32) n_delivery_time = 'Ca 3-4 veckor';
            else if(n_delivery_time <= 62) n_delivery_time = 'Ca 1-2 månader';
            else if(n_delivery_time > 70) n_delivery_time = 'Kontakta oss';
			*/
        }


        if($$('#p_price')) { $$('#p_price').set('html', easyPrice(n_price)); }
        if($$('#p_ord_price')) { $$('#p_ord_price').set('html', 'Ord. pris: ' + easyPrice(n_org_price)); }
        if($$('#p_in_stock')) { 
        	$$('#p_in_stock').set('html', n_in_stock);
            if(n_in_stock == 'Tillfälligt slut') {
            	// $('add_to_cart_button').style.display = 'none';
                if($$('#p_delivery_time')) { $$('#p_delivery_time').set('html', '-'); }
            }
            else 
            {
            	// $('add_to_cart_button').style.display = 'block';
                if($$('#p_delivery_time')) { $$('#p_delivery_time').set('html', n_delivery_time); }
            }
         }
	} else {
        if($$('#p_price')) { $$('#p_price').set('html', easyPrice(price)); }
        if($$('#p_ord_price')) { $$('#p_ord_price').set('html', 'Ord. pris: ' + easyPrice(org_price)); }
        if($$('#p_in_stock')) { $$('#p_in_stock').set('html', 'V&auml;lj f&ouml;rst variant'); }
        if($$('#p_delivery_time')) { $$('#p_delivery_time').set('html', 'V&auml;lj f&ouml;rst variant'); }
	}
}

window.addEvent('domready', function() {
	if($('add_to_cart_button')) {
		$('add_to_cart_button').addEvent('click', function(e) {
			e.stop();
			var frm = this.getParent('form');
			if(frm.getElements('.input_qty')[0]) {
            	var qty = frm.getElements('.input_qty')[0].get('value');
            } else var qty = 1;
			var req = new Request.HTML({
				method: 'get', 
				url: frm.get('action'),
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					document.location = frm.get('action');
				}
			}).send('cart_add='+$('cart_add_input').get('value')+'&qty='+qty+'&cjx=1');
			return false;
		});
    	/*
		$('add_to_cart_button').addEvent('click', function(e) {
			e.stop();
			$('webshop_add_to_cart').submit();
			return false;
		});
        */
	}

	$$('.choicesel').each(function(el) {
    	el.addEvent('change', function(e) {
			goUpdate();
        });
    });

	if(window.innerWidth > 600 && $('rating_overview') && $('product_body_inner')) {
		var pbi = $('product_body_inner');
		var ro = $('rating_overview');
		var newHeight = ((ro.offsetTop+ro.offsetHeight)-pbi.offsetTop-32)+'px';
		pbi.style.minHeight = newHeight;
	}

    });