



var const_defaultFontPixelSize = 15;

var const_additionalWidthValue = 10; //firefox y opera con 19 px

var const_minStringSize = 10;

var redimUl = 0;


function ul_resizing(){

    var elems = document.getElementsByClassName('menu_horiz');

    for(var inty = 0 ; inty < elems.length; inty ++) {
        
        anElems = elems[inty];


        var liFontSize = getcss('.'+anElems.parentNode.className, 'fontSize');

        if( liFontSize){

            try {
                liFontSize = liFontSize.extractIntegers();
            }catch(e){
             alert('va al catch: ' + liFontSize);
                liFontSize = liFontSize.match(/em/) ? const_defaultFontPixelSize: liFontSize;
            }
//             alert(liFontSize);
            liFontSize = liFontSize ? liFontSize: const_defaultFontPixelSize;
            //alert('final: ' + liFontSize);
        }else{
            //alert('va al default' + liFontSize);
            liFontSize = const_defaultFontPixelSize;
        }
        
        var thisLiVal = liFontSize;

        liFontSize = (parseInt(liFontSize) / 2)+1;
        liFontSize+=1;
        if(browser.isIE){
//             liFontSize+=1;
        }else{
//             liFontSize--;
        }

        var ulBro = anElems.firstChild;

        var lastOne = false;

        var ulSizes = new Array();

        var countUl = 0;

        while (ulBro){

            if(ulBro.nodeName == 'UL'){

                var ulId = ulBro.id;

                if(!ulId || ulId ==''){
                    ulId = 'KhF_' + Math.random();
                    ulBro.id=ulId;
                }
                countUl++;
                var bro = ulBro.firstChild;

                while (bro){

                    if(bro.nodeName == 'LI'){
//                         alert('y va ..');
                        var liChild = bro.firstChild;

                        var myId = 'tx_'+ Math.random();

                        bro.id= myId;

                        while(liChild){
                            if(liChild.nodeName == 'A'){


                                var spamChild = liChild.firstChild;
                                var linkFontSize = liChild.style.fontSize;
                                

//                                 alert(liFontSize);

//                                 alert(liFontSize + '  -  ' + linkFontSize);
                                while(spamChild){
                                    if(spamChild.nodeName == 'SPAN' ){
//                                         alert('r');
//                                         spamChild.id='spamy_F'+Math.random;
//                                         var liFontdze = $(spamChild.id).getStyle('font-size');
// 
//                                         alert(liFontdze + ' ' + liFontSize);
                                        var stringLong = spamChild.firstChild.data.split('').length;
        //                                 alert(spamChild.nodeName + ' ' + spamChild.firstChild.data + ' ' + stringLong+ ' ' + linkFontSize+' '+ liFontSize);

                                        if(stringLong > const_minStringSize) {
                                            redimUl = liFontSize *  stringLong;
//                                             alert( redimUl + ' '+ const_minStringSize + ' ' + liFontSize);
                                        }else{
                                            redimUl = liFontSize * const_minStringSize;
                                        }
  
                                        redimUl +=const_additionalWidthValue;

                                        try  {
                                            spamChild.style.width= redimUl +'px';
                                        }catch(e){
                                            alert(e + ' '+ e.message + '    '+redimUl);
                                        }
    //                                     alert(liFontSize + ' ' + redimUl);
                                    }else if(spamChild.nodeName == '#text' && spamChild.data != ''){
                                        var myStringy = spamChild.data;
                                        myStringy = myStringy.replace(/\s+/, ' ');
                                        var stringLong = myStringy.split('').length;
        //                                 alert(spamChild.nodeName + ' ' + spamChild.firstChild.data + ' ' + stringLong+ ' ' + linkFontSize+' '+ liFontSize);

                                        if(stringLong > const_minStringSize) {
                                            redimUl = liFontSize *  stringLong;
                                        }else{
                                            redimUl = liFontSize * const_minStringSize;
                                        }
                                        redimUl+= const_additionalWidthValue;
    //                                     liChild.style.width= redimUl +'px';
//                                         alert( redimUl + ' ' + stringLong + ' ('+myStringy+')' )
                                    }


                                    spamChild = spamChild.nextSibling;
    
                                }

                                liChild.style.width= redimUl +'px';

//                                   alert('li: '+liChild.style.width);
                            }
                            liChild = liChild.nextSibling;
                        }
                        var myUlChilds = bro.getElementsByTagName('UL');
                        for(var iac = 0; iac < myUlChilds.length; iac++){

//                             myUlChilds[iac].style.width= redimUl +'px';


//                             myUlChilds[iac].style.paddingLeft='3px';
                        }

                        bro.style.width= redimUl+'px';

                        ulSizes.push(redimUl);
                    }


                    bro = bro.nextSibling; /* tamano del LI*/
                }

                ulBro.style.width= ( parseInt(redimUl) -3 ) +'px';

//                 ulBro.style.background='yellow';

                redimUl = 0;

                lastOne = ulBro;

            }

            ulBro = ulBro.nextSibling;
        }

        var ulSizeFinally = 0;

        for(var i = 0; i < ulSizes.length; i++){
            ulSizeFinally += parseInt(ulSizes[i]);
        }
        elems[inty].style.padding = '0px';
        elems[inty].style.margin = '0px';

        if(inty==0){
//             elems[inty].style.background='green';
        }else{
//             elems[inty].style.background='pink';
        }

        if(!browser.isIE){
            ulSizeFinally = parseInt(ulSizeFinally) + (4 * ulSizes.length);

        }else if(browser.isIE){
            if(browser.isAdmin){
             ulSizeFinally +=13 + (countUl *2);
            }
            ulSizeFinally = parseInt(ulSizeFinally) + (6 * ulSizes.length);

        }

        elems[inty].style.width = ulSizeFinally+'px';
        
//          alert(elems[inty].style.width + ' es el final de este div ');
        if(lastOne ){
            if(browser.isNS){
                /* Horrible arreglo para que el menu no joda al elemento de abajo, dependiente del tamno de la fuente */
                thisLiVal = parseInt(thisLiVal);
                switch (thisLiVal){
                    case 9:
                    thisLiVal +=15;
                    break;
                    case 10:
                    thisLiVal +=16;
                    break;
                    case 11:
                    thisLiVal +=17;
                    break;
                    case 12:
                    thisLiVal +=18;
                    break;
                    case 13:
                    thisLiVal +=19;
                    break;
                    case 14:
                    thisLiVal +=20;
                    break;
                    case 15:
                    thisLiVal +=21;
                    break;
                    case 16:
                    thisLiVal +=22;
                    break;
                    case 17:
                    thisLiVal +=23;
                    break;
                    case 18:
                    thisLiVal +=24;
                    break;
                }
                elems[inty].style.height= thisLiVal +'px';
            }else{
                elems[inty].style.height= (parseInt(thisLiVal) + 12 ) +'px';
            }
        }else{

        }

    }

}

addEvent(window,'load',ul_resizing);


