//Page init
function body_Load()
{
	if(null===document.getElementById(IDPREFIX+'ContainerIFrame'))
	{
		body_Load_Box();
	}
	else
	{
		body_Load_Frames();
	}
	document.body.parentNode.style.overflow='hidden';
}
/****FRAMED BEGIN****/
//Previous height of the iframe
var oldHeight=0;
//Scrolls the contents frame (within the iframe) to the correct position
function scrollContent_Frames()
{
	var location=document.location.href;
	var pound=location.indexOf('#');
	if((-1!==pound)&&(null!==document.getElementById(IDPREFIX+'ContainerIFrame')))
	{
		var content=window.LegislationFrameset.getElementsByTagName('frame')[0];
		content.src=content.src+location.substr(pound);
	}
}
//Navigations the two inner frames upon anchor clicks on the main menu.
function doInnerNavigation(e)
{
	var evt,src,frames,framesLength,i,hash;
	evt=new CommonEvent(e);
	frames=window.LegislationFrameset.childNodes[1].childNodes[1].childNodes;
	framesLength=frames.length;
	hash=evt.sender.href.substr(evt.sender.href.indexOf('#'));
	for(i=0;i<framesLength;i++)
	{
		src=frames[i].src;
		if(-1===src.indexOf('#'))
		{
			frames[i].src+=hash;
		}
		else
		{
			src=src.substr(0,src.indexOf('#'));
			frames[i].src=src+hash;
		}
	}
	evt.cancel();
	return false;
}
function navigate(e)
{
	var evt,frames;
	evt=new CommonEvent(e);
	if('a'===evt.sender.tagName.toLowerCase())
	{
		if(true===window.IE)
		{
			frames=window.LegislationFrameset.frames;
			frames[0].document.location.replace(evt.sender.href);//TODO: fix this, previous location still gets stored.
		}
		else
		{
			frames=window.LegislationFrameset.getElementsByTagName('frame');
			frames[0].contentDocument.location.replace(evt.sender.href);
		}
		evt.cancel();
		return false;
	}
}
//Resizes the container iframe, attaches event listener to the window resize event and scrolls the contents to the desired location
function body_Load_Frames()
{
	var hash,iframe,frames,legislationTopMenu,legislationTopMenuAnchors,legislationTopMenuAnchorsLength,i,href;
	hash=document.location.hash;
	iframe=document.getElementById(IDPREFIX+'ContainerIFrame');
	if('object'===typeof iframe.contentDocument)
	{
		window.LegislationFrameset=iframe.contentDocument.getElementById('LegislationFrameSet');
	}
	else if(true===window.IE)
	{
		window.LegislationFrameset=(document.frames[IDPREFIX+'ContainerIFrame'].document).getElementById('content').parentNode.document;
	}
	else{return;}
	resizeHandler_Frames(null);
	attachEventListener('resize',window,resizeHandler_Frames);
	if(1<hash.length)
	{
		setTimeout(scrollContent_Frames,0);
	}
	if(true===window.IE)
	{
		frames=window.LegislationFrameset.frames;
		frames[0].document.childNodes[1].childNodes[1].style.width='88%';
		frames[1].document.childNodes[1].childNodes[1].style.width='88%';
		frames[0].document.childNodes[1].childNodes[1].style.paddingRight='2em';
		frames[1].document.childNodes[1].childNodes[1].style.paddingRight='1em';
		attachEventListener('click',frames[1].document.childNodes[1].parentNode,navigate);
	}
	else
	{
		frames=window.LegislationFrameset.getElementsByTagName('frame');
		frames[0].contentDocument.childNodes[1].childNodes[1].style.width='88%';
		frames[1].contentDocument.childNodes[1].childNodes[1].style.width='88%';
		frames[0].contentDocument.childNodes[1].childNodes[1].style.paddingRight='1em';
		frames[1].contentDocument.childNodes[1].childNodes[1].style.paddingRight='1em';
		attachEventListener('click',frames[1].contentDocument.childNodes[1].parentNode,navigate);
	}
	legislationTopMenu=document.getElementById('LegislationTopMenu');
	legislationTopMenuAnchors=legislationTopMenu.getElementsByTagName('a');
	legislationTopMenuAnchorsLength=legislationTopMenuAnchors.length;
	if('law'===document.location.href.substring(6+document.location.href.indexOf('?type='),document.location.href.indexOf('#')))
	{
		for(i=0;i<legislationTopMenuAnchorsLength;i++)
		{
			href=legislationTopMenuAnchors[i].href;
			if('law'===href.substring(6+href.indexOf('?type='),href.indexOf('#')))
			{
				attachEventListener('click',legislationTopMenuAnchors[i],doInnerNavigation);
			}
		}
	}
}
//Changes height and width of the container iframe
function resizeHandler_Frames(e)
{
	var container,height,diff,legislationHeader,underline;
	container=document.getElementById(IDPREFIX+'ContainerIFrame');
	if(null===container)
	{
		return;
	}
	container.style.width=container.offsetWidth+container.offsetLeft+'px';
	//Gecko uses window.innerHeight, MSIE uses the html's element offsetHeight
	height=(window.innerHeight)?window.innerHeight:document.getElementsByTagName('html')[0].offsetHeight;
	diff=oldHeight-height;
	if(0===diff)
	{//MSIE fires the resize events waaay too many times
		return;
	}
	container.height=(height-200)+'px';
	oldHeight=height;
	legislationHeader=document.getElementById(IDPREFIX+'LegislationHeader');
	underline=legislationHeader.nextSibling;
	legislationHeader.style.padding='0px 0px 0px 0px';
	legislationHeader.style.margin='0px 0px 0px 0px';
	underline.style.margin='-12px 0px 0px 0px';
	container.style.margin='-10px 0px 0px 0px';
	document.getElementById('Disclaimer').style.margin='0px 0px 0px 0px';
}
/****FRAMED END****/
/****BOXED BEGIN****/
//Page initialization
function body_Load_Box()
{
	var htmlElement=document.body.parentNode;
	htmlElement.style.overflow='hidden';
	resizeHandler_Box(null);
	attachEventListener('resize',window,resizeHandler_Box);
}
function resizeHandler_Box(e)
{
	var htmlElement,divs,legislationHeader,underline,disclaimer;
	legislationHeader=document.getElementById(IDPREFIX+'LegislationHeader');
	underline=legislationHeader.nextSibling;
	disclaimer=document.getElementById('Disclaimer');
	underline.style.position='relative';
	underline.style.top='-10px';
	legislationHeader.style.margin='-10px 0px 0px 0px';
	disclaimer.style.margin='0px 0px 0px 0px';
	htmlElement=document.body.parentNode;
	divs=document.body.getElementsByTagName('div');
	for(var i=0;i<divs.length;i++)
	{
		if(hasClass('LegislationContent LegislationMenu LegislationContainer',divs[i]))
		{
			divs[i].style.height=(htmlElement.clientHeight-180)+'px';
			if(hasClass('LegislationContainer',divs[i]))
			{
				divs[i].style.width=(htmlElement.clientWidth)+'px';
			}
			divs[i].style.margin='-10px 0px 0px 0px';
		}
	}
}
