AutoLayout.Box = function (id, width, height, title, excerpt, imageUrl, textColor, backgroundColor, backgroundImage, borderWidth)
{
	this.id = id;
	this.width = width;
	this.height = height;
	this.borderWidth = borderWidth != undefined ? borderWidth : 0;
	this.title = title;
	this.isTextBox = false;
	//this.excerpt = excerpt;
	this.imageUrl = imageUrl;
	if(textColor != undefined && backgroundColor != undefined && textColor != "" && backgroundColor != "")
	{
		this.textColor = textColor;
		this.backgroundColor = backgroundColor;
		this.backgroundImage = "url('" + backgroundImage + "')";
	}
	else
	{
		this.textColor = Ali.settings.defaultTextColor;
		this.backgroundColor = Ali.settings.defaultBackgroundColor;
		this.backgroundImage = Ali.settings.defaultBackgroundImage;
	}
	this.element = this.createElement();
	this.titleElement = this.createTitleElement();
	this.x = 0;
	this.y = 0;
	this.page = 0;
	this.fading = false;
};
AutoLayout.Box.createTextBox = function(description, color, backgroundColor)
{
	var box = new AutoLayout.Box(-1, 400, 400, "", "", "", color, backgroundColor, "");
	box.isTextBox = true;
	box.backgroundImage = "none";
	box.titleElement.html(description);
	box.titleElement.css({
		fontSize: "14px",
		textAlign: "left",
		height: this.height,// - 2 * this.borderWidth,
		paddingTop: "0px",
		opacity: 1
	});
	box.element.append(box.titleElement);
	return box;
};

AutoLayout.Box.prototype.createTitleElement = function()
{
	titleElement = $(document.createElement("p"));
	titleElement.addClass("title");
	titleElement.html(this.title);
	//alert(titleElement.height());
	var h = this.height - 2 * this.borderWidth;
	//alert(this.borderWidth);
	var pt = .5 * (h - 50);
	titleElement.css({
		color: this.textColor,
		backgroundColor: this.backgroundColor,
		padding: pt + "px 0px 0px 0px",
		height: (h - pt) + "px"
	});
	return titleElement;
};
AutoLayout.Box.prototype.createElement = function()
{
	element = $(document.createElement("div"));
	element.load(function()
	{
		Ali.log("loaded");
	});
	element.addClass("autolayout_box");
	element.attr("id", "autolayout_box_" + this.id);
	this.updateElementDimensions();
	element.css("backgroundImage", "url('" + this.imageUrl + "')");
	element.html(this.content);
	
	return element;
};
AutoLayout.Box.prototype.updateElementDimensions = function()
{
	element.css(
	{
		width: this.width + "px",
		height: this.height + "px"
	});
};
AutoLayout.Box.prototype.getHighlightedHeight = function(boxSet)
{
	return this.highlightedHeight == null ? boxSet.config.highlightBoxHeight : this.highlightedHeight;
};
AutoLayout.GridCache = function(tileSize)
{
	this.cache = {};
	this.tileSize = tileSize;
};
AutoLayout.GridCache.prototype.clear = function()
{
	this.cache = {};
};
AutoLayout.GridCache.prototype.isFree = function(x, y, w, h)
{
	x = Math.floor(x / this.tileSize);
	y = Math.floor(y / this.tileSize);
	w = Math.floor(w / this.tileSize);
	h = Math.floor(h / this.tileSize);
	//Ali.log("checking " + x + ", " + y + " - " + w + ", " + h);
	for(var i = x; i < x + w; i++)
		for(var j = y; j < y + h; j++)
			if(!this._isSingleFree(i, j))
				return false;
	//Ali.log("pass");
	return true;
};
AutoLayout.GridCache.prototype._isSingleFree = function(x, y)
{
	//Ali.log(x + "x" + y);
	if(this.cache[y] == undefined)
		return true;
	if(this.cache[y][x] == undefined)
		return true;
	return this.cache[y][x];
};
AutoLayout.GridCache.prototype.setUsed = function(x, y, w, h)
{
	x = Math.floor(x / this.tileSize);
	y = Math.floor(y / this.tileSize);
	w = Math.floor(w / this.tileSize);
	h = Math.floor(h / this.tileSize);

	//Ali.log("setting used:  " + x + ", " + y + " - " + w + ", " + h);
	
	for(var i = x; i < x + w; i++)
		for(var j = y; j < y + h; j++)
			this._setOccupied(i, j);
};
AutoLayout.GridCache.prototype._setOccupied = function(x, y)
{
	if(this.cache[y] == undefined)
		this.cache[y] = {};
	this.cache[y][x] = false;
};
