var BookingCalculator = new Class({
	Implements: [Options, Events],
	options: {
		debug: false
	},
	
	Total: 0,
	
	initialize: function(element,options){
		this.setOptions(options);
		
		
		
	}
});




window.addEvent('domready', function(){
	if( $('pickup-check') && ( $('pickup-check').getStyle('display') != 'none' ) && $('pickup-dropdown') )
	{
		var Check = $('pickup-check').getChildren('input');
		var Dropdown = $('pickup-dropdown').getChildren('select');
		
		if( Check[0].checked )
		{
			Dropdown[0].set('disabled','');
		}
		else
		{
			Dropdown[0].set('disabled','disabled');
			$$('#pickup-dropdown select option').set('selected','');
			$$('#pickup-dropdown select option:first-child').set('selected','selected');
		}
		
		Check[0].addEvent('click', function(){
			
			if( Check[0].checked )
			{
				Dropdown[0].set('disabled','');
			}
			else
			{
				Dropdown[0].set('disabled','disabled');
				$$('#pickup-dropdown select option').set('selected','');
				$$('#pickup-dropdown select option:first-child').set('selected','selected');
			}
			
		});
		
		
	}
	
	if( $('book-side-panel') )
	{
		var rateEUR = $('price-rate-eur').get('html');
		var rateUSD = $('price-rate-usd').get('html');
		var priceIsk = $('price-isk').get('html');
		var priceIsk = priceIsk.toInt();
		var priceEur = $('price-eur').get('html');
		var priceUsd = $('price-usd').get('html');
		
		var count = $$('#book-side-panel .item.number input');
		
		calculate();
		
		$$(count).addEvent('keyup', function(){
			calculate();
		});
		
		$$('#book-side-panel .item.check.valueadd input').addEvent('click', function(){
			calculate();
		});
		
		$$('#book-side-panel .button input').addEvent('click', function(e){
			//var myFx = new Fx.Scroll(window, { 'duration': 1000, 'link': 'cancel', 'transition': Fx.Transitions.Quad.easeInOut }).toElement( $('book-side-panel') );
			var pos = $('book-side-panel').getPosition();
			window.scrollTo( pos.x, pos.y );
		});
		
	}
	
	function isInteger(s)
	{
		return (s.toString().search(/^-?[0-9]+$/) == 0);
	}
	
	function calculate()
	{
		
		var count = $$('#book-side-panel .item.number input');
		var count = count[0].get('value');
		var count = count.toInt();
		var total = 0;
		
		$$('#book-side-panel .item.check.valueadd').each(function(item){
			
			if( item.getStyle('display') != 'none' )
			{
				var childs = item.getChildren();
				
				if( childs[2] )
				{
					var price = childs[2].get('html');
					var price = price.split(' ');
					var price = price[0].toInt();
					
					if( childs[0].checked )
						total = total + price;
				}
			}
			
		});
		var total_single_isk = (priceIsk+total).round();
		var total_single_eur = ((priceIsk+total)/ rateEUR ).round(2);
		var total_single_usd = ((priceIsk+total)/ rateUSD ).round(2);
		var total_isk = ( ( (priceIsk+total)  ) * count );
		var total_eur = ( ( (priceIsk+total) / rateEUR ) * count );
		var total_usd = ( ( (priceIsk+total) / rateUSD ) * count );

		//if( total_isk == '' || total_isk == 0 || total_isk == null || total_isk == 'undefinded' )
		if( isInteger( total_isk ) )
		{
			
			var total_isk = total_isk.round();
			var total_eur = total_eur.round(2);
			var total_usd = total_usd.round(2);
		}
		else
		{
			var total_isk = 0;
			var total_eur = 0;
			var total_usd = 0;
			
		}
		
		//console.log( total_single_isk );
		$('price-isk').set('html', total_single_isk );
		$('price-isk-total').set('html', total_isk );
		$('price-eur').set('html', total_eur );
		$('price-eur-single').set('html', total_single_eur );
		$('price-usd').set('html', total_usd );
		$('price-usd-single').set('html', total_single_usd );
		
		$('price-isk-total').highlight();
		$('price-eur-single').highlight();
		$('price-usd-single').highlight();
		$('price-eur').highlight();
		$('price-usd').highlight();
		$('price-isk').highlight();
	}
	
	
	
});
