////////////////////////////////////////////////////////////////////////////////
// Projekt:     $projekt.identity$
//              $projekt.identity.title$
//              http://www.x1seven.com/projekts/bamboo/
// Component:   $component.identity$
//              $component.identity.title$
// Version:     $Name: x17_sft_curium_1_0_1 $
//
// Copyright (c) $projekt.copyrightyears$, x1seven Pty. Ltd.
// All rights reserved
//
// License:     This library is free software; you can redistribute it and/or
//              modify it under the terms of the GNU Lesser General Public
//              License as published by the Free Software Foundation; either
//              version 2.1 of the License, or (at your option) any later
//              version.
//
//              This library is distributed in the hope that it will be useful,
//              but WITHOUT ANY WARRANTY; without even the implied warranty of
//              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//              GNU Lesser General Public License for more details.
//
//              You should have received a copy of the GNU Lesser General Public
//              License along with this library; if not, write to the Free
//              Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
//              Boston, MA  02110-1301  USA
//
// Contact:     For further information on x1seven Bamboo please refer to
//              http://www.x1seven.com/projekts/bamboo/contact.htm
//
// Title:       $file.title$
// Description: $file.description$
//
// CVS File:    $Source: /export/cvs_x17_sft/x17_sft_curium/core/source/coreimportclasses/com/x1seven/curium/curium_util.js,v $
// Revision:    $Revision: 1.6 $
// Date:        $Date: 2006/01/22 06:37:03 $
// Last Editor: $Author: brendonm $
//

	var g_regions = new Array();
	var g_regionNames = new Array();


	var m_curiumRulerDiv = null;
	function getCuriumRulerDiv() {

		if(m_curiumRulerDiv == null) {
			var rulerDiv = "<div id=\"curium_ruler\" style=\"position: absolute; top: 0px; left: 0px; width: 100%; \">&nbsp;</div>";
			document.body.innerHTML = rulerDiv + document.body.innerHTML;
		}

		m_curiumRulerDiv = document.getElementById("curium_ruler");

		return m_curiumRulerDiv;

	}

	function divHeight(id) {
		var element = document.getElementById(id);
		if(element) {
			return element.offsetHeight;
		}
		else {
			return 0;
		}
	}

	function getWindowWidth() {

		// Mozilla
		if(window.innerWidth) {
			var curiumRulerDiv = getCuriumRulerDiv();
			if(curiumRulerDiv.offsetWidth) {
				if(window.innerWidth != curiumRulerDiv.offsetWidth) {
					return curiumRulerDiv.offsetWidth;
				}
			}
			return (window.innerWidth);
		}


		// IE6
		if(document.documentElement.clientWidth) {
			return document.documentElement.clientWidth;
		}

		// IE DHTML-compliant any other
		if(document.body.clientWidth) {
			return document.body.clientWidth;
		}

	}

/*
	function getWindowWidth() {

		var windowWidth = 0;

		if(typeof(window.innerWidth) == 'number') {
			//Non-IE
			windowWidth = window.innerWidth;
		}

		else if(document.documentElement && (
			document.documentElement.clientWidth ||
			document.documentElement.clientHeight)) {

			//IE 6+ in 'standards compliant mode'
			windowWidth = document.documentElement.clientWidth;

		}

		else if(document.body && (
			document.body.clientWidth ||
			document.body.clientHeight)) {

			//IE 4 compatible
			windowWidth = document.body.clientWidth;
		}

		return windowWidth;

	}
*/

	/**
	 * Compare's an element's width and offsetWidth, and adjusts the width so
	 * that the offsetWidth equals the original width.
	 */
	function adjustWidthForOffset(id) {

		var region = document.getElementById(id);

		if(!region) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		// Get the numerical part of the width.  Assumes "px" suffix.
		var width = parseInt(
			region.style.width.substring(0, region.style.width.length - 2));

		// If the width and the offsetWidth disagree, then adjust the width
		if(width < region.offsetWidth) {
			var diff = (region.offsetWidth - width);
			region.style.width = width - diff;
		}

	}

	/**
	 * <p>
	 * Determines and returns the largest of the three supplied
	 * numerical values.
	 * </p>
	 */
	function max2(a, b) {
		if(a > b)
			return a;
		else
			return b;
	}

	/**
	 * <p>
	 * Determines and returns the largest of the three supplied
	 * numerical values.
	 * </p>
	 */
	function max3(a, b, c) {
		if(a > b && a > c)
			return a;
		else if(b > a && b > c)
			return b;
		else
			return c;
	}

	/**
	 * <p>
	 * Determines and returns the largest of the supplied
	 * numerical values.
	 * </p>
	 */
	function max4(a, b, c, d) {
		return max2(
			max2(a, b),
			max2(c, d));
	}

	/**
	 * <p>
	 * Determines and returns the largest of the supplied
	 * numerical values.
	 * </p>
	 */
	function max5(a, b, c, d, e) {
		return max2(
			max2(a, b),
			max3(c, d, e));
	}

	/**
	 * <p>
	 * Determines and returns the largest of the supplied
	 * numerical values.
	 * </p>
	 */
	function max6(a, b, c, d, e, f) {
		return max2(
			max3(a, b, c),
			max3(d, e, f));
	}

	function resizeDivLTW(id, left, top, width) {

		var div = document.getElementById(id);

		if(!div) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		div.style.left = left;
		if(top >= 0) div.style.top = top;
		if(width > 0) div.style.width = width;
		div.style.height = "";
		div.style.display = "";

	}

	function resizeDivH(id, height) {

		var div = document.getElementById(id);

		if(!div) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		if(height > 0) div.style.height = height;
		div.style.display = "";

	}

	function resizeDivLTWH(id, left, top, width, height) {

		var div = document.getElementById(id);

		if(!div) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		div.style.left = left;
		if(top >= 0) div.style.top = top;
		div.style.width = width;
		div.style.height = height;
		div.style.display = "";

	}


////////////////////////////////////////////////////////////////////////////////
// Set Up
//

	function curium_initializeDivAsVariableHeight(id) {

		var element = document.getElementById(id);

		if(!element) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		element.style.position = "absolute";
		element.style.overflowX = "hidden";

	}

	function curium_initializeDivAsFixedHeightNoScroll(id) {

		var element = document.getElementById(id);

		if(!element) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		element.style.position = "absolute";
		element.style.overflow = "hidden";

	}

	function curium_initializeDivAsFixedHeightVScroll(id) {

		var element = document.getElementById(id);

		if(!element) {
// TODO: Log a warning message to the JavaScript console
			return;
		}

		element.style.position = "absolute";
		element.style.overflow = "scroll";
		element.style.overflowX = "hidden";
		element.style.overflowY = "scroll";

	}

////////////////////////////////////////////////////////////////////////////////
// AsLoad Functionality
//

		var g_asloadLoopCount = 0;

		/**
		 * The AsLoad load loop is initiated as soon as possible after the page
		 * is loaded.
		 */
		function asload() {

			g_asloadLoopCount ++;
			var incomplete = false;

			for(var i = 0; i < g_regionNames.length; i ++) {

				if(!g_regions[i]) {

					var region = document.getElementById(g_regionNames[i]);

					if(region) {
						g_regions[i] = region;
/*
applyLayout(region);
*/
						curium_onload();
					}
					else {
						incomplete = true;
					}

				}

			}

			if(incomplete) {
				window.setTimeout("asload();", 1);
			}

/*
			else {
				alert("complete after " + g_asloadLoopCount + " iterations");
			}
*/

		}

/*
		function applyLayout(region) {
			region.style.backgroundColor = "#ff0000";
		}
*/

	g_regionNames[0] = "jumbo_banner";
	g_regionNames[1] = "global_navbar";
	g_regionNames[2] = "column1";
	g_regionNames[3] = "column2";
	g_regionNames[4] = "column3";
	g_regionNames[5] = "column4";
	function curium_onload() {

		curium_initializeDivAsFixedHeightNoScroll("jumbo_banner");
		curium_initializeDivAsFixedHeightNoScroll("global_navbar");
		curium_initializeDivAsVariableHeight("column1");
		curium_initializeDivAsVariableHeight("column2");
		curium_initializeDivAsVariableHeight("column3");
		curium_initializeDivAsVariableHeight("column4");

		curium_onresize();

	}

	function curium_onresize() {

		// Set up the browser-specific offset
		var offset = 0;
		if(navigator.appName.indexOf("Explorer") == -1) {
			//offset = 2;
		}

		// Get the window's width
		var windowWidth = getWindowWidth();

		// Work out how much free-space we'll have after fixed width columns
		// are layed out
		var totalFixedWidth = 645;
		var freeWindowWidth = windowWidth - totalFixedWidth;
		freeWindowWidth -= (offset * 9);


////////////////////////////////////////////////////////////////////////////////
// Calculate Column Widths
//

		////////////////////////////////
		// Col 1 (variable width)

		var col1_left = 0;
		var col1_minWidth = 1;
		var col1_width = max2(
			col1_minWidth,
			Math.round(freeWindowWidth / 100 * 50));

		////////////////////////////////
		// Col 2 (fixed width)

		var col2_left = col1_left + col1_width + offset;
		var col2_width = 150;

		////////////////////////////////
		// Col 3 (fixed width)

		var col3_left = col2_left + col2_width + offset;
		var col3_width = 15;

		////////////////////////////////
		// Col 4 (fixed width)

		var col4_left = col3_left + col3_width + offset;
		var col4_width = 150;

		////////////////////////////////
		// Col 5 (fixed width)

		var col5_left = col4_left + col4_width + offset;
		var col5_width = 15;

		////////////////////////////////
		// Col 6 (fixed width)

		var col6_left = col5_left + col5_width + offset;
		var col6_width = 150;

		////////////////////////////////
		// Col 7 (fixed width)

		var col7_left = col6_left + col6_width + offset;
		var col7_width = 15;

		////////////////////////////////
		// Col 8 (fixed width)

		var col8_left = col7_left + col7_width + offset;
		var col8_width = 150;

		////////////////////////////////
		// Col 9 (remainder)

		var col9_left = col8_left + col8_width + offset;
		var col9_minWidth = 1;
		var col9_width = max2(
			col9_minWidth,
			freeWindowWidth - (col1_width));


////////////////////////////////////////////////////////////////////////////////
// Resize Regions and Adjust Row Heights
//

		////////////////////////////////
		// Row 1

		// STEP 1 - Calculate the top value for the row.
		var row1_top = 0;
		var row1_height = 200;

		// STEP 2 - Apply the dimensions to rows ending in this row
		resizeDivLTW("jumbo_banner", col1_left, row1_top, col1_width + offset + col2_width + offset + col3_width + offset + col4_width + offset + col5_width + offset + col6_width + offset + col7_width + offset + col8_width + offset + col9_width);
		adjustWidthForOffset("jumbo_banner");
		// STEP 3 - Adjust this row's height to cover overflow of variable
		// height regions anchored to this row.
// (no variable-size regions anchored to this row)

		// STEP 4 - Reapply region heights
		resizeDivH("jumbo_banner", row1_height);

		////////////////////////////////
		// Row 2

		// STEP 1 - Calculate the top value for the row.
		var row2_top = row1_top + row1_height + offset;
		var row2_height = 20;

		// STEP 2 - Apply the dimensions to rows ending in this row
		resizeDivLTW("global_navbar", col1_left, row2_top, col1_width + offset + col2_width + offset + col3_width + offset + col4_width + offset + col5_width + offset + col6_width + offset + col7_width + offset + col8_width + offset + col9_width);
		adjustWidthForOffset("global_navbar");
		// STEP 3 - Adjust this row's height to cover overflow of variable
		// height regions anchored to this row.
// (no variable-size regions anchored to this row)

		// STEP 4 - Reapply region heights
		resizeDivH("global_navbar", row2_height);

		////////////////////////////////
		// Row 3

		// STEP 1 - Calculate the top value for the row.
		var row3_top = row2_top + row2_height + offset;
		var row3_height = 10;

		// STEP 2 - Apply the dimensions to rows ending in this row
// (no regions anchored to this row);

		// STEP 3 - Adjust this row's height to cover overflow of variable
		// height regions anchored to this row.
// (no variable-size regions anchored to this row)

		// STEP 4 - Reapply region heights
// (no variable-size regions anchored to this row

		////////////////////////////////
		// Row 4

		// STEP 1 - Calculate the top value for the row.
		var row4_top = row3_top + row3_height + offset;
		var row4_height = 50;

		// STEP 2 - Apply the dimensions to rows ending in this row
		resizeDivLTW("column1", col2_left, row4_top, col2_width);
		adjustWidthForOffset("column1");		resizeDivLTW("column2", col4_left, row4_top, col4_width);
		adjustWidthForOffset("column2");		resizeDivLTW("column3", col6_left, row4_top, col6_width);
		adjustWidthForOffset("column3");		resizeDivLTW("column4", col8_left, row4_top, col8_width);
		adjustWidthForOffset("column4");
		// STEP 3 - Adjust this row's height to cover overflow of variable
		// height regions anchored to this row.
		row4_height = max5(row4_height, divHeight("column1"), divHeight("column2"), divHeight("column3"), divHeight("column4"));

		// STEP 4 - Reapply region heights
		resizeDivH("column1", row4_height);
		resizeDivH("column2", row4_height);
		resizeDivH("column3", row4_height);
		resizeDivH("column4", row4_height);

	}

//window.onload = curium_onload;
window.onresize = curium_onresize;

// Launch the asload loop
window.setTimeout("asload();", 0);

