// ---------------------------------------------
// Javascript Menu Generator

var M = new MainMenu();

MainMenu.prototype.CreatePlainMenu=MMCreatePlainMenu;
MainMenu.prototype.CreateMenu=MMCreateMenu;


// ---------------------------------------------
// Build Menu: 

function WriteMenu(){
	document.write(M.CreateMenu());
/*
// Browser is capable:

    if (compareBrowser()){
        document.write(M.CreateMenu());
    }

// Browser is not capable:

    else{
        document.write(M.CreatePlainMenu());
    }
*/
}

// ---------------------------------------------
// Establish menu object:

function MainMenu(){

    this.items = new Array();
    this.SubMenusShowing = false;
    this.CurrentExpandedMenu = null;
    this.toggle = true;
}


// ---------------------------------------------
// Write menu to browser: 


function MMCreateMenu(){

    var Output = '';
    var CurrFileName = GetCurrFileName(); 

//First create the overall container:

    Output += '<table cellspacing="0" cellpadding="20" border="0" width="450px" id="MainMenu" class="MainMenu"><tr>';
    var m=0;
    var i=0;
    while(i<this.items.length){

//Then, for each menu item, check if it's a main item or not:

//Mainitem Routine:

        if (!this.items[i].subitem){ 

// === === === It's a main item === === ===

//Open a new main item:

            Output += '<td onmouseover="OverItem(this,\'' + this.items[i].caption + '_' + i + '_grp\')"  onmouseout="OutItem(this, \'' + 
this.items[i].caption + '_' + i +  '_grp\') "  id="' + this.items[i].caption + '_' + i + '_tab" class="'; 


//Check whether this is the URL of the current page:

            if ((CurrFileName == this.items[i].url)||(checkSelect(i))){Output += 'MenuItem_select';}
            else{Output += 'MenuItem_out';}

//If it has a URL, make it link:

            if (this.items[i].url.length > 0){
                if (this.items[i].url.indexOf('http')!=-1){Output += '" onclick="document.location=\'' + this.items[i].url + '\'">';}
                else {Output += '" onclick="document.location=\'' + this.items[i].url + '\'">';}
            }
            else{Output += '" onclick="MenuClick(\'' + this.items[i].caption + '_' + i +  '_grp\')">';}

//Add the caption:
            Output += '<span id="' + i + '">' + this.items[i].caption + '</span><br />';

//Add SubItem Container if subitems exist :
            if (eval(i+1)<this.items.length) 
            {           
                if (this.items[eval(i+1)].subitem)
                {Output += '<div class="MenuSubItem_contain" style="display:none" id="' + this.items[i].caption + '_' + i + '_grp">';}
            }
        }


//Subitem Routine:
        if (this.items[i].subitem){ 


// === === === It's a subitem === === === 

//Check whether it's the current page:

            if (CurrFileName == this.items[i].url){ 

//if so, then create a non-visible non-linking item:
                Output += '<div id="' + this.items[i].caption + '_' + i +  '_sub" class="MenuSubItem_select">';
            }

//Otherwise, create a non-visible linking item:
            else{
                Output += '<div id="' + this.items[i].caption + '_' + i +  '_sub" class="MenuSubItem_out" onmouseover="OverSubItem(this)" onmouseout="OutSubItem(this)" onclick="document.location=\'' + this.items[i].url + '\'">';
            }

//Add the subtitle:
                Output += '<span id="' + i + '">' + this.items[i].caption + '</span></div>';
        }

//increment item count:
        i++;

//If next item is a main item, end current main and subitems selection:

        if (i<parseInt(this.items.length)){ 
            if (!this.items[i].subitem){
                if (this.items[i-1].subitem){
                        Output += '</div></td>';
                }

//Otherwise, just end main selection:

                else{Output += '</td>'}
            }       
        }
    }


// ---------------------------------------------
// Write the Menu to the Browser:

// [Debugger] -- display menu HTML:
// alert(Output+ '</td></tr></table>');  

    return Output + '</td></tr></table>';
}


// ---------------------------------------------
// Check for selected SubMenu items:

function checkSelect(k) {

    var CurrFileName = GetCurrFileName();
    k++;
    if (k<M.items.length)
    {
        while (M.items[k].subitem){     
            if (CurrFileName==M.items[k].url){return true;}
            k++;
        }
    }
    return false;
}


// ---------------------------------------------
// Set Menu Object Properties:

function MMAddItem(Caption, URL, SubItem){

    var Item = new MenuItem(Caption, URL, SubItem);
    this.items[this.items.length] = Item;
}

MainMenu.prototype.AddItem=MMAddItem;


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

function MenuItem(Caption, URL, SubItem){

    this.caption = Caption;
    this.subitem = SubItem;
    this.url = URL;
}


// ---------------------------------------------
// MouseOver/Out Event Handlers for Main Categories:

function OverItem(El, MenuId){ 

// [Debugger] --signal when activated:
// alert("onmouseover for:" + Id)

    El.className = 'MenuItem_over';

    if (M.SubMenusShowing){
        if (M.CurrentExpandedMenu != MenuId){
            Contract(M.CurrentExpandedMenu);
        }
                Expand(MenuId);
    }
}

// ---------------------------------------------
function OutItem(El, MenuId){ 

// [Debugger] --signal when activated:
// alert("onmouseout for:" + Id)

    var CurrFileName = GetCurrFileName();
    var selection = parseInt(MenuId.split('_')[1]);

    if ((CurrFileName == M.items[selection].url)||(checkSelect(selection)))
    {El.className = 'MenuItem_select';}
    else {El.className = 'MenuItem_out';}
}


// ---------------------------------------------
//General event to contract open submenu:

function clearAll(){

    if (M.toggle) { 
        Contract(M.CurrentExpandedMenu);
        M.SubMenusShowing = false;
    }
}


// ---------------------------------------------
// Onclick Event Handlers:

function MenuClick(Id){ 

// [Debugger] --signal when activated:
// alert("onclick for:" + Id)

//Contract the previous submenu:

    if (Id!='Any'){

        if ((M.CurrentExpandedMenu != Id)&&(M.CurrentExpandedMenu != null)){
            Contract(M.CurrentExpandedMenu);
        }

        M.SubMenusShowing = !M.SubMenusShowing;
        M.toggle=!M.toggle;

        if (!M.SubMenusShowing){Contract(Id);}

        else{
            Expand(Id);
        }
    }


// ---------------------------------------------
//Test General Menu Contract:

    else {clearAll(); M.toggle=true; }
}


//Submenu Rollovers:

function OverSubItem(El){El.className = 'MenuSubItem_over';}
function OutSubItem(El){El.className = 'MenuSubItem_out';}


// ---------------------------------------------
// Expand SubMenu:

function Expand(Id){

    var el = document.getElementById(Id);
    if (el != null){el.style.display='block';}
    M.CurrentExpandedMenu = Id;
}


// ---------------------------------------------
// Collapse SubMenu:

function Contract(Id){

    var el = document.getElementById(Id);
    if (el != null){el.style.display='none';}
}


// ---------------------------------------------
// Determine current opened file:

function GetCurrFileName(){

    var FileName = '';
    var CurrLoc = window.location.href;
    var i = CurrLoc.length-1;

    while ((CurrLoc.charAt(i) != '\\')&&(CurrLoc.charAt(i) != '/')){
        FileName = CurrLoc.charAt(i) + FileName;
        i--;
    }

    if (FileName == '') {FileName = 'index.htm';}
    return FileName;
}



// ---------------------------------------------
// Write disabled menu to browser:

function MMCreatePlainMenu(){

    var Output = '';

//First create the overall table:
    Output += '<table class="plainTable" border="0" align="left"><tr>';

//Create a buffer for the closing code:
    var CloseCode = '';
    var i=0;
    while(i<this.items.length){

//Then, for each menu item, check if it's a main item or not:
        if (this.items[i].subitem){

//It's a subitem:

//Open a row:
            Output += '<tr><td class="subitem"><a class="subLinx" href="' + this.items[i].url + '">';

//Add the caption:
            Output += this.items[i].caption;

//Close the row:
            Output += '</a></td></tr>';
        }
        else{

//It's a main item, so:

//close the previous main item:
            Output += CloseCode;

//open a new main item:
            Output += '<td class="mainitem"><table border="0" cellspacing="0">';

//Open a row:
            Output += '<tr><td class="mainitem">'; 

//If it has a URL, make it link:
            if (this.items[i].url.length > 0){
                Output += '<a class="mainLinx" href="' + this.items[i].url + '">' + this.items[i].caption +'</a>';
            }

            else{
                Output += '<a class="linxHeader">' + this.items[i].caption + '</a>';
            }

//Close the cell:
            Output += '</td></tr>';     

//Set the close code for next time:
            CloseCode = '</table></td>';
        }
        i++;
    }

//Close the last main:
    Output += CloseCode;

//Close the main menu table:
    Output += '</tr></table>';


// Write the Menu to the Browser:

// [Debugger] -- display menu HTML:
// alert(Output);

    return Output;
}


// --------------------------------------------- 
//  Enter Menu Changes Here:
//  BEWARE OF IMPLICATIONS FOR GUI OF HOME PAGE ON SITE

/*
the javascript gives each item a sequential number (indicated by the commented number following each item below)
to make sure that each main item has a image shield the id javascript gives must appear in the open_style.css page for the table cell containing the main item image

So, if you add or delete an item from this listing:
 - change the number in comments at the end of each line in the listing of items on this page such that you end up with a sequential list
 - edit the id's in the block of code in the open_style.css file quoted below  such that the id for each main item contains the appropriate sequential number derived from the commented number at the end of each item in the list below

td#Programs_0_tab{
	background-image: url('../images/resizedshields/shield_1_full.gif');
	background-repeat: no-repeat;
	background-position: 5px 15px;
	height:90px;
}
td#Faculty_7_tab{
	background-image: url('../images/resizedshields/shield_2_full.gif');
	background-repeat: no-repeat;
	background-position: 5px 15px;
}
td#Courses_11_tab{
	background-image: url('../images/resizedshields/shield_3_full.gif');
	background-repeat: no-repeat;
	background-position: 5px 15px;
}
td#Resources_16_tab{
	background-image: url('../images/resizedshields/shield_4_full.gif');
	background-repeat: no-repeat;
	background-position: 4px 15px;
}
td#Workshop_17_tab{
	background-image: url('../images/resizedshields/shield_5_full.gif');
	background-repeat: no-repeat;
	background-position: 3px 15px;
}
td#Contact_18_tab{
	background-image: url('../images/resizedshields/shield_6_full.gif');
	background-repeat: no-repeat;
	background-position: 0px 15px;
}

*/

// Main Item:
M.AddItem('Programs', '', false); //0
// Sub-Items:
M.AddItem('Why Medieval Studies?', 'whyStudy.htm', true); //1
M.AddItem('Honours', 'program.htm#honours', true); //2
M.AddItem('Major/Double Major', 'program.htm#major', true); //3
M.AddItem('Minor/General', 'program.htm#minor', true); //4
M.AddItem('Scholarships and Bursaries', 'scholarship.htm', true); //5
M.AddItem('FAQ', 'faq.htm', true); //6

// Main Item:
M.AddItem('Faculty', '', false); //7
// Sub-Items:
M.AddItem('Staff', 'faculty.htm', true); //8
M.AddItem('Newsletter', 'newsletter.htm', true); //9
M.AddItem('Job Opportunities', 'jobs.htm', true); //10

// Main Item:
M.AddItem('Courses', '', false); //11
// Sub-Items:
M.AddItem('Study Abroad', 'abroad.htm', true); //12
M.AddItem('Offered This Winter', 'courses.htm', true); //13
M.AddItem('Offered This Summer', 'courses_summer.htm', true); //14
M.AddItem('Eligible Courses', 'courses_eligible.htm', true); //15

// Main Item:
M.AddItem('Resources', '', false); //16
// Sub-Items:
M.AddItem('Websites', 'resource_website.htm', true); //17
M.AddItem('Reading Room', 'resource_title.htm', true); //18
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Publications by Title', 'resource_title.htm', true); //19
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Publications by Author', 'resource_author.htm', true); //20
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Leeds Offprints', 'resource_offprint.htm', true); //21
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Speculum', 'resource_speculum.htm', true); //22
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Viator', 'resource_viator.htm', true); //23
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Variorum', 'resource_variorum.htm', true); //24
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Audio CD', 'resource_cd.htm', true); //25
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Course Packs', 'resource_coursepack.htm', true); //26
M.AddItem('&nbsp;&nbsp;&nbsp;&nbsp;Other', 'resource_other.htm', true); //27

// Main Item:
M.AddItem('Workshop', 'workshop_1.htm', false); //28

// Main Item:
M.AddItem('Contact', 'contact.htm', false); //29






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


