jQuery.noConflict();
jQuery(document).ready(function(){
	// Hide / show delivery input
	jQuery('#tx_hmrsimpleorder_pi1_delivery_usedelivery_yes').click(function(){
		jQuery('#delivery-address-div').show(); 
	});
	
	jQuery('#tx_hmrsimpleorder_pi1_delivery_usedelivery_no').click(function(){
		jQuery('#delivery-address-div').hide(); 
	});
	
	
	// Cycle images when frame1 is selected
	jQuery('.csc-frame-frame1 .csc-textpic-imagecolumn ul').cycle({
		'random': 1,
		'timeout': 5000
	});
	
	
	if(jQuery('.ordertable').length>0) {
		// Update on changes in fields
		jQuery('.ordertable input').change(function(){
			updateOrderSummary();
		});
	
		// Update on initial values
		updateOrderSummary();
	}
	


});


function updateOrderSummary() {
		// Order summary
	
	
		html = '';
		html += '<div class="quickstart">';
		html += '<h3>Din bestilling:</h3>';


		basket = jQuery('.ordertable .col1 input[value!=""]');
		if(basket.length>0) {
			html += '<p>Alle priser er ekskl. moms og levering</p>';
			html += '<table>';
			html += '<tr><th class="align-center">Antal</th><th>Beskrivelse</th><th class="price">Pris</th></tr>';
			totalPrice = 0;
			basket.each(function(){
				amount = parseInt(jQuery(this).val());
				
			
				if(amount>0) {
					descriptionAndPrice = jQuery(this).attr('title').split('|');
					description = descriptionAndPrice[0];
					price = amount*descriptionAndPrice[1];
				
					totalPrice += price;

				
					html += '<tr class="productrow">';
					html += '<td class="align-center">'+amount+'</td>';
					html += '<td><b>'+description+'</b></td>';
					html += '<td class="price">'+formatCurrencyDKK(price)+'-</td>';
					html += '</tr>';
					
				
					col2input = jQuery(this).parent().parent().parent().children('.col2:last').children('div').children('input');
					if(col2input.val()>0) {
						descriptionAndPrice = col2input.attr('title').split('|');
						description = descriptionAndPrice[0];
						price = col2input.val()*descriptionAndPrice[1];
						
						totalPrice += price;
					
						html += '<tr>';
						html += '<td class="align-center"></td>';
						html += '<td>+ '+col2input.val() + ' x ' +description+'</td>';
						html += '<td class="price">'+formatCurrencyDKK(price)+'-</td>';
						html += '</tr>';
					}
					
					col3input = jQuery(this).parent().parent().parent().children('.col3:last').children('div').children('input');
					if(col3input.val()>0) {
						descriptionAndPrice = col3input.attr('title').split('|');
						description = descriptionAndPrice[0];
						price = col3input.val()*descriptionAndPrice[1];
						
						totalPrice += price;
					
						html += '<tr>';
						html += '<td class="align-center"></td>';
						html += '<td>+ '+col3input.val() + ' x ' +description+'</td>';
						html += '<td class="price">'+formatCurrencyDKK(price)+'-</td>';
						html += '</tr>';

					}
					
					
				}
				
				
			});
			
			html += '<tr class="total"><td colspan="3" class="align-right">I alt DKK '+formatCurrencyDKK(totalPrice)+'-</td></tr>';
			html += '</table>';
		} else {
			html += '<p style="margin-top: 15px;">Din bestilling indeholder ingen varer.</p>';
		}




		html += '</div>';
		jQuery('#js-order-summary').html(html);
}


function formatCurrencyDKK(num) {
	return formatNumber(num,0,'.',',','','','-','');
}
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0');y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}


