/* Javascript March 1, 2010*/
///////////////////////////////////////////////////////////////////
//	BHMenu Version 0.50                      //  N E W  L O O K //
//  (c) Copyright 2006-2007 Suvadia.com      ///////////////////
//                                           /////
//                                          //
// progammer: Howard Brown.                //
/////////////////////////////////////////////

 
function BHMenu(bh)
{
	bhUI.createObject('Menu',this);
	var div,e;
	if(!bh.id)bh={id:bh};
	this.skin=bh.skin?bh.skin:'';
	this.parentMenu=bh.menu;
	div=bhUI.createElement();
	div.id='_'+bh.id;div.className=this.skin+'MenuGroup';
	div.style.zIndex=10;div.style.visibility='hidden';
	div.style.top=div.style.left='0px';div.style.position='absolute';
	div.onmouseover=bhUI.eventHandle(this.Tx+".onMouseOver");
	div.onmouseout=bhUI.eventHandle(this.Tx+".onMouseOut");
	div.onclick=bhUI.eventHandle(this.Tx+".event_click");
	if(bh.menu)bh.menu.div.appendChild(div);
	else document.body.appendChild(div);
	if((e=bhUI.element(bh.id))){
		e.addEventListener('mouseover',new Function(this.Tx+".show();"),false);
		e.addEventListener('mouseout',new Function(this.Tx+".autoclose=setTimeout('"+this.Tx+".hide()',500);"),false);
	}
	this.onmousemove=bhUI.eventHandle(this.Tx+".onMouseMove");
	this.pairing={node:e,x:0,y:0};
	this.autoclose=0;
	this.zIndex=10;
	this.height=0;
	this.div=div;
	this.Items=new Array();
	this.itemClick=bh.onclick?bh.onclick:__DF;
	if(bh.width)this.div.style.width=bh.width+'px';
}

BHMenu.prototype.changeWidth=function(width)
{
	this.div.style.width=width+'px';
}

BHMenu.prototype.addItem=function(id,caption,url)
{
	var a,div;
	div=this.div.addElement({},{})[1];
	div.id=(caption && id)?this.div.id+'_'+id:this.div.id+'_Item'+this.Items.length;
	div.className='MenuItem';
	div.style.zIndex=(this.zIndex-2);
	div.style.position='relative';
	div.innerHTML=caption?caption:id;
	var w=div.offsetWidth,h=div.offsetHeight,i=this.Items.length;
    var ydiv=div.addElement(),zdiv=div.addElement();
	zdiv.style.position=ydiv.style.position='absolute';
	zdiv.style.left=zdiv.style.top=ydiv.style.left=ydiv.style.top='0px';
	zdiv.style.width=ydiv.style.width=w+'px';
	zdiv.style.height=ydiv.style.height=h+'px';
	zdiv.id='MenuItem';
	a=zdiv.addElement('IMG');
	a.style.width=a.style.height ="100%";
	a.src="/newlook/img/clear.gif";
	this.Items.push((a.menu_item={no:i,url:url,div:div,Tv:this.Tv}));
	this.height+=div.offsetHeight;
	div.setOpacity(0.95);
	return Array(zdiv,div,ydiv); 
}

BHMenu.prototype.addMenu=function(bh)
{
	var itm,menu;
	var top=this.height;
	if(!bh.id)bh={id:bh,caption:arguments[1]};
	itm=this.addItem(bh.id,bh.caption);
	itm[1].onclick=function(){return false;};
	menu=new BHMenu({id:bh.id,skin:this.skin,width:bh.width,
					menu:this,onclick:bh.onclick});
	menu.zIndex++;
	menu.level=(this.level+1);
	menu.div.style.zIndex=menu.zIndex;
	menu.div.style.top=top+'px';
	menu.div.style.left=itm[0].offsetWidth-6+'px';
	itm[0].id="MenuExpansion";
	itm[0].className=this.skin+'MenuItemExpansion';
	return menu;
}


BHMenu.prototype.addCheck=function(id, caption, checked,url)
{
	var itm,b='MenuCheck';
	itm=this.addItem(id,caption,url);
	itm[0].id=b;b='MenuItem';
	itm[0].className=checked?b+'Checked':b+'UnChecked';
}

BHMenu.prototype.addRadio=function(id, caption, checked,url)
{
	var itm,b="MenuRadio";
	itm=this.addItem(id,caption,url);
	itm[0].id=b;
	itm[0].className=checked?b+'Checked':b+'UnChecked';
}

BHMenu.prototype.findItem=function(id)
{
	if(isNaN(id)){
		var i,real_id=this.div.id+'_'+ id;
		for(i=0;i<this.Items.length;i++){
			if(this.Items[i].div.id==real_id){
				return this.Items[i];
			}
		}
	}
	return this.Items[id-0];
}

BHMenu.prototype.setCaption=function(id,caption)
{
	this.findItem(id).div.innerHTML=caption;
}

BHMenu.prototype.setIcon=function()
{	
	var s,itm,a=arguments;
	if(a.length==1){
	 	itm=this.Items[this.Items.length-1];
	 	s=a[0];
	}else{
		itm=this.findItem(a[0]);	
		s=a[1];
	}
	itm.div.childNodes[1].className=s;
}

BHMenu.prototype.setState=function(i,state)
{
	var s,itm=this.findItem(i);	
	itm=itm.div.childNodes[2];
	if(itm.id=='MenuCheck')s='MenuItem';
	else s=itm.id;
	itm.className=s+['UnChecked','Checked','Disabled'][state];
}

BHMenu.prototype.setVisible=function(id, bool)
{
	var itm=this.findItem(id);
	itm.div.style.display=bool?'block':'none';
}

BHMenu.prototype.pair=function(id, x, y)
{
	this.pairing.node=typeof(id)=='string'?bhUI.element(id):id;
	this.pairing.node.onmouseover=new Function(this.Tx+".show();");
	this.pairing.node.onmouseout =new Function(this.Tx+".autoclose=setTimeout('"+this.Tx+".hide()',500);");
    this.pairing.x=x;
	this.pairing.y=y;
}

BHMenu.prototype.show=function()
{
	clearTimeout(this.autoclose);
	document.removeEventListener('mousemove',this.onmousemove,false);
	if(this.pairing.node){
		var e=this.pairing.node;
		var top=e.offsetHeight+this.pairing.y;
		var w=(e.offsetWidth-this.div.offsetWidth);
		var left=this.pairing.x+(w>0?w:0);
		while(e.offsetParent){
			  top+=e.offsetTop;left+=e.offsetLeft;
			  e=e.offsetParent;} 
		this.div.style.top=top+'px';
		this.div.style.left=left+'px';
	}
	this.div.style.visibility='visible';
}

BHMenu.prototype.hide=function()
{
	this.div.style.visibility='hidden';
}

BHMenu.prototype.hideAll=function()
{	
	this.div.style.visibility='hidden';
	var m=this.parentMenu;
	while(m!=null){m.div.style.visibility='hidden';
	m=m.parentMenu;}
}


BHMenu.prototype.onMouseMove=function(evt)
{
	if(this.div.style.visibility=='visible'){
		var b=bhUI.getScroll();
		if(!this.getItemAt(this.div,evt.clientX+b.left,evt.clientY+b.top)){	
			this.hideAll();
			document.removeEventListener('mousemove',this.onmousemove,false);
		}
	}
}

BHMenu.prototype.onMouseOver=function(evt)
{
	var o,s,i,div,m=evt.target.menu_item;
	if(!m)return;div=m.div;s=div.className;o='Over';
	if(s.length>5){if(s.substr(s.length-4)!=o)div.className=s+o;}
	if(evt.target.parentNode.id=='MenuExpansion'){
		s=div.id;i=s.lastIndexOf('_');
		bhUI.element(s.substr(i)).style.visibility='inherit';
	}
	clearTimeout(this.autoclose);
	document.addEventListener('mousemove',this.onmousemove,false);

}

BHMenu.prototype.getItemAt=function(p,x,y)
{
	var i,div,ox=0,oy=0,e=p;
	if(e.style.visibility=='hidden')return null;
	while(e.offsetParent){oy+=e.offsetTop;
	      ox+=e.offsetLeft;e=e.offsetParent;}
	nodes=p.childNodes;
	if(x > ox && x < (ox+p.offsetWidth)&&
	   y >= oy && y < (oy+p.offsetHeight)){
			return p;
	}
	for(i=0;i<nodes.length;i++){
		if(nodes[i].id.lastIndexOf('_')==0){
			div=arguments.callee(nodes[i],x,y);
		    if(div)return div;
		}
	}
	return null;
}

BHMenu.prototype.onMouseOut=function(evt)
{	
    var m=evt.target.menu_item;if(!m)return;
	var i,div=m.div;
	var w='MenuExpansion',o='Over',s,b=bhUI.getScroll();
	var x=evt.clientX+b.left,y=evt.clientY+b.top;
    if(evt.target.parentNode.id==w){
		s=div.id;i=s.lastIndexOf('_');var nameOf=s.substr(i);
		var e=bhUI.element(nameOf);
		if(!this.getItemAt(e,x,y))e.style.visibility='hidden';
	}
	s=div.className;
	if(s.length>5)if(s.substr(s.length-4)==o)div.className=s.substring(0,s.length-4);
	if(this.getItemAt(this.div,x,y))return;
	this.hide();
	if(this.parentMenu){
		div=this.parentMenu.div;
		n=(this.parentMenu.div.id+this.div.id);
		for(i=0;i<div.childNodes.length;i++){
			if(div.childNodes[i].id==n){
				div=div.childNodes[i];
				s=div.className;
				if(s.length>5)if(s.substr(s.length-4)==o)div.className=s.substring(0,s.length-4);
				break;
			}
		}
	}
}


BHMenu.prototype.event_click=function(evt)
{
	var div,k,id,o='UnChecked';
	if(!(k=evt.target.menu_item)||k.Tv!=this.Tv){
		return;
	}
	
	switch(evt.target.parentNode.id){
	case 'MenuCheck':{
			div=evt.target.parentNode;
			var s=div.className;
			if(s.length<12)break;
			if(s.substr(s.length-9)==o){
			div.className=s.substring(0,s.length-9)+o.substr(2);}
			else{div.className=s.substring(0,s.length-7)+o;}
		}break;
	case 'MenuRadio':{
			var i,e,s,t=evt.target.parentNode;
			s=t.className;if(s.length<12)break;
			if(s.substr(s.length-9)!=o)break;
			id=k.div.id;div=this.div;
			for(i=0;i<div.childNodes.length;i++){
			e=div.childNodes[i].firstChild;if(e.id==id){
			s=e.lastChild.className;if(s.length>12&&
			s.substr(s.length-9)!=o){
			e.lastChild.className=s.substring(0,s.length-7)+o;
			break;}}}
			s=t.className;
			t.className=s.substring(0,s.length-9)+o.substr(2);
		}break;
	case 'MenuExpansion': return;
	}
	
	var m=evt.target.menu_item;
	if(!m.url)this.itemClick(m.no);
	else{
		switch(typeof(m.url)){
		case 'string':self.location=m.url;break;
		case 'function':m.url();break;}
	}
	this.hideAll();
}

