(function(){
/*
	Class: Box 
	The base data object for a Box
*/
function Box() {
	this.container = null;
	this.item_list = new window['SB']['util']['List'];
	this.share_list = new window['SB']['util']['List'];
}

Box.prototype.toString = function() { return "[SB.core.Box]"; };

//Group: Basic Methods 
/*
	Function: setConfig
	Establishes the basic setup for the Box.
	
	inConfig - A configuration array (key/value)

	Configuration Array Options:
	name - The name of the Box 
*/
Box.prototype.setConfig = function( inConfig )
{

	if( inConfig.name )
		this.name = inConfig.name;

	if( inConfig.pallet_id )
		this.container_id = inConfig.pallet_id;

	if( inConfig.crate_id )
		this.id = this.box_id = inConfig.crate_id;

	if( inConfig.box_id )
		this.box_id = this.id = inConfig.box_id;

	if( inConfig.container_id )
		this.container_id = inConfig.container_id;

	if( inConfig.id )
		this.id = this.box_id = inConfig.id;

	if( inConfig.view_type )
		this.view_type = parseInt(inConfig.view_type);

	if( inConfig.owner_username )
		this.owner_username= inConfig.owner_username;

	if( inConfig.description )
		this.desc = inConfig.description;
	else
		this.desc = "";

	if( inConfig.shared )
		this.shared = inConfig.shared;

	if( inConfig.public_sharing )
		this.public = inConfig.public_sharing;
	else
		this.public = "0";

	if( inConfig.num_shared )
		this.num_shared = inConfig.num_shared;
	else
		this.num_shared = 0;

	if( inConfig.num_items )
		this.num_items = inConfig.num_items;
	else
		this.num_items = 0;

	if( inConfig.is_sharing_allowed )
		this.is_sharing_allowed = inConfig.is_sharing_allowed;
	else
		this.is_sharing_allowed = true;	
	
	if( inConfig.services )
	{
		for( var i = 0; i < inConfig.services.length; i++ )
		{
			if( inConfig.services[i].service_id == "1" )
			{
				this.twitter = inConfig.services[i];
				this.paired = true;
			}

			else if( inConfig.services[i].service_id == "2" )
			{
				this.delicious = inConfig.services[i];
				this.paired = true;
			}
			else if( inConfig.services[i].service_id == "3" )
			{
				this.tumblr = inConfig.services[i];
				this.paired = true;
			}
			else if( inConfig.services[i].service_id == "4" )
			{
				this.diigo = inConfig.services[i];
				this.paired = true;
			}


		}
	}
	else
	{
		this.twitter = null;
		this.delicious = null;
		this.tumblr = null;
		this.diigo = null;
		this.paired = false;
	}

	if( inConfig.disable_update)
		this.disable_update= inConfig.disable_update;
	else
		this.disable_update= 0;


	//SButil.EventManager.addListener("SEBoxUpdate", onSEBoxUpdate, this);
};

/*
	Function: setParent
	Points the internal reference to the Container that owns it

	*Should only be called by a Container or from Box itself*
*/
Box.prototype.setParent = function( inObj ) {
	this.container = inObj;
};




/*
	Function: move
	Assigns the box to a different container

	toContainer - destination container
*/
Box.prototype.move = function( toContainer ) 
{
	if( this.container != toContainer )
		toContainer.addBox( this );	
};

/*
	Function: addItem
	Adds the item to the item list

	inItem - Item to add to the list
*/
Box.prototype.addItem = function( inItem )
{
	if( inItem.box )
		inItem.box.removeItem( inItem );

	this.item_list.append( inItem );
	inItem.setParent( this );
};

/*
	Function: addItemToFront
	Adds the item to the front of the item list

	inItem - Item to add to the list
*/
Box.prototype.addItemToFront = function( inItem )
{
	if( inItem.box )
		inItem.box.removeItem( inItem );

	this.item_list.insertAt( inItem, 0 );
	inItem.setParent( this );
};

/*
	Function: removeItem
	Removes the item from the item list

	inItem - Item to remove from the list
*/
Box.prototype.removeItem = function( inItem )
{
	this.item_list.remove( inItem );
	inItem.setParent( null );
};

/*
*	Function trash 
*	Calls back to the server to remove the box object, if successful
*	an event is broadcast
*/
Box.prototype.trash = function()
{
	var url = "/box/remove";
	var callback = { success:onDeleteSuccess,
			 timeout:5000,
			 arguments: { id:this.id, obj:this} };
	YUIRequest.asyncRequest( 'POST', url, callback, "id="+this.id );		
};

//Delete callbacks
function onDeleteSuccess( inReq )
{
	param = { box_id:this.arguments.id };
	SButil.EventManager.fireEvent( "BoxDeleted", param );

	delete this.arguments.obj;
}

/*
	Function: onBoxUpdate
	Callback for when a box update is received via a server event
*/
function onSEBoxUpdate(inEvent, inArgs )
{
	var box = inArgs[0];
	
	if( box.id == this.id && box.modstatus != 2 )
	{
		if( box.name != this.name )
			this.name = box.name;	

		//if( box.pos != this.box_obj.pos )
			//this.reorder( box.pos );	
/*
		if( box.container_id && (box.container_id != this.container.id ))
		{
			var old_container = this.container;

			var container = SButil.StateManager.lookupContainer( box.container_id );
			if( container ) 
			{
				this.move( container );

				var ctx = { box:this, from:old_container, to:container };
				SButil.EventManager.fireEvent( "BoxMoved", ctx );
			}
		}
*/
	}
}


window['SB']['core']['Box'] = Box;

})();
