;(function($) {
	$.fn.widget = function(options) {
	  
		var settings = $.extend({}, $.fn.widget.defaults, options);
		//$.log('login init: ' + settings.loginSuccessPage);
			
		return this.each(function() {
			
			var $this = $(this);
			$this.find(".error").css("display", "none");
			var $result = $("#result");
			var $message = $(".msg");
			var $closeBtn = $("#closeBtn");
			var $calcBtn = $("#submitBtn");
			
			$result.hide();

			$calcBtn.click(function(){
				$this.find(".error").css("display", "none");
				
				var $dayValue = $("select[name=day] option:selected").val();
				var $hourValue = $("input#time").val();
				var $ampmValue = $("input:radio[name=ampm]:checked").val();
				
				if ($dayValue == "" || ($hourValue == "" || !Number($hourValue) || Number($hourValue) > 12 || Number($hourValue) <=0 )) {
					if ($dayValue == "") {
						//alert("no day!");
						$("select[name=day]").parent().find(".error").css("display", "inline");
					}
					if ($hourValue == "" || !Number($hourValue) || Number($hourValue) > 12 || Number($hourValue) <=0 ) {
						//alert("no hour!");
						$("input#time").parent().parent().find(".error").css("display", "inline");
					}
				} else {
					var $msg = "";
					var $totalDayHours = Number($dayValue) * 24;
					
					var $totalHours = Number($hourValue);
					if ($ampmValue != "AM") {
						if ($totalHours != 12) {
							$totalHours += 12;
						}
					}
					//
					var $currentDate = new Date();
					var $currentHour = $currentDate.getHours();
					/*$.log("Current hour is: " + $currentHour);
					$.log("Entered hour is: " + $hourValue);
					$.log("Considering ampm, the hour is: " + $totalHours);*/
					
					var $total = $totalDayHours + ($currentHour - $totalHours);
					var $remainingHours = 72 - $total;
					
					if ($remainingHours > 0 && $remainingHours <= 72) {
						$msg = "You have " + $remainingHours.toString() + " hours remaining to take Plan B One-Step. Please take it as soon as possible because the sooner you take it, the better it works.";
					} else if ($remainingHours > 72) {
						$msg = "Please select a time earlier than the present.";
					} else if ($remainingHours <= 0) {
						$msg = "Plan B One-Step should be used within 72 hours after unprotected sex. Please contact a healthcare professional for advice.";
					}
					//alert($msg);
					$message.text($msg);
					$result.fadeIn("slow");
				}

				return false;
			});
			
			$closeBtn.click(function(){
				$result.fadeOut(300);
				return false;
			});
			
		});
	};
	

	$.fn.widget.defaults = {
		errorClass: "error"
	};

})(jQuery);