$(function() {
		//Add dialog to DOM
		CreateDialog();
		
		//Function to determine total of all sliders
		function refreshFinal() {
			var total = 0;
			$('.time-slider').each(function(){
				total += $(this).slider('value')
			});
			modTotal = total * .723;
			//$('#no_taxstream').slider("value", total);
			//$('#taxstream').slider("value", modTotal);
			//var data_gather_time = $('#data_gathering').slider('value');
			//$('#data_gathering_time').text(data_gather_time);
			return total;
		}
				
		function resetCalc() {
			$('.time-slider').each(function(){
				$(this).slider('value', 0);
			})
		}
		
		$('.calc_dialog').click(function(){
			/*var time_saved = 0;
			time_saved = $('#no_taxstream').slider('value') - $('#taxstream').slider('value')
			$('#dialog').html('<p>You can save ' + Math.round(time_saved) + ' hours with TaxStream</p>');
			*/
			$('#dialog').html($('#calc').html());
			$('#dialog').dialog('open');
			return false;
		});
		
		function CreateDialog(){
			$("#dialog").dialog({
				open: function(e, ui){
					$(this).load("/incometax/provision/calc/calc.htm", function(){
						$(".time-slider").slider({
							animate: true,
							step: 1,
							range: "min",
							value: 0,
							min: 0,
							max: 100,
							slide: function(e, ui){
								$(this).closest('td').next().find('.hours').text(ui.value + " hours");
								//refreshFinal();	
							},
							change: refreshFinal()
						});	
					});		
				},
				width: 800,
				autoOpen: false,
				modal: true,
				draggable: false,
				resizable: false,
				minHeight: 375, 
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					Close: function(){
						$(this).dialog('close');
					},
					Reset: function(){
						resetCalc();
					},
					Calculate: function() {
						$('#calc').hide('fast');	
						var total = refreshFinal();
						var modTotal = Math.round(total * .723);
						var time_saved = total - modTotal;
						$('#dialog').load("/incometax/provision/calc/calculation.asp",function(){
								$("#no_taxstream").height(100).slider({
								range: "min",
								step: 25,
								min: 0,
								max: 1300,
								value: total,
								orientation: "vertical"
							}).slider("disable").find(".ui-slider-handle").addClass("no-handle"); 							
							$('#no_taxstream_value').text(total + " hours");
							$('#taxstream_value').text(modTotal + " hours");
							$("#taxstream").height(100).slider({
								range: "min",
								step: 5,
								min: 0,
								max: 1500,
								value: modTotal,
								orientation: "vertical"
							}).slider("disable").find(".ui-slider-handle").addClass("no-handle");
							$("#saved_time").text(time_saved + " hours ");
							$("#dialog").parents(".ui-dialog:eq(0)").find(".ui-dialog-buttonpane button:eq(2)").hide("fast");
							$("#dialog").parents(".ui-dialog:eq(0)").find(".ui-dialog-buttonpane button:eq(1)").hide("fast");
						});				
					}
				},
				close: function(){
						$('#dialog').remove();
						$('#dialog_link').after("<div id='dialog' title='Time Savings Calculator'></div>");
						CreateDialog();											
				}			
			});
		}
	});