/*
Title: allissues.js
Date: May, 2007
Author: jrh
Summary: This JavaScript file will be used by the All Issues
	pages for all the Journals distributions. It's purpose 
	is to dynamically generate a menu system from an array
	of data passed in my JRNL_TOOLBAR_NAV.pm called issueArray.
Constants:	TABLE_FORMAT - boolean - format for table structure
Globals:	issueArray - read from template, the data for the menu
		html_root - read from template, the root directory
		path - read from template, the distribution
		toc_name - read from template, the html page name
		year - the issue year
		volume - the issue volume number
		issue - the issue number
		rowStart - html to add to start of each row
		rowEnd - html to add to end of each row
*/

var TABLE_FORMAT = 1;

var year = "";
var volume = "";
var issue = "";

var specialCaseNames = new Array;
// ['journal-year-volume-issue'] = 'issue display name'
specialCaseNames['bull-1921-27-09'] = '9-10';
specialCaseNames['bull-1922-28-01'] = '1-2';
specialCaseNames['bull-1924-30-01'] = '1-2';
specialCaseNames['bull-1924-30-03'] = '3-4';
specialCaseNames['bull-1924-30-05'] = '5-6';
specialCaseNames['bull-1924-30-09'] = '9-10';
specialCaseNames['bull-1925-31-01'] = '1-2';
specialCaseNames['bull-1925-31-03'] = '3-4';
specialCaseNames['bull-1925-31-05'] = '5-6';
specialCaseNames['bull-1925-31-09'] = '9-10';

var rowStart = "<br />";
var rowEnd = "";

if (TABLE_FORMAT) {
	rowStart = "<tr><td>";
	rowEnd = "</td></tr>";
}

// Create the dropdown list of years. To be run by onLoad.
function loadYears() {
	count = 0;
	for (thisYear in issueArray) {
		document.getElementById('year').options[count] = new Option(thisYear,thisYear);
		count++;		
	}

    // Special Case, arrrrr!
    // mosc only has one volume per year and one issue per volume.
    if (path == "mosc") {
        yearData = "";
        for (thisYear in issueArray) {
            // This works since there is only one volume per year.
            for (thisVolume in issueArray[thisYear]) { };

            yearLink = "<a href=\""+html_root+"/"+path+"/"+thisYear+"-"+thisVolume+"-00/"+toc_name+"\">"+thisYear+"</a>";
            yearData += rowStart+yearLink+rowEnd;
        }

        document.getElementById('yearData').innerHTML = yearData;
    }
}

// Read in a year or volume and generate the menus.
function makeMenu(option,value) {
	if (option == 'year') {
		year = value;

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

		count = 0;
		volumeMenu = '';
		for (thisVolume in issueArray[year]) {                
            volumeName = thisVolume;

            if (volumeName < 10 && volumeName.length > 1) {
                volumeName = volumeName.substring(1);
            }

			// Volumes with no issues.
			if (issueArray[year][thisVolume] == '-1') {
				volumeLink = "<a href=\""+html_root+"/"+path+"/home-"+year+".html\">"+volumeName+"</a>";
				volumeMenu += rowStart+volumeLink+rowEnd;				
			}
            // Volumes with only a '00' issue.
            else if (issueArray[year][thisVolume] == '00' && count == 0) {
				volumeLink = "<a href=\""+html_root+"/"+path+"/"+year+"-"+thisVolume+"-00/"+toc_name+"\">"+volumeName+"</a>";
				volumeMenu += rowStart+volumeLink+rowEnd;	
            }
            // Normal Volumes with issues.
			else {
				volumeLink = "<a href=\"javascript:makeMenu('volume','"+thisVolume+"')\">"+volumeName+"</a>";
				volumeMenu += rowStart+volumeLink+rowEnd;
				count++;	
			}
		}

		// If we only have one volume, auto generate the list of issues.
		if (count == 1) {
			volumeMenu = rowStart+volumeName+rowEnd;
			makeMenu('volume',thisVolume);
		}

		document.getElementById('volume').innerHTML = document.getElementById('volume').innerHTML + volumeMenu;
	}
	else if (option == 'volume') {
		volume = value;
		document.getElementById('issue').innerHTML = "";

		issueMenu = '';
		for (thisIssue in issueArray[year][volume]) {
			issue = issueArray[year][volume][thisIssue];
			issueLink = issue;
            issueName = issue;
			if (year != '' && volume != '' && issue != '') {	
                if (issue == "00") {
                    issueName = "Miscellaneous";
                }

                // Handle Special Case issue names.
                if (specialCaseNames[path + '-' + year + '-' + volume + '-' + issue]) {
                    issueName = specialCaseNames[path + '-' + year + '-' + volume + '-' + issue];
                }

                // Drop preceding zeros, ie convert issueName "01" to "1"
                if (issueName < 10 && issueName.length > 1) {
                    issueName = issueName.substring(1);
                }

				issueLink = "<a href=\""+html_root+"/"+path+"/"+year+"-"+volume+"-"+issue+"/"+toc_name+"\">"+issueName+"</a>";
			}

            if (issueName == "Miscellaneous") {
                issueMenu = rowStart + issueLink + rowEnd + issueMenu;
            }
            else {
                issueMenu = issueMenu + rowStart + issueLink + rowEnd;
            }

			//issueMenu += rowStart+issueLink+rowEnd;
		}

		document.getElementById('issue').innerHTML = document.getElementById('issue').innerHTML + issueMenu;
	}
}
