﻿ $(document).ready(function() {
  $(document).includeReady(function () {
   if (subsection != "") {
    $("#" + subsection + "_sub").show();
    $("#" + subsection + "_arrow").css("background","url('/Style Library/Images/subnav/arrow_on.png') no-repeat");
    if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
     if ((subsection == "journey") || (subsection == "success")){
      $("#" + subsection + "_nav").css("height","10px");
     } else if (subsection == "newyou"){
       $("#" + subsection + "_nav").css("height","30px");
     } else {
      $("#" + subsection + "_nav").css("height","30px");
     }
    }
   }
   if (subpage != "") {
    $("#" + subpage + "_nav").css("color","#0e83cc");
   }
   if (subpage == "ready") {
    $("#bmi_bg").hide();
   }
    $("#co_zipcode").keypress(function(event) {
     if ( event.which == 13 ) {
     clinicClick();
   }
    });
    
    $("#co_feet, #co_inches, #co_weight").keypress(function(event) {
     if ( event.which == 13 ) {
     bmiClick();
     }
    });
    
    $("#feet, #inches, #weight").keypress(function(event) {
     if ( event.which == 13 ) {
     return btncalcBMI();
     }
    });
  });
  if (section == "home") {
    $("body").css('background', 'url("/Style Library/Images/home_background.jpg") top center no-repeat');
    jQuery('#mycarousel').jcarousel({
     wrap: 'circular',
     auto: 7,
     scroll: 1
    });
    $("#co_zipcode").keypress(function(event) {
     if ( event.which == 13 ) {
     clinicClick();
   }
    });
   }
   if (section == "fac") {
    $("body").css('background', 'url("/Style Library/Images/fac_background.jpg") top center no-repeat');
   }
   if (subpage == "prod_land") {
    jQuery('#mycarousel').jcarousel({
     wrap: 'circular',
     auto: 7,
     scroll: 1
    });
    jQuery('#mycarousel2').jcarousel({
     wrap: 'circular',
     auto: 7,
     scroll: 1
    });
   }
   if ((section != "home") && (section != "") && (section != "fac")) {
    $("#" +  section + "_nav").removeClass("nav_button_off");
    $("#" +  section + "_nav").addClass("nav_button_on");
    if (section == "provider") {
     $("#right_curve").attr("src","/Style Library/Images/nav/nav_curve_right_on.png");
    }
   }
   setTextSize(getCookie("text_size"));
   if (subpage == "ready") {
    if (getParameterByName("source") == "callout") {
     $("#weight").val(getParameterByName("weight"));
     $("#feet").val(getParameterByName("feet"));
     $("#inches").val(getParameterByName("inches"));
     return btncalcBMI();
    }
   }
   $("a.enlarge").fancybox();
   if ((section != "home") && (section != "")&& (section != "fac") && (section != "search")){
    printBreadcrumbs();
   }
   if (section != "provider") {
    $("#provider_nav").mouseover(function() {
     $("#right_curve").attr("src","/Style Library/Images/nav/nav_curve_right_over.png")
    });
    $("#provider_nav").mouseout(function() {
     $("#right_curve").attr("src","/Style Library/Images/nav/nav_curve_right_off.png")
    });
   }

 });

function arrowClick(menu){
 if ($("#" + menu + "_sub").css("display") == "none") {
  $("#" + menu + "_sub").show();
  $("#" + menu + "_arrow").css("background","url('/Style Library/Images/subnav/arrow_on.png') no-repeat");
    if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
     if ((menu == "journey") || (menu == "success")){
      $("#" + menu + "_nav").css("height","10px");
     } else if (menu == "newyou"){
       $("#" + menu + "_nav").css("height","30px");
     } else {
      $("#" + menu + "_nav").css("height","30px");
     }
    }
 } else {
  $("#" + menu + "_sub").hide();
  $("#" + menu + "_arrow").css("background","url('/Style Library/Images/subnav/arrow_off.png') no-repeat");
    if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
     if ((menu == "journey") || (menu == "success")){
      $("#" + menu + "_nav").css("height","30px");
     } else if (menu == "newyou"){
       $("#" + menu + "_nav").css("height","40px");
     } else {
      $("#" + menu + "_nav").css("height","50px");
     }
    }
 }
}

function recipeClick(menu){
 if ($("#" + menu).css("display") == "none") {
  $("#" + menu).show();
  $("#" + menu + "_arrow").css("background","url('/Style Library/Images/subnav/arrow_on.png') no-repeat");
 } else {
  $("#" + menu).hide();
  $("#" + menu + "_arrow").css("background","url('/Style Library/Images/subnav/arrow_off.png') no-repeat");
 }
}

function showHideTips() {
 if ($("#tips_content").css("display") == "none") {
  $("#tips_content").show();
 } else {
  $("#tips_content").hide();
 }
}

function clinicClick() {
    var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
    if (($("#co_zipcode").val() == null) || 
        ($("#co_zipcode").val().length < 5) || 
        ($("#co_zipcode").val().length > 5) || 
        (!numberRegex.test($("#co_zipcode").val()))) {
     alert("Please enter a valid zip code.");
    } else {
     zipValue = $("#co_zipcode").val()
     _gaq.push(['_trackPageview', '/find_a_clinic_callout'])
     if (zipValue == "00000") {
      window.location = "/Pages/clinic_noresult.aspx?zip=0";
     } else { 
      window.location = "/Pages/clinic_results.aspx?zip=" + zipValue;
     }
    }
}

function bmiClick() {
 _gaq.push(['_trackPageview', '/calculate_bmi_callout']);
 window.location = "/Pages/program/bmi-calculator.aspx#bmi?weight=" + $("#co_weight").val() + "&inches="  + $("#co_inches").val() + "&feet="  + $("#co_feet").val() + "&source=callout";
}

function getCookie(c_name){
	if (document.cookie.length>0){
  		c_start=document.cookie.indexOf(c_name + "=");
  	if (c_start!=-1){//-1 means not finding anything
    	c_start=c_start + c_name.length+1;
    	c_end=document.cookie.indexOf(";",c_start);
    	if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	}
  	}
	return "";
}

function setTextSize(size) {
	if(size ==  "large"){
  		$('#content_sidenav h2').css('font-size', '22px');
  		$('#content_no_sidenav h2').css('font-size', '22px');
  		$('#content_sidenav .bottom_callout h2').css('font-size', '18px');
  		$('p.references').css('font-size', '11px');
  		$('p.references strong').css('font-size', '15px');
  		$('#content_sidenav').css('font-size', '15px');
  		$('#content_no_sidenav').css('font-size', '15px');
        document.cookie = "text_size=large;path=/";
 	} else if (size ==  "x-large") {
  		$('#content_sidenav h2').css('font-size', '24px');
  		$('#content_no_sidenav h2').css('font-size', '24px');
  		$('#content_sidenav .bottom_callout h2').css('font-size', '20px');
  		$('p.references').css('font-size', '13px');
  		$('p.references strong').css('font-size', '17px');
  		$('#content_sidenav').css('font-size', '17px');
  		$('#content_no_sidenav').css('font-size', '17px');
        document.cookie = "text_size=x-large;path=/";
 	} else {
  		$('#content_sidenav h2').css('font-size', '20px');
  		$('#content_no_sidenav h2').css('font-size', '20px');
  		$('#content_sidenav .bottom_callout h2').css('font-size', '16px');
  		$('p.references').css('font-size', '9px');
  		$('p.references strong').css('font-size', '13px');
  		$('#content_sidenav').css('font-size', '13px');
  		$('#content_no_sidenav').css('font-size', '13px');
        document.cookie = "text_size=small;path=/";
 	}
}

function changeTextSize() {
    if (getCookie("text_size") == "small") {
        setTextSize("large");
    } else if (getCookie("text_size") == "large") {
        setTextSize("x-large");
    } else {
        setTextSize("small");
    }
}

function getParameterByName(name) {
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.href);
      if(results == null)
       return "";
      else
       return decodeURIComponent(results[1].replace(/\+/g, " "));
      }
      
      function confirmation(url) {
	var message = "By clicking the OK button below, you will leave OPTIFAST.com and link to a third party website that may have a different privacy policy and information collection practices.\n\nPlease be sure to review the privacy policy and terms and conditions posted at that site.";
	if (confirm(message)) {
	 window.open(url);
	}
}

function printBreadcrumbs() {
 pagename = $('h1').html();
 pagename = pagename.replace('<br>',' ');
 pagename = pagename.replace('<div style="font-size:20px;">(Formerly RESOURCE<sup>®</sup> BENEFIBER<sup>®</sup>)</div>','');
 if (section == 'why'){
  if (subpage != '') {
   $('.breadcrumb').append('<a href="/Pages/program.aspx">Why OPTIFAST</a> > ');
  }
 }
 if (section == 'weightloss'){ 
  if (subpage != '') {
   $('.breadcrumb').append('<a href="/Pages/weight-loss-and-you.aspx">Weight Loss and You</a> > ');
  }
 }
 if (section == 'products'){ 
  if (subpage != '') {
   $('.breadcrumb').append('<a href="/Pages/products.aspx">Product Information</a> > ');
  } 
 }
 if (section == 'inspiration'){ 
  if (subpage != '') {
   $('.breadcrumb').append('<a href="/Pages/inspiration.aspx">OPTIFAST Inspiration</a> > ');
  } 
 }
 if (section == 'provider'){ 
  if (subpage != '') {
   $('.breadcrumb').append('<a href="/Pages/become-a-provider.aspx">Become a Provider</a> > ');
  } 
 }
 if ((subsection == '') && (subpage != '')) {
  $('.breadcrumb').append(pagename);  
 } else if (subsection == subpage) {
   $('.breadcrumb').append(pagename);  
 } else {
  if (subsection == "directors") {
   $('.breadcrumb').append('<a href="/Pages/become-a-provider/medical-directors.aspx">OPTIFAST Medical Director</a> > ');
  }
  if (subsection == "journey") {
   $('.breadcrumb').append('<a href="/Pages/program/patient-journey.aspx">Patient Journey</a> > ');
  }
  if (subsection == "reasons") {
   $('.breadcrumb').append('Reasons To Lose Weight > ');
  }
  if (subsection == "resources") {
   $('.breadcrumb').append('<a href="/Pages/weight-loss-and-you/resources.aspx">Weight Loss Resources</a> > ');
   if ((subpage == "bev") || (subpage == "maindish") || (subpage == "smoothie") || (subpage == "snack") || (subpage == "soup")) {
    $('.breadcrumb').append('<a href="/Pages/weight-loss-and-you/resources/recipes.aspx">OPTIFAST Recipes</a> > ');
   }
  }
  if (subsection == "success") {
   $('.breadcrumb').append('<a href="/Pages/inspiration/success-stories.aspx">Success Stories</a> > ');
  }
  
  if (subsection == "newyou") {
   $('.breadcrumb').append('<a href="/Pages/inspiration/contest.aspx">OPTIFAST® "New You" Weight Loss Contest</a> > ');
   if ((subpage == "brenda") || (subpage == "james") || (subpage == "johanna")) {
    $('.breadcrumb').append('<a href="/Pages/inspiration/contest/new-you-contest-winners.aspx">"New You" Contest Winners</a> > ');
   }
   if ((subpage == "linda") || (subpage == "cheryl")) {
    $('.breadcrumb').append('<a href="/Pages/inspiration/contest/inspire-award-winners.aspx">Inspire Award Winners</a> > ');
   } 
  } 
  $('.breadcrumb').append(pagename);  
 } 
 $('.breadcrumb').append('</p>');
}

function submitSearch() {
 searchURL = "/Pages/search.aspx?search=" + $("#search").val()
 window.location = searchURL;
}
