// JavaScript Document
// Routines for web-doc display (frameless), version 2.06

// 06/03/2011: conditionalized dimension for Japanese doc, which still uses the R2010b layout.
// 01/06/2011: updated to match "top" setting in docset3.css for search box placement.


help_root = "/help/";
whichSectionHighlighted = "unassigned";
anchor_body_loadcontent = "unassigned";

// This JavaScript is not within a function.  It automatically initiates and changes the height of the Table of Contents and Body div elements to be in line with the visitor's browser settings. 


 var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }


if (location.pathname.indexOf("help/ja_JP/") > -1) {
  document.getElementById("frameless_contents").style.height = (myHeight - 202)+"px";
  document.getElementById("doc_wrapper").style.height = (myHeight - 180)+"px";
} else {
  document.getElementById("frameless_contents").style.height = (myHeight - 172)+"px";
  document.getElementById("doc_wrapper").style.height = (myHeight - 240)+"px";
}


// This JavaScript is not within a function.  It automatically initiates and replaces the frameless_contents div's original high-level links with the entire table of contents file, via AJAX (a way to include the file client-side, rather than server-side)

if (window.name.substr(0,5) == "index")
          {
           show_us_the_index();
          } else {
           show_us_the_toc()
          }



// To accommodate the Handle Graphics Property Browser, we modify the usual div elements to hide
// the frameless nav area and TOC area, if we detect the HG Prop Browser frameset is in effect.

if (parent.frames.length >= 3)
          {
           document.getElementById("frameless_contents").style.display = "none";
           document.getElementById("frameless_top").style.display = "none";
           document.getElementById("frameless_toc_header").style.display = "none";
           document.getElementById("doc_wrapper").style.height = "auto";
           document.getElementById("doc_wrapper").style.overflow = "visible"; 
           document.getElementById("doc_wrapper").style.position = "static";
           document.getElementById("doc_wrapper").style.width = "100%";
           document.getElementById("doc_wrapper").style.height = "auto";
           document.body.style.overflow = "auto";  // Fix for g592347
           if (navigator.appName == "Microsoft Internet Explorer")
             {
              document.body.style.marginLeft = "5px";
             };
           if (navigator.userAgent.indexOf("Safari") > -1) 
             {
              // Safari-specific fix for g592347 
              document.getElementById("doc_wrapper").style.height = (myHeight)+"px";
              document.getElementById("doc_wrapper").style.overflow = "auto";
              parent.frames[1].document.getElementById("hgpb_leftpanel").style.overflow = "auto";
              parent.frames[1].document.getElementById("hgpb_leftpanel").style.height = (window.innerHeight)+"px";
             };
          }


function toc_display(REQ)
{
 highlight_current_page();
}


function show_us_the_toc()
{
 highlight_current_page();
}


	
// resizeHeight is called when the body resizes.  It updates the height of the table of contents and body div elements

function resizeHeight() 
{
// For Handle Graphics Property Browser files, do nothing (except for Safari).
if (parent.frames.length >= 3) {
  if (navigator.userAgent.indexOf("Safari") > -1) {
    document.getElementById("doc_wrapper").style.height = (window.innerHeight)+"px";
    parent.frames[1].document.getElementById("hgpb_leftpanel").style.height = (window.innerHeight)+"px";
  }
  return; 
}


var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

if (location.pathname.indexOf("help/ja_JP/") > -1) {
  document.getElementById("frameless_contents").style.height = (myHeight - 202)+"px";
  document.getElementById("doc_wrapper").style.height = (myHeight - 180)+"px";
} else {
  document.getElementById("frameless_contents").style.height = (myHeight - 172)+"px";
  document.getElementById("doc_wrapper").style.height = (myHeight - 240)+"px";	
}


} // end resizeHeight





// hightlight_current_page gets called onload.  It highlights the current page in the TOC and opens all the preceding div elements
function highlight_current_page() {

var contentpathname = location.pathname + location.hash;

     var toc_frame = document.getElementById("navbody");
     var tocframeanchorelements = toc_frame.getElementsByTagName("a");
     var tocframedivelements = toc_frame.getElementsByTagName("div");
     var anchorhreffilename = "unassigned";
     var anchorhrefanchor = "unassigned";
     tocframeanchorelementmatch = "unassigned";
     var tocframetocitemid = "unassigned";
     
     
     len = tocframeanchorelements.length;


     for (i = 0; i < len; i++)
     {
       anchorhref = tocframeanchorelements[i].href;
       anchorhrefarray = anchorhref.split("mathworks.");
       anchorhreffilename = anchorhrefarray[(anchorhrefarray.length - 1)];
       anchorhreffilename = anchorhreffilename.match(/\/.*/)[0];

       if (anchorhreffilename == contentpathname)
       {

          tocframeanchorelementmatch = tocframeanchorelements[i];
          tocframetocitemid = tocframeanchorelementmatch.onclick.toString();
          tocframetocitemid = tocframetocitemid.match(/tocitem_[0-9]+/)[0];
	  
          tocframeanchorelementmatch.style.backgroundColor = "#d7d7d7";
	  
          if (tocframeanchorelementmatch.parentNode.getElementsByTagName("span")[0])
            {
             // If this node was collapsed on the last click, display as collapsed.
             if ((window.name.substr(0,3) == "toc") && (window.name.substr(4) == tocframetocitemid.replace(/item/, "sect") + ".none"))
               {
                // collapsed
                break;  // Found the match and highlighted it, so no need to go through the rest.
               }
             else
               { 
	        var newlyclickedspan_arrow = tocframeanchorelementmatch.parentNode.getElementsByTagName("span")[0];
                newlyclickedspan_arrow.innerHTML = "&#9660;&nbsp;";
                tocframesectionid = tocframetocitemid.replace(/item/, "sect");
                document.getElementById(tocframesectionid).style.display = "block";
               }
            }
       }
   
     } // end for loop



     // If we didn't get a match in the first loop, try again but without trying to match the named anchors.
     if ( tocframeanchorelementmatch == "unassigned" )
     {
       for (i = 0; i < len; i++)
       {
         anchorhref = tocframeanchorelements[i].href;
         anchorhrefarray = anchorhref.split("mathworks.");
         anchorhreffilename = anchorhrefarray[(anchorhrefarray.length - 1)];
         anchorhreffilename = anchorhreffilename.match(/\/.*/)[0];

         if (anchorhreffilename.replace(/#.*$/,"") == contentpathname.replace(/#.*$/,""))
         {
            tocframeanchorelementmatch = tocframeanchorelements[i];
            tocframetocitemid = tocframeanchorelementmatch.onclick.toString();
            tocframetocitemid = tocframetocitemid.match(/tocitem_[0-9]+/)[0];
            tocframeanchorelementmatch.style.backgroundColor = "#d7d7d7";
            if (tocframeanchorelementmatch.parentNode.getElementsByTagName("span")[0])  	  
              {
               // If this node was collapsed on the last click, display as collapsed.
               if ((window.name.substr(0,3) == "toc") && (window.name.substr(4) == tocframetocitemid.replace(/item/, "sect") + ".none"))
                 {
                  // collapsed
                 }
               else
                 { 
  	        var newlyclickedspan_arrow = tocframeanchorelementmatch.parentNode.getElementsByTagName("span")[0];
                  newlyclickedspan_arrow.innerHTML = "&#9660;&nbsp;";
                  tocframesectionid = tocframetocitemid.replace(/item/, "sect");
                  document.getElementById(tocframesectionid).style.display = "block";
                 }
               break;  // Found the match and highlighted it, so no need to go through the rest.
              }
         }
       } // end for loop
     }  
     // end of second test. 


     if ( tocframeanchorelementmatch != "unassigned" )
     {
       var tocframetocitempara = document.getElementById(tocframetocitemid);

       var tocframeparent = tocframetocitempara.parentNode;

       while (tocframeparent.tagName == "DIV" && tocframeparent.id != "navbody")
       {
         tocframeparent.style.display = "block";
       
         var parent_item_id_number = tocframeparent.id.match(/[0-9]+$/)[0];
         var parent_item_id = "tocitem_" + parent_item_id_number;
         var parent_toc_item = document.getElementById(parent_item_id);

         parent_toc_item.firstChild.innerHTML = "&#9660;&nbsp;";

         tocframeparent = tocframeparent.parentNode;
       }
     }

     //The section below is necessary to attach desired onclick events to the TOC and doc_wrapper div elements 
     //toclinks = document.getElementById("navbody").getElementsByTagName("a");
     bodylinks = document.getElementById("doc_wrapper").getElementsByTagName("a");
     for(i=0; i<bodylinks.length; i++)
     {
	if ((bodylinks[i].onclick == null) && (bodylinks[i].target != "_top") && (bodylinks[i].target != "_blank"))
        {	
          whichHref = bodylinks[i].href.split("#");
          splitHref = whichHref[0].split("/help/");
          splitLocation = location.pathname.split("/help/");
          if ((splitHref[1] == splitLocation[1]) && (whichHref[1] != "" ) && (whichHref[1] != "top_of_page" )) {
            bodylinks[i].onclick = body_loadcontent;
          } else {
            // Formerly, this line executed for both toc and index.
            if (window.name.substr(0,3) == "toc") { bodylinks[i].onclick = return_toc; };
          }
        }
     }
	 
} // end highlight_current_page


function return_toc()
{
  if (window.name != "right_frame") {	
     window.name = "toc";	
  };
}

function body_loadcontent()
{

     if ( window.anchor_body_loadcontent != "unassigned" ) {
        window.anchor_body_loadcontent.style.backgroundColor = "#ffffff";
     }
     var toc_frame_body_loadcontent = document.getElementById("navbody");
     var tocframeanchorelements_body_loadcontent = toc_frame_body_loadcontent.getElementsByTagName("a");
     var len_body_loadcontent = tocframeanchorelements_body_loadcontent.length;

     for (i = 0; i < len_body_loadcontent; i++)
      {
       anchorhref_body_loadcontent = tocframeanchorelements_body_loadcontent[i].href;
  	
       if (anchorhref_body_loadcontent == this.href)
       {
        anchor_body_loadcontent = tocframeanchorelements_body_loadcontent[i];
		
        if ( window.whichSectionHighlighted != "unassigned" ) {
          window.whichSectionHighlighted.style.backgroundColor = "#ffffff";
        }

       if ( window.tocframeanchorelementmatch != "unassigned" ) {
         window.tocframeanchorelementmatch.style.backgroundColor = "#ffffff";
       }

       anchor_body_loadcontent.style.backgroundColor = "#d7d7d7";
       anchor_body_loadcontent.scrollIntoView();
       tocframetocitemid_body_loadcontent = anchor_body_loadcontent.onclick.toString();
       tocframetocitemid_body_loadcontent = tocframetocitemid_body_loadcontent.match(/tocitem_[0-9]+/)[0];

       if (document.getElementById(tocframetocitemid_body_loadcontent).getElementsByTagName("span")[0]) {
         whichSectionHighlightedSpan = document.getElementById(tocframetocitemid_body_loadcontent).getElementsByTagName("span")[0];
         tocframetocsectid_body_loadcontent = tocframetocitemid_body_loadcontent.replace(/item/, "sect");

         whichSection = document.getElementById(tocframetocsectid_body_loadcontent);
         whichSection.style.display = "block";
         window.whichSectionHighlightedSpan.innerHTML = "▼&nbsp;";
       }
       }
     } // end for loop
	  
} // end body_loadcontent





function registerlastclicked(whichSection) {

window.name = "toc." + whichSection;

whichHref = document.getElementById(whichSection).getElementsByTagName("a")[0].href;


if ( window.anchor_body_loadcontent != "unassigned" ) {
window.anchor_body_loadcontent.style.backgroundColor = "#ffffff";
}
if ( window.whichSectionHighlighted != "unassigned" ) {
window.whichSectionHighlighted.style.backgroundColor = "#ffffff";
}

if ( window.tocframeanchorelementmatch != "unassigned" ) {
	
window.tocframeanchorelementmatch.style.backgroundColor = "#ffffff";
whichHref = whichHref.split("#");
splitHref = whichHref[0].split("/help/");
splitLocation = location.pathname.split("/help/");
if ((splitHref[1] == splitLocation[1]) && (whichHref[1] != "" )) {
whichSectionHighlighted = document.getElementById(whichSection).getElementsByTagName("a")[0];
whichSectionHighlighted.style.backgroundColor = "#d7d7d7";
if (document.getElementById(whichSection).getElementsByTagName("span")[0]) {
whichSectionHighlightedSpan = document.getElementById(whichSection).getElementsByTagName("span")[0];
}


}
}

} // end registerlastclicked


function toggletoc(whichSection) {

tocstateinfo = "toc." + whichSection;

if ((window.splitHref[1] == window.splitLocation[1]) && (window.whichHref[1] != "" )) {
		
	whichSection = document.getElementById(whichSection);
	
if (whichSection.style.display) {	
	if (whichSection.style.display == "block")	{
		whichSection.style.display = "none";
                tocstateinfo = tocstateinfo + ".none";
	} else {
	whichSection.style.display = "block";
        tocstateinfo = tocstateinfo + ".block";
	}
	
	
} else {
	whichSection.style.display = "block";
        tocstateinfo = tocstateinfo + ".block";
}

if (window.whichSectionHighlightedSpan.innerHTML == "▼&nbsp;") {
window.whichSectionHighlightedSpan.innerHTML = "•&nbsp;";
} else {
window.whichSectionHighlightedSpan.innerHTML = "▼&nbsp;";
}


}
window.name = tocstateinfo;

} // end toggletoc


function load_index(which_tab) {

  if (which_tab.id == "navitemcontents") {
     window.name = "toc";
     document.getElementById("navitemindex").className = "";
     document.getElementById("navitemcontents").className = "activebg";
     document.getElementById("navbody_index").style.display = "none";
     document.getElementById("navbody").style.display = "block";

  } else {
     window.name = "index";
     document.getElementById("navitemcontents").className = "";
     document.getElementById("navitemindex").className = "activebg";
     if ((!document.getElementById("navbody_index").childNodes[1]) || (document.getElementById("navbody_index").childNodes[1] == null) || (document.getElementById("navbody_index").childNodes[1] == "")) 
       {
         index_file = help_root + index_subpath;  // Note that index_subpath is set in the document file that includes this file. 
         new Ajax.Request(index_file,
           {
            method: "post",
            onComplete: index_display
           });

       } else {
        document.getElementById("navbody").style.display = "none";
        document.getElementById("navbody_index").style.display = "block";
       }
  }

} // end load_index


function index_display(REQ)
{
        if (window.name.substr(0,5) != "index") {window.name = "index";}

	responseTxt = REQ.responseText;
	
	document.getElementById("navbody_index").innerHTML = responseTxt;
	//ajax_loaded = 1;
	document.getElementById("navbody").style.display = "none";
	document.getElementById("navbody_index").style.display = "block";
	//indexlinks = document.getElementById("navbody_index").getElementsByTagName("a");

////        for(i=0; i<indexlinks.length; i++) 
////          {
////           indexlinks[i].onclick = loadcontent;
////          }


        // After redisplaying the index div, scroll to the entry that was clicked.
        if (window.name.substr(0,6) == "index.") {
           scrollPos = window.name.substr(6);
           scrollPos = scrollPos.replace(/^.*scroll\=/, "");
           scrollPos = scrollPos.replace(/\..*/, "");
           entryOfInterest = window.name.substr(6);
           entryOfInterest = entryOfInterest.replace(/^.*entry\=/, "");
           entryOfInterest = entryOfInterest.replace(/\..*/, "");
           document.getElementById("frameless_contents").scrollTop = scrollPos;
           if (navigator.appName == "Microsoft Internet Explorer")
             {
              // Have to do this twice for IE, or we go to the wrong place.
              document.getElementById("frameless_contents").scrollTop = scrollPos;
             };
           document.getElementById(entryOfInterest).firstChild.style.backgroundColor = "#d7d7d7";
        }
		
        highlight_current_page();

}  // end index_display


function show_us_the_index()
{
  document.getElementById("navbody").style.display = "none";
  document.getElementById("navbody_index").style.display = "block";
  document.getElementById("navitemcontents").className = "";
  document.getElementById("navitemindex").className = "activebg";
  if ((!document.getElementById("navbody_index").childNodes[1]) || (document.getElementById("navbody_index").firstChild == null) ||  (document.getElementById("navbody_index").firstChild == "")) 
    {
     if (index_subpath == "") return;  // Bail out if no index for this document.
     index_file = help_root + index_subpath;  // Note that index_subpath is set in the document file that includes this file. 

     if ((window.name.substr(0,5) == "index") && (window.name.indexOf("/")>0)) 
       {
        index_file = window.name.replace(/^.*?\//, "/");  // The index file path is part of the data passed from the last page via this variable.
       };

     new Ajax.Request(index_file,
       {
        method: "post",
        onComplete: index_display
       });

    } else {
       document.getElementById("navbody").style.display = "none";
       document.getElementById("navbody_index").style.display = "block";
    }

} // end show_us_the_index



// This routine is used to record the scroll position of the contents div at the time
// an index entry is clicked on.  
function recIxEntry(whichEntry)
{
  window.name = "index.entry=" + whichEntry + ".scroll=" + document.getElementById("frameless_contents").scrollTop + "." + index_file;

}  // end recIxEntry




function load_index_seg(alphagroup)
{
  index_file = index_file.replace(/helpindex.*.html/, "helpindex.seg_" + alphagroup + ".html");
  window.name = "index.0." + index_file;

  new Ajax.Request(index_file,
    {
     method: "post",
     onComplete: index_display
    });

} // end load_index_seg

