// menus.js
// Main navigation menus and location bars
// This script defines the structure of the site's principal navigation

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function add_section( title, href )
{
	var html = ""
	var url = document.URL

	// This function will highlight the section corresponding to the current page.
	// To do this, two independent mechanisms will be used.
	// 1. If global variable page_category_slug has been set on the page (prior to calling this script),
	//    we will check if page_categopry occurs in the section href and use it as a criterion.
	// 2. If the (1) returns false, we will aditionally check if href of the section is contained with the url of the current page.

	if(typeof(page_category_slug) !== 'undefined') {
		if (href.search( page_category_slug ) >= 0) {
		   html = html + '<li id="selected">'
		}
	}

	if (html == "") {
	    if ( href!='/' && url.search( href )>=0 ) 
		{
		   html = html + '<li id="selected">'
		}
	    else if ( ( right(url, 9) == 'site.com/' || right(url, 11) == 'site.co.uk/' ) && ( href == '/' ) )
	    {
		   html = html + '<li id="selected">'
	    }
		else
		{
		   html = html + '<li>'
		}
	}

    html = html + '<a href="' + href + '">' + title + '</a></li>'

	return html
}

function main_menu( )
// Output a navigable primary menu
// to be placed in the left colum.
{
   var html = ""
   
   // This spacing added if an ad is placed above the menu
   // html = html + '<br>&nbsp;'
   
   html = html + '<h3>Shortcuts</h3>'
   html = html + '<ul>'
      html = html + add_section('Home', '/')
      html = html + add_section("What's New?", '/whats-new')
      html = html + add_section('Search', '/search')
   html = html + '</ul>'

   html = html + '<br><br>'
   html = html + '<h3>Editorial</h3>'
   html = html + '<ul>'
      html = html + add_section("Editor's Blog", '/category/10-editorial/editor')
      html = html + add_section('About This Site', '/category/10-editorial/site-information')
   html = html + '</ul>' 

   html = html + '<br><br>'
   html = html + '<h3>Community</h3>'
   html = html + '<ul>'
      html = html + add_section('Discussion Forums', '/category/20-community/discussion-forums')
      html = html + add_section('Fly With a Spitfire!', '/category/20-community/fly-with-a-spitfire')
      html = html + add_section('Spitfire Gift Ideas', '/category/20-community/spitfire-gift-ideas')
   html = html + '</ul>' 

   html = html + '<br><br>'
   html = html + '<h3>Reviews</h3>'
   html = html + '<ul>'
      html = html + add_section('Product News & Reviews', '/category/30-reviews')
   html = html + '</ul>'

   html = html + '<br><br>'
   html = html + '<h3>History & Operations</h3>'
   html = html + '<ul>'
      html = html + add_section('Articles', '/category/40-history/articles')
      html = html + add_section('Spitfire in Service', '/category/40-history/spitfire-in-service')
   html = html + '</ul>'

   html = html + '<br><br>'
   html = html + '<h3>Reference</h3>'
   html = html + '<ul>'
      html = html + add_section('Camouflage & Markings', '/category/50-reference/camouflage-markings')
      html = html + add_section('Variants & Technology', '/category/50-reference/variants-technology')
   html = html + '</ul>'

   html = html + '<br><br>'
   html = html + '<h3>Media Library</h3>'
   html = html + '<ul>'
      html = html + add_section('Historic Photographs', '/category/60-photos/historic')
      html = html + add_section('Videos', '/category/60-photos/video')
      html = html + add_section('Photo Galleries', '/category/60-photos/galleries')
   html = html + '</ul>'

   html = html + '<br><br>'
   html = html + '<h3>Hobbies & Arts</h3>'
   html = html + '<ul>'
      html = html + add_section('Art & Memorabilia', '/category/70-hobbies/art-memorabilia')
      html = html + add_section('Scale Modelling', '/category/70-hobbies/modelling')
   html = html + '</ul>'
   
   document.write( html )
}


function location_bar( )
// Based on the URL of the current document, output a clickable path
// providing links to parent chapters of the current page.
{
	var html
	var href
	
	var url = document.URL
	var title = document.title

	html = ""
	html = html + "";
    html = html + "&raquo; <a href='/'>Home</a>"

	if (url.search("/blog/")>=0) 
	{
	   html = html + " &raquo; <a href='/blog/'>What's New?</a>"
       // RSS feed available in this section
       html = html + "&nbsp; | &nbsp;"		
	   html = html + "<a href='http://feeds2.feedburner.com/spitfiresite/blog'><img border=0 src='/styleguide/icon_rss.gif' width=14 height=14 align='top'></a>"
	}
	else if (url.search("/calendar/")>=0) 
	{
	   html = html + " &raquo; <a href='/calendar/'>Event Calendar</a>"
	}
	else if (url.search("/search/")>=0) 
	{
	   html = html + " &raquo; <a href='/search/'>Search Site</a>"
	}
	else if (url.search("/history/")>=0) 
	{
	   html = html + " &raquo; <a href='/history/'>History and Operations</a>"
	   if (url.search("/history/articles/")>=0) 
	   {
	      html = html + " &raquo; <a href='/history/articles/'>Articles</a>"
	   }
	   else if (url.search("/history/spitfire-in-service/")>=0) 
	   {
	      html = html + " &raquo; <a href='/history/spitfire-in-service/'>Spitfire in Service</a>"
	   }
    }
	else if (url.search("/reference/")>=0) 
	{
	   html = html + " &raquo; <a href='/reference/'>Reference</a>"
	   if (url.search("/reference/camouflage-markings/")>=0) 
	   {
	      html = html + " &raquo; <a href='/reference/camouflage-markings/'>Camouflage & Markings</a>"
	   }
	   else if (url.search("/reference/variants-technology/")>=0) 
	   {
	      html = html + " &raquo; <a href='/reference/variants-technology/'>Variants & Technology</a>"
	   }
	}   
	else if (url.search("/photos/")>=0) 
	{
	   html = html + " &raquo; <a href='/photos/'>Media Library</a>"
	   if (url.search("/photos/historic/")>=0) 
	   {
	      html = html + " &raquo; <a href='/photos/historic/'>Historic Photographs</a>"
          // RSS feed available in this section
          html = html + "&nbsp; | &nbsp;"		
	      html = html + "<a href='/photos/historic/rss.xml'><img border=0 src='/styleguide/icon_rss.gif' width=14 height=14 align='top'></a>"
	   }
	   else if (url.search("/photos/galleries/")>=0) 
	   {
	      html = html + " &raquo; <a href='/photos/galleries/'>Photo Galleries</a>"
	   }
	   else if (url.search("/photos/video/")>=0) 
	   {
	      html = html + " &raquo; <a href='/photos/video/'>Videos</a>"
          // RSS feed available in this section
          html = html + "&nbsp; | &nbsp;"		
	      html = html + "<a href='/photos/video/rss.xml'><img border=0 src='/styleguide/icon_rss.gif' width=14 height=14 align='top'></a>"
	   }
	}   
	else if (url.search("/reviews/")>=0) 
	{
	   html = html + " &raquo; <a href='/hobbies/'>Hobbies & Arts</a>"
	   html = html + " &raquo; <a href='/reviews/'>Product News & Reviews</a>"
	}
	else if (url.search("/hobbies/")>=0) 
	{
	   html = html + " &raquo; <a href='/hobbies/'>Hobbies & Arts</a>"
	   if (url.search("/hobbies/modelling/")>=0) 
	   {
	      html = html + " &raquo; <a href='/hobbies/modelling/'>Scale Modelling</a>"
	   }
	   else if (url.search("/hobbies/art-memorabilia/")>=0) 
	   {
	      html = html + " &raquo; <a href='/hobbies/art-memorabilia/'>Art & Memorabilia</a>"
          // RSS feed available in this section
          html = html + "&nbsp; | &nbsp;"		
	      html = html + "<a href='/hobbies/art-memorabilia/rss.xml'><img border=0 src='/styleguide/icon_rss.gif' width=14 height=14 align='top'></a>"
	   }
	}   
	else
	{
		// Home page

		// RSS feed "for the entire site".
        html = html + "&nbsp; | &nbsp;"		
		html = html + "<a href='http://feeds2.feedburner.com/spitfiresite/blog'><img border=0 src='/styleguide/icon_rss.gif' width=14 height=14 align='top'></a> <a href='http://feeds2.feedburner.com/spitfiresite/blog'>RSS Feed</a>"
		
    }

	// del.icio.us
    // html = html + "&nbsp; | &nbsp;"		
	// href = encodeURI( "http://del.icio.us/post?url=" + url + "&title=" + title )
	// html = html + " <a href='" + href + "'>del.icio.us</a>"
	// digg		
	// href = encodeURI( "http://www.digg.com/submit" )
	// html = html + " &nbsp;<a href='" + href + "'><img border=0 src='/styleguide/icon_digg.gif' width=14 height=14 align='top'></a> <a href='" + href + "'>Digg</a>"
	// google
	// href = encodeURI( "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + url + "&title=" + title )
	// html = html + " &nbsp;<a href='" + href + "'><img border=0 src='/styleguide/icon_google_bookmark.gif' width=14 height=14 align='top'></a> <a href='" + href + "'>Google</a>"

	// Bookmark this page (IE only)
    // var ver = navigator.appName
    // var num = parseInt(navigator.appVersion)
    // if ((ver == "Microsoft Internet Explorer") && (num >= 4)) 
    // {
	//   html = html + "&nbsp; | &nbsp;"		
    //   html = html + '<a href="javascript:window.external.AddFavorite(document.URL,document.title);"'
    //   html = html + ' onMouseOver=" window.status=' + "' '" + '; return true"'
    //   html = html + ' onMouseOut=" window.status=' + "' '" + '; return true"'
    //   html = html + '>Bookmark this page</a>'
    // }


	html = html + ""

    document.write( html )
	
	// email = name + "&#64;" + server
	// text = linktext;
	
	/* if ( linktext == '' )
	{
		text = email;
	}
	else if ( typeof(linktext) == "undefined" ) 
	{
		text = email;
	} */
	
}


function previous_next_bar( noOfParts )
// Based on the URL of the current document, 
// output a << previous | next >> links
// to preceding and following parts of a multi-part feature.
{
	var url;			// url of current page
	var index;		
	var partNo;			// part number for the current page
	var noOfDigits;		// length of the part number as embedded in the url
	var prevPartNo;		// part number for the preceding page
	var prevUrl;		// url of a preceding page
	var prevLabel;		// link text for a preceding page
	var nextPartNo;		// part no. of the subsequent page	
	var nextUrl;		// url of a subsequent page
	var nextLabel;		// link text for a subsequent page
	
	// handle omitted function parameter(s)
	if (typeof noOfParts == 'undefined') {
		noOfParts = 1;
	}
	noOfDigits = noOfParts.toString().length;
	
	// search for the ending part of the current url
	url = window.location.href;
	index = url.lastIndexOf( ".htm" );

	if (index >= 0) {
		// extract the part number form the end of the current url and remove any leading zeroes.
		partNo = url.slice( index - noOfDigits, index );
		while (partNo.slice( 0, 1) == '0') {
			partNo = partNo.slice( 1, partNo.length);
		}
		partNo = parseInt( partNo );
		
		if (partNo >= 1 && partNo <= noOfParts) {
			document.write( '<p class="navigation">' );
			if (partNo > 1) {
				prevPartNo = partNo - 1;
				prevLabel = 'Part ' + prevPartNo;
				prevPartNo = '00000' + prevPartNo.toString()
				prevPartNo = prevPartNo.slice( prevPartNo.length - noOfDigits, prevPartNo.length );
				prevUrl = url.slice( 0, index - noOfDigits ) + prevPartNo + url.slice( index, url.length );
				document.write( '&laquo; Return to ' + prevLabel.link( prevUrl ) );
			}
			if (partNo > 1 && partNo < noOfParts) {
				document.write( " &nbsp;|&nbsp; " );
			}
			if (partNo < noOfParts) {
				nextPartNo = partNo + 1;
				nextLabel = 'Part ' + nextPartNo;
				nextPartNo = '00000' + nextPartNo.toString()
				nextPartNo = nextPartNo.slice( nextPartNo.length - noOfDigits, nextPartNo.length );
				nextUrl = url.slice( 0, index - noOfDigits ) + nextPartNo.toString() + url.slice( index, url.length );
				document.write( 'Continue to ' + nextLabel.link( nextUrl ) + ' &raquo;' );
			}
			document.write( '</p>' );
		}
		else {
			document.write( '' ); // do nothing
		}
	}
}
