// Menu
// author: Evgenij Elizarov
// Creation date: 10.03.2003
// + form.js

mnuHorizontalPos 	= 1;
mnuVerticalPos 		= 0;

//Initialize menu object
function objMenu() {
	var a = objMenu.arguments;

	if (a.length < 1){alert("menu: no param!"); return false;}  //chek arguments

	this.name       = a[0];

    // default settigs
	this.x			= 0;
	this.y			= 0;
	this.itemW		= 100;
	this.itemH		= 20;
	this.visible	= 1;
	this.position   = mnuVerticalPos;
	this.bgcolor	= "#FFFFFF";
	this.bgcolor2	= "#BABABA";
	this.showTimeout= 400;
	this.hideTimeout= 400;

	//reset setigns if set
	if(a[1] !== null)this.x 		= a[1];	//x
	if(a[2] !== null)this.y 		= a[2];	//y
	if(a[3] !== null)this.itemW 	= a[3];	//itemW
	if(a[4] !== null)this.itemH 	= a[4];	//itemH
	if(a[5] !== null)this.visible  = a[5];	//visible
	if(a[6] !== null)this.position = a[6];	//position
	if(a[7] !== null)this.bgcolor  = a[7];	//bgcolor
	if(a[8] !== null)this.bgcolor2 = a[8]; //bgcolor2
	if(a[9] !== null)this.hideTimeout = a[9]; //Hide Timeout

    this.item      = new Array();
    this.itemCount = 0;
	this.timeoutID = 0;

	//Methods
	this.add  			= mnuAdd;
	this.hide 			= mnuHideAll;
	this.showStart		= mnuStartShowTimeout;
	this.showStop		= mnuStopShowTimeout;
	this.hideStart		= mnuStartHideTimeout;
	this.hideStop		= mnuStopHideTimeout;
	this.coolshow 		= mnuCoolShowItems;
}

function mnuItem(){
    var a = mnuItem.arguments;
    if (a == null){alert("mnuItem : no param!"); return false;} //chek arguments

	this.id			= a[0];
	this.name       = "item["+this.id+"]";
	this.level   	= a[1];  // menu level
	this.parent 	= a[2];  // parent
	this.event		= a[3];	 // event
	this.cls		= a[4];	 // class
	
	// default settings
	this.body       = this.name;
	this.x		= 0;
	this.y		= 0;
	this.w		= 100;
	this.h		= 20;
	this.visible    = 1;
	this.bgcolor	= "#FFFFFF";
	this.bgcolor2	= "#BABABA";
	this.showTimeout= 400;
	
    //reset setigns if set
	if(a[5] !== null)this.body	   	= a[5];	 //body
	if(a[6] !== null)this.x 	   	= a[6];	 //x
	if(a[7] !== null)this.y 	   	= a[7];	 //y
	if(a[8] !== null)this.w 	   	= a[8];	 //w
	if(a[9] !== null)this.h   	   	= a[9];	 //h
	if(a[10] !== null)this.visible = a[10]; //visible
	if(a[11] !== null)this.bgcolor = a[11]; //bgcolor
	if(a[12] !== null)this.bgcolor2 = a[12]; //bgcolor2

	this.item      = new Array();
    this.itemCount = 0;

    //Methods
    this.add  		= mnuAddSub;
	this.show 		= mnuShowItems;
	this.showStart	= mnuStartShowTimeout;
	this.showStop	= mnuStopShowTimeout;
	this.hide 		= mnuHideAll;
	this.hideItem 	= mnuItemHideAll;
	this.select		= mnuSelect;
	this.unselect	= mnuUnSelect;

    with(this){
    	this.form = new form(x,y,w,h,visible,bgcolor,body,event,cls);
    }
}

//Add menu item object
function mnuAdd(b, link, iClass, width){
      with (this){
		  if(position == mnuVerticalPos){
  		  	iX = x;          	
          	iY = y;
          	for(i=0;i<itemCount;i++){ iY = iY + item[i].h; }
		  }else{
		  	iX = x;
		   	for(i=0;i<itemCount;i++){ iX = iX + item[i].w; }
          	iY = y;
		  }

          iId    = itemCount;
		  iW 	 = (width != '') ? width : itemW;
          iH     = itemH;
          iV     = visible;
          iBgc   = bgcolor;
		  iBgc2  = bgcolor2;
		  iEvent = 'onMouseOut="'+this.name+'.item['+iId+'].unselect();'+this.name+'.hideStart();" ';		  			
		  iEvent += 'onMouseOver="'+this.name+'.item['+iId+'].select();'+this.name+'.hide();'+this.name+'.showStart('+iId+');" ';
		  LinkEvent = (link != "") ? "document.location.href='"+link+"';" : "";
		  iEvent += 'onClick="'+this.name+'.item['+iId+'].hide();'+LinkEvent+'"';
		  
          iBody = ((NN && br.version < 5) && link != "") ? '<a href="'+link+'">' : '';
          iBody += b;
          iBody += ((NN && br.version < 5) && link != "") ? '</a>' : '';

          item[iId] = new mnuItem(iId,0,this,iEvent,iClass,iBody,iX,iY,iW,iH,iV,iBgc,iBgc2);
          itemCount++;
     }
}

function mnuAddSub(b, link, iClass, width){
      with (this){
		  iId   = itemCount;
		  iW = (width != '') ? width : w;
          iH    = h;
          iV    = 0;
          iBgc  = bgcolor;
		  iBgc2 = bgcolor2;
          iLev 	= level + 1;

 		  //Build menu tree
		  obj = this;
		  menuTree = obj.name;
		  for(i=iLev;i>0;i--){
			obj = obj.parent;
		  	menuTree = obj.name + "." + menuTree;
		  }

		  if(level == 0 && obj.position == mnuHorizontalPos){
			iX   = x;
    	    iY   = y + h + (h * (itemCount));
		  }else{
		  	iX   = x + w;
            iY   = y + (h * (itemCount));
		  }

		  //alert(obj.name);
		  iEvent =  'onMouseOut="'+menuTree+'.item['+iId+'].unselect();'+obj.name+'.hideStart();" ';
		  iEvent += 'onMouseOver="VisibleAllCombobox(0);'+obj.name+'.hideStop();'+menuTree+'.item['+iId+'].select();'+menuTree+'.hideItem();'+menuTree+'.item['+iId+'].show();"';
		  LinkEvent = (link != "") ? "document.location.href='"+link+"';" : "";
		  iEvent += 'onClick="'+LinkEvent+'"';

          iBody = ((NN && br.version < 5) && link != "") ? '<a href="'+link+'">' : '';                    
          iBody += b;
          iBody += ((NN && br.version < 5) && link != "") ? '</a>' : '';

          item[iId] = new mnuItem(iId,iLev,this,iEvent,iClass,iBody,iX,iY,iW,iH,iV,iBgc,iBgc2);
          itemCount++;
     }
}

//Menu function
function mnuShowItems(){
    //menu.hide();
	with(this){
		if(itemCount > 0){
        	for(i=0;i<itemCount;i++){
				item[i].form.setVisible('show')
			}
		}
    }
}

function mnuSelect(){
	with(this){
		form.setBgcolor(this.bgcolor2);
    }
}

function mnuUnSelect(){
	with(this){
		form.setBgcolor(this.bgcolor);
    }
}

function Interval(func,msec){
	intervalID = window.setInterval(func,msec);
}

var timeoutID = 0;
function Timeout(func,msec){
	return window.setTimeout(func,msec);
}

var slowShow_i = 0;
function slowShow(obj,count){
	if(slowShow_i >= count){
	     slowShow_i = 0;
	     clearInterval(intervalID);
     }else{
         obj.item[slowShow_i].form.setVisible('show');
	     slowShow_i++;
	}
}

function mnuCoolShowItems(id){
     this.hide();
     if (this.itemCount > 0){
          if(id != null){
               with(this){
                    item[id].itemCount;
                    Interval("slowShow("+this.name+".item["+id+"],"+item[id].itemCount+")",4);
               }
          }
     }
}

function mnuStopHideTimeout(){
	with(this){
		clearTimeout(timeoutID);
	}
}


function mnuStartHideTimeout(){
	with(this){
		clearTimeout(timeoutID);
		timeoutID = Timeout(this.name+".hide();VisibleAllCombobox(1);",hideTimeout);
	}
}

function mnuStopShowTimeout(){
	with(this){
		clearTimeout(timeoutID);
	}
}

function mnuStartShowTimeout(id){
	with(this){
		clearTimeout(timeoutID);		
		timeoutID = Timeout(this.name+".item["+id+"].show();VisibleAllCombobox(0);",showTimeout);
	}
}


function mnuHideAll(){	
	var i;
    with(this){
		clearTimeout(timeoutID);		
	    if (itemCount > 0){
            for(i=0;i<itemCount;i++){
				if(item[i].itemCount > 0){
					this.item[i].hide();
				}
				if(item[i].level !== 0){
					item[i].form.setVisible('hide');
				}
			}
    	}
	}
	return ;
}

function mnuItemHideAll(){
	var i;
     with(this){
	    if (itemCount > 0){  	
            for(i=0;i<itemCount;i++){
				if(item[i].itemCount > 0){
					this.item[i].hide();
				}
				//if(item[i].level !== 0){
				//	item[i].form.setVisible(0);
				//}
			}
    	}
	}
	return ;
}

function VisibleAllCombobox(vis){
 if(IE && br.version > 5){
  oSelects = document.getElementsByTagName('SELECT'); 
  if (oSelects.length > 0) { 
   for (i = 0; i < oSelects.length; i++) { 
    oSlt = oSelects[i]; 
    visibility = (vis == 1) ? 'visible' : 'hidden'; 
    if (oSlt.style.visibility != visibility) {oSlt.style.visibility = visibility;} 
   }
  }
 } 
}
