// ----------------------------------------------------------------------------
// generators.js - updated 2010 July 14
// ----------------------------------------------------------------------------

function GenerateLastUpdated()
{
   document.writeln( "Document updated ", UTCAsString( new Date( document.lastModified ) ), " UTC" );
}

function ScrollToPageAnchor( anchorId )
{
   if ( isIE )
      var obj = document.anchors( anchorId, 0 );
   else
      var obj = document.anchors[anchorId];
   var pos = findPos( obj );
   self.scrollTo( 0, pos[1] - 10 );
}

function ToggleDivExpansion( id )
{
   var divs = document.getElementsByTagName( "div" );
   for ( var i = 0; i < divs.length; ++i )
   {
      var divId = divs[i].getAttribute( "id" );
      if ( divId && divId == id )
      {
         divs[i].style.display = (divs[i].style.display == "none") ? "block" : "none";
         break;
      }
   }
}

function ToggleChildrenDivExpansion( prefix )
{
   var divs = document.getElementsByTagName( "div" );
   for ( var i = 0; i < divs.length; ++i )
   {
      var divId = divs[i].getAttribute( "id" );
      if ( divId && divId.beginsWith( prefix ) )
         divs[i].style.display = (divs[i].style.display == "none") ? "block" : "none";
   }
}

function scrollXOffset()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
   return w ? w : 0;
}

function scrollYOffset()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
   return h ? h : 0;
}

function ElementWidth( elem )
{
   if ( elem != null && elem.style != null )
   {
      var style = window.getComputedStyle( elem, null );
      return parseInt( style.getPropertyValue( "width" ) ) +
            parseInt( style.getPropertyValue( "padding-left" ) ) +
            parseInt( style.getPropertyValue( "padding-right" ) );
   }
   return 0;
}

function ElementHeight( elem )
{
   if ( elem != null && elem.style != null )
   {
      var style = window.getComputedStyle( elem, null );
      return parseInt( style.getPropertyValue( "height" ) ) +
            parseInt( style.getPropertyValue( "padding-top" ) ) +
            parseInt( style.getPropertyValue( "padding-bottom" ) );
   }
   return 0;
}

function AdjustCSSPageComponents()
{
   // Acquire the available width on the browser, including the vertical
   // scroll bar, if present. This is just window.innerWidth on a
   // standards-compliant browser.
   var availableWidth = 0;
   if ( typeof( window.innerWidth ) == 'number' )
      availableWidth = window.innerWidth; // A real browser, that is, not IE.
   else if ( document.documentElement && document.documentElement.clientWidth )
      availableWidth = document.documentElement.clientWidth; // IE 6+ sucker in 'standards compliant mode'

   var contentsWidth = 0;
   var elem = document.getElementById( "contents" );
   if ( elem != null && elem.style != null )
      contentsWidth = ElementWidth( elem );

   var tocWidth = 0;
   elem = document.getElementById( "toc" );
   if ( elem != null && elem.style != null )
      tocWidth = ElementWidth( elem );

   var thumbsWidth = 0;
   elem = document.getElementById( "thumbs" );
   if ( elem != null && elem.style != null )
      thumbsWidth = ElementWidth( elem );

   var fixedWidth = contentsWidth + tocWidth + thumbsWidth;
   var marginWidth = Math.max( 88, (availableWidth - fixedWidth) >> 1 );
   var yOffset = scrollYOffset();
   var scrollHeight = document.body.scrollHeight;

   // Center page contents
   elem = document.getElementById( "container" );
   if ( elem != null && elem.style != null )
   {
      elem.style.width = (marginWidth + fixedWidth + 16).toString() + "px";
      elem.style.marginLeft = "0px";
      elem.style.marginRight = "";
      elem.style.height = (scrollHeight + yOffset).toString() + "px";
   }

   elem = document.getElementById( "logo" );
   if ( elem != null && elem.style != null )
   {
      elem.style.width = marginWidth.toString() + "px";
      elem.style.marginLeft = "0px";
      elem.style.textAlign = "right";

      var marginTop = parseInt( window.getComputedStyle( elem, null ).getPropertyValue( "padding-top" ) );
      elem.style.height = (scrollHeight + yOffset - marginTop).toString() + "px";
   }

   elem = document.getElementById( "thumbs" );
   if ( elem != null && elem.style != null )
   {
      var marginTop = parseInt( window.getComputedStyle( elem, null ).getPropertyValue( "padding-top" ) );
      elem.style.height = (scrollHeight + yOffset - marginTop).toString() + "px";
   }

   elem = document.getElementById( "contents" );
   if ( elem != null && elem.style != null )
   {
      var marginTop = parseInt( window.getComputedStyle( elem, null ).getPropertyValue( "padding-top" ) );
      elem.style.height = (scrollHeight + yOffset - marginTop).toString() + "px";
   }

   elem = document.getElementById( "container" );
   if ( elem != null && elem.style != null )
      elem.style.visibility = "visible";
}

function AdjustCSSPageHeight()
{
   var yOffset = scrollYOffset();
   var scrollHeight = document.body.scrollHeight;

   var elem = document.getElementById( "container" );
   if ( elem != null && elem.style != null )
      elem.style.height = (scrollHeight + yOffset).toString() + "px";

   elem = document.getElementById( "logo" );
   if ( elem != null && elem.style != null )
   {
      var marginTop = parseInt( window.getComputedStyle( elem, null ).getPropertyValue( "padding-top" ) );
      elem.style.height = (scrollHeight + yOffset - marginTop).toString() + "px";
   }

   elem = document.getElementById( "thumbs" );
   if ( elem != null && elem.style != null )
   {
      var marginTop = parseInt( window.getComputedStyle( elem, null ).getPropertyValue( "padding-top" ) );
      elem.style.height = (scrollHeight + yOffset - marginTop).toString() + "px";
   }

   elem = document.getElementById( "contents" );
   if ( elem != null && elem.style != null )
   {
      var marginTop = parseInt( window.getComputedStyle( elem, null ).getPropertyValue( "padding-top" ) );
      elem.style.height = (scrollHeight + yOffset - marginTop).toString() + "px";
   }
}

window.onload = function()
{
   IE6Warning();
   AdjustCSSPageComponents();
}

window.onresize = function()
{
   AdjustCSSPageComponents();
}

window.onscroll = function()
{
   AdjustCSSPageHeight();
}

var __toc_print_first = true;

function TOCItem( parent, name, text, href )
{
   this.parent = parent;

   if ( parent != null )
   {
      parent.children.push( this );
      if ( !parent.name.isEmpty() )
         this.name = parent.name + '_' + name;
      else
         this.name = name;
   }
   else
      this.name = name;

   this.text = text;
   this.href = href;
   this.children = new Array;

   this.hasIndex = function( index )
   {
      if ( this.name == index )
         return true;

      for ( var i = 0; i < this.children.length; ++i )
         if ( this.children[i].hasIndex( index ) )
            return true;

      return false;
   };

   this.isAnchorRef = function()
   {
      return !(this.href.isHttpDocument() ||
               this.href.beginsWith( "javascript:" ) ||
               this.href.beginsWith( '.' ) || this.href.beginsWith( '/' ));
   };

   this.isRootRef = function()
   {
      return this.href.beginsWith( '/' );
   };

   this.print = function( level, baseUrl, index )
   {
      if ( this.text.isEmpty() )
         document.writeln( "<hr />" );
      else
      {
         var isIndex = this.name == index;

         document.write( "<div id=\"", this.name, "\"" );

         if ( this.children.length > 0 )
            document.write( " onclick=\"ToggleChildrenDivExpansion( \'", this.name, "_\' );\"" );

         document.write( ">" );

         if ( isIndex )
            document.write( "<div class=\"tocIndex\">" );

         if ( level > 0 )
         {
            document.write( "<div class=\"tocSubItem" );
            if ( level > 1 )
               document.write( " tocPageContents" );
            document.writeln( "\"><ul style=\"padding-left: ", 2.5 + 1.2*(level - 1), "em;\"><li>" );
         }
         else
         {
            document.write( "<p" );
            if ( __toc_print_first )
            {
               __toc_print_first = false;
               document.write( " class=\"firstLine\"" );
            }
            document.writeln( ">" );
         }

         if ( this.href.isEmpty() )
            document.write( this.text );
         else
         {
            document.write( "<a href=\"" );

            if ( this.isRootRef() )
               document.write( baseUrl + this.href );
            else if ( this.isAnchorRef() )
               document.write( "javascript:ScrollToPageAnchor( '", this.href, "' );" );
            else
               document.write( this.href );

            document.write( "\">", this.text, "</a>" );
         }

         if ( level > 0 )
            document.write( "</li></ul></div>" );
         else
            document.writeln( "</p>" );

         if ( isIndex )
            document.write( "</div>" );

         document.write( "</div>" ); // name

         document.writeln();
      }
   };

   this.printLevel = function( level, baseUrl, index )
   {
      for ( var i = 0; i < this.children.length; ++i )
         with ( this.children[i] )
         {
            print( level, baseUrl, index );
            if ( level < 1 || hasIndex( index ) )
               printLevel( level+1, baseUrl, index );
         }
   };
}

function GenerateStandardTOC( index )
{
   var toc = new TOCItem( null, "", "", "" );

   //

   new TOCItem( toc, "home", "Home", "/index.html" );

   //

   var
   root = new TOCItem( toc, "pixinsight", "PixInsight", "" );

   var
   root1 = new TOCItem( root,"features", "Platform Features", "/features/index.html" );
   new TOCItem( root1, "", "Modular, Portable Platform",     "01" );
   new TOCItem( root1, "", "Object-Oriented User Interface", "02" );
   new TOCItem( root1, "", "Five Pixel Data Formats",        "03" );
   new TOCItem( root1, "", "Advanced Masking System",        "04" );
   new TOCItem( root1, "", "Multiple Preview Interface",     "05" );
   new TOCItem( root1, "", "Command-Line Interface",         "06" );
   new TOCItem( root1, "", "Advanced JavaScript Runtime",    "07" );
   new TOCItem( root1, "", "Comprehensive Set of Processes", "08" );
   new TOCItem( root1, "", "Advanced FITS Format Support",   "09" );

   root1 = new TOCItem( root, "sysreq", "System Requirements", "/sysreq/index.html" );
   new TOCItem( root1, "", "Operating System",           "01" );
   new TOCItem( root1, "", "Processor",                  "02" );
   new TOCItem( root1, "", "RAM",                        "03" );
   new TOCItem( root1, "", "Video Mode",                 "04" );
   new TOCItem( root1, "", "Free Hard Disk Space",       "05" );
   new TOCItem( root1, "", "Hard Disk Performance",      "06" );
   new TOCItem( root1, "", "Parallel Swap File Storage", "07" );

   new TOCItem( root, "screenshots", "Screen Shots", "/screenshots/index.html" );

   new TOCItem( root, "faq", "Product FAQ", "/faq/index.html" );

   //

   root = new TOCItem( toc, "resources", "Resources", "" );

   root1 = new TOCItem( root, "videos", "Video Tutorials", "/videos/index.html" );
   new TOCItem( root1, "", "Official PixInsight Video Tutorials",         "01" );
   new TOCItem( root1, "", "Other Sites with PixInsight Video Tutorials", "02" );

   new TOCItem( root, "examples", "Processing Examples", "/examples/index.html" );

   root1 = new TOCItem( root, "tutorials", "Tutorials", "/tutorials/index.html" );
   new TOCItem( root1, "", "Current Tutorials",                "01" );
   new TOCItem( root1, "", "Obsolete Tutorials",               "02" );
   new TOCItem( root1, "", "Obsolete PixInsight LE Tutorials", "03" );

   new TOCItem( root, "studios", "PixInsight Studios", "http://www.youtube.com/user/PixInsight" );

   new TOCItem( root, "gallery", "Image Gallery", "/gallery/index.html" );

   //

   root = new TOCItem( toc, "license", "Software Licenses", "" );
   new TOCItem( root, "trial", "Request Trial License", "/trial/index.html" );
   new TOCItem( root, "commercial", "Buy Commercial License", "/buy/index.html" );

   //

   root = new TOCItem( toc, "download", "Downloads", "" );
   new TOCItem( root, "dist", "Software Distribution", "/dist/index.html" );

   root1 = new TOCItem( root, "free", "Free Downloads", "/download/index.html" );
   new TOCItem( root1, "", "StarGenerator Databases",            "01" );
   new TOCItem( root1, "", "PixInsight Web Badges",              "02" );
   new TOCItem( root1, "", "Legacy PixInsight LE Documentation", "03" );

   //

   root = new TOCItem( toc, "community", "Community", "" );
   new TOCItem( root, "forum", "PixInsight Forum", "http://pixinsight.com/forum/" );
   new TOCItem( root, "wiki", "PixInsight Wiki", "http://pixinsight.com/wiki/" );
   new TOCItem( root, "blogs", "PixInsight Blogs", "http://pixinsight.com/blogs/" );

   //

   root = new TOCItem( toc, "developer", "Developer", "" );

   root1 = new TOCItem( root, "pcl", "PixInsight Class Library", "/developer/pcl/index.html" );
   new TOCItem( root1, "", "A Toolset to Build New Imaging Tools", "01" );
   new TOCItem( root1, "", "Portable Development Platform",        "02" );
   new TOCItem( root1, "", "Contributing PCL Development",         "03" );
   new TOCItem( root1, "", "Supported C++ Compilers",              "04" );

   root1 = new TOCItem( root, "pcldoc", "PCL Documentation", "/developer/pcl/doc/index.html" );
   new TOCItem( root1, "", "PCL Reference Documentation", "/developer/pcl/doc/html/index.html" );

   new TOCItem( root, "pjsr", "PixInsight JavaScript Runtime", "/developer/pjsr/index.html" );

   //

   root = new TOCItem( toc, "company", "Company", "" );
   new TOCItem( root, "about", "About Us", "/about/index.html" );
   new TOCItem( root, "contact", "Contact Us", "/contact/index.html" );
   new TOCItem( root, "privacy", "Privacy Policy", "/privacy/index.html" );
   new TOCItem( root, "terms", "Terms & Conditions", "/terms/index.html" );
   new TOCItem( root, "antipiracy", "Report Piracy", "/antipiracy/index.html" );

   var baseUrl = IsOnLine() ? "" : "/home/juan/PixInsight/www";

   document.writeln( "<div id=\"toc\">" );
   document.writeln( "  <div class=\"tocText\">" );
   toc.printLevel( 0, baseUrl, index );
   document.writeln( "  <br />" );
   document.writeln( "  </div>" );
   document.writeln( "</div>" );
}

function GenerateStandardLogo()
{
   var baseUrl = IsOnLine() ? "/" : "/home/juan/newsite/";
   document.writeln( "<div id=\"logo\">" );
   document.writeln( "<a href=\"http://pixinsight.com/\"><img src=\"", baseUrl, "logos/PixInsight_vert.png\" /></a>" );
   document.writeln( "</div>" );
}

function GenerateStandardFooter()
{
   document.writeln( "<p style=\"margin-top: 1em; padding-top:1em; border-top: 2px solid #d0d0d0;\"><a href=\"/terms/index.html\">Copyright</a> &copy; 2010 Pleiades Astrophoto S.L.</p>" );
   document.writeln( "<p class=\"small\">" );
   document.writeln( "<script type=\"text/javascript\">" );
   document.writeln( "GenerateLastUpdated();" );
   document.writeln( "</script>" );
   document.writeln( "<br/><br/></p>" );
}

function GenerateDownloadLink( url, text )
{
   var baseUrl = IsOnLine() ? "/" : "/home/juan/newsite/";
   document.writeln( "<img style=\"width: 6px; height: 9px; margin-right: 5px;\" src=\"",
                     baseUrl,
                     "graphics/arrow-right.png\" /><a href=\"", url, "\">", text, "</a>" );
}

function GenerateCollapsibleLink( divId, paragraph )
{
   var baseUrl = IsOnLine() ? "/" : "/home/juan/newsite/";
   document.writeln( "<img style=\"width: 6px; height: 9px; margin-right: 5px;\" src=\"",
                     baseUrl,
                     "graphics/arrow-right.png\" />",
                     "<a class=\"collapsibleLink\" href=\"javascript:void( 0 );\" onclick=\"ToggleDivExpansion( '", divId, "' );\">",
                     paragraph , "</a>" );
}

function GenerateIE6Warning()
{
   document.writeln( "<div id=\"IE6Warning\">" );
   document.writeln( "<h2>Obsolete IE Version</h2>" );
   document.writeln( "<p>You are using an obsolete version of MS Internet Explorer (IE 6 or older)" );
   document.writeln( "that we no longer support in our website.</p>" );
   document.writeln( "<p>In order to explore our website correctly, you should upgrade to Internet" );
   document.writeln( "Explorer 7/8 or, much better, use a standards-compliant web browser such as" );
   document.writeln( "<a href=\"http://www.mozilla.com/\">Mozilla Firefox</a>, " );
   document.writeln( "<a href=\"http://www.opera.com/\">Opera</a> or " );
   document.writeln( "<a href=\"http://www.apple.com/safari/download/\">Safari</a>.</p>" );
   document.writeln( "<p>If you are interested in our reasons to discontinue support for IE6, we" );
   document.writeln( "strongly recommend you to read the" );
   document.writeln( "<a href=\"http://www.webdesignerwall.com/general/trash-all-ie-hacks/\">IE bug</a>" );
   document.writeln( "articles on <a href=\"http://www.positioniseverything.net/\">positioniseverything.net</a>." );
   document.writeln( "Also take a look at <a href=\"http://www.webdesignerwall.com/general/trash-all-ie-hacks/\">this page</a>" );
   document.writeln( "on <a href=\"http://www.webdesignerwall.com/\">WebDesignerWall</a>.</p>" );
   document.writeln( "<p><a href=\"javascript:void( 0 );\"" );
   document.writeln( "onclick=\"document.getElementById('IE6Warning').style.visibility='hidden'\"><u>Close this Message</u></a></p>" );
   document.writeln( "</div>" );
}

// ----------------------------------------------------------------------------
