load google API navtree ondemand (remove request from <head>).
Also move the code for highlighting the sidenav into a separate method
that we can call at a later time, such as after the gms reference is loaded into nav.

Change-Id: I924d68b2b9dbfed83f81d1fd418e06b6c79a9010
diff --git a/tools/droiddoc/templates-sdk/assets/js/docs.js b/tools/droiddoc/templates-sdk/assets/js/docs.js
index 10d42b2..1b878b3 100644
--- a/tools/droiddoc/templates-sdk/assets/js/docs.js
+++ b/tools/droiddoc/templates-sdk/assets/js/docs.js
@@ -6,6 +6,7 @@
 var NAV_PREF_PANELS = "panels";
 var nav_pref;
 var isMobile = false; // true if mobile, so we can adjust some layout
+var mPagePath; // initialized in ready() function
 
 var basePath = getBaseUri(location.pathname);
 var SITE_ROOT = toRoot + basePath.substring(1,basePath.indexOf("/",1));
@@ -200,21 +201,16 @@
     $("#header li.distribute a").addClass("selected");
   }
 
+  // set global variable so we can highlight the sidenav a bit later (such as for google reference)
+  // and highlight the sidenav
+  mPagePath = pagePath;
+  highlightSidenav();
 
-  // select current page in sidenav and header, and set up prev/next links if they exist
+  // set up prev/next links if they exist
   var $selNavLink = $('#nav').find('a[href="' + pagePath + '"]');
   var $selListItem;
   if ($selNavLink.length) {
-
-    // Find this page's <li> in sidenav and set selected
     $selListItem = $selNavLink.closest('li');
-    $selListItem.addClass('selected');
-    
-    // Traverse up the tree and expand all parent nav-sections
-    $selNavLink.parents('li.nav-section').each(function() {
-      $(this).addClass('expanded');
-      $(this).children('ul').show();
-    });
 
     // set up prev links
     var $prevLink = [];
@@ -592,6 +588,24 @@
 // END of the onload event
 
 
+function highlightSidenav() {
+  // select current page in sidenav and header, and set up prev/next links if they exist
+  var $selNavLink = $('#nav').find('a[href="' + mPagePath + '"]');
+  var $selListItem;
+  if ($selNavLink.length) {
+
+    // Find this page's <li> in sidenav and set selected
+    $selListItem = $selNavLink.closest('li');
+    $selListItem.addClass('selected');
+    
+    // Traverse up the tree and expand all parent nav-sections
+    $selNavLink.parents('li.nav-section').each(function() {
+      $(this).addClass('expanded');
+      $(this).children('ul').show();
+    });
+  }
+}
+
 
 function toggleFullscreen(enable) {
   var delay = 20;
@@ -2386,7 +2400,6 @@
   me.node.depth = 0;
 
   get_google_node(me, me.node);
-
 }
 
 function new_google_node(me, mom, text, link, children_data, api_level)
@@ -2458,15 +2471,30 @@
 function showGoogleRefTree() {
   init_default_google_navtree(toRoot);
   init_default_gcm_navtree(toRoot);
-  resizeNav();
 }
 
 function init_default_google_navtree(toroot) {
-  init_google_navtree("gms-tree-list", toroot, GMS_NAVTREE_DATA);
+  // load json file for navtree data
+  $.getScript(toRoot + 'gms_navtree_data.js', function(data, textStatus, jqxhr) {
+      // when the file is loaded, initialize the tree
+      if(jqxhr.status === 200) {
+          init_google_navtree("gms-tree-list", toroot, GMS_NAVTREE_DATA);
+          highlightSidenav();
+          resizeNav();
+      }
+  });
 }
 
 function init_default_gcm_navtree(toroot) {
-  init_google_navtree("gcm-tree-list", toroot, GCM_NAVTREE_DATA);
+  // load json file for navtree data
+  $.getScript(toRoot + 'gcm_navtree_data.js', function(data, textStatus, jqxhr) {
+      // when the file is loaded, initialize the tree
+      if(jqxhr.status === 200) {
+          init_google_navtree("gcm-tree-list", toroot, GCM_NAVTREE_DATA);
+          highlightSidenav();
+          resizeNav();
+      }
+  });
 }
 
 /* TOGGLE INHERITED MEMBERS */
diff --git a/tools/droiddoc/templates-sdk/head_tag.cs b/tools/droiddoc/templates-sdk/head_tag.cs
index 4fa520f..9778389 100644
--- a/tools/droiddoc/templates-sdk/head_tag.cs
+++ b/tools/droiddoc/templates-sdk/head_tag.cs
@@ -47,10 +47,6 @@
   var devsite = <?cs if:devsite ?>true<?cs else ?>false<?cs /if ?>;
 </script>
 <script src="<?cs var:toroot ?>assets/js/docs.js" type="text/javascript"></script>
-<?cs if:reference.gms || reference.gcm || google?>
-<script src="<?cs var:toroot ?>gms_navtree_data.js" async type="text/javascript"></script>
-<script src="<?cs var:toroot ?>gcm_navtree_data.js" async type="text/javascript"></script>
-<?cs /if ?>
 
 <script type="text/javascript">
   var _gaq = _gaq || [];