var URL=self.location.pathname;
var thisPage;
var timerResize=null;
var winSizeChecksum=""; // used to work around IE onResize bug (fires repeatedly!)
var diffPageIframe=0; // constant - set onLoad

var showDebug = false;

function debug (s, b_Append) {
	if (!showDebug)
		return;
	else if ($.browser.msie)
		with (window) {
			if (!b_Append) defaultStatus="";
			else if (defaultStatus !="") defaultStatus+=" / ";
			defaultStatus+=s;
		}
}


var Pages = {
	Home: {
		path: '/index.asp'
	,	frameContentWidth: 0
	}
,	Solar: {
		path: '/products/solar.asp'
	,	frameContentWidth: 1041 // 1036 + 5
	}
,	Solar2: {
		path: '/products/cdn_solar.asp'
	,	frameContentWidth: 907 // 902 + 5
	}
,	Lighting: {
		path: '/products/lighting.asp'
	,	frameContentWidth: 1003 // 998 + 5
	}
,	GreenRoofs: {
		path: '/products/greenroofs.asp'
	,	frameContentWidth: 767 // 762 + 5
	}
,	Windows: {
		path: '/products/windows.asp'
	,	frameContentWidth: 602 // 597 + 5
	}
,	Rainwater: {
		path: '/products/rainwater.asp'
	,	frameContentWidth: 992 // 987 + 5
	}
,	Composting: {
		path: '/products/composting.asp'
	,	frameContentWidth: 992 // 987 + 5 - SAME as Rainwater
	}
}

// set thisPage object
switch ( URL.substr( URL.lastIndexOf("/")+1 ) ) {
	case "solar.asp":  		thisPage = Pages.Solar;			break;
	case "cdn_solar.asp":  	thisPage = Pages.Solar2;		break;
	case "lighting.asp":  	thisPage = Pages.Lighting;		break;
	case "greenroofs.asp":  thisPage = Pages.GreenRoofs;	break;
	case "windows.asp":  	thisPage = Pages.Windows;		break;
	case "rainwater.asp":  	thisPage = Pages.Rainwater;		break;
	case "composting.asp":  thisPage = Pages.Composting;	break;
	default:  				thisPage = Pages.Home;
}


$(document).ready( function () {
	initNavbar();

	if ($("#mainframe").length)
		resizeIframe(); // init iframe & sidebar sizes
	else
		resizeSidebar(); // init sidebar ONLY

	// attach event to catch resizing of window
	window.onresize = onWindowResize;
	//document.body.onresize = onWindowResize;
});


function initNavbar () {
	$("#topnav2 A").each( function (idx) {
		if (idx==0) { // 1st nav link (Home)
			if (URL.lastIndexOf("/")==0) {
				this.className="Current"; // HOMEPAGE
				return false; // BREAK EACH
			}
		}
		else if (URL.indexOf( $(this).attr('href') )>=0) {
			this.className="Current"; // PRODUCT PAGE
			return false; // BREAK EACH
		}
	});
}


function onWindowResize () {
	if (timerResize) clearTimeout( timerResize );
	var resize=(thisPage.frameContentWidth > 0) ? 'resizeIframe()' : 'resizeSidebar()';
	timerResize = setTimeout( resize, 200 );
}


function resizeSidebar () {
	// called only when there is no iframe, else resizeIframe() is called
	if ($.browser.msie) return; // only needed for Firefox (maybe other non-IE?)
	var table = $("#main table");
	var td_Sidebar=$("TD#left");
	// SET HEIGHT of the left sidebar to fit the table - so Firefox displays background all the way to bottom of table
	td_Sidebar.height( 'auto' ); // so table will auto-size before measuring it
	td_Sidebar.height( table.height() ); // resize sidebar cell to 100% height
}

function resizeIframe () {
	// get Window size
	var winWidth = getClientWidth();
	var winHeight = getClientHeight();
	if (!$.browser.msie) winHeight -= 3; // need -3 to fit in Firefox window

	// avoid continuous resize-event bug in IE by verifying that window size has really changed
	var checksum = winWidth * winHeight;
	if (winSizeChecksum != checksum) winSizeChecksum = checksum;
	else return; // unnecessary loop!

	// get Elements
	var table = $("#main table");
	var td_Sidebar=$("TD#left");
	var td_Iframe=$("TD#framecell");
	var iframe = $("#mainframe");

	// SET HEIGHT of the left sidebar to fit the table - so Firefox displays background all the way to bottom of table
	td_Sidebar.height( winHeight - table.offset().top );
	// SET HEIGHT of the iframe to fit the page
	iframe.height( winHeight - iframe.offset().top );

	winWidth = getClientWidth(); // MUST UPDATE winWidth AFTER setting 'heights' (above) so vertical scrollbar is gone (makes winWidth 'wider')
	var iframeWidth = iframe.width(); // ALT: iframe[0].offsetWidth
	if (!diffPageIframe) diffPageIframe = winWidth - iframeWidth; // calc winWidth required BEYOND iframe width - RUN ONCE ONLY

	// HIDE SIDEBAR to fit framed-page, if necessary
	if (iframeWidth < thisPage.frameContentWidth) {
		td_Sidebar.filter(":visible").hide().end(); // hide the sidebar
		td_Iframe.css({ paddingLeft: 0 }); // remove left-padding on iframe cell
	}
	// SHOW SIDEBAR if page is resized and now has enough width
	else if (winWidth >= (thisPage.frameContentWidth + diffPageIframe)) {
		td_Sidebar.filter(':hidden').show().end();
		td_Iframe.css({ paddingLeft: '' }); // restore left-padding on iframe cell
	}

	// DEBUG
	if (!showDebug)
		return;
	else if ($.browser.msie)
		debug(
			"iframe="+ iframe.width() +
			" / content="+ thisPage.frameContentWidth +
			" / req="+ (thisPage.frameContentWidth + diffPageIframe) +
			" / window="+ winWidth +
			""
		, false);
	else if (false) // DISABLED
		alert(
			"window="+ winWidth +"\n"+
			"req="+ (thisPage.frameContentWidth + diffPageIframe) +"\n"+
			"iframe="+ iframe.width() +"\n"+
			"content="+ thisPage.frameContentWidth +"\n"+
			""
		);
}


// HELPER FUNCTIONS

function getElementWidth (e) {
	if (typeof e=="string") e = $("#"+e);
	return (e.style.pixelWidth) ? e.style.pixelWidth : e.offsetWidth;
}

function getElementHeight (e) {
	if (typeof e=="string") e = $("#"+e);
	return (e.style.pixelHeight) ? e.style.pixelHeight : e.offsetHeight;
}


function getClientWidth () {
	var v=0, d=document, w=window;
	if ((!d.compatMode||d.compatMode=='CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
		v=d.documentElement.clientWidth;
	else if (d.body&&d.body.clientWidth)
		v=d.body.clientWidth;
	else if (xDef(w.innerWidth,w.innerHeight,d.height)) {
		v=w.innerWidth;
		if (d.height>w.innerHeight) v-=16;
	}
	return v;
}

function getClientHeight () {
	var v=0, d=document, w=window;
	if ((!d.compatMode||d.compatMode=='CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientHeight)
		v=d.documentElement.clientHeight;
	else if (d.body && d.body.clientHeight)
		v=d.body.clientHeight;
	else if (xDef(w.innerWidth,w.innerHeight,d.width)) {
		v=w.innerHeight;
		if (d.width>w.innerWidth) v-=16;
	}
	return v;
}


function xNum () {
	for (var i=0; i<arguments.length; ++i)
		if ( isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;
	return true;
}

function xStr (s) {
	for (var i=0; i<arguments.length; ++i)
		if (typeof(arguments[i])!='string') return false;
	return true;
}

function xDef () {
	for (var i=0; i<arguments.length; i++)
		if (typeof (arguments[i])=='undefined') return false;
	return true;
}

function xCamelize (cssPropStr) {
	var i, c, a = cssPropStr.split('-');
	var s = a[0];
	for (i=1; i<a.length; ++i) {
		c = a[i].charAt(0);
		s += a[i].replace(c, c.toUpperCase());
	}
	return s;
}

function xGetComputedStyle (e, p, i) {
	e=$(e)[0];
	if (!e) return null;
	var s, v = 'undefined', dv = document.defaultView;
	if (dv && dv.getComputedStyle) {
		s = dv.getComputedStyle(e,'');
		if (s) v = s.getPropertyValue(p);
	}
	else if (e.currentStyle)
		v = e.currentStyle[xCamelize(p)];
	else
		return null;
	return i ? (parseInt(v) || 0) : v;
}

function getOffsetWidth (e, w) {
	e=$(e)[0];
	if (!e) return 0;
	else if (e !=document && !e.tagName) return 0
	if (xNum(w)) {
		if (w<0) w = 0;
		else w=Math.round(w);
	}
	else w=-1;
	var css=xDef(e.style);
	if (e==document || e.tagName.toLowerCase()=='html' || e.tagName.toLowerCase()=='body') {
		w = xClientWidth();
	}
	else if (css && xDef(e.offsetWidth) && xStr(e.style.width)) {
		if (w>=0) {
			var pl=0,pr=0,bl=0,br=0;
			if (document.compatMode=='CSS1Compat') {
				var gcs = xGetComputedStyle;
				pl=gcs(e,'padding-left',1);
				if (pl !== null) {
					pr=gcs(e,'padding-right',1);
					bl=gcs(e,'border-left-width',1);
					br=gcs(e,'border-right-width',1);
				}
				// Should we try this as a last resort?
				// At this point getComputedStyle and currentStyle do not exist.
				else if (xDef(e.offsetWidth,e.style.width)) {
					e.style.width=w+'px';
					pl=e.offsetWidth-w;
				}
			}
			w-=(pl+pr+bl+br);
			if (isNaN(w)||w<0) return;
			else e.style.width=w+'px';
		}
		w=e.offsetWidth;
	}
	else if (css && xDef(e.style.pixelWidth)) {
		if (w>=0) e.style.pixelWidth=w;
		w=e.style.pixelWidth;
	}
	return w;
}

function getOffsetHeight (e,h) {
	if (!(e=xGetElementById(e))) return 0;
	else if (e !=document && !e.tagName) return 0
	if (xNum(h)) {
		if (h<0) h = 0;
		else h=Math.round(h);
	}
	else h=-1;
	var css=xDef(e.style);
	if (e==document || e.tagName.toLowerCase()=='html' || e.tagName.toLowerCase()=='body') {
		h = xClientHeight();
	}
	else if (css && xDef(e.offsetHeight) && xStr(e.style.height)) {
		if (h>=0) {
			var pt=0,pb=0,bt=0,bb=0;
			if (document.compatMode=='CSS1Compat') {
				var gcs = xGetComputedStyle;
				pt=gcs(e,'padding-top',1);
				if (pt !== null) {
					pb=gcs(e,'padding-bottom',1);
					bt=gcs(e,'border-top-width',1);
					bb=gcs(e,'border-bottom-width',1);
				}
				// Should we try this as a last resort?
				// At this point getComputedStyle and currentStyle do not exist.
				else if (xDef(e.offsetHeight,e.style.height)) {
					e.style.height=h+'px';
					pt=e.offsetHeight-h;
				}
			}
			h-=(pt+pb+bt+bb);
			if (isNaN(h)||h<0) return;
			else e.style.height=h+'px';
		}
		h=e.offsetHeight;
	}
	else if (css && xDef(e.style.pixelHeight)) {
		if (h>=0) e.style.pixelHeight=h;
		h=e.style.pixelHeight;
	}
	return h;
}
