/*
 * Custom JScript for Quality Home Brew Supplies
 *
 * Copyright (c) 2009 21 Designs
 * $Date: 2009-07-17 12:00:00 +1000 (Sat, 17 July 2009) $
 * $Rev: 0.9 $
 *
 * NB: The site is basically a 3 column layout, however as the centre column contains
 * a variable number of boxes and heights we split these into there own entities to
 * ensure everything is positioned nicely on screen
 */

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();

/* Left column */
var columnOneHeight = 0;
/* The next 3 all reside in the center column */
var columnCenterHeight = 0;
var columnFlashHeight = 0;
var columnCenterBottomHeight = 0;
/* Right column */
var columnTwoHeight = 0;
/* Gaps between center columns */
columnGapsHeight = 0;
/* Variable for the adjusments later on */
var newHeight = 0;

/*
 *	Check each object exists, and if it does assign it's height to a variable
 *	With the DIV columnCenterBottom, we add it's current vertical position (offsetTop)
 *	to the height to get a true indication of how far down the page it really goes
 * Obviously this is broken into 2 parts: one for browsers that support the
 * 'document.all' reference and one for those that only support the
 * 'document' reference within the javascript code.
 */
if (BrowserDetect.browser == 'Explorer') {
	if (document.all["columnOne"]) { columnOneHeight = document.all["columnOne"].scrollHeight; }
	if (document.all["columnFlash"]) { columnFlashHeight = 270; }
	if (document.all["columnCenter"]) { columnCenterHeight = document.all["columnCenter"].scrollHeight; }
	if (document.all["columnCenterBottom"]) { columnCenterBottomHeight = document.all["columnCenterBottom"].scrollHeight; }
	if (document.all["columnTwo"]) { columnTwoHeight = document.all["columnTwo"].scrollHeight; }
	if (document.all["columnFlash"]) {
		if (document.all["columnCenter"]) {
			if (document.all["columnCenterBottom"]) {
				columnGapsHeight = 20;
			} else {
				columnGapsHeight = 10;
			}
		} else {
			if (document.all["columnCenterBottom"]) {
				columnGapsHeight = 10;
			}
		}
	} else {
		if (document.all["columnCenter"]) {
			if (document.all["columnCenterBottom"]) {
				columnGapsHeight = 10;
			}
		}
	}
}
else {
	if (document.getElementById("columnOne")) { columnOneHeight = document.getElementById("columnOne").scrollHeight; }
	if (document.getElementById("columnFlash")) { columnFlashHeight = 270; }
	if (document.getElementById("columnCenter")) { columnCenterHeight = document.getElementById("columnCenter").scrollHeight; }
	if (document.getElementById("columnCenterBottom")) { columnCenterBottomHeight = document.getElementById("columnCenterBottom").scrollHeight; }
	if (document.getElementById("columnTwo")) { columnTwoHeight = document.getElementById("columnTwo").scrollHeight; }
	if (document.getElementById("columnFlash")) {
		if (document.getElementById("columnCenter")) {
			if (document.getElementById("columnCenterBottom")) {
				columnGapsHeight = 20;
			} else {
				columnGapsHeight = 10;
			}
		} else {
			if (document.getElementById("columnCenterBottom")) {
				columnGapsHeight = 10;
			}
		}
	} else {
		if (document.getElementById("columnCenter")) {
			if (document.getElementById("columnCenterBottom")) {
				columnGapsHeight = 10;
			}
		}
	}
}

/* Here, we're checking how far down we have to go to align each column and storing the value in the variable 'newHeight' */
if (columnOneHeight > columnFlashHeight + columnCenterHeight + columnCenterBottomHeight + columnGapsHeight) {
	if (columnOneHeight > columnTwoHeight) {
		var newHeight = columnOneHeight - 28;
	}
	else {
		var newHeight = columnTwoHeight - 28;
	}
}
else {
	if (columnTwoHeight < columnFlashHeight + columnCenterHeight + columnCenterBottomHeight + columnGapsHeight) {
		var newHeight = columnFlashHeight + columnCenterHeight + columnCenterBottomHeight + columnGapsHeight - 28;
	}
	else {
		var newHeight = columnTwoHeight - 28;
	}
}

/* Now we adjust the height and position of any DIV's that are shorter than the others so all are aligned nicely on both top and bottom edges */
if (BrowserDetect.browser == 'Explorer') {
	if (document.all["columnOne"]) {
		document.all["columnOneWrapper"].style.height = newHeight + 'px';
		document.all["columnOneContent"].style.height = '100%';
	}
	if (document.getElementById("columnFlash")) {
		if (document.getElementById("columnFlash").className == 'columnFlashTop') {
			if (document.all["columnCenter"]) {
				document.all["columnCenter"].style.top = columnFlashHeight + 10 + 'px';
				if (!document.all["columnCenterBottom"]) {
					document.all["columnCenterWrapper"].style.height = newHeight - columnFlashHeight - columnGapsHeight + 'px';
				}
				document.all["columnCenterContent"].style.height = '100%';
			}
			if (document.all["columnCenterBottom"]) {
				document.all["columnCenterBottom"].style.top = columnFlashHeight + 10 + columnCenterHeight + 10 + 'px';
				document.all["columnCenterBottomWrapper"].style.height = newHeight - columnFlashHeight - columnCenterHeight - columnGapsHeight + 'px';
				document.all["columnCenterBottomContent"].style.height = '100%';
			}
		}
		if (document.getElementById("columnFlash").className == 'columnFlashMiddle') {
			document.all["columnFlash"].style.top = columnCenterHeight + 10 + 'px';
			if (document.all["columnCenterBottom"]) {
				document.all["columnCenterBottom"].style.top = columnFlashHeight + 10 + columnCenterHeight + 10 + 'px';
				document.all["columnCenterBottomWrapper"].style.height = newHeight - columnFlashHeight - columnCenterHeight - columnGapsHeight + 'px';
				document.all["columnCenterBottomContent"].style.height = '100%';
			}
		}
		if (document.getElementById("columnFlash").className == 'columnFlashBottom') {
			if (document.all["columnCenterBottom"]) {
				document.all["columnCenterBottom"].style.top = columnCenterHeight + 10 + 'px';
				document.all["columnCenterBottomWrapper"].style.height = newHeight - columnFlashHeight - columnCenterHeight - columnGapsHeight + 'px';
				document.all["columnCenterBottomContent"].style.height = '100%';
			}
		}
	}
	if (document.all["columnTwo"]) {
		document.all["columnTwoWrapper"].style.height = newHeight + 'px';
		document.all["columnTwoContent"].style.height = '100%';
	}
}
else {
	if (document.getElementById("columnOne")) {
		document.getElementById("columnOneWrapper").style.height = newHeight + 'px';
		document.getElementById("columnOneContent").style.height = '100%';
	}
	if (document.getElementById("columnFlash")) {
		if (document.getElementById("columnFlash").className == 'columnFlashTop') {
			if (document.getElementById("columnCenter")) {
				document.getElementById("columnCenter").style.top = columnFlashHeight + 10 + 'px';
				if (!document.getElementById("columnCenterBottom")) {
					document.getElementById("columnCenterWrapper").style.height = newHeight - columnFlashHeight - columnGapsHeight + 'px';
				}
				document.getElementById("columnCenterContent").style.height = '100%';
			}
			if (document.getElementById("columnCenterBottom")) {
				document.getElementById("columnCenterBottom").style.top = columnFlashHeight + 10 + columnCenterHeight + 10 + 'px';
				document.getElementById("columnCenterBottomWrapper").style.height = newHeight - columnFlashHeight - columnCenterHeight - columnGapsHeight + 'px';
				document.getElementById("columnCenterBottomContent").style.height = '100%';
			}
		}
		if (document.getElementById("columnFlash").className == 'columnFlashMiddle') {
			document.getElementById("columnFlash").style.top = columnCenterHeight + 10 + 'px';
			if (document.getElementById("columnCenterBottom")) {
				document.getElementById("columnCenterBottom").style.top = columnFlashHeight + 10 + columnCenterHeight + 10 + 'px';
				document.getElementById("columnCenterBottomWrapper").style.height = newHeight - columnFlashHeight - columnCenterHeight - columnGapsHeight + 'px';
				document.getElementById("columnCenterBottomContent").style.height = '100%';
			}
		}
		if (document.getElementById("columnFlash").className == 'columnFlashBottom') {
			if (document.getElementById("columnCenterBottom")) {
				document.getElementById("columnCenterBottom").style.top = columnCenterHeight + 10 + 'px';
				document.getElementById("columnCenterBottomWrapper").style.height = newHeight - columnFlashHeight - columnCenterHeight - columnGapsHeight + 'px';
				document.getElementById("columnCenterBottomContent").style.height = '100%';
			}
		}
	}
	if (document.getElementById("columnTwo")) {
		document.getElementById("columnTwoWrapper").style.height = newHeight + 'px';
		document.getElementById("columnTwoContent").style.height = '100%';
	}
}

/* Finally, reposition the footer DIV */
if (BrowserDetect.browser == 'Explorer') {
	document.all["columnFooter"].style.top = newHeight + 38 + 'px';
}
else {
	document.getElementById("columnFooter").style.top = newHeight + 38 + 'px';
}




















