﻿$(function() {

    // set hide/reveal links behavior if any
    if ( $("a.reveal").length > 0 ) {
        $("a.reveal").click ( function() { 
            var elementToReveal = $(this).attr("id");
            $("#" + elementToReveal + "Hidden").fadeToggle();
            return false;
        });
    }
    
    // Get the body class    
    var bodyClassArray = $("body").attr("class").split(" ");
    var bodyClass = null;

    $.each(bodyClassArray,function(i,n){
        if ( n != "sIFR-active" ) {
			bodyClass = n;
		}
    });
    
    switch(bodyClass)
	  {
     case "toolsdashboard":
            
            // WEIGHT LOSS TRACKER
            if ( $(".weightLossTrackerMiniTool").length > 0 ) {
                // ie6 save new details fix
                if ($.browser.msie && $.browser.version.substr(0,1)<7) {
                   $("a.joinLink").css("border-top","1px solid #fff");
                }
            }
            
            // RECIPE TOOL
            if ( $("#miniResults").length > 0 ) {
                 $('#column1 div.miniToolNav ul li:last-child a').css("border-right","0");
            }
            
            // ACTIVITY TOOL
             if ( $("#activityMiniToolDiv").length > 0 ) {
                //Hide all
                $("#expertOpinionDiv").hide(); $("#expertOpinionsLi").toggleClass("in", false); 
                $("#activityDiv").hide(); $("#activitiesLi").toggleClass("in", false);    
                //Show activities by default
                $("#activityDiv").show(); $("#activitiesLi").toggleClass("in", true);
                
                //Click functions             
                $("li#activitiesLi a").click(function() {                
                    $("#expertOpinionDiv").hide(); $("#expertOpinionsLi").toggleClass("in", false);                      
                    $("#activityDiv").show(); $("#activitiesLi").toggleClass("in", true);
                });
                  
                 $("li#expertOpinionsLi a").click(function() {                
                    $("#activityDiv").hide(); $("#activitiesLi").toggleClass("in", false);                      
                    $("#expertOpinionDiv").show(); $("#expertOpinionsLi").toggleClass("in", true);
                }); 
             }
            // MEALPLANNER
            if ( $("#todaysPlanView").length > 0 ) {
                $("#mealPlannerRecipes ul[id=recipeCarousel]").jcarousel({ scroll: 1 });

                $(".jcarousel-skin-tools div.recipeTeaserContent h3, .jcarousel-skin-tools ul#recipeCarousel li a img").hover(
                    function () {
                      $(this).parent().parent().css("border","1px solid #39892F");
                    }, 
                    function () {  
                      $(this).parent().parent().css("border","1px solid #CCCCCC");
                    }
                );
                
                // ie6 carousel hover states for non-css:hover-able boxes
                if ($.browser.msie && $.browser.version.substr(0,1)<7) {
                    $("#mealPlannerRecipes .jcarousel-skin-tools .jcarousel-next").hover(
                        function () {
                          $(this).css("background-position","0 -140px");
                        }, 
                        function () {  
                          $(this).css("background-position","0 -200px");
                        }
                    );
                
                    $("#mealPlannerRecipes .jcarousel-skin-tools .jcarousel-prev").hover(
                        function () {
                          $(this).css("background-position","0 -170px");
                        }, 
                        function () {  
                          $(this).css("background-position","0 -230px");
                        }
                    );
                }
                
                 // hide answers
                 $("div.answer").hide();
               
                 // attach click behavior to questions to enable toggling of answers
                 $("div.question").removeClass("in").unbind('click').click(function(){
                      if ( $(this).hasClass("in") ) {
                          // then answer visible
                          $(this).toggleClass("in").next().slideUp("fast");
                          if ($("#ref").length > 0 &&  $("#ref").prev().hasClass("xref")){
                              $("#ref").hide().prev().toggleClass("xref");
                              if ($("#ref").prev().hasClass("in")){
                                  $("#ref").prev().toggleClass("in");
                              }
                          }
                      } else {
                          // then answer hidden
                          $(this).toggleClass("in").next().slideDown("fast");
                      }
                  });
                
                $("img.close").click(function(){
                    $(this).closest("div.answer").slideUp("fast").prev().toggleClass("in");
                    if ($("#ref").length > 0 &&  $("#ref").prev().hasClass("xref")){
                        $("#ref").hide().prev().toggleClass("xref");
                        if ($("#ref").prev().hasClass("in")){
                            $("#ref").prev().toggleClass("in");
                        }
                    }
                  });
                      
                 // hide all tabs and show todays plan by default
                 hideAllMealPlannerDivs();
                 showMealPlannerDiv('#todaysPlanView','#todaysPlanLi');
                 
                 $("li#todaysPlanLi a").click(function() {
                    showMealPlannerDiv('#todaysPlanView','#todaysPlanLi'); 
                  });
                 
                 $("li#mealTipsLi a").click(function() {
                    showMealPlannerDiv('#mealTipsView','#mealTipsLi'); 
                  });
                 
                 $("li#eatingOutTipsLi a").click(function() {
                    showMealPlannerDiv('#eatingOutTipsView','#eatingOutTipsLi'); 
                  });
                 
                 $("li#shoppingTipsLi a").click(function() {
                    showMealPlannerDiv('#shoppingTipsView','#shoppingTipsLi'); 
                  });
            
                // display tips immediately if coming to this page straight from a "view tips" link
                var thislink = window.location.href;
                var checkTabJumpLink = thislink.substring(thislink.lastIndexOf(".") + 6, thislink.length);
                
                switch(checkTabJumpLink)
                {
                  case "mTips":
                      showMealPlannerDiv('#mealTipsView','#mealTipsLi'); 
                  break;
                
                  case "eTips":
                      showMealPlannerDiv('#eatingOutTipsView','#eatingOutTipsLi'); 
                  break;
                
                  case "sTips":
                      showMealPlannerDiv('#shoppingTipsView','#shoppingTipsLi'); 
                  break;
                }
            }
		 break;
		 
		 case "recipedetails":
		      $("#previouslyViewedRecipes ul").jcarousel({ scroll:1 });

          $("div.recipeTeaserContent h3, ul#recipeCarousel li a img").hover(
              function () {
                $(this).parent().parent().css("border","1px solid #39892F");
              }, 
              function () {  
                $(this).parent().parent().css("border","1px solid #CCCCCC");
              }
          );
          
          /* get last 5 characters and check if it's a "#print" addres */
          var thislink = window.location.href;
          var checkPrintJumpLink = thislink.substring(thislink.lastIndexOf(".") + 6, thislink.length);
          
          if ( checkPrintJumpLink == "print" ) {
            window.print();
          }
          
          // ie6 carousel hover states for non-css:hover-able boxes
          if ($.browser.msie && $.browser.version.substr(0,1)<7) {
              $("#previouslyViewedRecipes .jcarousel-skin-tools .jcarousel-next").hover(
                    function () {
                      $(this).css("background-position","0 -140px");
                    }, 
                    function () {  
                      $(this).css("background-position","0 -200px");
                    }
                );

                $("#previouslyViewedRecipes .jcarousel-skin-tools .jcarousel-prev").hover(
                    function () {
                      $(this).css("background-position","0 -170px");
                    }, 
                    function () {  
                      $(this).css("background-position","0 -230px");
                    }
                );
          }
          break;
          
     case "mealplanner":
          $('marquee').marquee();
          break;
     
     case "registeruser":
    			$("input[type=text], input[type=password]").addClass("text");
    			
          // ie6 select overlap fix
          if ($.browser.msie && $.browser.version.substr(0,1)<7) {
              $("li#calendarLi input").bind( "click focus", function() { 
                  $("li#activityLi select").css("visibility","hidden");
              });
              
              $("li#calendarLi input").bind( "blur", function() { 
                  $("li#activityLi select").css("visibility","visible");
              });
          }
    			break;
     
     case "mycheckin":
    			$("input[type=text]").addClass("text");
    			if ( $("ol.checkinWeightQuestions").length > 0 ) { 
    			  $("a.reveal").unbind();
    			  
    			  $("a.reveal").toggle( 
    			    function () {
    			        $(this).parent().parent().height(215, 
    			            $("#dietrelatedeffectsHidden").fadeToggle("fast"));
    			        return false;
    			    }, 
    			    function () {
    			        $("#dietrelatedeffectsHidden").fadeToggle("fast", 
    			            function() { 
    			                $(this).parent().height(80)
    			            }
    			         );
    			        return false;
    			    }
    			  );
    			}
    			break;
     
     case "myaccount":
          // ie6 select overlap fix
          if ($.browser.msie && $.browser.version.substr(0,1)<7) {
              $("li#calendarLi input").bind( "click focus", 
                  function() { 
                     $("div#myaccount li select").css("display","none");
                  }
              );
              
              $("li#calendarLi input").bind( "blur", 
                  function() { 
                      $("div#myaccount li select").css("display","block");
                  }
             );
          }
		    	$("select").addClass("narrow");
    			$("input[type=text], input[type=password]").addClass("text");
    			break;
    
    case "myalliplanstartdate":
          if ( ( $("li#dietDates img").length && $("li#capsuleDates img").length ) > 0 ) {
          
              $("li#dietDates img, li#capsuleDates img").hover( 
                  function() {
                      $(this).css("cursor","pointer");
                  }, 
                  function () {
                      $(this).css("cursor","default");
                  }
              );
              
              $("li#dietDates img, li#capsuleDates img").click( function() {
                  $(this).prev().focus();
              });
          }
          break;
    } 
}); 

function showMealPlannerDiv(div,li) {
        hideAllMealPlannerDivs();
        
        $(div).show();            
        $(li).toggleClass("in", true);
}
    
function hideAllMealPlannerDivs () {       
        $("#todaysPlanView").hide(); $("#todaysPlanLi").toggleClass("in", false);                
        $("#mealTipsView").hide(); $("#mealTipsLi").toggleClass("in", false); 
        $("#eatingOutTipsView").hide(); $("#eatingOutTipsLi").toggleClass("in", false);
        $("#shoppingTipsView").hide(); $("#shoppingTipsLi").toggleClass("in", false);           
}

$.fn.fadeToggle = function (s, fn){
    return (this.is(":visible"))
        ? this.fadeOut(s, fn)
        : this.fadeIn(s, fn);
};
