

/**
 * CLASS Menu
 */
function Menu(id){

    /*menu id*/
    this.id=id;

    /*cookie settings*/
    this.useCookie=false;
    this.expireDays=1000;

    /*images settings*/
    this.useImgOnClick=false;
    this.imgListSrc='';
    this.imgFolderOpenSrc='';
    this.imgFolderCloseSrc='';

    /**
   * Funkcia pre inicializaciu menu
   */
    function init_menu(){

        elMenu=document.getElementById(this.id);
        liList=elMenu.getElementsByTagName('li');

        //ak je nastavena cookie, rozbalim polozky menu
        if(this.useCookie){
            var activeBlocks = this.getCookie(this.id);

            activeBlocks=activeBlocks.split('*');
        }

        if(liList.length!=0){
            //vsetko co nie je potrebne, skryjem
            for(i=0; i<liList.length; i++){
                ulList=liList[i].getElementsByTagName('ul');
                if(ulList.length!=0){
                    for(x=0; x<ulList.length; x++){

                        //ak su pouzite cookies, zobrazim viditelne polozky
                        if(this.useCookie){
                            if(activeBlocks.indexOf(ulList[x].id) == -1){
                                ulList[x].style.display='none';
                                if(this.useImgOnClick){
                                    li=ulList[x].parentNode;
                                    imgList=li.getElementsByTagName('img');
                                    imgList[0].src=this.imgFolderCloseSrc;
                                }

                            }
                            else{
                                ulList[x].style.display='block';
                                if(this.useImgOnClick){
                                    li=ulList[x].parentNode;
                                    imgList=li.getElementsByTagName('img');
                                    imgList[0].src=this.imgFolderOpenSrc;
                                }
                            }
                        }
                        else{
                            //inak vsetko skryjem
                            ulList[x].style.display='none';
                            //ak je pouzity obrazok, nastavim rezim ,,zatvoreny,,
                            if(this.useImgOnClick){
                                li=ulList[x].parentNode;
                                imgList=li.getElementsByTagName('img');
                                imgList[0].src=this.imgFolderCloseSrc;
                            }
                        }
            
                    }
                }
                else{
                    if(this.useImgOnClick){
                        imgList=liList[i].getElementsByTagName('img');
                        imgList[0].src=this.imgListSrc;
                    }
                }
            }
        }
    }


    /**
   * Zobrazi blok mojho najblizsieho vnoreneho zoznamu
   */
    function showMyChildBlock(item){
        if(item!=null){
            ulList=item.getElementsByTagName('ul');
            if(ulList.length!=0){
        
                ulList[0].style.display='block';
            }
        }
    }

    /**
   * Skryje blok mojho najblizsieho vnoreneho zoznamu
   */
    function hideMyChildBlock(item){
        if(item!=null){
            ulList=item.getElementsByTagName('ul');
            if(ulList.length!=0){
                ulList[0].style.display='none';
            }
        }
    }


    /**
   * Zobrazenie kontextoveho menu
   */
    function showContextMenu(e, box, dataElId){
        //ziskam data pre menu
        //data su ulozene externe, mimo blok
        var elMenuData=document.getElementById(dataElId);

        var clickId=getMouseClickButtonId(e);

        //ak je id 1, kliklo sa pravym, skryjem menu
        if(clickId==1){
            this.hideContextMenu(box, dataElId);
            return;
        }//ak sa kliklo pravym, presuniem menu dalej
        else if(clickId==3){
    
            //ak nemam data, nemozem pracovat dalej
            if(elMenuData==null)
                return;

            var elChildMenu = document.getElementById(this.id);

            //ak je menu zobrazene, tak ho zmazem a zobrazim na inom mieste
            if(elChildMenu!=null){
                this.hideContextMenu(box, dataElId);
            }
    
            //vytvorim si menu
            var elNewMenu = document.createElement('ul');
            var divIdName = this.id;
            elNewMenu.setAttribute('id',divIdName);

            //nasledne do neho preklopim data pre menu
            elNewMenu.innerHTML = elMenuData.innerHTML;


            //potrebujem zobrazit suradnice menu na potrebnu poziciu v boxe

            var xPos=getMouseXY(e, box).x;
            var yPos=getMouseXY(e, box).y;

            elNewMenu.style.position='absolute';
            elNewMenu.style.top=yPos+'px';
            elNewMenu.style.left=xPos+'px';
     

            //informujem o sebe rodica
            box.appendChild(elNewMenu);
            //inicializacia menu
            this.init_menu(this.id);
        }

    }

    /**
   * Skrytie kontextoveho menu
   */
    function hideContextMenu(box, dataElId){
        var elChildMenu = document.getElementById(this.id);
        if(elChildMenu!=null){
            box.removeChild(elChildMenu);
        }
    }

    /**
   * Funkcia pre zobrazenie alebo skrytie mojho vnoreneho zoznamu
   */
    function clickToSetMyChildren(item){

        //ziskam si moje rodicovske ,,li,,
        elLi=item.parentNode;

        if(elLi!=null){
            //zistim najblizsie ul
            ulList=elLi.getElementsByTagName('ul');
            if(ulList.length!=0){
                //viem ze prvy element je moje dieta
                x=0;
                var ulBlock=ulList[x];

                if(ulBlock.style.display=='none'){
                    ulBlock.style.display='block';
                    if(this.useCookie){
                        this.setBlockIdToCookie(this.id, ulBlock.id);
                    }
                    if(this.useImgOnClick){
                        imgList=elLi.getElementsByTagName('img');
                        imgList[0].src=this.imgFolderOpenSrc;
                    }

                }
                else{
                    ulBlock.style.display='none';
                    if(this.useCookie){
                        unsetBlockIdFromCookie(this.id, ulBlock.id);
                        ulChildList=ulBlock.getElementsByTagName('ul');
                        if(ulChildList.length > 0){
                            for(x=0; x<ulChildList.length; x++){
                                unsetBlockIdFromCookie(this.id, ulChildList[x].id);
                            }
                        }
                    }
                    if(this.useImgOnClick){
                        imgList=elLi.getElementsByTagName('img');
                        imgList[0].src=this.imgFolderCloseSrc;
                    }
                }
            }
        }
    }

    /**
   * Funkcia pre zobrazenie vsetkych poloziek
   */
    function showAllMenuItems(){
        elMenu=document.getElementById(this.id);
        liList=elMenu.getElementsByTagName('li');

        if(liList.length!=0){
            for(i=0; i<liList.length; i++){
                ulList=liList[i].getElementsByTagName('ul');
                if(ulList.length!=0){
                    for(x=0; x<ulList.length; x++){
                        ulList[x].style.display='block';
                    }
                }
            }
        }
    }

    /**
   * Funkcia pre skytie vsetkych poloziek
   */
    function hideAllMenuItems(){
        elMenu=document.getElementById(this.id);
        liList=elMenu.getElementsByTagName('li');

        if(liList.length!=0){
            for(i=0; i<liList.length; i++){
                ulList=liList[i].getElementsByTagName('ul');
                if(ulList.length!=0){
                    for(x=0; x<ulList.length; x++){
                        ulList[x].style.display='none';
                    }
                }
            }
        }

        //vymazem cookie, ak je pouzivana
        if(this.useCookie){
            this.setCookie(this.id, '');
        }
    }

    /*
   * Funkcia nastavi do cookie, ze je blok zobrazeny
   */
    function setBlockIdToCookie(id,value){
        var activeBlocks=getCookie(id);

        if(activeBlocks.length!=0){
            if(activeBlocks.toString().indexOf(value)== -1)
                value=activeBlocks+'*'+value;
        }
  
        setCookie(id,value);
    }

    /*
   * Funkcia odstrani blok z cookie
   */
    function unsetBlockIdFromCookie(id,value){
        var activeBlocks=getCookie(id);

        activeBlocks=activeBlocks.split('*');

        for(i=0; i<activeBlocks.length; i++){
            if(activeBlocks[i]==value){
                delete activeBlocks[i];
            }
        }

        var a=0;
        var newActiveBlocks=new Array();
        for(i=0; i<activeBlocks.length; i++){
            if(activeBlocks[i]!=null){
                newActiveBlocks.push(activeBlocks[i]);
            }
        }

        value=newActiveBlocks.join('*');

        setCookie(id,value);
    }

    /**********************************************************************/
    /***************************POMOCNE FUNKCIE****************************/
    /**********************************************************************/


    /**
   * Funkcia pre ziskane pozicie kurzora pre kontextove menu
   */
    function getMouseXY(e, box){
        if (!e) e = window.event;

        if (e){

            if (e.pageX || e.pageY){
                //(FF,Moz,Opera7)
                mousex = e.pageX - box.offsetLeft;
                mousey = e.pageY - box.offsetTop;
                algor = '[e.pageX]';
                if (e.clientX || e.clientY)
                    algor += ' [e.clientX] ';
            }
            else if (e.clientX || e.clientY){
                //IE6,FF,Moz,Opera7
                //mousex = e.clientX + document.body.scrollLeft;
                //mousey = e.clientY + document.body.scrollTop;

                mousex = e.offsetX;
                mousey = e.offsetY;
                algor = '[e.clientX]';
                if (e.pageX || e.pageY)
                    algor += ' [e.pageX] ';
            }
        }
        return {
            x:mousex,
            y:mousey
        }
    }

    /**
   * Funkcia pre kontextove menu, ktorym tlacidlom sa kliklo
   */
    function getMouseClickButtonId(e){
        if (!e)
            e = window.event;
    
        //mousedown Opera nepodporuje oncontextmenu
        if(e.type=='click'){
            return 1;
        }
        else if(e.type=='contextmenu' || e.type=='mousedown'){
            return 3;
        }
        else{
            return 0;
        }
    }

    /**
   * Ziskanie cookie hodnoty
   */
    function getCookie(c_name){
        if (document.cookie.length>0){
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1){
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return "";
    }

    /**
   * Nastavenie cookie
   */
    function setCookie(c_name,value){
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+this.expireDays);
        document.cookie=c_name+ "=" +escape(value)+
        ((this.expireDays==null) ? "" : ";expires="+exdate.toGMTString());
    }


    ///CONSTRUCTOR
    this.init_menu=init_menu;
    this.showMyChildBlock=showMyChildBlock;
    this.hideMyChildBlock=hideMyChildBlock;
    this.clickToSetMyChildren=clickToSetMyChildren;
    this.showContextMenu=showContextMenu;
    this.hideContextMenu=hideContextMenu;
    this.showAllMenuItems=showAllMenuItems;
    this.hideAllMenuItems=hideAllMenuItems;
    this.setBlockIdToCookie=setBlockIdToCookie;
    this.unsetBlockIdFromCookie=unsetBlockIdFromCookie;
    this.getMouseXY=getMouseXY;
    this.getMouseClickButtonId=getMouseClickButtonId;
    this.getCookie=getCookie;
    this.setCookie=setCookie;

    if(!Array.indexOf){
        Array.prototype.indexOf = function(obj){
            for(var i=0; i<this.length; i++){
                if(this[i]==obj){
                    return i;
                }
            }
            return -1;
        }
    }

    /**
   * Osetrenie, opera nepozna oncontextmenu
   */
    this.onOperaContextMenu = function(e, box, dataElId) {
        if(navigator.appName=="Opera"){
            if (e.button == 2) {
        
                this.showContextMenu(e, box, dataElId);
        
            }
        }
    }

}


