
var arrelrelativa = "/llengcat/"; 
var imgrelpath = arrelrelativa + "/img/";    

// estructura que suporta la informacio previa per posteriorment generar els DIVS dins la pagina

var menus = [];
var submenus = [];
var menudiv = [];

function crear_menu_interior(id, desc, estil1, link, amplada, alcfila)
{
    var width = ((typeof amplada=='undefined' || amplada=='' || amplada==null)?'':Number(amplada));
    var tdheight = ((typeof alcfila=='undefined' || alcfila=='' || alcfila==null)?16:Number(alcfila));
    var dy = 0
    
    // estils = 'textGriOn/textgriOff/b9d/tg9d'
    // menu02/menuintoff/menulink/menutext
    estils = estil1 + "/menuintoff/menucolorlink/menucolortext"

    var i = menus.length;
    menus[i] = {'id':id, 'desc':escape(desc), 'estils':estils, 'link':link, 
                'dx':0, 'width':width, 'extra':0, 'tdheight':tdheight, 'dy':dy,
                'menu':0, 'tipusmenu':'i'};
    if(id!=null && id!='') {submenus[id] = []; }
    
    xControlMen(id,id+'Kapa',estils,500);
}

function crear_menu_historic(id, desc, link, despldx, amplada, extra, alcfila, vertical)
{
    var dx    = ((typeof despldx=='undefined' || despldx=='' || despldx==null)?0:Number(despldx)); //valor per defecte
    var width = ((typeof amplada=='undefined' || amplada=='' || amplada==null)?'':Number(amplada));
    var extra = ((typeof extra=='undefined' || extra=='' || extra==null)?0:Number(extra));
    var tdheight = ((typeof alcfila=='undefined' || alcfila=='' || alcfila==null)?16:Number(alcfila));
    var dy = 5

    estils = 'historicmenu/historicmenuoff/historiclink/historictext'
	
    var i = menus.length;
    menus[i] = {'id':id, 'desc':desc, 'estils':estils, 'link':link, 
                'dx':dx, 'width':width, 'extra':extra, 'tdheight':tdheight, 'dy':dy,
                'menu':1, 'tipusmenu':'h'};
    if(id!=null && id!='') {submenus[id] = [];}
    
    xControlMen(id,id+'Kapa',estils,200);
}


function crear_menu(id, desc, nummenu, link, despldx, amplada, extra, alcfila, vertical)
{
    var dx    = ((typeof despldx=='undefined' || despldx=='' || despldx==null)?0:Number(despldx)); //valor per defecte
    var width = ((typeof amplada=='undefined' || amplada=='' || amplada==null)?92:Number(amplada));
    var extra = ((typeof extra=='undefined' || extra=='' || extra==null)?0:Number(extra));
    var tdheight = ((typeof alcfila=='undefined' || alcfila=='' || alcfila==null)?16:Number(alcfila));
    var dy = 5

    estils = 'menu' + nummenu + '/menuoff/menulink/menutext'
	        // 'textAzuOnFixe/textAzuOffFixe/b10Fixe/tg10Fixe'
	        // 'textRojOnFixe/textRojOffFixe/b10Fixe/tg10Fixe'
	        // 'textVerOnFixe/textVerOffFixe/b10Fixe/tg10Fixe'    
  
    var i = menus.length;
    menus[i] = {'id':id, 'desc':desc, 'estils':estils, 'link':link, 
                'dx':dx, 'width':width, 'extra':extra, 'tdheight':tdheight, 'dy':dy, 
                'menu':1, 'tipusmenu':'g'};
    if(id!=null && id!='') {submenus[id] = [];}
    
    xControlMen(id,id+'Kapa',estils,200);
}


function crear_submenu(idmenu, id, desc, link)
{

    if(idmenu == null || idmenu == '') {return;}
    if(typeof submenus[idmenu] == 'undefined') { alert('Menu no definit '+ idmenu); return;}
    if((desc == null || desc == '') && ( link == null || link == '' )) {return;}
 
    var i = submenus[idmenu].length;
    submenus[idmenu][i] = {'id':id, 'desc':escape(desc), 'link':link};
}



// en execucio...

function menu_td_onmouseover(td,id)
{
    onmouseover_menu(td,id,1)
}
function menu_td_onmouseout(td,id)
{
    onmouseout_menu(td,id,1)
}

function menu_link_onmouseover(td,id)
{
    onmouseover_menu(td,id,0)
}
function menu_link_onmouseout(td,id)
{
    onmouseout_menu(td,id,0)
}


function menu_onmouseover(td, id, istd)
{
    // validacio si existeix
    var i;
    for(i=0;i<menus.length;i++) {
        if(menus[i]['id'] == id) { break; }
    }
    if(i>=menus.length) { return; }

    
    if(typeof istd == "undefined") {
        // comparem td, imatges i links
        istd = (typeof td.align!="undefined") && (typeof td.src=="undefined")
    } 
    
    dx = menus[i]['dx']
    if(!istd) { if(menus[i]['tipusmenu']=='i') {dx+=6;} }

    setLyr(td,id+'Kapa',-dx);
    if(!istd) {
        s = 'xCM'+id+'.showOpc(\'opc1'+id+'\',\'opc1'+id+'Enc\',0)'
    } else {
        s = 'xCM'+id+'.actvOpc(\'opc1'+id+'\',\'opc1'+id+'Enc\',0)'
    }
    eval(s)
    
    // sample:
    //   setLyr(this,'TreballarKapa',-65);
    //   xCMpc4.actvOpc('opc1Treballar','opc1TreballarEnc',0)
}

function menu_onmouseout(td, id, istd)
{
    // validacio si existeix
    var i;
    for(i=0;i<menus.length;i++) {
        if(menus[i]['id'] == id) { break; }
    }
    if(i>=menus.length) { return; }
    
    if(typeof istd == "undefined") {
        // comparem td, imatges i links
        istd = (typeof td.align!="undefined") && (typeof td.src=="undefined")
    }

    if(!istd) {
        s = 'xCM'+id+'.showOpc(\'opc1'+id+'\',\'opc1'+id+'Enc\',1)'    
    } else {
        s = 'xCM'+id+'.actvOpc(\'opc1'+id+'\',\'opc1'+id+'Enc\',1)'
    }
    eval(s)
    
    // sample:
    //   xCMpc4.actvOpc('opc1Treballar','opc1TreballarEnc',1)
}


function inserir_menus()
{
    set_arrelrelativa();
    
    var i, is, dv;
    // per cada menu inserim un DIV amb les opcions del submenu
    for(i=0;i<menus.length;i++) {
        if(menus[i]['menu']) {
            dv = genera_div(i);
        } else {
            dv = genera_div(i); //_interior
        }
        menudiv[i] = dv
        document.write(dv + "\n");
    }
}

// //xivato on line
//javascript:void(document.formalvi.ta.value=menudiv[0]);<br>
//<form name=formalvi>
//<textarea name=ta cols=80 rows=7></textarea><br>
//</form>

// ********************************* //
// ****   FUNCIONS AUXILIARS    **** //
// ********************************* //

// genera el DIV del menu i (segons estructura, podem accedir a submenus)
function genera_div(i)
{
    var id = menus[i]['id'];
    var desc = unescape(menus[i]['desc']);    // &nbsp;&nbsp;Viure&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    var link = linkrelatiu(menus[i]['link'])
    var dy = menus[i]['dy']
    var width = menus[i]['width']
    var dx = menus[i]['dx']
    var extra = menus[i]['extra']
    var tdheight = menus[i]['tdheight']
    var estils = menus[i]['estils'].split('/');
    var td1nowrap = ""
    
    if(width == "nowrap") {width=''; td1nowrap=" nowrap ";}
    var width2 = width + dx
    var width3 = width + extra
    var colspan = 1
    
    var s = "",tr1="",td1="",td2="", td3="",td4="",td5="",td6="";
    
    s = ""
    var layers = (document.layers?1:0)
    if(layers) {
        s += "<LAYER id=\"" + id + "Kapa\"  visibility=hide >" + "\n"
    } else {
        s += "<DIV id=\"" + id + "Kapa\" " + " style=\"LEFT: 0px; TOP: 0px; VISIBILITY: hidden; " + (width!=""?"WIDTH: " + width2 + "px;":"") + " POSITION: absolute\">" + "\n"
    }


    // MAXIM DE MAXIM D'AMPLADA DELS MENUS
    s += "<TABLE width=300 cellSpacing=0 cellPadding=0 summary=\"\" border=0><TR><TD>" + "\n"

    s += " <TABLE cellSpacing=0 cellPadding=0 summary=\"\" border=0" + (width!=""?" width=" + (dx+width+extra):"") + ">" + "\n"
    s += "  <TR>" + "\n"

    if(dx>0) {
    s += "    <TD width=" + dx + "><IMG height=5 alt=\"\"" + "\n"
    s += "         src=\"" + imgrelpath + "sp.gif\" width=" + dx + " border=0></TD>" + "\n"
    colspan++
    }    
    s += "    <TD" + (width!=""?" width=" + (width):"") + ">" + "\n"

    s += "      <table " + (width!=""?" width=" + (width):"") + " cellSpacing=0 cellPadding=0 border=0>" + "\n"
    if(dy>0) {
    s += "        <tr>" + "\n"
    s += "          <TD colspan=2 bgColor=#ffffff><IMG height=" + dy + " alt=\" \"" + "\n"
    s += "              src=\"" + imgrelpath + "sp.gif\" border=0></TD>" + "\n"
    s += "        </tr>" + "\n"
    }
    s += "        <tr>" + "\n"
    s += "          <TD class=" + estils[0] + " width=6><IMG height=1 alt=\"\" src=\"" + imgrelpath + "sp.gif\" width=6 border=0></TD> " + "\n" 
    s += "          <TD class=" + estils[0] + " id=opc1" + id + " " + td1nowrap + " " + "\n" 
    s += "           onmouseover=\"xCM" + id + ".actvOpc('opc1" + id + "','opc1" + id + "Enc',0)\" " + "\n"
    s += "           onmouseout=\"xCM" + id + ".actvOpc('opc1" + id + "Enc','opc1" + id + "Enc',1)\" " + "\n"
    s += "           height=16 " + (width!=""?" width=" + (width-6):"") + "><A class=" + estils[2] + " id=opc1" + id + "Enc"  + "\n"
    s += "              onmouseover=\"xCM" + id + ".showOpc('opc1" + id + "','opc1" + id + "Enc',0)\" " + "\n"
    s += "              onmouseout=\"xCM" + id + ".showOpc('opc1" + id + "Enc','opc1" + id + "Enc',1)\" " + "\n"
    s += "              href=\"" + link + "\">" + desc + "</A></TD>" + "\n"
    s += "        </tr>" + "\n"
    s += "      </table>" + "\n"

    s += "    </TD>" + "\n"
    if(extra>0) {
    s += "    <TD width=" + extra + "><IMG height=5 alt=\"\"" + "\n"
    s += "         src=\"" + imgrelpath + "sp.gif\" width=" + extra + " border=0></TD>" + "\n"
    colspan++
    }
    s += "  </TR>" + "\n"
    
    if(typeof submenus[id] == 'undefined') {
    } else {
    
        var subj;
        for(subj=0;subj<submenus[id].length;subj++) {
            //s += "subopcion(" + (subj+2) + ",'" + id + "','" + id + "','" +  estils[1] + "','" +  estils[3] + "','" +  submenus[id][subj]['desc'] + "','" +  submenus[id][subj]['link'] + "',true,null,"+ colspan + ","+ tdheight + ");" + "\n"
            indice = (subj+2)
            numCapa = id
            nomCapa = id
            stTD = estils[1]
            stA = estils[3]
            nomOp = submenus[id][subj]['desc']
            url = linkrelatiu(submenus[id][subj]['link']) + ""
            
            onmouse = ''
            trheight = tdheight
        
            strfila = ""; onmouse = "";
            if ((document.getElementById || document.all)) {
                onmouse = ' onmouseover=xCM'+numCapa+'.actvOpc("opc'+indice+nomCapa+'","opc'+indice+nomCapa+'Enc",0) onmouseout=xCM'+numCapa+'.actvOpc("opc'+indice+nomCapa+'","opc'+indice+nomCapa+'Enc",1)'
                
                strfila += '  <tr><td height=' + trheight + ' ' + (colspan>1?'colspan=' + colspan:'') + onmouse + ' id="opc'+indice+nomCapa+'" class="'+stTD+'">' + "\n"
                strfila += '    <table ' + (width!="" && colspan==1?" width=" + (width+dx+extra):"") + ' border="0" cellspacing="0" cellpadding="0" class="'+stA+'">' + "\n"
                strfila += '      <tr>' + "\n"
                strfila += '        <td width="6">' + "<IMG height=1 alt=\"\" src=\"" + imgrelpath + "sp.gif\" width=6 border=0>" + '</td>' + "\n"
                strfila += '        <td ' + (width!="" && colspan==1?" width=" + (width+dx+extra-6):"") + ' class="'+stA+'"><a href="'+url+'" id="opc'+indice+nomCapa+'Enc" class="'+stA+'">'+ unescape(nomOp)+'</a></td>' + "\n"
                strfila += '      </tr>' + "\n"
                strfila += '    </table>' + "\n"
                strfila += '</td></tr>' + "\n"
            } else if (document.layers) {
                onmouse = 'onmouseover=xCM'+numCapa+'.showOpc("opc'+indice+nomCapa+'","opc'+indice+nomCapa+'Enc",0) onmouseout=xCM'+numCapa+'.showOpc("opc'+indice+nomCapa+'","opc'+indice+nomCapa+'Enc",1)'
                
                strfila += '  <tr><td height=' + trheight + ' ' + (colspan>1?'colspan=' + colspan:'') + ' id="opc'+indice+nomCapa+'" class="'+stTD+'">'  + "\n"
                strfila += '    <table ' + (width!="" && colspan==1?" width=" + (width+dx+extra):"") + ' border="0" cellspacing="0" cellpadding="0" class="'+stA+'">' + "\n"
                strfila += '      <tr>' + "\n"
                strfila += '        <td width="6">' + "<IMG height=1 alt=\"\" src=\"" + imgrelpath + "sp.gif\" width=6 border=0>" + '</td>' + "\n"
                strfila += '        <td ' + (width!="" && colspan==1?" width=" + (width+dx+extra-6):"") + ' class="'+stA+'"><a href="'+url+'" id="opc'+indice+nomCapa+'Enc" class="'+stA+'"' + onmouse + '>'+unescape(nomOp)+'</a></td>' + "\n"
                strfila += '      </tr>' + "\n"
                strfila += '    </table>' + "\n"
                strfila += '  </td></tr>' + "\n"
            }
            s += strfila;
    
        }
        
        // sample:
        //   opcion(2,'Viure',3,'textVerOff','tg10',"Índex Viure",'http://www.gencat.net/viure/index.htm',true);
    }

    s += " </TABLE>" + "\n"
    s += "</TD></TR></TABLE>" + "\n"

    if(layers) {
        s += "</LAYER>" + "\n"
    } else {
        s += "</DIV>" + "\n"
    }
    
    return s;
}

var arrelrelativa_inicial = "";
var idlogo = "logogencat";
var imgpath = "img/logotip.gif";

function set_arrelrelativa()
{
    if(arrelrelativa_inicial=="") {arrelrelativa_inicial = arrelrelativa;}

    var logo = eval("document." + idlogo)
    if(typeof logo == "undefined") { 
	    //alert(' no logo '); 
	    window.status = "no logo";
	    return; 
	} else {window.status=''}
    var srclogo = logo.src;
    
    var loc = document.location.href;
    loc = loc.substr(srclogo.indexOf(imgpath))
    
    i = loc.indexOf("?")
    if(i>=0) {loc = loc.substring(0,i); }
    i = loc.indexOf("#")
    if(i>=0) {loc = loc.substring(0,i); }

    res = ""
    while( (i = loc.indexOf("/")) >=0) {
        res += "../"
        loc = loc.substr(i+1)
    }
    arrelrelativa = res    
    imgrelpath = "/llengcat/img/"; 
    

    // refresca relatiu a precarrega d'imatges per fer el rollover de gencat
    if (document.images) {
    	tamany_11_Off = new Image(17,16); tamany_11_Off.src= imgrelpath + "mt11_0.gif";
    	tamany_11_On = new Image(17,16); tamany_11_On.src= imgrelpath + "mt11_1.gif";
    	tamany_14_Off = new Image(17,16); tamany_14_Off.src= imgrelpath + "mt14_0.gif";
    	tamany_14_On = new Image(17,16); tamany_14_On.src= imgrelpath + "mt14_1.gif";
    	tamany_17_Off = new Image(17,16); tamany_17_Off.src= imgrelpath + "mt17_0.gif";
    	tamany_17_On = new Image(17,16); tamany_17_On.src= imgrelpath + "mt17_1.gif";
    }
              
}


function linkrelatiu(url)
{
    if(arrelrelativa_inicial.length == 0) { return url; }
    
    i = url.indexOf(arrelrelativa_inicial)
    if(i==0) {
        //if(url != "#" && url != "") {alert("rel " + url + "  " + url.substr(arrelrelativa_inicial.length))}
        return arrelrelativa + url.substr(arrelrelativa_inicial.length)
    } else {
        return url;
    }
}

function obrirajuda(fitxer)
{
  var w2 = window.open(fitxer,'obrirajuda','width=522,height=400,top=100,left=139,scrollbars=yes,resizable=yes');
  w2.focus();	
}


// si dubixem les imatges dins del modul alvimenu.js podem fer ruta relativa de les imatges...

// precarrega d'imatges per fer el rollover de gencat
if (document.images) {
	tamany_11_Off = new Image(17,16); tamany_11_Off.src= imgrelpath + "mt11_0.gif";
	tamany_11_On = new Image(17,16); tamany_11_On.src= imgrelpath + "mt11_1.gif";
	tamany_14_Off = new Image(17,16); tamany_14_Off.src= imgrelpath + "mt14_0.gif";
	tamany_14_On = new Image(17,16); tamany_14_On.src= imgrelpath + "mt14_1.gif";
	tamany_17_Off = new Image(17,16); tamany_17_Off.src= imgrelpath + "mt17_0.gif";
	tamany_17_On = new Image(17,16); tamany_17_On.src= imgrelpath + "mt17_1.gif";
}


function imatge_a11()
{
	if(typeof tamanyCSS == "undefined" || tamanyCSS == "") {
		document.write("<img src='" + imgrelpath + "mt11_1.gif' name='img11' width=17 height=16 alt='mitjà 11' border=0>");
	} else if (tamanyCSS == "tamany11") {
		document.write("<img src='" + imgrelpath + "mt11_1.gif' name='img11' width=17 height=16 alt='mitjà 11' border=0>");
	} else {
		document.write("<img src='" + imgrelpath + "mt11_0.gif' name='img11' width=17 height=16 alt='mitjà 11' border=0>");
	}
}				

function imatge_a14() 
{
	if(typeof tamanyCSS == "undefined" || tamanyCSS == "") {
		document.write("<img src='" + imgrelpath + "mt14_0.gif' name='img14'  width=17 height=16 alt='gran 14' border=0>");
	} else if (tamanyCSS == "tamany14"){
		document.write("<img src='" + imgrelpath + "mt14_1.gif' name='img14'  width=17 height=16 alt='gran 14' border=0>");
	} else{
		document.write("<img src='" + imgrelpath + "mt14_0.gif' name='img14'  width=17 height=16 alt='gran 14' border=0>");
	}
}

function imatge_a17()
{
	if(typeof tamanyCSS == "undefined" || tamanyCSS == "") {
		document.write("<img src='" + imgrelpath + "mt17_0.gif' name='img17'  width=17 height=16 alt='màxim 17' border=0>");
	} else if (tamanyCSS == "tamany17"){
		document.write("<img src='" + imgrelpath + "mt17_1.gif' name='img17'  width=17 height=16 alt='màxim 17' border=0>");
	} else{
		document.write("<img src='" + imgrelpath + "mt17_0.gif' name='img17'  width=17 height=16 alt='màxim 17' border=0>");
	}
}
