//= require <mootools-core-1.3-full-nocompat>
//= require <uiframework/Panel>
//=  require <uiframework/URLUtil>
var BottomRightColumn = function()
{
	this.Extends = Panel;
	this.states = /^(category|listing|action|home)$/;

	this.initialize = function()
	{
		this.parent("bottomRightColumn");
		this.borderData(
		{
			topLeft : "img/top-left-lower-right-column.png",
			top : "img/border-height35px.png"
		});
		this.backgroundStyle(
		{
			"background" : '#A5B7C9'
		});
		this.content = {};
	};

	this.updateDisplayList = function(width, height)
	{
		this.parent(width, height);
		switch (this.currentState)
		{
			case "listing":
			case "category":
			case "home":
				this.layoutHomeContent();
				break;
		}
	};

	/**
	 * Sets the initial state of this control depending on the properties in the
	 * hash object or search string if present.
	 * 
	 * @param hash
	 *            Object containing the key => value pairs that make up the
	 *            hash.
	 */
	this.setInitializationState = function(hash /* Object */)
	{
		var state;
		for ( var key in hash)
		{
			if (this.states.test(key))
			{
				state = key;
				break;
			}
		}
		if (!state && location.search)
		{
			this.setStateBySearch();
			return;
		}
		else
			state = "home";
		this.state(state);
	};

	this.setStateBySearch = function()
	{
		var hashObj = URLUtil.searchStringToObject(location.search);
		for ( var key in hashObj)
		{
			if (this.states.test(key))
			{
				this.state(key);
				return;
			}
		}
		this.state("home");
	};

	this.layoutHomeContent = function()
	{
		var padding = 20;
		this.recentlyAddedHeader.move(padding, padding);

		this.recentListings.setActualSize(this.width() - padding, this.height() - this.recentlyAddedHeader._y - this.recentlyAddedHeader.height() - padding);
		this.recentListings.move(padding, this.recentlyAddedHeader._y + this.recentlyAddedHeader.height() + 10);
	};

	this.commitNewState = function()
	{
		this.parent();
		switch (this.newState)
		{
			case "listing":
			case "category":
			case "home":
				this.showHomeContent(true);
				break;
		}
	};

	this.showHomeContent = function(show)
	{
		if (!this.recentListings && !show)
			return;
		if (!this.recentListings)
		{
			var domElement = $("recentListings") || 'div';
			var options = domElement != "div" ? null : this.content['recentListings'];
			this.recentListings = new UIComponent(domElement, options);

			domElement = $('recentlyAddedHeader') || 'h3';
			options = domElement != 'h3' ? null : this.content['recentlyAddedHeader'];
			this.recentlyAddedHeader = new UIComponent(domElement, options);
		}
		var operation = show ? this.addChild.bind(this) : this.removeChild.bind(this);
		operation(this.recentListings);
		operation(this.recentlyAddedHeader);
	};
};
BottomRightColumn = new Class(new BottomRightColumn());
