/*
Title: allseries.js
Date: July, 2007
Author: ser 
Summary: This JavaScript file will be used by the Author Resource 
	pages. It's purpose is to dynamically generate a menu system
Globals: seriesArray - read from template, the data for the menu
         html_root - the root directory
	 path - location of files
	 series - name of series
	 lang_stored - amslatex or amstex with series abbr. prepended
	 file - name of file
*/

var series = "";
var lang_stored = "";
var file = "";
var path = "packages";
var html_root = "/authors";

// Create the dropdown list of series. To be run by onLoad.
function loadSeries(type)
{
	if (seriesArray.length<1)
	{
		document.getElementById('error').innerHTML = "<p>An error has occurred on the page.  Please try again later.</p>";
		return;
	}
	var result = "";
	var label = "";
	var name = "";
	var match1 = /ams/;
	var match2 = /other/;
	var match3 = /generic/;
	var oGroup1 = document.getElementById('ams');
	if (type == 'journal') var oGroup2 = document.getElementById('other');
	var oGroup3 = document.getElementById('generic');
	var oSelect = document.getElementById('series');
	for (thisSeries in seriesArray)
	{
		var oOption= document.createElement('option');
		result = thisSeries.split("|");
		name = result[0];
                label = result[1];
		oOption.value = thisSeries;
		oOption.innerHTML = name;
		if (match1.exec(label))
		{
			oGroup1.appendChild(oOption);
		}
		else if (match2.exec(label))
		{
			if (type == 'journal') oGroup2.appendChild(oOption);
		}
		else if (match3.exec(label))
		{
			oGroup3.appendChild(oOption);
		}

		oSelect.appendChild(oGroup1);
		if (type == 'journal') oSelect.appendChild(oGroup2);
		oSelect.appendChild(oGroup3);
			
	}
}

// Read in a series or lang to generate lists.
function makeMenu(option,value)
{
        if (option == 'series')
        {
                series = value;

		var htmlStart = "<li><span class=\"individualFile\">";
		var htmlEnd = "</span></li>";
		var langList = '';

                // Clear the current lang and files menus.
                document.getElementById('lang').innerHTML = "";
		document.getElementById('files').innerHTML = "";

		count = 0;
                for (thislang in seriesArray[series])
                {
		        result = thislang.split("|");
			if (result[1] == 'amslatex')
			{
				// they wanted to change what was displayed on screen
				result[1] = 'latex';
			}
                        lang = result[1];
			langLink = "<a href=\"javascript:makeMenu('lang','"+thislang+"')\">"+lang+"</a>";
			langList += htmlStart+langLink+htmlEnd;
			count++;
                }
		document.getElementById('lang').innerHTML = document.getElementById('lang').innerHTML + langList;

                // If we only have only one lang, auto generate the list of files.
                if (count == 1)
                {
                        makeMenu('lang',thislang);
                }
	}
	else if (option == 'lang')
	{
		//Clear the current files menu
		document.getElementById('files').innerHTML = "";

		lang_stored = value;

		var fileList = '';
		//var fileName;
		//var linkName;
		var fileLink;
		var fileDisplay

                var result = lang_stored.split("|");
		var seriesAbbr = result[0];
                var lang = result[1];

		var filePre = "<img src=\"/authors/images/winzip-icon.gif\" alt=\"Download zip file\" width=\"33\" height=\"35\" hspace=\"4\" vspace=\"4\" border=\"0\" align=\"left\" />";
		filePre += "<a href=\""+html_root+"/"+path+"/"+seriesAbbr+"/"+lang+"/"+seriesAbbr+"_"+lang+".zip\" title=\"Download this file\">";
		filePre += "Click to download the zip file containing all the author files listed below</a> </p><br clear=\"all\"/>";
		filePre += "<p>If you don't have a zip application, are unable to obtain one or have difficulties ";
		filePre += "downloading this .zip file, you may download one or all of the individual files that ";
      		filePre += "appear below. <a href=\"/jourhtml/ziphelp.html\" target=\"_blank\">Need help with the .zip file?</a></p>";
		filePre += "<p>";
		filePre += "<div class=\"fileDownload\">";
		filePre += "<p><img src=\"/authors/images/icon_textfile.gif\" alt=\".\" width=\"37\" height=\"33\" align=\"left\"";
		filePre += " /><strong>Alternatively, you may download files individually</strong>";
		filePre += "<ul>";

		var filePost = "</ul></div>";

		for (thisfile in seriesArray[series][lang_stored])
		{
			fileLink = '';
			file = seriesArray[series][lang_stored][thisfile]
			//result = file.split("|");
			//fileName = result[0];
			//linkName = result[1]; 
			//if (linkName == null) {
			//	linkName = fileName;
			//}
			fileLink = "<li><span class=\"individualFile\"><a href=\""+html_root+"/"+path+"/"+seriesAbbr+"/"+lang+"/"+file+"\">"+file+"</a></span></li>";
			fileList += fileLink;
		}
	 	fileDisplay = filePre+fileList+filePost;
		document.getElementById('files').innerHTML = fileDisplay; 
	}
}

