/**
* med.govt.nz | layout helpers
* mark@shift.co.nz
*/
var pageBox;
var mode = 'narrow';

window.onload = function() {
	resetPageBox();
	(checkMode() == 'narrow') ? switchStylesheet('narrow', applyNarrowMode)
						 	  : switchStylesheet('wide', applyWideMode);
}

window.onresize = switchMode;

function $(name) {
	return document.getElementById(name);
}

function resetPageBox() {
	pageBox = $('page-frame').offsetHeight;
}

function checkMode() {
	return (document.body.clientWidth > 975) ? 'wide' : 'narrow';
}

function getExtraElm() {
	elm = $('homepage-extra');
	if (!elm) elm = $('page-extra');
	return elm;
}

function applyWideMode() {
	resetPageBox();
	elm = getExtraElm();
	if (elm) {
		elm.style.top = "40px";
		elm.style.height = pageBox-96+"px";
	}
	applyLayoutHeight();
}

function applyNarrowMode() {
	resetPageBox();
	elm = getExtraElm();
	if (elm) elm.style.height = "auto";
	applyLayoutHeight();
}

function applyLayoutHeight() {
	nHeight = (document.all) ? $('page-frame').offsetHeight-14 : $('page-frame').offsetHeight;
	
	//check if homepage or not
	if( $('home-gutter') )
	{
		$('home-gutter').style.height = nHeight + 208 + "px";
	}
	else
	{
		$('gutter').style.height = nHeight + 152 + "px";
	}
}

function switchStylesheet(toMode, applyMode) {
		if(toMode=='narrow')
		{
			$('layout-mode').href = $('layout-mode').href.replace(/wide/i,'narrow');
		}
		else
		{
			$('layout-mode').href = $('layout-mode').href.replace(/narrow/i,'wide');
		}
		mode = toMode;
		setTimeout(applyMode, 300);
}

function switchMode() {
	if(checkMode() != mode) {
		switch(mode) {
			case 'wide':   switchStylesheet('narrow', applyNarrowMode); break;
			case 'narrow': switchStylesheet('wide', applyWideMode); break;
		}
	}
}
