
function preload()
{
	// detect either mac or PC and serve content accordingly
	if(getbd().isMac && false)
	{
		window.onresize=function()
		{
			var s=phocus.DOM2.getWindowSize();
			var w=nw=s[0];
			var h=nh=s[1];
			var x,y;
			var nr=w/h;
			var r=800/600;
			if(nr>r)
				nh=w/r;
			else
				nw=h*r;
			x=((w-nw)/2);
			y=((h-nh)/2);
			
			var imgs=document.getElementById('bg').getElementsByTagName('img');
			for(var i=0;i<imgs.length;i++)
			{
				var op=imgs[i];
				op.style.position='absolute';
				op.style.width=Math.round(nw)+'px';
				op.style.height=Math.round(nh)+'px';
				op.style.left=Math.round(x)+'px';
				op.style.top=Math.round(y)+'px';
			}
		}
		
		window.onload=function()
		{
			window.onresize();
			
			maingal=new Gallery('bg');
			var mainmenu=document.getElementById('mainmenu');
			var lis=mainmenu.childNodes;
			// looping through the initial level of menu items
			for(var i=0;i<lis.length;i++)
			{
				var op=lis[i];
				if(op.tagName != "LI")
					continue;
				if(op.getElementsByTagName('ul').length)
					var menu=Dropdownmanager.add(new Dropdown(op));
				else
					continue;
				// looping through any sub-sub-menus that may exist
				var sublist=op.getElementsByTagName('ul')[0];
				var childer=sublist.childNodes;
				for(var j=0;j<childer.length;j++)
				{
					var subop=childer[j];
					if(subop == undefined || subop.tagName != "LI")
						continue;
					if(subop.getElementsByTagName('ul').length)
						Dropdownmanager.add(new Dropdown(subop),menu);
				}
			}
		}
	} else
	{
		// get the elements
		var imgmenu=document.getElementById('bgmenu');
		var imgs=imgmenu.getElementsByTagName('a');
		
		// create the flash object
		var so = new SWFObject("assets/swf/bg.swf", "background", "800", "600", "8", "#FFFFFF");
		so.addParam('wmode','opaque');
		// loop through the links
		for(var i=0;i<imgs.length;i++)
			so.addVariable('image'+(i+1),imgs[i].href);
		so.write("bg");
		
		// remove the HTML menu
		imgmenu.parentNode.removeChild(imgmenu);
		
		// create the onload function
		window.onload=function()
		{
			var mainmenu=document.getElementById('mainmenu');
			var lis=mainmenu.childNodes;
			// looping through the initial level of menu items
			for(var i=0;i<lis.length;i++)
			{
				var op=lis[i];
				if(op.tagName != "LI")
					continue;
				if(op.getElementsByTagName('ul').length)
					var menu=Dropdownmanager.add(new Dropdown(op));
				else
					continue;
				// looping through any sub-sub-menus that may exist
				var sublist=op.getElementsByTagName('ul')[0];
				var childer=sublist.childNodes;
				for(var j=0;j<childer.length;j++)
				{
					var subop=childer[j];
					if(subop == undefined || subop.tagName != "LI")
						continue;
					if(subop.getElementsByTagName('ul').length)
						Dropdownmanager.add(new Dropdown(subop),menu);
				}
			}
		}
	}
}


// gallery
Gallery=function(name)
{
	this.name=name;
	this.menu=document.getElementById(this.name+'menu');
	this.element=document.getElementById(this.name);
	
	var as=this.menu.getElementsByTagName('a');
	for(var i=0;i<as.length;i++)
	{
		var op=as[i];
		var href=op.href;
		op.href='javascript:void(0);';
		op.p=this;
		op.title=href;
		op.onmouseup=function()
		{
			this.p.goimg(this.title);
		}
	}
}
$pr=Gallery.prototype;
$pr.goimg=function(href)
{
	// make the old image transparent
//	var oldimg=this.element.getElementsByTagName('img')[0];
//	this.setopacity(0.5,oldimg);
	
	// add the preloader
	this.pl=phocus.DOM2.addNode(document.body,'div')[0];
	this.pl.className='galpl';
	
	// disable buttons
	var as=this.menu.getElementsByTagName('a');
	for(var i=0;i<as.length;i++)
		as[i].onmouseup='';
	
	// add the new image
	var img=phocus.DOM2.addNode(this.element,'img')[0];
	img.p=this;
	img.onload=function()
	{
		this.p.animateup(this);
	}
	img.src=href;
	this.setopacity(0,img);
}
$pr.animateup=function(img)
{
	// make the old image opaque
//	var oldimg=this.element.getElementsByTagName('img')[0];
//	this.setopacity(1,oldimg);
	
	// removing the preloader
	phocus.DOM2.removeNode(this.pl);
	
	this.setopacity(0,img);
	window.onresize();
	
	this.animator=new phocus.Animator(img,null,0,1,10);
	this.animator.round=false;
	this.animator.method='setopacity';
	this.animator.object=this;
	this.animator.addListener(this,'finished');
	this.animator.run();
}
$pr.setopacity=function(v,img)
{
	if(typeof img.filters=='object')
	{
		if(img.filters.length>0 && typeof img.filters.alpha=='object' && typeof img.filters.alpha.opacity=='number')
			img.filters.alpha.opacity=Math.round(v*100);
	}else
	{
		img.style.opacity=Math.round(v*1000)/1000;
	}
}
$pr.finished=function(img)
{
	var imgs=this.element.getElementsByTagName('img');
	for(var i=0;i<imgs.length;i++)
	{
		if(imgs[i]!=img)
			imgs[i].parentNode.removeChild(imgs[i]);
	}
	
	// re-enable buttons
	var as=this.menu.getElementsByTagName('a');
	for(var i=0;i<as.length;i++)
	{
		var op=as[i];
		op.onmouseup=function()
		{
			this.p.goimg(this.title);
		}
	}
}

// dropdown
Dropdownmanager=
{
	ds:[],
	add:function(d,p)
	{
		if(p!=undefined && p.add != undefined)
		{
			return p.add(d);
		}
		d.manager=this;
		this.ds.push(d);
		return d;
	},
	closeall:function()
	{
		for(var i=0;i<this.ds.length;i++)
		{
			if(this.ds[i].isopen && !this.ds[i].mouseover)
				this.ds[i].close(true);
		}
	}
};
function Dropdown(element_li)
{
	this.issub=false;
	this.ds=[];
	this.add=function(d)
	{
		this.hassub=true;
		var as=d.dropdown.parentNode.getElementsByTagName('a');
		as[0].className='hassub';
		d.issub=true;
		d.manager=this;
		this.ds.push(d);
		return d;
	}
	this.closeall=function()
	{
		for(var i=0;i<this.ds.length;i++)
		{
			if(this.ds[i].isopen && !this.ds[i].mouseover)
				this.ds[i].close(true);
		}
	}
	
	this.op=element_li;
	var as=this.op.getElementsByTagName('a');
	this.but=as[0];
//		as[0].style.width=(as[0].offsetWidth)+'px';

	this.outclass=this.but.className;
	this.dropdown=this.op.getElementsByTagName('ul')[0];
	this.dstyle=this.dropdown.style;
	
	var p=this;
	for(var i=0;i<as.length;i++)
	{
		as[i].onmouseover=function()
		{
			p.open();
		}
		as[i].onmouseout=function()
		{
			p.close();
		}
	}
	
	this.running=false;
	this.isopen=false;
	this.mouseover=false;
}
$pr=Dropdown.prototype;
$pr.open=function()
{
	if(this.mouseover)
		return;
	this.mouseover=true;
	this.but.className=this.outclass+' hover';
	this.dropdown.className='over';
	if(this.issub)
	{
		var a=this.op.getElementsByTagName('a')[0];
		a.className+=' over';
	}
	if(!this.isopen)
	{
		this.isopen=true;
		var h = this.dropdown.offsetHeight;
		this.dstyle.height='1px';
		this.dstyle.overflow='hidden';
		if((getbd().isMac && getbd().isFirefox))
		{
			this.endopen(true);
		}else if(typeof phocus == 'object')
		{
			this.run=new phocus.Animator(this.dstyle,'height',1,h,5);
			this.run.transform='%%val%%px';
			this.run.round=true;
			this.run.addListener(this,'endopen');
			this.run.run();
			this.running=true;
//			this.run=phocus.Runtime.addrun(this,'runclose',[1,h],0,10,this,this.endopen);
		}
		else
			this.endopen();
		this.manager.closeall();
		this.dstyle.zIndex='101';
	} else
	{
		this.endopen();
	}
	clearInterval(this.closer);
}
$pr.close=function(override,interval)
{
	this.mouseover=false;
	if(this.issub)
		this.manager.close(true,500);
	
	if(override)
	{
		clearInterval(this.closer);
		this.killanimator();
	}
	
	if(this.isopen)
	{
		if((getbd().isMac && getbd().isFirefox) || getbd().isOpera)
		{
			this.endclose(true);
			this.but.className=this.outclass;
		} else
		{
			var _doclose=this.doclose;
			var _obj=this;
			this.closer=setInterval(function(){_doclose.apply(_obj)},interval ? interval : override ? 10 : 400);
			this.dstyle.zIndex='100';
		}
	}
}
$pr.doclose=function()
{
	if(!this.mouseover)
	{
		var h = this.dropdown.offsetHeight;
		this.dstyle.overflow='hidden';
		this.dstyle.height=h+'px';
		if((getbd().isMac && getbd().isFirefox) || getbd().isOpera)
		{
			this.endclose(true);
		} else if(typeof phocus == 'object')
		{
			this.run=new phocus.Animator(this.dstyle,'height',h,1-h,3);
			this.run.transform='%%val%%px';
			this.run.round=true;
			this.run.addListener(this,'endclose');
			this.run.run();
			this.running=true;
//			this.run=phocus.Runtime.addrun(this,'runclose',[h,1-h],0,4,this,'endclose');
		} else
			this.endclose();
		this.but.className=this.outclass;
//		this.dropdown.className='out';
	}
	clearInterval(this.closer);
}
$pr.endclose=function(override)
{
	if(this.issub)
		this.op.lastChild.previousSibling.className=this.op.lastChild.previousSibling.className.split(' ')[0];
	this.running=false;
	this.dstyle.height='';
	this.isopen=false;
	this.dropdown.className='';
}
$pr.endopen=function(override)
{
	if(this.mouseover==false)
		this.close(true);
	
	if(this.issub && this.mouseover)
		this.manager.open();
	this.running=false;
	this.dstyle.overflow='visible';
	this.dstyle.height='';
	this.killanimator();
}
$pr.killanimator=function()
{
	if(this.run != null && typeof this.run != 'undefined' && this.run.kill != 'undefined')
	{
		this.run.kill();
		this.run=null;
	}
}
delete $pr;

