// Yes these are hardcoded.  Get over it.
var cellHeight = 140, cellWidth = 163;
var numCols = 6;
var cells = [];
var addedItems = [];

function positionItems(container, items, jq)
{
	var itemsLeft = items.slice(0);
	container.html(" ");
	
	// Go through each element, find somewhere to put it
	while (itemsLeft.length > 0)
	{
		var rawItem = itemsLeft.shift();
		var item = rawItem.html;
		var $elem = jq(item);
		var iw = rawItem.iw, ih = rawItem.ih;
		var itemWidth = Math.ceil(iw / cellWidth);
		var itemHeight = Math.ceil(ih / cellHeight);
	
		// Find the position that has this available width AND height
		var foundRow = false;
		var xPos = 0, yPos = 0;
	
		// Go through each row...
		for (var y = 0; y < cells.length && !foundRow; ++y)
		{
			var r = cells[y];
			for (var x = 0; x < r.length && !foundRow; ++x)
			{
				// Find a starting space
				if (r[x] == 0 && (numCols >= x+itemWidth))
				{
					var cellFull = false;
					for (var yCell = y; yCell < y+itemHeight && !cellFull; ++yCell)
					{
						if (cells[yCell] == null)
							cellFull = true;	// New empty row, we can definitely fit it here
						else
						{
							for (var xCell = x; xCell < x+itemWidth && !cellFull; ++xCell)
								cellFull = (cells[yCell][xCell] == 1);
						}
					}
	
					if (!cellFull)
					{
						xPos = x;
						yPos = y;
						foundRow = true;
						markCellsFull(xPos, yPos, itemWidth, itemHeight);
					}
				}
			}
		}
	
		// If we didn't find something, need to create new row(s) and place it there
		//  Note we only do this if the item is not a filler - we don't create new rows for unecessary content
		if (!foundRow && rawItem.filler != "1")
		{
			xPos = Math.floor(Math.random()*(numCols-itemWidth+1));
			yPos = cells.length;
			markCellsFull(xPos, yPos, itemWidth, itemHeight);
			foundRow = true;
		}
	
		if (foundRow)
			addItemAtPosition(container, $elem, xPos*cellWidth, yPos*cellHeight, true);
		else
			addItemAtPosition(container, $elem, -1000, -1000, false);		// Move it out of the way if we don't want it
	}
	
//	container.css({height: cells.length*cellHeight});
	if (isMobile) {
		container.css({height: cells.length*cellHeight});
	} else {
		container.animate({height: cells.length*cellHeight}, "slow");
	}
}

function markCellsFull(xPos, yPos, itemWidth, itemHeight)
{
	for (var yCell = yPos; yCell < yPos+itemHeight; ++yCell)
	{
		if (cells[yCell] == null)
		{
			var newRow = [];
			for (var x = 0; x < numCols; ++x)
			newRow.push( (x >= xPos && x < itemWidth+xPos) ? 1 : 0);
			
			cells.push(newRow);
		}
		else
		{
			for (var xCell = xPos; xCell < xPos+itemWidth; ++xCell)
			{
				cells[yCell][xCell] = 1;
			}
		}
	}
}


function addItemAtPosition(container, item, x, y, animate)
{
	container.append(item);
	
	if (animate && !isMobile)	// isMobile comes form scripts.js.  Yucky.
	{
		// Start top middle...
		item.css({position:"absolute", left:x, top: Math.max(0, y-300), opacity:0});
		
		// And animate into position
		item.delay(250+(y*0.7)).animate({top:y, opacity:1}, 'fast');
	}
	else
	{
		item.css({position:"absolute", left:x, top: y, opacity:1});
	}
	
	addedItems.push(item);
}

function clearAllItems(container)
{
	if (isMobile) {
		container.html(" ");
	} else {
		// Go through every item and shrink it!
		for (var i = 0; i < addedItems.length; ++i)
		{
			var item = addedItems[i];
			var t = 0;
			try {	// Sometimes fails in IE
				var t = item.position().top;
			} catch (ex) { }
			item.animate({top:Math.max(0, t-300), opacity:0}, 'fast');
			
	//		item.animate({width:0, height:0, left:l+(item.outerWidth()/2), top:t+(item.outerHeight()/2)}, 'slow');
	//		item.animate({left:-1000}, 'slow');
		}
		
		container.animate({height: 200}, "slow");
	}
	
	cells = [];
}
