CMMC = function(){
	this.itemCNT=0
    this.itemHeight = 30
    this.c = {
        waitTime: 2000
    };
    this.init = function(tree, aActiveIDX){
        this.bWork = true;
        document.onmousedown = function(){
            window.CMM.hideMenu();
        }
        this.aActiveIDX = aActiveIDX;
        this.tree = tree;//alert(this.tree.length);
    };
    this.start = function(){
        this.e = new CMA();
        this.setup(this.tree, []);
    }
    //call by html element
    this.sl = function(aIds){//alert(aIds)
        if (!this.bWork)
            return;

        //hide levels greater than current level
        if (this.showedLevel)
            this.hhl(aIds.length - 1);

        this.rt();
        this.showedLevel = [];
        for (var i = 0; i < (aIds.length); i++) {//alert("level "+i)
            this.showedLevel[i] = aIds[i];
        };//alert(aIds+' '+this.showedLevel)
        //recursive by levels
        var i, j, cs, l;
        cs = this.ids2set(aIds);
        //alert(cs);
        //by set
        l = aIds.length;
        for (j = 0; j < cs.length; j++) {
            aIds[l] = cs[j];//alert(aIds)
            this.showElement(aIds);//@todo ,i*100
        };
        //ustawianie elementów powyżej jeśli wychodzą
        var elId = this.idForA(aIds)
        var element = new CME(elId);
        if (element.getY() > 500) {
            for (j = 0; j < cs.length; j++) {
                aIds[l] = cs[j];
                var elId = this.idForA(aIds)
                var element = new CME(elId);
                element.setY(element.getY() - this.itemHeight * 5)
            };
                    }
    };
    this.showElement = function(aIds, to){

        var hasSibling = false;//alert(typeof(aIds));
        var aPar = [];
        for (var i = 0; i < (aIds.length - 1); i++) {//valid copy construct - break refrence
            aPar[i] = aIds[i];
        };//alert(aIds+' '+aPar)
        //alert(this.vConf+' '+(aIds.length-1))
        //alert(this.idForA(aPar))
        var parent = new CME(this.idForA(aPar));
        var elId = this.idForA(aIds)
        var element = new CME(elId);

        //have prev. sibling
        if (aIds[aIds.length - 1] > 0) {
            hasSibling = true;
            var sIds = [];
            for (var i = 0; i < aIds.length; i++) {
                sIds[i] = aIds[i];
            };
            sIds[sIds.length - 1] -= 1;
            prevSibling = new CME(this.idForA(sIds));
            var y = prevSibling.getY() + prevSibling.getHeight();//alert(prevSibling.getOffsetY()+' '+prevSibling.getHeight()+' '+this.vConf[aIds.length-1].y_spacing)
            //if(top.cDebug)cDeb.println('cmm,  ps '+prevSibling.getY());
            //logDebug('prev. sibling '+sIds+' '+y)
        }
        else
            if (aIds.length == 2) {//fl
                var y = 170;

            }
            else {
                var y = parent.getOffsetY();//alert(parent.getOffsetY()+' '+parent.getHeight()+' '+this.vConf[aIds.length-1].y_offset)
            }
        //first level
        if (aIds.length == 2) {
            var x = parent.getOffsetX()-20;
        }
        else {
            var x = parent.getOffsetX() + parent.getWidth()-25;//alert(parent.getX()+' '+parent.getWidth()+' '+this.vConf[aIds.length-1].x_offset);
        } //alert(x+' '+y)
        //alert(parent.r.offsetWidth)

        element.setX(x);
        element.setY(y);//logDebug(element.getY()+' '+element.getOffsetY());
        if (typeof(to) == 'number') {
            setTimeout('CMM.showElLater("' + elId + '")', to);
        }
        else {
            //if(top.cDebug)cDeb.println(element.ua.opera);
            //if(element.ua.opera)element.setVisible(true);
        }
    };
    this.showElLater = function(elId){
        var element = new CME(elId);
        element.setVisible(true);
    }
    this.hideElement = function(aIds){//alert(this.idForA(aIds))
        var e = new CME(this.idForA(aIds));

        e.setX(-2000)
    };
    this.hideLevel = function(ids){//alert(ids)
        var set = this.ids2set(ids);
        var l = ids.length;
        for (i = 0; i < set.length; i++) {
            ids[l] = set[i];
            this.hideElement(ids);
        };
            };
    this.hideMenu = function(limit){//alert('hideMenu')
        if (!this.bWork)
            return;
        if (!this.showedLevel)
            return;

        //alert(this.showedLevel)
        var downLimit = typeof(limit) == 'undefined' ? 0 : limit;
        for (j = this.showedLevel.length - 1; j >= downLimit; j--) {//from leaf
            var cIds = [];
            for (i = 0; i <= j; i++) {
                cIds[i] = this.showedLevel[i];//@todo maybe in soft transition reverse queue
            };//alert(cIds)
            this.hideLevel(cIds);
        };
            };
    this.st = function(){//return//@todo undefined
        if (!this.bWork)
            return;
        this.tId = setTimeout('CMM.hideMenu()', this.c.waitTime);
    };
    this.rt = function(){
        clearTimeout(this.tId);
        this.tId = null;
    };
    this.idForA = function(aIds){//alert(typeof(aIds))
        return 'cm-' + aIds.join('-');
    };
    /**
     *ids array to set
     *have aray of positions in tree
     *trans. this to set for this leaf
     */
    this.ids2set = function(aIds){
        var cs = this.tree[aIds[0]];//alert(cs.length);
        for (i = 1; i < aIds.length; i++) {
            cs = cs[aIds[i]];//alert(aIds[i])
        };
        var ret = [];
        for (i = 0; i < cs.length; i++) {
            ret[i] = i;
        };
        return ret;
    };
    this.positions = [];

    /**
     *Setup html elements
     *@param a array
     *@param p array
     */
    this.setup = function(a1, p1){//alert(a1+' '+p1);
        var a = [];
        for (var i = 0; i < a1.length; i++) {
            a[i] = a1[i];
        };
        var p = [];
        for (var i = 0; i < p1.length; i++) {
            p[i] = p1[i];
        };
        var l = p.length;//alert(a.length);
        var maxW = 0;
        var tw;
        for (i = 0; i < a.length; i++) {
            p[l] = i;//alert('p in '+p);
            //active?
            var bActive = true;
            for (var j = 0; j < p.length; j++) {
                if (p[j] !== this.aActiveIDX[j]) {
                    bActive = false;
                    break;
                }
            }
            var idStr = p.join('-');//alert(idStr);
            var idsA = p.join(',');
            var ov = !bActive ? ";window.CMM.ov(this," + (p.length - 1) + ")" : '';//alert(idsA+' '+ov);
            var ou = !bActive ? ";window.CMM.ou(this," + (p.length - 1) + ")" : '';
            var aEl = document.getElementById('cm-a-' + idStr);
            document.getElementById('cm-'+idStr).style.zIndex=p.length*100+window.CMM.itemCNT++
            if (bActive) {
                // alert(aEl.className)
                aEl.className = aEl.className + '-active';
            }
            var hrefStr = aEl.href
            if (hrefStr.charAt(hrefStr.length - 1) == "#") {
                //alert("ok");return
                aEl.style.cursor = "default"
            }
            var aElE = new CME(aEl)
            if (aElE.ua.ie)
                tw = aEl.offsetWidth;
            else
                tw = aElE.getWidth();
            //if(top.cDebug)cDeb.println('tw '+tw);

            //if(i==0)alert(tw)
            if (tw > maxW)
                maxW = tw;
            //if(top.cDebug)cDeb.println(aEl.innerHTML+' '+tw+ ' '+maxW);
            //if(idStr=='1-1-7'){alert(aEl.innerHTML)}
            aEl.onmouseout = new Function("if(window.CMM){window.CMM.st()" + ou+"}");
            aEl.onmousedown = new Function("e", "try{window.event.cancelBubble=true}catch(er){try{e.stopPropagation()}catch(er2){}}");
            if (typeof(a[i]) == 'object') {//have child //alert(p);
                aEl.onmouseover = new Function("if(window.CMM){window.CMM.sl([" + idsA + "])" + ov+"}");
                // var p2=[];for(var j=0;j<p.length;j++){p2[j]=p[j];};
                this.setup(a[i], p);
            }
            else {
                aEl.onmouseover = new Function("if(window.CMM){window.CMM.hhl(" + (p.length - 1) + ")" + ov+"}");
                aEl.onclick = new Function("if(window.CMM)window.CMM.de()");
            }
        };
        if (l == 0) {
            return
        }
        maxW = maxW + 1;

        for (i = 0; i < a.length; i++) {
            p[l] = i;//alert('p in '+p);
            var idStr = p.join('-');//alert(idStr);
            var aEl = document.getElementById('cm-a-' + idStr);
            aEl.style.width = maxW + 'px';
        }
    };
    /**
     *@param level above that hide descendant
     */
    this.hhl = function(level){//alert(level)
        if (!this.bWork)
            return;
        //	if(this.showedLevel.length>level)this.hideLevel(this.showedLevel);
        this.hideMenu(level);
        this.rt();
    };
    this.ov = function(rA, l){//@todo check how level
        //this.aActiveIDX
        //this.e.ov(rA,l);
    };
    this.ou = function(rA, l){//@todo check how level
        //this.e.ou(rA,l);
    };
    this.de = function(){
        this.bWork = false;
    };

};


