/*
<script type="text/javascript" language="JavaScript">
CMM=new CMMC();
For levels configuration'l be helper tree struct
CMM.init([0,
[1,2,
[3,4]
]
],[{x_offset:,y_offset:,y_spacing:}]);
//@todo ,y_requeue:
</script>

*/
CMMC=function(){
  this.c={waitTime:2000};
	  this.init=function(tree,vConf,aActiveIDX){
				this.bWork=true;
				document.onmousedown=function() { window.CMM.hideMenu(); }
		    this.aActiveIDX=aActiveIDX;
		    this.tree=tree;//alert(this.tree.length);
		    this.vConf=vConf;
		    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++){
			    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);
			    };
		    };
	  this.showElement=function(aIds){
						    
		    var hasSibling=false;//alert(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 element=new CME(this.idForA(aIds));
						element.setVisible(true);
		    //have prev. sibling
		    if(aIds[aIds.length-1]>0){//alert()
			    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()+this.vConf[aIds.length-1].y_spacing;//alert(prevSibling.getOffsetY()+' '+prevSibling.getHeight()+' '+this.vConf[aIds.length-1].y_spacing)
			   //logDebug('prev. sibling '+sIds+' '+y) 
						}else if(aIds.length==2){//fl
							//alert(parent.getOffsetY())
			    var y=parent.getOffsetY()+parent.getHeight()+this.vConf[aIds.length-1].y_offset;
			    		    var em=new CME('cm-cont');
if(em.ua.ie){
y=y+29
}
			    }
		    else{
			    var y=parent.getOffsetY()+this.vConf[aIds.length-1].y_offset;//alert(parent.getOffsetY()+' '+parent.getHeight()+' '+this.vConf[aIds.length-1].y_offset)
			    }
		    //first level
		    if(aIds.length==2){
			    var x=parent.getOffsetX()+this.vConf[aIds.length-1].x_offset;
			    }
		    else
			    var x=parent.getOffsetX()+parent.getWidth()+this.vConf[aIds.length-1].x_offset;//alert(parent.getX()+' '+parent.getWidth()+' '+this.vConf[aIds.length-1].x_offset);
		    //alert(x+' '+y)
		    element.setX(x);
		    element.setY(y);//logDebug(element.getY()+' '+element.getOffsetY());
		    };
	  this.hideElement=function(aIds){//alert(this.idForA(aIds))
		    var e=new CME(this.idForA(aIds));
		    e.setVisible(false);
		    };
	  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);
		    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)+")":'';
			    document.getElementById('cm-a-'+idStr).onmouseout=new Function("window.CMM.st()"+ou);
			    document.getElementById('cm-a-'+idStr).onmousedown=new Function("window.event.cancelBubble=true");
				document.getElementById('cm-a-'+idStr).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);
								document.getElementById('cm-a-'+idStr).onmouseover=new Function("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{
				    document.getElementById('cm-a-'+idStr).onmouseover=new Function("window.CMM.hhl("+(p.length-1)+")"+ov);
								//document.getElementById('cm-a-'+idStr).onclick=new Function("window.CMM.de()");
							}
			    };
		    };
	  /**
	  *@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;
						};
	  };


