/**
* Author: Nick Bogaerts 13/11/2007
* Description: Alters the number of rows per cells in the document
*
* CHANGELOG:
*
*/

function resize(bForced) {
	// Modified by a.charless on 22 july 2009 to fix the 1024 issue.	
	// var iThreshold = 1008;
	var iThreshold = 700;
	var iCellsBelow = 2;
	var iCellsAbove = 3;
	var sCellClass = "jsResize";
	var sContentClass = "content";
	var sGlobalContainerClass = "globalContainer"
	var sThresholdID = "extWidth";

	var oGlobalContainer = document.getElementsByClassName(sGlobalContainerClass, null, "DIV");
	if  (!oGlobalContainer.length) return false;
	oGlobalContainer = oGlobalContainer[0];
	var oContent = document.getElementsByClassName(sContentClass, oGlobalContainer, "DIV");
	if  (!oContent.length) return false;
	oContent = oContent[0];
	var iWidth = window.innerWidth;
	if(!iWidth) iWidth = document.body.clientWidth;
	if (bForced) oContent.id = "randomId";
	if ((bForced || oGlobalContainer.id) && iWidth < iThreshold) {
		//alert("2nd iWidth="+iWidth+"iThreshold="+iThreshold);
		oGlobalContainer.id = "";
		setCellsPerRow(iCellsBelow);
	} else if ((bForced || !oGlobalContainer.id) && iWidth >= iThreshold) {
		//alert("3rd iWidth="+iWidth+"iThreshold="+iThreshold);
		oGlobalContainer.id = sThresholdID;
		setCellsPerRow(iCellsAbove);
	}
	
	function setCellsPerRow(iCells) {
		if (iCells < 1) return false;
		var oCells = document.getElementsByClassName(sCellClass, oContent, "DIV");
		for (var i = 0; i < oCells.length; i++) {
			removeClass(oCells[i], "ienofloat");
			if (i / iCells == Math.round(i / iCells)) {
				if (i) addClass(oCells[i - 1], "ienofloat");
				addClass(oCells[i], "cleft");
			} else removeClass(oCells[i], "cleft");
		}
		return true;
	}
}

function jsfOnLoadSetSize(event) {
	resize(true);
}

function jsfOnWindowResize(event) {
	resize(false);
}

if (!isMSIE || MSIEVersion >= 6.0) jsfAttachEvent(window, "load", jsfOnLoadSetSize);
if (!isMSIE || MSIEVersion >= 6.0) jsfAttachEvent(window, "resize", jsfOnWindowResize);
