var IMG='/spsys/pixel.gif';
window.HSmenu;

function bw_check(){
	var is_major=parseInt(navigator.appVersion);
	this.nver=is_major;
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById?1:0;
	this.opera=window.opera?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1&&this.dom&&!this.opera)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1&&this.dom&&!this.opera)?1:0;
	this.ie4=(document.all&&!this.dom&&!this.opera)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.ns6=(this.dom&&parseInt(this.ver)>=5)?1:0;
	this.ie3=(this.ver.indexOf("MSIE")&&(is_major<4));
	this.hotjava=(this.agent.toLowerCase().indexOf('hotjava')!=-1)?1:0;
	this.ns4=(document.layers&&!this.dom&&!this.hotjava)?1:0;
	this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera);
	this.ver3=(this.hotjava||this.ie3);
	this.opera7=((this.agent.toLowerCase().indexOf('opera 7')>-1) ||
			(this.agent.toLowerCase().indexOf('opera/7')>-1));
	this.operaOld=this.opera&&!this.opera7;
	return this;
};

function none(){}

function nn(val){
return val != null;
}

function und(val) {
return typeof(val)=='undefined';
}

function Menu() {
var items=ITEM;
this.bw=new bw_check();
window.HSmenu=this;
window.HSmenuHideTimers=null;

	this.rel=items[0].pos=="relative";
	this.divs=[];
	this.root=[];
	this.root.par=null;
	this.root.cd=[];
	this.root.fmt=items[0];
	this.parpos=[];
	this.root.pos=[0,0];
	this.root.fmt.pos=this.root.pos;
	this.root.frameoff=items[0].pos?items[0].pos:[0,0];
	this.items=[];
	this.itemnames=[];
	this.root.lvl=new CMenuLevel(this, this.root);

	this.get_div=function (name){
		return this.bw.ns4?document.layers[name]:
		  document.getElementById?document.getElementById(name):
		    document.all[name];
	}

	this.move=function(n,s,m){
		var d=HSmenu.get_div(items[n].name);
		var x=HSmenu.bw.ns4?d.pageX:domPageX(d);
		var y=HSmenu.bw.ns4?d.pageY:domPageY(d);
		HSmenu.root.pos=[x,y];
		HSmenu.parpos[n]=new Array(2);
  		HSmenu.parpos[n]=[x,y];
	};

	for (var k=1; k<items.length;k++)
	this.move(k);

	for (var i=1;i<items.length;i++)
		if (!und(items[i])) {
		this.itemnames[i]=items[i].name;
		this.itemindex=i;
		new CMenuItem(this, this.root, items[i], und(items[i].format)?items[i].sub[0]:items[i].format,i);
		}

	this.drawOther=function(){
	var s="";

	for (var i=0;i<this.items.length;i++) {
	   if (this.items[i].par!=this.root)
	     s+=this.items[i].draw();
	     }

	if(this.ns4)
	  document.write('<layer z-index="0" visibility="hidden"></layer>');

	if(!this.ns4)
	  document.write('<div style="z-index:0;position:absolute;visibility:hidden"></div>');
	return s;
	};


	this.init=function(){ document.write(this.drawOther()); };

	this.hide=function(){
		if (this.root.fmt.popup)
	  	  this.root.lvl.vis(0);
		else {
	  		for (var i=0;i<this.root.cd.length;i++)
	    		  if (this.root.cd[i].lvl)
	      	  this.root.cd[i].lvl.vis(0);
		  this.root.lvl.a=null;
		  this.root.lvl.draw();

		    if (this.root.fmt.hidden_top)
	  		this.root.lvl.vis(0);
	  	}
	};
this.init();

	if (HSmenu.bw.ns4)
	  window.onresize=resizeHandler;
	else
	  window.onresize=DomOnResize;
};


function CMenuLevel(menu, par){
	this.menu=menu;
	this.par=par;
	this.v=0;
	this.vis=function(s){
		var ss=this.v;
		this.v=s;
		var l=this.par.cd.length;
		  for (var i=0;i<l;i++){
		    var n=this.par.cd[i];
		      if ( n.hc() && n.lvl.v && !s )
		        n.lvl.vis(s);
			n.vis(s);
		  }

		if (!s)
		  this.a=null;
	};


	this.setA=function(idx,s){
		var n=this.menu.items[idx];
		  if (nn(this.a)&&n.par.lvl!=this.a.par.lvl)
		    return;
		  if(s&&n.hc())n.lvl.vis(1);

		  if( s && n!= this.a && nn(this.a) && this.a.hc() && this.a.lvl.v )
		    this.a.lvl.vis(0);

		this.a=n;
		this.draw();
	};

	this.draw=function(){
		  if (this.menu.root.lvl==this&&this.menu.root.fmt.hidden_top)
		    return;

		  for (var i=0;i<this.par.cd.length;i++)
		    if (this.par.cd[i]==this.a)
		      this.par.cd[i].setVis('o');
		     else
		      this.par.cd[i].setVis('n');
	}
};

function CMenuItem(menu, par, item, format,item_index) {
	  if (und(item)) return;

	this.lvl=null;
	this.par=par;
	this.code=item.code;
	this.ocode=item.ocode || item.code;
	this.targ=und(item.target)?"":'target="'+item.target+'" ';
	this.url=und(item.url)?'javascript:none();':item.url;
	this.fmt=format;
	this.menu=menu;
	this.bw=menu.bw;
	this.cd=[];
	this.index=menu.items.length;
	menu.items=menu.items.concat([this]);
	this.pindex=par.cd.length;
	par.cd=par.cd.concat([this]);

	this.get_divnames=function() {
		var diva=["b","s","o","n","e"];
		for (var i=0; i < diva.length; i++)
			menu.divs[this.index+diva[i]]=this.id+diva[i];

	};



	     if (und(item_index)) {//child
		this.id=menu.itemnames[menu.itemindex]+'_'+this.index;
		this.get_divnames();
	}  else //parent
	        this.id=menu.itemnames[item_index];

	this.v=0;
	this.state='n';
	this.hc=function(){return this.cd.length > 0};
	this.hac=function(){return this.hc()&&this.cd[0].vis};

	this.gen_code=function(state, off){
		var res='';
		var bgc; var fc; var end_code;
                bgc=state=='n'?this.style.color.bgON:this.style.color.bgOVER;
                fc=state=='n'?this.style.color.fontON:this.style.color.fontOVER;
                	if (!this.bw.ns4) {
		    res +='<div style="backround-color:'+bgc+
		    ';color:'+fc+';font-family:'+this.style.font.face+';'+
		    'font-size:'+this.style.font.size+'pt;'+
		    'font-weight:'+this.style.font.weight+';">';
		    end_code='</div>';
                	} else {
		    	res +=(this.style.font.weight=="bold"?'<b>':'')+
			'<font color='+fc+
		    	' face="'+this.style.font.face+'; style="font-size:"'+
		    	this.style.font.size+'pt;">';
		    	end_code='</font>'+
		    	(this.style.font.weight=="bold"?'</b>':'');
		    	}
		    res+='&nbsp;'+(state=='n'?this.code:this.ocode)+end_code;
		return res;
	};


	this.linkIt=function() {
	return '<a href="'+this.url+'">'+
	'<img src="'+IMG+'" width="'+this.size[1]+
	'"height="'+this.size[0]+'" border="0"></a>';
	};

	this.draw=function(){
		var bl=bt=this.style.border;
		var br=bb=this.style.border*2;
	var s=this.style.shadow;
	this.z=10;
	var z=(!this.style.shadow?"":adiv(this.menu.bw, this.id+"s", this.z,
						this.pos[0]+s, this.pos[1]+s,
						this.size[1], this.size[0],
						this.style.color.shadow, "", "")
		)+
	   (!this.style.border?"":adiv(this.menu.bw, this.id+"b", this.z, this.pos[0],
					this.pos[1], this.size[1], this.size[0],
					this.style.color.border, "", "")
		)+
	   adiv(this.menu.bw, this.id+"n", this.z, this.pos[0]+bl, this.pos[1]+bt,
	   	this.size[1]-br, this.size[0]-bb, this.style.color.bgON,
		this.gen_code('n', parseInt(bt)+parseInt(bb)),
		'',
		'onmouseover="mEvent('+this.index+',\'o\');"'
		)+
	   adiv(this.menu.bw, this.id+"o", this.z, this.pos[0]+bl, this.pos[1]+bt,
	   	this.size[1]-br, this.size[0]-bb, this.style.color.bgOVER,
		this.gen_code('o', bt+bb)
		)+
	   adiv(this.menu.bw, this.id+"e", this.z, this.pos[0]+bl, this.pos[1]+bt,
	   	this.size[1]-br, this.size[0]-bb, '',
		this.linkIt(),
		'',
		'onmouseover="mEvent('+this.index+',\'o\');"'+
		'onmouseout="mEvent('+this.index+',\'t\');"');
	return z;
	};


	this.vis=function(s){
		  if (this.style.shadow)
		   this.visDiv(this.id+"s",s);
		  if (this.style.border)
		    this.visDiv(this.id+"b",s);
		  if (!s) {
		    this.visDiv(this.id+"o",0);
		    this.visDiv(this.id+"n",0);
		    this.state="n";
		  } else if (this.state=="n")
		    this.visDiv(this.id+"n",1);
		  else
		    this.visDiv(this.id+"o",1);

		  this.visDiv(this.id+"e",s)
	};

	this.setVis=function(n){
		  if (this.state!=n){
		    if (n=="n") {
		      this.visDiv(this.id+"n",1);
		      this.visDiv(this.id+"o",0);
		    };

		  if (n=="o") {
		    this.visDiv(this.id+"n",0);
		    this.visDiv(this.id+"o",1);
		    };

		  };
		this.state=n;
	};

	this.visDiv=this.bw.ns4? visDivNS:visDivDom;

	this.getf=function(obj, name){
		  if (!und(obj) && nn(obj) && !und(obj.fmt)) {
		    if (!und(obj.fmt[name])) return obj.fmt[name];

		    if (obj.par!=this.menu.root && obj.par && obj.par.sub
		    	&& obj.par.sub[0][name]) return obj.par.sub[0][name];

		   return this.getf(obj.par, name);
		  };
		return;
	};
	this.style=this.getf(this, "style");
	this.size=this.getf(this, "size");

		if (this.par==this.menu.root)
		  this.fmt.pos=this.getf(this, "pos");

	this.prev=this.pindex==0? null : this.par.cd[this.pindex-1];

	this.setPosFromParent=function(n){
		var ioff=this.fmt.itemoff;
		//var loff=this.fmt.leveloff;
	    if (this.prev==null)
	      this.pos=[menu.parpos[n][0]+ioff[1], menu.parpos[n][1]+ioff[0]];
	    else
	      this.pos=[this.prev.pos[0]+ioff[1], this.prev.pos[1]+ioff[0]];
	};

	this.sub=item.sub;
	  if (!und(this.sub) && !und(this.sub.length)&& this.sub.length>0) {
	  	this.lvl=new CMenuLevel(menu, this);
		  for (var i=1;i<this.sub.length;i++) 
		    if (!und(this.sub[i]))
		      new CMenuItem(this.menu, this, this.sub[i], und(this.sub[i].format)?
		        this.sub[0]: this.sub[i].format);
		        
	  };

this.setPosFromParent(menu.itemindex);
return this;
};

function adiv(bw,name,z,left,top,width,height,bgc,code,otherCSS, otherDIV){
	return bw.ns4?'<layer align=left  id="'+name+'" z-index="'+z+'" left="'+left+
		'" top='+top+'" width="'+width+'" height="'+height+'"'+
		(bgc!=""?' bgcolor="'+bgc+'"':'')+
		(otherCSS?' style="'+otherCSS:'')+
		'" visibility="hidden" '+
		(otherDIV?otherDIV:'')+'>'+code+'</layer>\n':
			'<div align=left id="'+name+'" style="position:absolute; z-index:'+z+
			'; left:'+left+'px;top:'+
			top+'px;width:'+width+'px;height:'+height+'px;visibility:hidden'+
			(bgc!=""?';background-color:'+bgc+'':'')+';'+
			(otherCSS?otherCSS:'')+'" '+(otherDIV?otherDIV:'')+'>'+
			code+'</div>';
};

function visDivNS(d,s){
	if (!HSmenu.get_div(d)) return;
	HSmenu.get_div(d).visibility=s?'show':'hide';
};

function visDivDom(d,s){
	if (!HSmenu.get_div(d)) return;
	HSmenu.get_div(d).style.visibility=s?'visible':'hidden';
};

//function linkIt(url) { document.location.href=url; };

function mEvent(node_index, e) {
	if (nn(window.HSmenuHideTimers)) {
	  window.clearTimeout(window.HSmenuHideTimers);
	  window.HSmenuHideTimers=null;
	};
	switch(e){
	 case "o":window.HSmenu.items[node_index].par.lvl.setA(node_index,1);
		//window.status=url?document.location.href:'';

	   if (window.HSmenu.onmouseover)
	     window.HSmenu.onmouseover(window.HSmenu.items[node_index]);
	   break;
	 case "c":
	   if (window.HSmenu.items[node_index].hc())
	     window.HSmenu.items[node_index].lvl.vis(!window.HSmenu.items[node_index].lvl.v);
	   else
	     for (var i=0;i<window.HSmenu.root.cd.length;i++)
	       if (nn(window.HSmenu.root.cd[i].lvl))
	         window.HSmenu.root.cd[i].lvl.vis(0);
	       if (window.HSmenu.onclick)
	         window.HSmenu.onclick(window.HSmenu.items[node_index]);
	  break;
	 case "t":window.HSmenuHideTimers=setTimeout('window.HSmenu.hide()',
	 						und(window.HSmenu.root.fmt.delay)
							?600:window.HSmenu.root.fmt.delay);
		//window.status='';
	       if (window.HSmenu.onmouseout)
	         window.HSmenu.onmouseout(window.HSmenu.items[node_index]);
	  break;
	  };
return true;
};

function domPageX(el){
	var x=el.offsetLeft;
	var parent=el.offsetParent;
	  while(parent && parent!=document.body) {
	    x += parent.offsetLeft;
	    parent=parent.offsetParent;
	  };

return x;
};

function domPageY(el){
	var x=el.offsetTop;
	var parent=el.offsetParent;
	  while(parent && parent!=document.body) {
	    x += parent.offsetTop;
	    parent=parent.offsetParent;
	  };
return x;
};

	if (und(window.HSmenuHideTimers))
	  window.HSmenuHideTimers=[];
	  window.oldCMOnLoad=window.onload;

function CMOnLoad(){
	var bw=new bw_check();
	  if (bw.ns4 || bw.opera){
	    window.origWidth=window.innerWidth;
	    window.origHeight=window.innerHeight;

	  if (bw.opera && !window.operaResizeTimer) resizeHandler();

	  if (bw.ns4)
	    window.onresize=resizeHandler;
	  //} else {
	   // setTimeout('window.HSmenu.move();', 500);
	    //window.onresize=window.HSmenu.move();
	  };

	  if (typeof(window.oldCMOnLoad)=='function') window.oldCMOnLoad();

};

window.onload=new CMOnLoad();

function resizeHandler() {
	  if (window.reloading) return;
	var reload=window.innerWidth != window.origWidth ||
						window.innerHeight != window.origHeight;
	window.origWidth=window.innerWidth;
	window.origHeight=window.innerHeight;

	  if (reload) {
	    window.reloading=1;
	    document.location.reload();
	    return
	  };

	  if (new bw_check().opera)
	    window.operaResizeTimer=setTimeout('resizeHandler()',500);
};

function DomOnResize() {
	var c=null;
	var pos=[];
	var cpos=[];
	var offset=[0,0];
	var j=0;
	
	  var d=HSmenu.get_div(HSmenu.itemnames[1]);
	  pos[0]=HSmenu.bw.ns4?d.pageX:domPageX(d);
	  pos[1]=HSmenu.bw.ns4?d.pageY:domPageY(d);

   	  for (var k in HSmenu.divs) { j++;
	  c=HSmenu.get_div(HSmenu.divs[k]);
	  cpos[0]=HSmenu.bw.ns4?d.pageX:domPageX(c);
	  cpos[1]=HSmenu.bw.ns4?d.pageY:domPageY(c);
		  if (cpos[0] < HSmenu.parpos[1][0]) offset[0]=pos[0]-(HSmenu.parpos[1][0]-cpos[0]);
		  else offset[0]=pos[0]+(cpos[0]-HSmenu.parpos[1][0]);
		  
		  if (cpos[1] < HSmenu.parpos[1][1]) offset[1]=pos[1]-(HSmenu.parpos[1][1]-cpos[1]);
		  else offset[1]=pos[1]+(cpos[1]-HSmenu.parpos[1][1]);
		  
		  //if (j < 2) alert(offset);
  			c.style.left=offset[0];
  			c.style.top=offset[1];
		} 
		
   	  for (var i=1; i<HSmenu.itemnames.length; i++) {
	  	d=HSmenu.get_div(HSmenu.itemnames[i]);
	    pos[0]=HSmenu.bw.ns4?d.pageX:domPageX(d);
	    pos[1]=HSmenu.bw.ns4?d.pageY:domPageY(d);
   	    
   	    HSmenu.parpos[i][0]=pos[0];
   	    HSmenu.parpos[i][1]=pos[1];
   	  }
		
};


