
var defaultMenuItemSize = [22, 185];
var defaultMenuItemOffset = [21, 0];
var defaultMenuItemLevel = [21, 0];
var defaultSubmenuLevel = [defaultMenuItemOffset[0]/3, defaultMenuItemSize[1]-5];
var defaultMenuBorder    = 1;
var defaultMenuShadow    = 2;
var defaultSubmenuColor  = new MenuColor(bgTitle,menuShadow,bgBanner,menuChoice);
var defaultMenuColor     = new MenuColor(bgTitle,menuShadow,bgBanner,menuChoice);  
var defaultMenuCSS       = new MenuCSS('clsCMON','clsCMOver');
var defaultExtraOffset   = newFormatItemoff([defaultMenuItemOffset[0]+9, defaultMenuItemOffset[1]]);

function MenuCSS (aON, aOVER){
    this.ON = aON;
    this.OVER = aOVER;
}

function MenuColor (aBorder, aShadow, aBgON, aBgOVER) /* colors */ 
{
    this.border = aBorder;
    this.shadow = aShadow;
    this.bgON   = aBgON;
    this.bgOVER = aBgOVER;
}

function MenuStyle (aBorder, aShadow, aColor, aCSS)
{
    this.border = aBorder;
    this.shadow = aShadow;
    this.color  = aColor;
    this.css    = aCSS;
}

function MenuFormat (aitemoff, aleveloff, aStyle, asize)
{
    this.itemoff = aitemoff;
    this.leveloff = aleveloff;
    this.style = aStyle;
    this.size = asize;

}

function newFormatItemoff (aitemoff)
{
    return new MenuFormat(aitemoff, null, null, null);
}

function newFormatLeveloff (aleveloff)
{
    return new MenuFormat(null, aleveloff, null, null);
}

function newFormatStyle (astyle)
{
    return new MenuFormat(null, null, astyle, null);
}

function newFormatSize (asize)
{
    return new MenuFormat(null, null, null, asize);
}


var allMenuItems = new Array;
var currentMenuItem = null;
function addToAllMenuItems(anitem)
{
    var i = allMenuItems.length;
    if (allMenuItems[anitem.id] == null) {
        allMenuItems[anitem.id] = anitem;
        allMenuItems[i] = anitem;
        return true;
    }
    return false; /* the menu item already exists... */
}


function LTCMenuItem(aid, acode, aurl, aformat)
{
    this.id     = aid;
    this.parent = null;   /* should be an LTCMenu */
    this.code   = acode;
    if (aurl.substr(0,4) == "http")
    {
        this.url = aurl;
    }
    else 
    {
        this.url = goUp+aurl;
    }
    this.format = aformat;
    this.submenu    = null;

    this.setSubmenu = function(asub){
        this.submenu = asub;
        if (this.submenu != null) {asub.parent = this;}
    }

}

function LTCMenu (aid, apos, aitemoff, aleveloff, asize, aborder, ashadow, acolor, acss)
{
    this.id     = aid;
    this.parent = null;
    this.pos    = apos;
    this.format = new MenuFormat(aitemoff, aleveloff, 
                                 new MenuStyle(aborder, ashadow, acolor, acss),
                                 asize);
    this.items  = new Array;

    this.addMenuItem = function(anitem){
        if (!addToAllMenuItems(anitem)) {
            alert("Menu item with id = "+anitem.id+" is a duplicate");
        }
        else {
           var i = this.items.length;
           this.items[i] = anitem;
           anitem.parent = this;
        }
    }

}

function makeQuickSubmenu(id)
{
    return new LTCMenu(id ,  /* id */
                    null,  /* position */
                    defaultMenuItemOffset,  /* item offset */
                    defaultSubmenuLevel,  /* level offset */
                    defaultMenuItemSize,   /* size */
                    defaultMenuBorder,       /* border width */
                    defaultMenuShadow,       /* shadow width */
                    defaultSubmenuColor,
                    defaultMenuCSS);
}



var ci = 0;
var si = 0; /* submenu item to add... */
var jsm = null;
var currentMenuItem = null;
var workingMenuDepth = 0;
function makeJSMenu(menu,currentID)
{

    currentMenuItem = currentID;
    ci = 0;
    si = 0;
    /* start with the basic formatting... */
    workingMenuDepth = 0;

    jsm = new Array();
    var zeroLevel = {"pos":null, "itemoff":null, "leveloff":null, "style":null, "size":null}
    jsm.push(zeroLevel);
    jsm[0].style = {"border":null, "shadow":null, "color":null, "css":null};
    jsm[0].style.color        = {"border":null, "shadow":null, "bgON":null, "bgOVER":null};
    jsm[0].style.css          = {"ON":null, "OVER":null}           ;

    jsm[0].pos                = menu.pos                           ;
    if (menu.format != null) {
       jsm[0].itemoff            = menu.format.itemoff                ;
       jsm[0].leveloff           = menu.format.leveloff               ;

       if (menu.format.style != null) {
          jsm[0].style.border       = menu.format.style.border           ;
          jsm[0].style.shadow       = menu.format.style.shadow           ;

          if (menu.format.style.color != null) {
             jsm[0].style.color.border = menu.format.style.color.border     ;
             jsm[0].style.color.shadow = menu.format.style.color.shadow     ;
             jsm[0].style.color.bgON   = menu.format.style.color.bgON       ;
             jsm[0].style.color.bgOVER = menu.format.style.color.bgOVER     ;
          }

          if (menu.format.style.css != null) {
             jsm[0].style.css.ON       = menu.format.style.css.ON           ;
             jsm[0].style.css.OVER     = menu.format.style.css.OVER         ;
          }
       }
       jsm[0].size               = menu.format.size                   ;
    }

    /* start working down the menu items... 
    /* We're gonna copy over all the main level items first, including
       whatever we need to do to drill down to the current item. 
       Then we'll go back and add the sub items...               
    */
    formatMenuItems(menu, 0)

    actualMenuDepth = Math.max(workingMenuDepth+10, minimumMenuDepth);

    return jsm;
}

function formatMenuItems(menu, indentAmount){

    for (var i=0;i<menu.items.length;i++) {

        ci++;
        mi = menu.items[i];
        jsm.push(formatMenuItem(mi, indentAmount)); /* set up code, url, format */
        workingMenuDepth = workingMenuDepth  + jsm[0].size[0];

        if (mi.submenu != null ) {
            if (isParentOfCurrentItem(mi)) {
                formatMenuItems(mi.submenu, indentAmount+2)
            }
            else {
                jsm[jsm.length-1].sub = addSubmenu(mi);
            }
        }
    }
}

function formatMenuItem(mi, indent)
{
    var indentImage = "<img src='"+goUp+"img/bluebar.gif' align=middle width=7 heigh=7 border=0>"
    /* var indentString = "";   for (var i = 1;i<=indent;i++) {indentString = indentString + "-"} */
    var indentString = "";   for (var i = 1;i<=indent/2;i++) {indentString = indentString + indentImage}
    if (indent > 0) {indentString = indentString + " ";}

    var fmi = {"code":null, "url":null, "format":null, "sub":null, "target":"_top"};

    var elipse = '';
    if (mi.submenu != null && !isParentOfCurrentItem(mi)) { elipse = ' ...'; }

    if (currentMenuItem != null && mi.id == currentMenuItem) {
        fmi.code = indentString+"<B>"+mi.code+elipse+"</B>"
    }
    else {
       fmi.code   = indentString+mi.code+elipse;
    }
    fmi.url    = mi.url;
    fmi.format = null;
    fmi.sub    = new Array();

    fmi.format = {'itemoff':null, 'leveloff':null, 'size':null, 'style':null};

    if (mi.format != null) {
        if (mi.format.itemoff != null) {
           fmi.format.itemoff = mi.format.itemoff;
           workingMenuDepth = workingMenuDepth + fmi.format.itemoff[0] - jsm[0].size[0]
        }

        fmi.format.leveloff = mi.format.leveloff;
        fmi.size = mi.format.size;

        if (fmi.size != null) {
            workingMenuDepth = workingMenuDepth + fmi.size[0] - jsm[0].size[0]
        }

        if (mi.format.style != null) {
            fmi.format.style = {"border":null, "shadow":null, "color":null};
            fmi.format.style.color = {"border":null, "shadow":null, "bgON":null, "bgOVER":null};
            fmi.format.style.css   = {"ON":null, "OVER":null};
            fmi.format.style.border = mi.format.style.border;
            fmi.format.style.shadow = mi.format.style.shadow;

            if (mi.format.style.color != null) {
               fmi.format.style.color.border = mi.format.style.color.border;
               fmi.format.style.color.shadow = mi.format.style.color.shadow;
               fmi.format.style.color.bgON   = mi.format.style.color.bgON  ;
               fmi.format.style.color.bgOVER = mi.format.style.color.bgOVER;
            }

            if (mi.format.style.css != null) {
                fmi.format.style.css.ON   = mi.format.style.css.ON;
                fmi.format.style.css.OVER = mi.format.style.css.OVER;
            }
        }
    }

    return fmi;
}


function isParentOfCurrentItem(mi)
{
    if (currentMenuItem == null) {
        return false;
    }
    var currentMIObject = allMenuItems[currentMenuItem];
    if (currentMIObject == null) {
        return false;
    }
    var parent = currentMIObject.parent;
    var answer = false;
    while (!answer && parent != null) {
        if (parent == mi) {
            answer = true;
        }
        parent = parent.parent;
    }
    return answer;
}

function addSubmenu(mi)
{
    var menu = mi.submenu;

    /* do the basic formatting first... */
    var fmi = new Array;
    fmi.push({"pos":null, "itemoff":null, "leveloff":null, "style":null, "size":null});


    fmi[0].pos                = menu.pos                           ;
    if (menu.format != null) {
       if (menu.format.itemoff != null) {fmi[0].itemoff = menu.format.itemoff ;}
       if (menu.format.leveloff != null) {fmi[0].leveloff = menu.format.leveloff ;}

       if (menu.format.style != null) {
           fmi[0].style              = {"border":null, "shadow":null, "color":null, "css":null};
           fmi[0].style.color        = {"border":null, "shadow":null, "bgON":null, "bgOVER":null}; 
           fmi[0].style.css          = {"ON":null, "OVER":null}           ;
          fmi[0].style.border       = menu.format.style.border           ;
          fmi[0].style.shadow       = menu.format.style.shadow           ;

          if (menu.format.style.color != null) {
             fmi[0].style.color.border = menu.format.style.color.border     ;
             fmi[0].style.color.shadow = menu.format.style.color.shadow     ;
             fmi[0].style.color.bgON   = menu.format.style.color.bgON       ;
             fmi[0].style.color.bgOVER = menu.format.style.color.bgOVER     ;
          }

          if (menu.format.style.css != null) {
             fmi[0].style.css.ON       = menu.format.style.css.ON           ;
             fmi[0].style.css.OVER     = menu.format.style.css.OVER         ;
          }
       }
       fmi[0].size               = menu.format.size                   ;
    }

    /* now do the menu items... */
    var si = 0;
    for (var i=0;i<menu.items.length;i++) {

        si++;
        mi = menu.items[i];
        fmi[si] = formatMenuItem(mi, 0); /* set up code, url, format */

        if (mi.submenu != null ) {
                fmi[si].sub = addSubmenu(mi);
        }
        else {
            fmi[si].sub = new Array();
        }
    }

    return fmi

}

function setSubmenuForMenuItem(parentID, submenu)
{
    var mi = allMenuItems[parentID];
    if (mi == null) {
        alert("Trying to add submenu to item "+id+". Item can't be found.");
    }
    else {
        mi.setSubmenu(submenu)
    }

}


function makeLTCMenu(menuName, position, currentID)
{
    return new COOLjsMenu(menuName, makeJSMenu(makeTopLevelMenu(position), currentID));
}
