/* Copyright 2010 FitnessKeeper, Inc.  All Rights Reserved. */

$(function()
{
   $("select, input:checkbox, input:radio, input:file").uniform();
   selectSize();
   $("select").change(function(){selectSize()});

   $("#signupForm .signupRow label").labelOver("over-apply")

   var splitsHeight = $("#splits").height();
   var splitsWidth = $("#splits").width();
   $("#splits .greydient-bg").attr("height", splitsHeight).attr("width", splitsWidth);
   $("#splits .greydient-bg-box").css("height", splitsHeight).css("width", splitsWidth);
   $("#splits .greydient.l, #splits .greydient.r").css("height", splitsHeight-16);
   
   var searchBoxHeight = $(".route_search").height();
   var searchBoxWidth = $(".route_search").width();
   $(".route_search .greydient-bg").attr("height", searchBoxHeight).attr("width", searchBoxWidth);
   $(".route_search .greydient-bg-box").css("height", searchBoxHeight).css("width", searchBoxWidth);
   $(".route_search .greydient.l, .route_search .greydient.r").css("height", searchBoxHeight-16);

   var peopleSearchHeight = $("#peopleSearch").height();
   var peopleSearchWidth = $("#peopleSearch").width();
   $("#peopleSearch .greydient-bg").attr("height", peopleSearchHeight).attr("width", peopleSearchWidth);
   $("#peopleSearch .greydient-bg-box").css("height", peopleSearchHeight).css("width", peopleSearchWidth);
   $("#peopleSearch .greydient.l, #peopleSearch .greydient.r").css("height", peopleSearchHeight-16);

   var streetTeammateMeHeight = $(".streetTeammate.me .streetTeamContainer").height();
   var streetTeammateMeWidth = $(".streetTeammate.me .streetTeamContainer").width();
   $(".streetTeammate.me .streetTeamContainer .greydient-bg").attr("height", streetTeammateMeHeight).attr("width", streetTeammateMeWidth);
   $(".streetTeammate.me .streetTeamContainer .greydient-bg-box").css("height", streetTeammateMeHeight).css("width", streetTeammateMeWidth);
   $(".streetTeammate.me .streetTeamContainer .greydient.l, .streetTeammate.me .streetTeamContainer .greydient.r").css("height", streetTeammateMeHeight-16);

   var streetTeamMonthlyHeight = $(".monthlyActivitiesBox").height();
   var streetTeamMonthlyWidth = $(".monthlyActivitiesBox").width();
   $(".monthlyActivitiesBox .greydient-bg").attr("height", streetTeamMonthlyHeight).attr("width", streetTeamMonthlyWidth);
   $(".monthlyActivitiesBox .greydient-bg-box").css("height", streetTeamMonthlyHeight).css("width", streetTeamMonthlyWidth);
   $(".monthlyActivitiesBox .greydient.l, .monthlyActivitiesBox .greydient.r").css("height", streetTeamMonthlyHeight-16);

   var pendingInvitationsHeight = $("#pendingInvitations").height();
   var pendingInvitationsWidth = $("#pendingInvitations").width();
   $("#pendingInvitations .greydient-bg").attr("height", pendingInvitationsHeight).attr("width", pendingInvitationsWidth);
   $("#pendingInvitations .greydient-bg-box").css("height", pendingInvitationsHeight).css("width", pendingInvitationsWidth);
   $("#pendingInvitations .greydient.l, #pendingInvitations .greydient.r").css("height", pendingInvitationsHeight-16);

   var settingsPictureHeight = $("#settingsPicture").height();
   var settingsPictureWidth = $("#settingsPicture").width();
   $("#settingsPicture .greydient-bg").attr("height", settingsPictureHeight).attr("width", settingsPictureWidth);
   $("#settingsPicture .greydient-bg-box").css("height", settingsPictureHeight).css("width", settingsPictureWidth);
   $("#settingsPicture .greydient.l, #settingsPicture .greydient.r").css("height", settingsPictureHeight-16);
   
   var signupFormContainerHeight = $("#signupFormContainer").height();
   var signupFormContainerWidth = $("#signupFormContainer").width();
   $("#signupFormContainer .greydient-bg").attr("height", signupFormContainerHeight).attr("width", signupFormContainerWidth);
   $("#signupFormContainer .greydient-bg-box").css("height", signupFormContainerHeight).css("width", signupFormContainerWidth);
   $("#signupFormContainer .greydient.l, #signupFormContainer .greydient.r").css("height", signupFormContainerHeight-16);
   
   var activityDetailSummaryContainerHeight = $("#activityDetailSummary").height();
   var activityDetailSummaryContainerWidth = $("#activityDetailSummary").width();
   $("#activityDetailSummary .greydient-bg").attr("height", activityDetailSummaryContainerHeight).attr("width", activityDetailSummaryContainerWidth);
   $("#activityDetailSummary .greydient-bg-box").css("height", activityDetailSummaryContainerHeight).css("width", activityDetailSummaryContainerWidth);
   $("#activityDetailSummary .greydient.l, #activityDetailSummary .greydient.r").css("height", activityDetailSummaryContainerHeight-16);

   var routeDetailSummaryContainerHeight = $("#routeDetailSummary").height();
   var routeDetailSummaryContainerWidth = $("#routeDetailSummary").width();
   $("#routeDetailSummary .greydient-bg").attr("height", routeDetailSummaryContainerHeight).attr("width", routeDetailSummaryContainerWidth);
   $("#routeDetailSummary .greydient-bg-box").css("height", routeDetailSummaryContainerHeight).css("width", routeDetailSummaryContainerWidth);
   $("#routeDetailSummary .greydient.l, #routeDetailSummary .greydient.r").css("height", routeDetailSummaryContainerHeight-16);
    
   var streetTeammateListContainerHeight = $("#streetTeamContainer").height();
   var streetTeammateListContainerWidth = $("#streetTeamContainer").width();
   $("#streetTeamContainer .greydient-bg").attr("height", streetTeammateListContainerHeight).attr("width", streetTeammateListContainerWidth);
   $("#streetTeamContainer .greydient-bg-box").css("height", streetTeammateListContainerHeight).css("width", streetTeammateListContainerWidth);
   $("#streetTeamContainer .greydient.l, #streetTeamContainer .greydient.r").css("height", streetTeammateListContainerHeight-16);
    
   var profileStatsBoxContainerHeight = $("#profileBox").height();
   var profileStatsBoxContainerWidth = $("#profileBox").width();
   $("#profileBox .greydient-bg").attr("height", profileStatsBoxContainerHeight).attr("width", profileStatsBoxContainerWidth);
   $("#profileBox .greydient.l, #profileBox .greydient.r").css("height", profileStatsBoxContainerHeight-16);
   
   var userInfoContainerHeight = $("#userInfo").height();
   $("#userInfo .greyBorder.l, #userInfo .greyBorder.r").css("height", userInfoContainerHeight-16);
   
   var profileNavListWidth = $("#profileDropdownBox .toggleTextMain").width();
   var profileNavListUsernameWidth = profileNavListWidth + 83;
   var shadowBgWidth = profileNavListUsernameWidth - 26;
   var shadowBoxTopBottom = profileNavListUsernameWidth - 40;
   if (profileNavListWidth < 52)
   {
      profileNavListUsernameWidth = 135;
      shadowBgWidth = 109;     
      shadowBoxTopBottom = 95;               
   }
   var profileNavListHeight = $("#profileNavList").height();
   $("#profileNavList").css("width", profileNavListUsernameWidth);            
   $("#profileNavList .shadowBox-bg").css("height", profileNavListHeight-26).css("width", shadowBgWidth);
   $("#profileNavList .shadowBox.t, #profileNavList .shadowBox.b").css("width", shadowBoxTopBottom);
   $("#profileNavList .shadowBox.l, #profileNavList .shadowBox.r").css("height", profileNavListHeight-40);

   var streetTeamOverlayHeight = $("#streetTeamOverlay").height();
   var streetTeamOverlayWidth = $("#streetTeamOverlay").width();
   $("#streetTeamOverlay .shadowBox-bg").css("height", streetTeamOverlayHeight-26).css("width", streetTeamOverlayWidth-26);
   $("#streetTeamOverlay .shadowBox-bg .greydient-bg").attr("height", streetTeamOverlayHeight-26).attr("width", streetTeamOverlayWidth-26);
   $("#streetTeamOverlay .shadowBox.t, #streetTeamOverlay .shadowBox.b").css("width", streetTeamOverlayWidth-40);
   $("#streetTeamOverlay .shadowBox.l, #streetTeamOverlay .shadowBox.r").css("height", streetTeamOverlayHeight-40);

   var bodyMeasurementsContainerHeight = $("#bodyMeasurementsContainer").height();
   var bodyMeasurementsContainerWidth = $("#bodyMeasurementsContainer").width();
   $("#bodyMeasurementsContainer .greydient-bg").attr("height", bodyMeasurementsContainerHeight).attr("width", bodyMeasurementsContainerWidth);
   $("#bodyMeasurementsContainer .greydient-bg-box").css("height", bodyMeasurementsContainerHeight).css("width", bodyMeasurementsContainerWidth);
   $("#bodyMeasurementsContainer .greydient.l, #bodyMeasurementsContainer .greydient.r").css("height", bodyMeasurementsContainerHeight-16);
   
   $("#viewFullscreen").click(function(){
      if ($("#header").hasClass("loggedout"))
      {
         $("#headerContent, #subNav, #content, #footer, #viewFullscreen, #footer2, #createButtons, #postNewActivityHeader, #activityStats, #activityDetailSummary, #activityChartContainer, .mainContentLeft, .mainContentRight, #editData, .edit h3, #routeActivityList, #routesMain h2, #routesMain p, #routeDetailSummary").fadeOut(function(){
            $("body").addClass("fullscreen");
            $("#backFullscreen, #headerContent, #content").fadeIn();
            var mapHeight = $(window).height() - 115;
            var mapWidth = $(window).width();
            $("#headerContent").css("width", mapWidth-20);
            $("#content.route").css("margin-top", 0);
            $("#editMap").css("width", mapWidth).css("margin-bottom", "0");
            $("#map_area, #mapping_tool").css("height", mapHeight).css("width", mapWidth);
            $("#map_canvas").css("height", mapHeight-35).css("width", mapWidth);
            $("#map_area .greyBorder.lt, #map_area .greyBorder.rt").css("height", mapHeight-35);
            $("#activityMedia, #activityMedia .blackBox, #activityMedia .blackBox-content").css("width", mapWidth-20);
            $(".fullscreen .create #mapRoute, .fullscreen .edit #mapRoute").css("top", mapHeight-34);
            $("#activityMedia .blackBox-t, #activityMedia .blackBox-b").css("width", mapWidth-40);
         });
      } 
      else
      {
         $("#headerContent, #subNav, #content, #footer, #viewFullscreen, #footer2, #createButtons, #postNewActivityHeader, #activityStats, #activityDetailSummary, #activityChartContainer, .mainContentLeft, .mainContentRight, #editData, .edit h3, #routeActivityList, #routesMain h2, #routesMain p, #routeDetailSummary").fadeOut(function(){
            $("body").addClass("fullscreen");
            $("#backFullscreen, #headerContent, #content").fadeIn();
            var mapHeight = $(window).height() - 54;
            var mapWidth = $(window).width();
            $("#headerContent").css("width", mapWidth-20);
            $("#content.route").css("margin-top", 0);
            $("#editMap").css("width", mapWidth).css("margin-bottom", "0");
            $("#map_area, #mapping_tool").css("height", mapHeight).css("width", mapWidth);
            $("#map_canvas").css("height", mapHeight-35).css("width", mapWidth);
            $("#map_area .greyBorder.lt, #map_area .greyBorder.rt").css("height", mapHeight-35);
            $("#activityMedia, #activityMedia .blackBox, #activityMedia .blackBox-content").css("width", mapWidth-20);
            $(".fullscreen .create #mapRoute, .fullscreen .edit #mapRoute").css("top", mapHeight-34);
            $("#activityMedia .blackBox-t, #activityMedia .blackBox-b").css("width", mapWidth-40);
         });
      }

      fullscreenGmapAdjust();
   });
   
   $(window).resize(function(){
      if ($("body").hasClass("fullscreen"))
      {
         if ($("#header").hasClass("loggedout"))
         {
            var mapHeight = $(window).height() - 115;
            var mapWidth = $(window).width();
            $("#headerContent").css("width", mapWidth-20);
            $("#editMap").css("width", mapWidth).css("margin-bottom", "0");
            $("#map_area, #mapping_tool").css("height", mapHeight).css("width", mapWidth);
            $("#map_canvas").css("height", mapHeight-35).css("width", mapWidth);
            $("#map_area .greyBorder.lt, #map_area .greyBorder.rt").css("height", mapHeight-35);
            $("#activityMedia, #activityMedia .blackBox, #activityMedia .blackBox-content").css("width", mapWidth-20);
            $(".fullscreen .create #mapRoute, .fullscreen .edit #mapRoute").css("top", mapHeight-34);
            $("#activityMedia .blackBox-t, #activityMedia .blackBox-b").css("width", mapWidth-40);
         } 
         else
         {
            var mapHeight = $(window).height() - 54;
            var mapWidth = $(window).width();
            $("#headerContent").css("width", mapWidth-20);
            $("#editMap").css("width", mapWidth).css("margin-bottom", "0");
            $("#map_area, #mapping_tool").css("height", mapHeight).css("width", mapWidth);
            $("#map_canvas").css("height", mapHeight-35).css("width", mapWidth);
            $("#map_area .greyBorder.lt, #map_area .greyBorder.rt").css("height", mapHeight-35);
            $("#activityMedia, #activityMedia .blackBox, #activityMedia .blackBox-content").css("width", mapWidth-20);
            $(".fullscreen .create #mapRoute, .fullscreen .edit #mapRoute").css("top", mapHeight-34);
            $("#activityMedia .blackBox-t, #activityMedia .blackBox-b").css("width", mapWidth-40);
         }

         fullscreenGmapAdjust();
      }
   });

   $("#backFullscreen, #backToActivity").click(function(){
      $("#headerContent, #backFullscreen, #content").fadeOut(function(){
         $("body").removeClass("fullscreen");
         $("#headerContent").css("width", "970px");
         $("#content.route").css("margin-top", "30px");
         $("#editMap").css("width", "720px").css("margin-bottom", "30px");
         $("#map_area, #mapping_tool").css("height", "435px").css("width", "720px");
         $("#map_canvas").css("height", "400px").css("width", "720px");
         $("#map_area .greyBorder.lt, #map_area .greyBorder.rt").css("height", "392px");
         $("#activityMedia, #activityMedia .blackBox, #activityMedia .blackBox-content").css("width", "700px");
         $("#activityMedia .blackBox-t, #activityMedia .blackBox-b").css("width", "680px");
         $(".create #mapRoute, .edit #mapRoute").css("top", "446px");
         $("#viewFullscreen, #headerContent, #subNav, #content, #footer2, #createButtons, #postNewActivityHeader, #activityStats, #activityDetailSummary, #activityChartContainer, .mainContentLeft, .mainContentRight, #editData, .edit h3, #routeActivityList, #routesMain h2, #routesMain p, #routeDetailSummary").fadeIn();

         fullscreenGmapAdjust();
      });
   });
   
   var webContentHeight = $("#webContent").height();
   var webContentWidth = $("#webContent").width();
   $("#webContent .shadowBox-bg").css("height", webContentHeight-26).css("width", webContentWidth-26);
   $("#webContent .shadowBox-bg .greydient-bg").attr("height", webContentHeight-26).attr("width", webContentWidth-26);
   $("#webContent .shadowBox.t, #webContent .shadowBox.b").css("width", webContentWidth-40);
   $("#webContent .shadowBox.l, #webContent .shadowBox.r").css("height", webContentHeight-40);
   
   $("#midNavWeb").click(function(){
      $("#midNaviPhone, #midNavAndroid").removeClass("selected");
      $("#midNavWeb").addClass("selected");
      $("#subContentiPhone:visible, #subContentAndroid:visible").fadeOut(function(){
         $("#subContentWeb").fadeIn();
      });
   });
   
   $("#midNaviPhone").click(function(){
      $("#midNavAndroid, #midNavWeb").removeClass("selected");
      $("#midNaviPhone").addClass("selected");
      $("#subContentAndroid:visible, #subContentWeb:visible").fadeOut(function(){
         $("#subContentiPhone").fadeIn();
         var iPhoneContentHeight = $("#iPhoneContent").height();
         var iPhoneContentWidth = $("#iPhoneContent").width();
         $("#iPhoneContent .shadowBox-bg").css("height", iPhoneContentHeight-26).css("width", iPhoneContentWidth-26);
         $("#iPhoneContent .shadowBox-bg .greydient-bg").attr("height", iPhoneContentHeight-26).attr("width", iPhoneContentWidth-26);
         $("#iPhoneContent .shadowBox.t, #iPhoneContent .shadowBox.b").css("width", iPhoneContentWidth-40);
         $("#iPhoneContent .shadowBox.l, #iPhoneContent .shadowBox.r").css("height", iPhoneContentHeight-40);
      });
   });
   
   $("#midNavAndroid").click(function(){
      $("#midNaviPhone, #midNavWeb").removeClass("selected");
      $("#midNavAndroid").addClass("selected");
      $("#subContentiPhone:visible, #subContentWeb:visible").fadeOut(function(){
         $("#subContentAndroid").fadeIn();
         var androidContentHeight = $("#androidContent").height();
         var androidContentWidth = $("#androidContent").width();
         $("#androidContent .shadowBox-bg").css("height", androidContentHeight-26).css("width", androidContentWidth-26);
         $("#androidContent .shadowBox-bg .greydient-bg").attr("height", androidContentHeight-26).attr("width", androidContentWidth-26);
         $("#androidContent .shadowBox.t, #androidContent .shadowBox.b").css("width", androidContentWidth-40);
         $("#androidContent .shadowBox.l, #androidContent .shadowBox.r").css("height", androidContentHeight-40);
      });
   });
   
   var loginBoxHeight = $("#loginBox").height();
   var loginBoxWidth = $("#loginBox").width();
   $("#loginBox .shadowBox-bg").css("height", loginBoxHeight-26).css("width", loginBoxWidth-26);
   $("#loginBox .shadowBox-bg .greydient-bg").attr("height", loginBoxHeight-26).attr("width", loginBoxWidth-26);
   $("#loginBox .shadowBox.t, #loginBox .shadowBox.b").css("width", loginBoxWidth-40);
   $("#loginBox .shadowBox.l, #loginBox .shadowBox.r").css("height", loginBoxHeight-40);
   
   $("#toggleSignup").click(function(){
      $("#toggleSignup, #signinContainer:visible, #forgotContainer:visible").fadeOut(function(){
         var loginBoxHeight = $("#loginBox").height();
         var loginBoxWidth = $("#loginBox").width();
         $("#loginBox .shadowBox-bg").css("height", loginBoxHeight-26).css("width", loginBoxWidth-26);
         $("#loginBox .shadowBox-bg .greydient-bg").attr("height", loginBoxHeight-26).attr("width", loginBoxWidth-26);
         $("#loginBox .shadowBox.t, #loginBox .shadowBox.b").css("width", loginBoxWidth-40);
         $("#loginBox .shadowBox.l, #loginBox .shadowBox.r").css("height", loginBoxHeight-40);
         $("#toggleLogin, #signupFormContainer").fadeIn();
      });
   });
   
   $("#toggleLogin").click(function(){
      $("#toggleLogin, #signupFormContainer, #forgotContainer:visible").fadeOut(function(){
         var loginBoxHeight = $("#loginBox").height();
         var loginBoxWidth = $("#loginBox").width();
         $("#loginBox .shadowBox-bg").css("height", loginBoxHeight-26).css("width", loginBoxWidth-26);
         $("#loginBox .shadowBox-bg .greydient-bg").attr("height", loginBoxHeight-26).attr("width", loginBoxWidth-26);
         $("#loginBox .shadowBox.t, #loginBox .shadowBox.b").css("width", loginBoxWidth-40);
         $("#loginBox .shadowBox.l, #loginBox .shadowBox.r").css("height", loginBoxHeight-40);
         $("#toggleSignup, #signinContainer").fadeIn();
      });
   });
  
   $("#saveAsRouteButton").toggle(function(){
      $("#saveAsRouteButton .checkbox").addClass("selected");
   }, function() {
      $("#saveAsRouteButton .checkbox").removeClass("selected");
   });
 
   $("#makeRoutePublicButton").toggle(function(){
      $("#makeRoutePublicButton .checkbox").addClass("selected");
   }, function() {
      $("#makeRoutePublicButton .checkbox").removeClass("selected");
   });

   $("#uploadHeartRate").click(function(){
      $("#uploadHeartRateOverlay, #uploadHeartRateBox").fadeIn("fast");
      var uploadHeartRateOverlayHeight = $("#uploadHeartRateOverlay").height();
      var uploadHeartRateOverlayWidth = $("#uploadHeartRateOverlay").width();
      $("#uploadHeartRateOverlay .shadowBox-bg").css("height", uploadHeartRateOverlayHeight-26).css("width", uploadHeartRateOverlayWidth-26);
      $("#uploadHeartRateOverlay .shadowBox-bg .greydient-bg").attr("height", uploadHeartRateOverlayHeight-26).attr("width", uploadHeartRateOverlayWidth-26);
      $("#uploadHeartRateOverlay .shadowBox.t, #uploadHeartRateOverlay .shadowBox.b").css("width", uploadHeartRateOverlayWidth-40);
      $("#uploadHeartRateOverlay .shadowBox.l, #uploadHeartRateOverlay .shadowBox.r").css("height", uploadHeartRateOverlayHeight-40);
   });

   $("#heartRateHelpLink").click(function(){
      $("#uploadHeartRateBox").fadeOut("fast", function(){
         $("#heartRateHelpBox").fadeIn();
         var uploadHeartRateOverlayHeight = $("#uploadHeartRateOverlay").height();
         var uploadHeartRateOverlayWidth = $("#uploadHeartRateOverlay").width();
         $("#uploadHeartRateOverlay .shadowBox-bg").css("height", uploadHeartRateOverlayHeight-26).css("width", uploadHeartRateOverlayWidth-26);
         $("#uploadHeartRateOverlay .shadowBox-bg .greydient-bg").attr("height", uploadHeartRateOverlayHeight-26).attr("width", uploadHeartRateOverlayWidth-26);
         $("#uploadHeartRateOverlay .shadowBox.t, #uploadHeartRateOverlay .shadowBox.b").css("width", uploadHeartRateOverlayWidth-40);
         $("#uploadHeartRateOverlay .shadowBox.l, #uploadHeartRateOverlay .shadowBox.r").css("height", uploadHeartRateOverlayHeight-40);
      });
      return false;
   });

   $("#heartRateBackLink").click(function(){
      $("#heartRateHelpBox").fadeOut("fast", function(){
         $("#uploadHeartRateBox").fadeIn();
         var uploadHeartRateOverlayHeight = $("#uploadHeartRateOverlay").height();
         var uploadHeartRateOverlayWidth = $("#uploadHeartRateOverlay").width();
         $("#uploadHeartRateOverlay .shadowBox-bg").css("height", uploadHeartRateOverlayHeight-26).css("width", uploadHeartRateOverlayWidth-26);
         $("#uploadHeartRateOverlay .shadowBox-bg .greydient-bg").attr("height", uploadHeartRateOverlayHeight-26).attr("width", uploadHeartRateOverlayWidth-26);
         $("#uploadHeartRateOverlay .shadowBox.t, #uploadHeartRateOverlay .shadowBox.b").css("width", uploadHeartRateOverlayWidth-40);
         $("#uploadHeartRateOverlay .shadowBox.l, #uploadHeartRateOverlay .shadowBox.r").css("height", uploadHeartRateOverlayHeight-40);
      });
      return false;
   });
   
   $("#heartRateCloseLink, #heartRateHelpCloseLink").click(function(){
      $("#uploadHeartRateOverlay, #heartRateHelpBox").fadeOut("fast");
      return false;
   });

   $("#saveHeartRate").click(function(){
      $("#uploadHeartRateOverlay, #heartRateHelpBox").fadeOut("fast");
      //save heartrate upload TO DO
   });

   $("#addMapLink").click(function(){
      $("#noMap").fadeOut(function(){
         $("#editMap, #activityDelete").fadeIn();
      });
      return false;
   });

   $(".lightBoxLink").fancybox({
      titlePosition : "inside",
      transitionIn  : "fade",
      transitionOut : "fade"
   });

   $(".twitterLightbox, .facebookLightbox, .foursquareLightbox, .zeoLightbox").fancybox({
      titlePosition : "inside",
      transitionIn  : "fade",
      transitionOut : "fade",
      width         : 500,
      height        : 337,
      padding       : 0
   });

   $(".withingsLightbox").fancybox({
      titlePosition    : "inside",
      transitionIn     : "fade",
      transitionOut    : "fade",
      width            : 500,
      height           : 397,
      padding          : 0
   });

   $(".helpLink").fancybox({
      titlePosition     : "inside",
      transitionIn      : "fade",
      transitionOut     : "fade",
      width               : 320,
      height              : 120
   });

   $(".bodyMeasurementsLink").fancybox({
      titlePosition     : "inside",
      transitionIn      : "fade",
      transitionOut     : "fade",
      width               : 280,
      height              : 264
   });

   $(".fancyPicture").fancybox({
      padding            : 10,
      cyclic             : true,
      autoDimensions     : true,
      hideOnContentClick    :  true,
      overlayOpacity     : 0.6
   });

   $(".iframeLink").fancybox({
      padding            : 0,
      autoDimensions     : true,
      overlayOpacity     : 0.6
   });

   $("#profileNavButton").toggle(function(){
      $("#profileNavList").fadeIn("fast");
   }, function() {
      $("#profileNavList").fadeOut("fast");
   });

   var noMapGradientWidth = $("#noActivityMap .content .mainText span.text").width();
   $("#noActivityMap .content .mainText span.gradient").css("width", noMapGradientWidth);

   $(".rkLiveLight, .rkLiveLightMenu").cycle({ 
      fx:            "fade", 
      speed:         0, 
      timeout:       1500
   });
   
   $(".feedItem .icon .liveIcon").cycle({ 
      fx:            "fade", 
      speed:         0, 
      timeout:       1500
   });

   if ( $("#editActivityType").hasClass("edit") ) {
      $(".activityTypeItem, .equipmentTypeItem").addClass("tiptip");
   }

   $(".tiptip").tipTip();
   $(".tiptiphome").tipTip({defaultPosition: "bottom", edgeOffset: 14});

   $("#content.profile .avatarSmall").mouseenter(function(){
      $("#content.profile .avatarSmall .pictureSettingsBox").show();
      $("#content.profile .avatarSmall .pictureSettingsBox").animate({
          opacity: 1.0
      }, 500);
   }).mouseleave(function(){
       $("#content.profile .avatarSmall .pictureSettingsBox").animate({
          opacity: 0.0
      }, 500);
   });
   
   // used to wire up all "alerted" link tags
   $("a[alertMessage]").click(function(event){
      event.preventDefault();                
      var href = $(this).attr("href");
      jAlert($(this).attr("alertMessage"), $(this).attr("alertTitle"),
                function(result){
                   if (result){
                      // redirect to target href
                      window.location.href = href;
                   }                               
                });
      return false;
   });
 
   // This makes any element with a link attribute a clickable link
   $("*[link]").click(function() {
      window.location.href = $(this).attr("link");          
   });

   bindButtonsToLinks();

   $("#profileDropdownBox .toggleItem").click(function() {
      var href = $(this).find("a.buttonLink").attr("href");
      if (href) window.location.href = href;
   });

   // Have to disable the default behavior of the contained link or else we get a double request
   $("#profileDropdownBox .toggleItem").find("a.buttonLink").click(function(event){
      event.preventDefault();
   });
   
   // Bind comments control events
   bindCommentsControlEvents();
});

function bindCommentsControlEvents()
{
   // unbind any existing (for ajax cases so we don't get double-bound)
   $(".commentsDelete,.postcomment-submit,.writeCommentPrompt,.commentTextArea").unbind();
   
   $(".commentsDelete").click(deleteComment);
   $(".postcomment-submit").click(addComment);

   $(".writeCommentPrompt").click(function()
   {
      $(this).hide();
      $(this).closest(".commentsResponseBox").find(".commentsBoxContainer").show().find(".commentTextArea").focus();
   });

   $(".commentTextArea").blur(function()
   {
      if ($.trim($(this).val()).length == 0)
      {
         var writeCommentPrompt = $(this).closest(".commentsResponseBox").find(".writeCommentPrompt");
         
         if (writeCommentPrompt.length > 0)
         {
            $(this).val("");
            $(this).closest(".commentsResponseBox").find(".commentsBoxContainer").hide();

            if ($(this).closest(".commentsResponseBox").siblings(".comments").find(".commentsBody").length > 0)
            {
               writeCommentPrompt.show();
            }
            else
            {
               $(this).closest(".commentsWrapper").hide();
            }
         }
      }
   });
}

function fullscreenGmapAdjust()
{
   if (typeof(mapController) != "undefined")
   {
      mapController.respondToResize();
   }   
   else if (typeof(rkMap) != "undefined")
   {
      rkMap.gmap.checkResize();
   }
}

// This is for links inside buttons
function bindButtonsToLinks()
{
   $(".blueButton-small, .blueButton-medium, .blueButton-large, .menuItem, .profileNavListItem, .navItem, .mainNav, .homeNavItem").click(function() {
      var href = $(this).find("a").attr("href");
      if (href) window.location.href = href;
   });

   // Have to disable the default behavior of the contained link or else we get a double request
   $(".blueButton-small, .blueButton-medium, .blueButton-large, .menuItem, .profileNavListItem, .navItem, .mainNav, .homeNavItem").find("a").click(function(event){
      event.preventDefault();
   });
}

window.onscroll = function()
{
   if ( $("#header").hasClass("homepage") )
   {
      if (window.XMLHttpRequest)
      {
         if (document.documentElement.scrollTop > 269 || self.pageYOffset > 269)
         {
            $("#reportsHeaderLegend").css("position","fixed").css("top","10px").css("margin-top","0");
            $("#reportsHeaderGhost").show();
         }
         else if (document.documentElement.scrollTop < 269 || self.pageYOffset < 269)
         {
            $("#reportsHeaderLegend").css("position","relative").css("top","0").css("margin-top","20px");
            $("#reportsHeaderGhost").hide();
         }
      }
   }
   else
   {
      if (window.XMLHttpRequest)
      {
         if (document.documentElement.scrollTop > 208 || self.pageYOffset > 208)
         {
            $("#reportsHeaderLegend").css("position","fixed").css("top","10px").css("margin-top","0");
            $("#reportsHeaderGhost").show();
         }
         else if (document.documentElement.scrollTop < 208 || self.pageYOffset < 208)
         {
            $("#reportsHeaderLegend").css("position","relative").css("top","0").css("margin-top","20px");
            $("#reportsHeaderGhost").hide();
         }
      }
   }
}

var rkGrowl = {
   show: function(mainText, subText, type, linkUrl)
   {
      $("#notificationBar").show();
      $("#notificationBar .mainText").text(mainText);
      $("#notificationBar .subText").text(subText);
      $("#notificationContent").addClass(type);
      $("#notificationBar").animate({
         bottom: "0",
         opacity: "1.0"
      }, 500);

      if (type == "message")
      {
         $("#notificationBar").click(function(){
            window.location = linkUrl;
         });

         setTimeout("rkGrowl.hide()", 20000);
      }
      else
      {
         $("#notificationBar").click(function(){
            rkGrowl.hide();
         });

         setTimeout("rkGrowl.hide()", 4500);
      }
   },
   hide: function()
   {
      $("#notificationBar").animate({ 
         bottom: "-111px",
         opacity: "0.0"
      }, 500);
      $("#notificationContent").removeClass("success error message");
   }
};

function selectSize() {
   $("select").each(function(index) {
      if(!$.browser.msie){
         $(this).css("width", $(this).parent().width());
      }
   });
}

function formatPace(pace) // pace in decimal minutes (per mi or km)
{
   var avgPaceMin = Math.floor(pace);
   var avgPaceSec = Math.floor(Math.round((pace - avgPaceMin) * 60));

   if (avgPaceSec == 60)
   {
      avgPaceSec = 0;
      avgPaceMin++;
   }

   var avgPaceSecStr = avgPaceSec.toString();

   if (avgPaceSec < 10)
   {
      avgPaceSecStr = "0" + avgPaceSecStr;
   }

   return avgPaceMin + ":" + avgPaceSecStr;
}

function formatTime(totalMin, showZeroHours)
{
   var hours = Math.floor(Math.floor(totalMin) / 60);
   var min = Math.floor(totalMin) % 60;
   var sec = Math.floor((totalMin - Math.floor(totalMin)) * 60.0);

   if (sec == 60)
   {
      sec = 0;
      min++;
   }

   if (min == 60)
   {
      min = 0;
      hours++;
   }

   var secStr = sec.toString();

   if (sec < 10)
   {
      secStr = "0" + secStr;
   }

   var minStr = min.toString();

   if (min < 10)
   {
      minStr = "0" + minStr;
   }

   if (showZeroHours || hours > 0)
   {
      return hours + ":" + minStr + ":" + secStr;
   }
   else
   {
      return minStr + ":" + secStr;
   }
}

function generateDataPointIndexes(chartData)
{
   for (var i = 0; i < chartData.series.length; i++)
   {
      if (chartData.series[i].data.length > 0)
      {
         chartData.series[i].data = $.map(chartData.series[i].data, function(value, index) {
            if (value.length == undefined)
            {
               return { y: value, index: index };
            }
            else if (value.length == 2)
            {
               return { x: value[0], y: value[1], index: index };
            }
         });
      }
   }
}

function addComment()
{
   var postButton = $(this);
   postButton.hide(); //to prevent double clicking
   $.ajaxSetup({scriptCharset: "UTF-8" , contentType: "application/x-www-form-urlencoded; charset=UTF-8"});
    
   $.post(addCommentUrl,
   {
      parentObjectType : postButton.attr("parentObjectType"),
      parentObjectId   : postButton.attr("parentObjectId"),
      commentText      : postButton.closest("form").find(".commentTextArea").val()
   },
   function(resultHtml)
   {
      postButton.closest("form").find(".commentTextArea").val("");
      var newCommentDiv = $(resultHtml);
      newCommentDiv.find(".commentsDelete").click(deleteComment);
      newCommentDiv.appendTo("#commentsDiv" + postButton.attr("commentsBoxId"));
      newCommentDiv.closest(".comments").show();
      postButton.closest(".commentsResponseBox").find(".writeCommentPrompt").show();
      postButton.closest(".commentsResponseBox").find(".commentsBoxContainer.hidden").hide();

      if ($("div.mainContentRight").hasClass("nosplits"))
      {
         var commentContainerHeight = $(".commentsOuterContainer").height();
         $(".mainContentRight").css("height", commentContainerHeight + 20);
      }

      postButton.show();
      refreshXfbml();
   });
}
 
function deleteComment()
{
   var deleteCommentButton = $(this);
   var commentId = deleteCommentButton.attr("commentId");

   jAlert("Are you sure you want to delete this comment?", "Delete Comment", function(result)
   {
      if (result)
      {
         $.post(deleteCommentUrl, { commentId : commentId },
         function(result)
         {
            if (result)
            {
               if ($("#comment" + commentId).closest(".comments").find(".commentsBody").length == 1)
               {
                  $("#comment" + commentId).closest(".comments").hide();
                  $("#comment" + commentId).closest(".comments").siblings(".commentsResponseBox").find(".writeCommentPrompt").hide();
               }
               
               $("#comment" + commentId).remove();

               if ($("div.mainContentRight").hasClass("nosplits"))
               {
                  var commentContainerHeight = $(".commentsOuterContainer").height();
                  $(".mainContentRight").css("height", commentContainerHeight + 20);
               }
            } 
            else 
            {
                alert("Unable to remove comment");
            }      
         });
      }
   });
}
