
/**
 *	Initialize global vars
 */
var choices			= new Object();
var prices			= new Object();
var totalPrice		= 0;
var language		= null;
var transportField	= null;
var extraKmField	= null;
var totalItem		= null;
var transportRow	= null;

/**
 *	Calculate transport costs
 */
function calculateTransport(value){
	if(parseInt(value)){
		transportPrice = parseInt(value) * $E('input[name=transport_multiplier]').value * $E('input[name=price_transport]').value;
		transportField.value = parseInt(value);
	}else{
		transportPrice = 0;
		transportField.value = '';
	}
	
	prices.transport = transportPrice;
	
	updateTotal();
}

/**
 *	Calculate extra kilometers
 */
function calculateExtraKm(value){
	if(parseInt(value)){
		extraKmPrice = parseInt(value) * $E('input[name=price_extra_km]').value;
		extraKmField.value = parseInt(value);
	}else{
		extraKmPrice = 0;
		extraKmField.value = '';
	}
	
	prices.extra_km = extraKmPrice;
	
	updateTotal();
}

/**
 *	Calculate price for hours
 */
function changeHours(key){
	// Set option for hours
	value = parseInt($E('input[name='+key+']').value);
	choices.hours	= key;
	prices.hours	= value;
	
	// Clear extra hours
	//if(field = $E('input[name=extra]')){
	//	field.click();
	//}

	updateTotal();
}

/**
 *	Set a key/value
 */
function setValue(key, value){
	choices[key] = key;
	prices[key] = value;
	
	updateTotal();
}

/**
 *	Set a choice
 */
function setChoice(key, value, price, connectedRadio, connectedIndex){
	// Set or unset a choice
	if(value){
		prices[key]		= price;
		choices[key] 	= value;
	}else{
		prices[key]		= null;
		choices[key]	= null;
	}
	
	// Activate connected field
	if(connectedRadio){
		if(connectedFields = $$('input[name='+connectedRadio+']')){
			if(!connectedFields[connectedIndex].checked){
				connectedFields[connectedIndex].click();	
			}
		}
	}
	
	updateTotal();
}

/**
 *	Set transport multiplier
 */
function setTransportMultiplier(multiplier){
	if($('transport_multiplier_total')){
		if(multiplier){
			$E('input[name=transport_multiplier]').value = multiplier;
			$('transport_multiplier_total').innerHTML = multiplier;
		}else{
			$E('input[name=transport_multiplier]').value = parseInt($('transport_multiplier_total').innerHTML);
		}
	
		calculateTransport(transportField.value);
	}
}

/**
 *	Update total costs
 */
function updateTotal(){
	
	// Disable transport 
	if(choices.deliverytype && choices.deliverytype == 'price_afgehaald'){
		prices.transport = 0;
		if(transportRow) transportRow.setStyle('visibility', 'hidden');
	}else{
		if(transportRow) transportRow.setStyle('visibility', 'visible');
	}
	
	// Calculate total costs
	var total = 0;
	for(key in prices){
		if(prices[key] != null){
			if(parseFloat(prices[key])){
				// Value is price
				total += prices[key];
			}else if(field = $E('input[name='+key+']')){
				// Value is key to hidden field
				total += parseFloat(field.value);	
			}
		}
	}

	totalItem.innerHTML = new String(Number(total).toFixed(2)).replace('.', ',');
	return total;
}

/**
 *	Add product to shoppingcart
 */
function addToCart(){
	// Gather post params
	var postParams = {
		choices: 				choices,
		prices: 				prices,
		totalPrice:				updateTotal(),
		id: 					getHiddenValue('product_id'),
		transport_multiplier:	getHiddenValue('transport_multiplier')
	}
	
	// Send the request
	new Json.Remote('/'+language+'/shoppingcart/addattraction/', {
		onComplete: function(response){
			document.location = '/'+language+'/shoppingcart/';
		}.bind(this)
	}).send(postParams);
	
	return false;
}

/**
 *	Get value from hidden field
 */
function getHiddenValue(name){
	if(el = $E('input[name='+name+']')){
		return el.value;	
	}
	return null;
}

/**
 *	Initialize shoppingcart
 */
fireOn = Window.ie ? 'load' : 'domready';
window.addEvent(fireOn, function(){
	
	// Assign fields
	transportField	= $('transport');
	extraKmField	= $('extrakm');
	totalItem		= $('total');
	language		= getHiddenValue('language');
	transportRow	= $E('table.detail tr.transport');
	
	// Select default radiobuttons
	$$('input.default').each(function(radio){
		radio.click();								  
	});
	
	// Select first field for hours
	if(field = $E('input[name=hour]')){
		field.click();
	}
	
	// Select first field for days
	if(field = $E('input[name=days]')){
		field.click();
	}
	
	// Select first field for deliverytype
	if(field = $E('input[name=deliverytype]')){
		field.click();
	}
	
	// Set default transport multiplier (fetched from template)
	setTransportMultiplier();
	
	// Assign handlers voor addToCart
	if($('addToCart')){
		$('addToCart').addEvent('click', addToCart);
	}

});

