function getElementsByClass(searchClass,node,tag) 
{
	var classElements = new Array();
  
 	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

	for (i=0, j=0; i < elsLen; i++) 
	{
		if (pattern.test(els[i].className)) 
		{
			classElements[j] = els[i];
			j++;
		}
	}
	
	return classElements;
}

ResizePromoBox = function()
{

	var trace = false; // when set to true, sends Trace alerts to page.
	
	ClearTrace();
    if (trace)
		Trace("Resize function called " + Date());
	
	 if (trace)
		Trace("myMinX: " + myMinX);
	
	/*
        Dynamically modifies (width and padding of) merchandising spots on homepage and other pages that use
        ProductMerchantCtrl User Control.
    */
    
	// declare this at the top of the pages instead   
	// var myMinX = 270;
    
	// loop through so we set each group of nuggets. If the next group doesn't exist it will terminate and therefore won't ever reach 100
	for (var c=0; c<100; c++) 
	{	
		if (trace)
			Trace("Currently on pass " + (c+1) + " (c = " + c + ")");
		
		if (c>0)
		{
			var promoBoxContainerId = "promoBoxCont" + c;
			var nuggetname = "nugget" + c;
			var nuggetclear = "nugget_clear";
		}
		else
		{
			var promoBoxContainerId = "promoBoxCont";
			var nuggetname = "nugget";
			var nuggetclear = "nugget_clear";
		}
		var promoBoxClassName = "midcolfl";
		
		if (!document.getElementById(promoBoxContainerId))
		{
			c = 12;
			return;
		}
	
		// this is the smallest width that the centre panel can shrink to
		var minContainerWidth = myMinX;
		var minPromoBoxWidth = myMinX;
		var promoboxMarginWidth = 10;
		
		// returns an array of promobox elements
		var promoBoxArray = getElementsByClass(nuggetname,null,"div");
		var promoBoxClearArray = getElementsByClass(nuggetclear,null,"div");
		var promoBoxCount = promoBoxArray.length	
		
		// find out what the current width of the centre panel is
		var centrePanel = document.getElementById(promoBoxContainerId);
		
		// a DOM bug in Firefox versions prior to 1.5 gives us an incorrect offsetWidth value - this sorts it out!
		if(is_fx && (parseInt(is_minor)<1.5)){centrePanel.style.position = "absolute";}
		var containerWidth = parseInt(centrePanel.offsetWidth);	
		if(is_fx && (parseInt(is_minor)<1.5)){centrePanel.style.position = "relative";}
		
		// compensate for a rendering bug in Opera by making the container width smaller
		if(navigator.userAgent.toLowerCase().indexOf('opera')!=-1){containerWidth -= 20}
	
		// a safety check to make sure we don't have a smaller centre panel than we expect
		if (containerWidth < minContainerWidth){containerWidth = minContainerWidth;}
		
		// stop container becoming larger than the window (400 is ~width of sidebars)
		if (containerWidth > (window.innerWidth-415))
		{
			containerWidth = window.innerWidth-415;	
		}
		
		
		
		
		if(trace)
		{
			Trace("window width: " + window.innerWidth);
			Trace("Center Panel width: " + centrePanel.offsetWidth);
			Trace("Container width: " + containerWidth);
			Trace("Promo box count: " + promoBoxArray.length);
		}
			
		// Work out how many promo boxes we can fit on each row
		var promoBoxesPerRow = parseInt(containerWidth / minPromoBoxWidth);	
		if (promoBoxesPerRow > 4){promoBoxesPerRow = 4;}	
		if (promoBoxesPerRow > promoBoxCount){promoBoxesPerRow = promoBoxCount;}
		
		/*
		
		// if we calculate 3 boxes per row, but end up with one box orphaned, revert to 2 boxes per row - looks neater
		if (((promoBoxCount%promoBoxesPerRow)==1) && promoBoxesPerRow==3){promoBoxesPerRow = 2}
		// if we calculate 4 boxes per row, but end up with 2 boxes orphaned, revert to 3 boxes per row - looks neater
		if (((promoBoxCount%promoBoxesPerRow)==2) && promoBoxesPerRow==4){promoBoxesPerRow = 3}
		
		*/
		
		if(trace)
		{
			Trace("Box Count Modulus: " + (promoBoxCount%promoBoxesPerRow));
		}
		
		// figure out how wide each promo box should be
		var newPromoBoxWidth = parseInt((containerWidth - ((promoBoxesPerRow-1)*promoboxMarginWidth)) / promoBoxesPerRow);
		
		
		
		// hack for FF rendering issue when there are only 2 boxes available and they both appear on one line
		// if((promoBoxCount == 2) && (promoBoxesPerRow == 2) && (is_fx)){newPromoBoxWidth -= 8}
	
	
		
		if(trace)
		{
			Trace("Boxes per row: " + promoBoxesPerRow);
			Trace("New promo box width: " + newPromoBoxWidth);
		}
		
		
		
		// iterate through the promo boxes applying width and margin to each
		for (var i=0;i<=promoBoxArray.length-1;i++){
			
			promoBoxClearArray[i].className = nuggetclear;
			
			// the end box of each row should be handled differently (ie; 0 margin)
			if (((i+1) / promoBoxesPerRow).toString().indexOf(".") == -1)
			{
				promoBoxArray[i].style.marginRight = 0;
				promoBoxClearArray[i].className += " clr "+nuggetclear+"maxwidth";
			}
			else
			{
				promoBoxArray[i].style.marginRight = promoboxMarginWidth + "px";
			}
			
			promoBoxArray[i].style.width = newPromoBoxWidth + "px";
			
			promoBoxArray[i].style.cssFloat = "left";
			promoBoxArray[i].style.marginBottom = "10px";
		
			if (trace)
				Trace("PromoBox ClassName: " + promoBoxClearArray[i].className);
				
		}	
	}
}


function Trace(message)
{
    if(!document.getElementById("promoBoxContDebug")){return}
    var traceBox = document.getElementById("promoBoxContDebug");
    traceBox.innerHTML += message + "<br />";
}

function ClearTrace()
{
    if(!document.getElementById("promoBoxContDebug")){return}
    var traceBox = document.getElementById("promoBoxContDebug");
    traceBox.innerHTML = "";
}

