if(typeof jsEffects == 'undefined')
	jsEffects = true;


Event.observe(window,'load', function() {
	$$('input[type="radio"]').each(function (element) {
		Event.observe(element,'click',updateNoise);
	});
	$$('input[type="checkbox"]').each(function (element) {
		Event.observe(element,'click',updateNoise);
	});
	updateNoise();

	//Nach 3 Sekunden CallBanner einblenden
	if(showCallBanner) {
		setTimeout(function() {
			new Effect.Opacity('contact-us', { from: 0.0, to: 1, duration: 0.5 });
		},3000);

		//13 Sekunden später wieder ausblenden und Text rein
		setTimeout(function() {
			new Effect.Opacity('contact-us', { from: 1, to: 0, duration: 0.5,afterFinish:function() { $('contact-us').hide(); } });
			new Effect.Opacity('call-us-text', { from: 0, to: 1, duration: 0.5 });
		},13000);
	}
});


function updateNoise() {
	var costPerWatt = 0.0619;
	var values = calcNoiseAndPrice();
	var containerWidth = $('noiseContainer').getWidth();

	var widthNoise = ( Number(values[0]) / maxNoise ) * Number(containerWidth);
	if(widthNoise > containerWidth) widthNoise = containerWidth;
	if(widthNoise < 0) widthNoise = 0;

	var widthPower = ( Number(values[2]) / maxPower ) * Number(containerWidth);
	if(widthPower > containerWidth) widthPower = containerWidth;
	if(widthPower < 0) widthPower = 0;

	var widthGamerpoints = ( Number(values[3]) / maxGamerpoints ) * Number(containerWidth);
	if(widthGamerpoints > containerWidth) widthGamerpoints = containerWidth;
	if(widthGamerpoints < 0) widthGamerpoints = 0;

	if(jsEffects) {
		new Effect.Parallel([

			new Effect.Morph('noisemeter', {
				style: 'width:' + widthNoise + 'px;',
				sync: true,
				transition: Effect.Transitions.spring,
				beforeUpdate: function(e) {
					var curNoise = ($('noisemeter').getWidth() / containerWidth) * maxNoise;
					$('sonevalue').update(SmMath.kaufm(curNoise) + ' sone' );
					$('noisemeter').setStyle('width', widthNoise+'px');
					submitSone(curNoise);
				},
				afterFinish: function() {
					$('sonevalue').update(SmMath.kaufm(values[0]) + ' sone');
					$('noisemeter').setStyle('width', widthNoise+'px');
					submitSone(values[0]);
				}
			}),

			new Effect.Morph('powermeter', {
				style: 'width:' + widthPower + 'px;',
				sync: true,
				transition: Effect.Transitions.spring,
				beforeUpdate: function(e) {
				var curNoise = Math.round(($('powermeter').getWidth() / containerWidth) * maxPower);
					$('powervalue').update(curNoise + ' Watt ('+SmMath.kaufm(curNoise*costPerWatt)+'&euro;*)');
				},
				afterFinish: function() {
					$('powervalue').update(values[2] + ' Watt ('+SmMath.kaufm(values[2]*costPerWatt)+'&euro;*)');
				}
			}),


			new Effect.Morph('gamerpointsmeter', {
				style: 'width:' + widthGamerpoints + 'px;',
				sync: true,
				transition: Effect.Transitions.spring,
				afterFinish: function() {
					$('gamerpointsmeter_points').update('<span>'+values[3]+'/'+maxGamerpoints+' Punkte</span>');
				}
			})

		]);
	} else {
		$('noisemeter').setStyle({width: widthNoise+'px'});
		$('sonevalue').update(SmMath.kaufm(values[0]) + ' sone');
		submitSone(values[0]);

		$('powermeter').setStyle({width: widthPower+'px'});
		$('powervalue').update(values[2] + ' Watt ('+SmMath.kaufm(values[2]*costPerWatt)+'&euro;*)');

		$('gamerpointsmeter').setStyle({width: widthGamerpoints+'px'});
		$('gamerpointsmeter_points').update('<span>'+values[3]+'/'+maxGamerpoints+' Punkte</span>');
	}

	updateBasket();
}

function calcNoiseAndPrice() {
	var noise = baseNoise;
	var preis = basePrice;
	var power = basePower;
	var gamerpoints = baseGamerpoints;
	$$('input[class="radio"]').each(function (element) {
		if($F(element) != null) {
			noise += data[$F(element)][1];
			preis += data[$F(element)][0];
			power += data[$F(element)][2];
			gamerpoints += data[$F(element)][4];
		}
	});

	if(gamerpoints > maxGamerpoints) gamerpoints = maxGamerpoints;

	if(noise < 0) noise = 0;

	return [noise,preis,power,gamerpoints];
}

function getHeightForMeter(noiseValue,maxHeight) {
	return ( Number(noiseValue) / maxNoise ) * Number(maxHeight);
}

function updateBasket() {
	var preis = basePrice;
	$('configureForm').getInputs('radio').each(function (e) {
		var groupId    = e.name.match(/tx_smconfig_pi1\[group\]\[(\d+)\]/);
		var nameField  = 'nameFor_' + groupId[1];
		var priceField = 'priceFor_' + groupId[1];

		if(e.checked && $(nameField)) {
			$(nameField).update(data[e.value][5]);
			var vorzeichen = '+';
			if(Number(data[e.value][0]) < 0) {
				vorzeichen = '-';
			}
			$(priceField).update(vorzeichen+'&nbsp;' + SmMath.kaufm(Math.abs(data[e.value][0])) + '&nbsp;&euro;');
			preis += data[e.value][0];

			//Bildwechsel?
			switchImage(groupId[1],data[e.value]);

			$('row_' + groupId[1]).show();
		}
	});

	$('configureForm').getInputs('checkbox').each(function (e) {
		var groupId = e.name.match(/tx_smconfig_pi1\[group\]\[(\d+)\]\[(\d+)\]/);
		var nameField = 'nameFor_' + groupId[1] + '_' + groupId[2];
		var priceField = 'priceFor_' + groupId[1]+ '_' + groupId[2];

		if($(nameField)) {
			if(e.checked) {
				preis += data[e.value][0];
				$('row_' + groupId[1] + '_' + groupId[2]).show();
			} else {
				$('row_' + groupId[1] + '_' + groupId[2]).hide();
			}
		}
	});

	$('gesamtPreis').update(SmMath.kaufm(preis));
	if(discount > 0) {
		var netto         = SmMath.getNettoFromBrutto( preis );
		var haendlerNetto = SmMath.absolutRabattAufSumme(discount, netto);
		var marge         = netto - haendlerNetto;

		$('gesamtPreisRetail').update( SmMath.kaufm( haendlerNetto ) );
		$('gesamtDiscount').update(    SmMath.kaufm( marge ) );
		$('gesamtPreisNetto').update(  SmMath.kaufm( netto ) );
	}
}

function updateNoisesimulator(noise){
	document.getElementById("noisesimulator").src = "/fileadmin/templates/sm-noisesimulator.php?soundVolume=" + (noise/2);
}
function switchImage(groupId,productArray) {
	//Wenn Bildwechsel fuer diese Kategorie erlaubt ist
	if(groups[groupId][2] ) {
		if(productArray[6] == '') {
			var theImage = '<img src="' + groups[groupId][1] +'" />';
		} else {
			var theImage = '<img src="' + productArray[6] +'" />';
			//var theImage = new Element('img',{src: productArray[6]});
		}
		$('group-image-wrap-' + groupId).update(theImage);
		//console.log('imageswitch',productArray[6]);
	}
}
