/*
	Weight Loss Calculator Weightloss
*/

var Weightloss = window.Weightloss || {};

/** We will put all of our variables and resources (URL-s, listings etc) **/
Weightloss.data = {
};

/**
 * JavaScript implementation of isset() function
 *
 * Usage example:
 *
 * if(isset(undefined, true) || isset('Something')) {
 *   // Do stuff
 * }
 *
 * @param value
 * @return boolean
 */
Weightloss.isset = function(value) {
  return !(typeof(value) == 'undefined' || value === null);
};

/**
 * Parse numeric value and return integer or float
 *
 * @param String value
 * @return mixed
 */
Weightloss.parseNumeric = function(value) {
  if(typeof(value) == 'number') {
    return value;
  } else if(typeof(value) == 'string') {
    if(value.indexOf('.') > -1) {
      var separator = '.';
    } else if(value.indexOf(',') > -1) {
      var separator = ',';
    } else {
      return value == '' ? 0 : parseInt(value);
    } // if
    
    var separator_pos = value.indexOf(separator);
    return parseInt(value.substring(0, separator_pos)) + parseFloat('0.' + value.substring(separator_pos + 1));
  } else {
    return NaN;
  }
};

/**
 * Parse numeric value and return integer or float
 *
 * @param String value
 * @return mixed
 */
Weightloss.launch = function() {
	window.open("/weight-loss-calculator-popup","_blank","status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=no,scrollbars=no,width=600,height=405"); 
};

// Switching Steps Plugin
Weightloss.Steps = function() {

	// Result
	return {
		
		/**
		* Initialize Panel
		*/
		init : function() {
    		Weightloss.data.step = 1;
			jQuery('#weightloss-steps').cycle({ 
				fx:    'scrollUp', 
				cleartype: true,
				cleartypeNoBg: true,
				sync:   0,
				speed:   700,
				startingSlide: Weightloss.data.step - 1,
				after: function() {
	    			Weightloss.Steps.move();
					switch(parseFloat(Weightloss.data.step)) {
						case 1:
							if(!Weightloss.data.back) {
								Weightloss.Steps.nextOnOff(false);
							}
							Weightloss.Steps.prevOnOff(false);
							break;
						case 2:
							if(Weightloss.data.back) {
								Weightloss.Steps.nextOnOff(true);
								Weightloss.Steps.prevOnOff(true);
							} else {
								if(jQuery('.weight-radio:checked').val() > 0) {
									Weightloss.Steps.nextOnOff(true);
								} else {
									Weightloss.Steps.nextOnOff(false);
								}
								Weightloss.Steps.prevOnOff(true);
							}
							break;
						case 3:
								Weightloss.Steps.nextOnOff(false);
								Weightloss.Steps.prevOnOff(true);
							break;
						default:
							break;
					}
					eval('Weightloss.Step' + Weightloss.data.step + '.init()');
				},
				timeout: 0
			});
			
			jQuery('.weight-body-step a').click(function() {
				opener.location.href = this.href; 
				return false;
			});
		},
		
		move : function() {
			jQuery('#nav-highlight').fadeOut('fast');
			switch(parseFloat(Weightloss.data.step)) {
				case 1:
					slidePos = 0;
					break;
				case 2:
					slidePos = 200;
					break;
				case 3:
					slidePos = 406;
					break;
				default:
					slidePos = 0;
					break;
			}
			jQuery('#nav-highlight').animate({marginLeft: slidePos + 'px'},'fast',function() {jQuery('#nav-highlight').fadeIn('fast')});
		},

		nextOnOff : function(enabled) {
			if(enabled) {
				jQuery('#next-step a').unbind()
				jQuery('#next-step a').click(function() {
					Weightloss.data.step = parseFloat(Weightloss.data.step + 1);
	    			jQuery('#weightloss-steps').cycle(Weightloss.data.step - 1);
	    			Weightloss.data.back = false;
	    			return false; 
				});
				jQuery('#next-step a').removeClass('disabled');
			} else {
				jQuery('#next-step a').addClass('disabled');
				jQuery('#next-step a').unbind()
				jQuery('#next-step a').click(function() {
	    			return false; 
				});
			}
		},

		prevOnOff : function(enabled) {
			if(enabled) {
				jQuery('#previous-step a').unbind()
				jQuery('#previous-step a').click(function() {
					Weightloss.data.step = parseFloat(Weightloss.data.step - 1);
	    			jQuery('#weightloss-steps').cycle(Weightloss.data.step - 1);
	    			Weightloss.data.back = true;
	    			return false; 
				});
				jQuery('#previous-step a').removeClass('disabled');
			} else {
				jQuery('#previous-step a').addClass('disabled');
				jQuery('#previous-step a').unbind()
				jQuery('#previous-step a').click(function() {
	    			return false; 
				});
			}
		}
	} // init
}();

// Step 1
Weightloss.Step1 = function() {
	
	// Result
	return {
	
		/**
		* Initialize Panel
		*/
		init : function() {
			// attach handler to form's submit event 
			jQuery('#bmiForm').submit(function() { 
				Weightloss.Step1.calc();
			    // return false to prevent normal browser submit and page navigation 
			    return false; 
			});
			
    		Weightloss.data.step = 1;
		},
	
		/**
		* Calculate BMI & Target Weight
		*/
		calc : function() {
			var weight = jQuery('#bmiWeight').val() ? jQuery('#bmiWeight').val() : 0;
			var height = (12 * jQuery('#bmiFeet').val()) + ((jQuery('#bmiInches').val() * 1));
			var age = jQuery('#bmiAge').val() ? jQuery('#bmiAge').val() : 0;

			if ((weight == 0 || age == 0)) {
				jQuery('#calc-message').html('Please enter numeric values for your weight (in pounds) and for your age.');
				return false;
			} else {
				if (isNaN(weight) || isNaN(age)) {
					jQuery('#calc-message').html('Please enter numeric values for your weight (in pounds) and for your age.');
					return false;
				} else {
					var bmi = Math.round(655 + (4.35 * weight) + (4.7 * height) - (4.7 * age));
					var bmitarget = Math.round(((height * height) * 21) / 703);
					jQuery('#bmiScore').html(bmi);
					jQuery('#weightScore').html(bmitarget);
					Weightloss.data.bmi = bmi;
					Weightloss.data.weight = weight;
				}
			}
			Weightloss.Steps.nextOnOff(true);
			return true;
		}
	} // init
}();

// Step 2
Weightloss.Step2 = function() {
	
	// Result
	return {
	
		/**
		* Initialize Panel
		*/
		init : function() {
			jQuery('.weight-radio').each(function() {
				jQuery(this).change(function() {
					Weightloss.data.weightGoal = jQuery(this).val();
					Weightloss.Steps.nextOnOff(true);
				});
			});
			
			jQuery('.activity-radio').each(function() {
				jQuery(this).change(function() {
					Weightloss.data.activity1 = jQuery(this).val();
				});
			});
			
			jQuery('.activity-radio2').each(function() {
				jQuery(this).change(function() {
					Weightloss.data.activity2 = jQuery(this).val();
				});
			});
			
		}
	} // init
}();

// Step 3
Weightloss.Step3 = function() {
	
	// Result
	return {
	
		/**
		* Initialize Panel
		*/
		init : function() {
    		var lose = false;
    		var maintain = false;
    		if (Weightloss.data.weightGoal == "1"){
    			lose = true;
    		}

    		var weight = parseFloat(Weightloss.data.weight);
    		var bmi = parseFloat((Weightloss.data.bmi) ? Weightloss.data.bmi : 0);
    		var activity1 = parseFloat((jQuery('.activity-radio:checked').val()) ? jQuery('.activity-radio:checked').val() : 0);
    		var activity2 = parseFloat((jQuery('.activity-radio2:checked').val()) ? jQuery('.activity-radio2:checked').val() : 0);
    		var activityTotal = activity1 + activity2;
    		
    		bmi = bmi + activity1 + activity2;
    		
    		var showText = '';
    		var showImage = '';
    		switch(true) {
    			case (lose && (bmi >= 2000) && (activityTotal >= 200)):
    				showText = '1-1';
    				showImage = '1500';
    				break;
    			case (lose && (bmi >= 2000) && (activityTotal >= 75 && activityTotal < 200)):
    				showText = '1-2';
    				showImage = '1800';
    				break;
    			case (lose && (bmi >= 2000) && (activityTotal < 75)):
    				showText = '1-3';
    				showImage = '1800';
    				break;
    			case (lose && (bmi >= 1700 && bmi < 2000) && (activityTotal >= 200)):
    				showText = '2-1';
    				showImage = '1500';
    				break;
    			case (lose && (bmi >= 1700 && bmi < 2000) && (activityTotal >= 75 && activityTotal < 200)):
    				showText = '2-2';
    				showImage = '1500';
    				break;
    			case (lose && (bmi >= 1700 && bmi < 2000) && (activityTotal < 75)):
    				showText = '2-3';
    				showImage = '1500';
    				break;
    			case (lose && (bmi >= 1500 && bmi < 1700) && (activityTotal >= 200)):
    				showText = '3-1';
    				showImage = '1200';
    				break;
    			case (lose && (bmi >= 1500 && bmi < 1700) && (activityTotal >= 75 && activityTotal < 200)):
    				showText = '3-2';
    				showImage = '1200';
    				break;
    			case (lose && (bmi >= 1500 && bmi < 1700) && (activityTotal < 75)):
    				showText = '3-3';
    				showImage = '1500';
    				break;
    			case (lose && (bmi >= 1200 && bmi < 1500) && (activityTotal >= 200)):
    				showText = '4-1';
    				showImage = '1200';
    				break;
    			case (lose && (bmi >= 1200 && bmi < 1500) && (activityTotal >= 75 && activityTotal < 200)):
    				showText = '4-2';
    				showImage = '1200';
    				break;
    			case (lose && (bmi >= 1200 && bmi < 1500) && (activityTotal < 75)):
    				showText = '4-3';
    				showImage = '1200';
    				break;
    			case (lose && (bmi < 1200) && (activityTotal >= 75)):
    				showText = '5-1';
    				showImage = '1200';
    				break;
    			case (lose && (bmi < 1200) && (activityTotal < 75)):
    				showText = '5-2';
    				showImage = '1200';
    				break;
    			case (bmi >= 2000) && (activityTotal >= 75):
    				showText = '6-1';
    				showImage = '1800';
    				break;
    			case (bmi >= 2000) && (activityTotal < 75):
    				showText = '6-2';
    				showImage = '1800';
    				break;
    			case (bmi >= 1700 && bmi < 2000) && (activityTotal >= 75):
    				showText = '7-1';
    				showImage = '1500';
    				break;
    			case (bmi >= 1700 && bmi < 2000) && (activityTotal < 75):
    				showText = '7-2';
    				showImage = '1500';
    				break;
    			case (bmi >= 1500 && bmi < 1700) && (activityTotal >= 75):
    				showText = '8-1';
    				showImage = '1500';
    				break;
    			case (bmi >= 1500 && bmi < 1700) && (activityTotal < 75):
    				showText = '8-2';
    				showImage = '1500';
    				break;
    			case (bmi >= 1200 && bmi < 1500) && (activityTotal >= 75):
    				showText = '9-1';
    				showImage = '1200';
    				break;
    			case (bmi >= 1200 && bmi < 1500) && (activityTotal < 75):
    				showText = '9-2';
    				showImage = '1200';
    				break;
    			case (bmi < 1200) && (activityTotal >= 75):
    				showText = '10-1';
    				showImage = '1200';
    				break;
    			case (bmi < 1200) && (activityTotal < 75):
    				showText = '10-2';
    				showImage = '1200';
    				break;
    			default:
    				break;
    		}
		jQuery('#bmiScoreFinal').html(bmi);
    		jQuery('.results-message').hide();
    		jQuery('#results-' + showText).show();
    		jQuery('.mif-1200-cal-plan a, .mif-1500-cal-plan a, .mif-1800-cal-plan a').removeClass('selected');
    		jQuery('.mif-' + showImage + '-cal-plan a').addClass('selected');
    		if(lose) {
	    		jQuery('#results-5-3').show();
    		} else {
	    		jQuery('#results-10-3').show();
    		}
		}
	} // init
}();
