/*
 * Javascript Functions
 *
 * Copyright (c) CLICKSPORTS
 * $Rev$
 * $Author$
 * $Date$
 *
 */

document.observe('dom:loaded', function() {
	
	startTabs('navigation');
	var pcal = new PriceCalculator();	
});

/**
 * Start a new instance of Control.Tabs on target element
 * @param {Object} elm Element to use
 */
function startTabs(elm) {

	var elm = $(elm);
	if(!elm) return;
	
	$$('.tab_content').invoke('hide') 
	var tab = new Control.Tabs(elm, { setClassOnContainer: true });
	var prev = $('prev');
	var next = $('next');

	// If we are on the first tab, hide previous button
	if(tab.links.first() == tab.activeLink) prev.hide(); 

	tab.links.each(function(t) {
		
		t.observe('click', function(e) {
			
			var elm = Event.element(e);
			
			if (elm !== tab.links.last() && elm !== tab.links.first()) {
			
				$('next').show();
				$('prev').show();
			}
			else {
			
				if (tab.links.last() == elm) {
				
					$('next').hide();
					$('prev').show();
				}
				else if (tab.links.first() == elm) {
				
					$('prev').hide();
					$('next').show();
				}
			}
		});
	});

	$('prev').observe('click', function(e) {

		if(this.links.last() == this.activeLink) next.show();

		this.previous();		
		if(this.links.first() == this.activeLink) prev.hide();
		
		Event.stop(e);
	}.bindAsEventListener(tab));
	
	$('next').observe('click', function(e) {

		// Show next button if we are on the last option
		if(this.links.first() == this.activeLink) prev.show();
		
		this.next();
		
		// Hide next link on last tab
		if(this.links.last() == this.activeLink) next.hide();
		
		Event.stop(e);
	}.bindAsEventListener(tab));
}

/**
 * Calculate prices given on the form
 */
var PriceCalculator = Class.create({
	
	prods: {
		bautrockner_ad_430: 4.75,
		bautrockner_ad_620: 3.95,
		spezialgeblaese_airmax_3600: 2.95,
		ueberdruckanlage_ab_500: 8.95,
		elektroheizer_15kw: 8.95,
		ozongeruchsneutralisator: 21.95
	},
	
	initialize: function() {
		
		this.sel = $$('#product-feature-list select');
		this.container = $('request-show');
		this.days = $('duration');

		this.price = 0;
		this.keys = Object.keys(this.prods);

		this.initListeners().calculatePrice();
		this.showPrice();
	},
	
	initListeners: function() {
		
		this.sel.each(function(s) { s.observe('change', this.calculatePrice.bind(this)); }, this);
		this.days.observe('change', this.calculatePrice.bind(this));
		
		return this;
	},
	
	calculatePrice: function() {
		
		this.price = 0;
		var d = this.days.value;
		
		this.sel.each(function(s) {
			
			var n = s.readAttribute('name');
			var v = s.value;
			
			if(this.keys.indexOf(n) !== -1) {
				
				this.price += ((this.prods[n] * v) * d);
			}
		}, this);
		
		this.showPrice();
		return this;
	},
	
	showPrice: function() {
		
		if (this.price <= 0 || isNaN(this.price)) 
			var msg = '<strong>Noch keine Produkte ausgewählt</strong>';
		else {
			
			var pricestr = this.price.toFixed(2).toString().replace('.', ',');			
			var msg = '<strong>' + pricestr + ' &euro;</strong>zzgl. MwSt und Versand</strong>';
		}
		
		this.container.update(msg);
	}
	
});
