/*******************************************************
 * rollmenu.js                                         *
 *                                                     *
 * Menù javascript                                     *
 *                                                     *
 *******************************************************/

var captions,links,targets;
var voices,astable,divchar;
var clink,clinksel,csep;
var activeItem;

/////////////////////////////////////////////

// initiate the menu
function initMenu(mvoices,mastable,mdivchar){
    voices=mvoices;
    divchar=mdivchar;
    astable=mastable;
    captions=Array(voices);
    links=Array(voices);
    targets=Array(voices);
    activeItem=-1;
}

// set the css of items
function initCSS(mclink,mclinksel,mcsep){
    clink=mclink;
    clinksel=mclinksel;
    csep=mcsep;
}

// set the selected link active from the first time (default=-1 -> none)
function setActiveItem(num){
    activeItem=num;
}

// set the captions of every item
function setMenuCaption(num,caption){
    captions[num]=caption;
}

// first arg is the menu number, the others are the links
function setMenuLinks(){
    al=arguments.length;

    if (al<1) return;
    
    num=arguments[0];
    links[num]=new Array(al-1);
    for (var i = 0; i < arguments.length-1; i++)
        links[num][i]=arguments[i+1];
}

// first arg is the menu number, the others are the targets
function setMenuTargets(){
    al=arguments.length;

    if (al<1) return;

    num=arguments[0];
    targets[num]=new Array(al-1);
    for (var i = 0; i < arguments.length-1; i++)
        targets[num][i]=arguments[i+1];
}

function renderMenu(){
    var htmlCode,itemCss;
    htmlCode="";
    
    if (astable){
        htmlCode+="<table border=\"0\">";
    }
    
    for (var i=0;i<voices;i++){
        if (i==activeItem)
            itemCss=clinksel;
        else
            itemCss=clink;
            
        if (astable)
            htmlCode+="td class=\""+itemCss+"\">"+captions[i]+"</td>";
        else{
            htmlCode+="<a id=\"amenu"+i+"\" name='mylinktopmenu' class=\""+itemCss+"\" href=\"javascript:goMenuLink("+i+");\">";
            htmlCode+=captions[i]+"</a>";
            if (i<voices-1)
				if (divchar!="")
					htmlCode+="span class=\""+csep+"\">&nbsp;"+divchar+"</span>&nbsp;";
				else
					htmlCode+="&nbsp;&nbsp;";
        }
    }
    
    if (astable){
        htmlCode+="</table>";
    }

    document.write(htmlCode);
}

function swapActive(num){
    document.getElementById("amenu"+num).className=clinksel;
    if (activeItem!=-1 && activeItem!=num)
        document.getElementById("amenu"+activeItem).className=clink;

    activeItem=num;
}

function goMenuLink(num){
    swapActive(num);

    ls=links[num];
    tg=targets[num];
    
    for (var i=0;i<ls.length;i++)
        window.open(ls[i],tg[i]);
}
