
if( $('#calculator').length )
{
	window['GL'] = window.GL || {};
	window['GL']['Widget'] = window.GL.Widget || {};
	window['GL']['Widget']['Calculator'] = window.GL.Widget.Calculator || {};
	
	function Init()
	{
		$('#cost_date #datePicker')
		.hide()
		.before(' | <a href="#datePicker" id="datePickerToggle">ändra</a>');	
		
		$('#cost_date #datePickerToggle')
		.click(function(event){ 
			event.preventDefault();
			$($(this).attr('href')).show(); 
		})
		
		$('#calculator input#calculateCost')
		.click(function(event){ 
			event.preventDefault();
			GL.Widget.Calculator.GetCost(this);
		});
		
		if( $('.ui-custom-ComboBox').length ) GL.Widget.Calculator.InitComboBox();
	}
	window.GL.Widget.Calculator['Init'] = Init;
	
	function AddExtra()
	{
		var selectedOpt = $('.ui-widget-ComboBox option:selected');
		var inp = $('#' + $('label.' + $(selectedOpt).attr('class') ).attr('for') );
		
		// Show input
		$('.ui-widget-ComboBox option:selected').remove();
		$(inp).parents('li').removeClass('offscreen');
		
		// Set new value
		var oval = $(inp).val() != "" ? $(inp).val() : 0;
		$(inp).val(parseInt(oval) + 1);
		
		
		
	}
	window.GL.Widget.Calculator['AddExtra'] = AddExtra;
	
	
	function GetCost(btn)
	{	
		var a = $(btn).attr('id');
		var q = '?' + $('#calculator input').serialize() + '&action=' + a;
		
		/*$.ajax({url: 'widget.calculator.php' + q,
			   method: 'POST',
			   type: 'html',
			   beforeSend: function(){ 
			   		$(btn).attr('disabled', 'disabled');
				},
			   success: function(data){
				   	$('#cost_finalize .result').html(data)
					$(btn).removeAttr('disabled');
			   }
		})*/
		
	}
	window.GL.Widget.Calculator['GetCost'] = GetCost;
	
	function InitComboBox()
	{	
		var boxWrap = $('#calculator .ui-custom-ComboBox');
		
		$(boxWrap).before('<div class="ui-widget-ComboBox"><select></select></div><input type="image" src="/img/btns/blue/btn-plus.gif" name="addExtra" id="addExtra" alt="Lägg till" />')
		
		$(boxWrap)
		.find('label')
		.each(function(i){
			var wrap = $(this).parent('li');		   	
			var isSet = ( $(wrap).find('input[type=text]').val() != "" ? true : false );
			if( !isSet ) $(wrap).addClass('offscreen');
			
			$(this).addClass('combo-' + i);
			if( !isSet ) $('.ui-widget-ComboBox select').append('<option class="combo-'+i+'">' + $(this).text() + '</option>')
			
		});
		
		$('#calculator input#addExtra')
		.click(function(event){ 
			event.preventDefault();
			GL.Widget.Calculator.AddExtra();
		});
	}
	window.GL.Widget.Calculator['InitComboBox'] = InitComboBox;
	
}
GL.Widget.Calculator.Init();
