$(document).ready(function(){
	
	$('.reveal-on-animate')
		.hide();
	
	$('.reveal-on-second-animate')
		.hide();
		
	$('.comp-calc-final')
		.hide();
	
	
	/*
	//
	// FIRST DROP DOWN //
	//
	*/
	
	$('#injury_type').change(function() {
	
		if (typeof(id) == 'undefined') {
			var animate = true;
		} else {
			var animate = false;
		}
		
		// hide everything! //
		$('.reveal-on-animate')
			.hide('slow',function() {
				$(this).remove();
			})


		$('.reveal-on-second-animate')
			.hide('slow',function() {
				$(this).remove();
			})

		$('.comp-calc-final')
			.hide();
		
		$('#injury_type option').each(function() {
			if ($(this).attr('selected')) {
				id = $(this).val();
			}
		});
		
		$.get('/calculator.php', { stage: '1' , id: id  }, function (data) {
						
			$('.stage1').after(data);
			//sIFR.replaceElement("#sub_content label", named({sFlashSrc: "/_assets/fonts/Adobe_Garamond_Pro.swf", sColor: "#616A88", sWmode: "transparent"}));
			
			$('.remove-on-animate').hide();

			$('.delete-on-animate').remove();
			
			// reset the form //
			$('.calc-reset').click(function() {
				reset_form();
				return false;
			});

			if (animate) {
				$('div.comp-calc-collapsed div.scc').css('position','relative').css('marginLeft',0).animate({marginLeft: -79,width: 265},'slow');
			}

			$('.reveal-on-animate').show('slow');

			$('.reveal-on-second-animate').show('slow');

			$('.delete-on-second-animate').remove();

			$("#injury_desc").prepend('<option value="" class="delete-on-second-animate">- Description -</option>');
				
			
			
			
			/*
			//
			// SECOND DROP DOWN //
			//
			*/

			$('#injury_desc').change(function() {

				$('#injury_desc option').each(function() {
					if ($(this).attr('selected')) {
						desc = $(this).val();
					}
				});

				$('.reveal-on-second-animate')
					.hide('slow',function() {
						$(this).remove();
					})

				$.get('/calculator.php', { stage: '2' , id: desc  }, function (data) {

					$('.stage-2-hook').after(data);
					

					$('.delete-on-second-animate')
						.remove();

					$('.reveal-on-second-animate')
						.show('slow');

				});

			});
			
		});	
		
	});
	
	

	
	
	
	/*
	//
	// BUTTON CLICK //
	//
	*/
	$('#btn-comp-calc').click(function() {
		
		// check that a value has been set... //
		
		var claim = null;
		
		$('.calc-values input').each(function() {
			if (this.checked) {
				claim = this.value;
			}
		});
		
		if (claim == null) {
			// no value selected so show error message //
			$('.calc-info').hide();
			$('.remove-on-animate').hide();
			
			$('.calc-error').remove();
			
			$('.calc-info').after('<div class="comp-calc-text remove-on-animate calc-error">Please fill in all of the fields!</div>');
			$('.calc-error').show('slow');


		} else {
			
			$('.calc-error').remove();
			$('.remove-on-animate').hide();
			$('.delete-on-animate').remove();

			// collapse the form //
			$('div.comp-calc-collapsed div.scc')
				.css('position','relative')
				.css('marginLeft',-79)
				.animate({marginLeft: 0, width: 186},'slow');		

			// hide the form and then display the results! //
			$('.comp-calc-initial')
				.hide('slow', function() {

					$.get('/calculator.php', { stage: '3' , id: claim  }, function (data) {

						$('.comp-calc-final')
							.empty()
							.append(data)
							.show('slow');

							// reset the form //
							$('.calc-reset').click(function() {
								reset_form();
								return false;
							});
							
							
					});
				});
				
		}
		
		
		// make sure it doesn't submit! //
		return false;
	});
	
	
	function reset_form() {
		// reset the values in the fields
		$('.calc-values input').each(function() {
			if (this.checked) {
				this.checked = false;
			}
		});
		
		$('.calc-error').remove();
		$('.remove-on-animate').hide();
		$('.delete-on-animate').remove();

		// collapse the form //
		$('div.comp-calc-collapsed div.scc')
			.css('position','relative')
			.animate({marginLeft: 0, width: 186},'slow');	

		$('.comp-calc-final')
			.hide('slow',function() {
				$('.remove-on-animate')
					.show();

				$('.reveal-on-animate')
					.hide();

				$('.reveal-on-second-animate')
					.hide();

				$('.comp-calc-initial')
					.show('slow');

				$("#injury_type")
					.prepend('<option value="" class="delete-on-animate">- Injury Type -</option>');
					document.getElementById('injury_type').selectedIndex = 0;
				delete id;
		});
		

		// make sure it doesn't submit! //
		return false;
	}
	

});