(function(){
/*
	Class: Item 
	The base data object for a Item 
*/
function Item() { this.box = null; this.p_config = null; }

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

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

	Configuration Array Options:
	url - The url of the image for the Item 
	title - The title for the Item 
	item_id - The id for the Item 
*/
Item.prototype.setConfig = function( inConfig )
{
	this.p_config = inConfig;

	if( inConfig.username)
		this.box_username = inConfig.username;

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

	if( inConfig.link )
	{
		if( inConfig.link.match(/^sbopen/) )
		{
			var linkparts = inConfig.link.split("|");

			this.link = "sbopen://"+linkparts[1];
			this.link_host = inConfig.username+" Computer";
		}
		else
		{
			this.link = inConfig.link;
			this.link_host = inConfig.link_host;
		}
	}

	if( inConfig.image_url )
	{
		this.image_url = this.p_config.url = this.url = inConfig.image_url;
		if( !this.image_url.length ) this.image_url = "/item/image?id="+inConfig.id;
	}
	else
	{
		this.image_url = "/item/image?id="+inConfig.id;
	}

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

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

	if( inConfig.id)
		this.id = this.p_config.item_id = this.item_id = inConfig.id;

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

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

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

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

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

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

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

	if( inConfig.text )
	{
		this.text_data = inConfig.text;
		if( !this.text_data.length )
		{
			this.text_data = "No Text Available";
		}
	}
	else
	{
		this.text_data = "No Text Available";
	}

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

	this.comments = [];

	if( inConfig.comments != null )
	{
		for( el in inConfig.comments )
		{
			var c = new SBCore.Comment();
			c.setConfig( inConfig.comments[el] );
			c.item = this;

			this.comments.push( c );	
		}		
	}

	this.tags = [];

	if( inConfig.tags != null )
	{
		for( el in inConfig.tags )
		{
			if( inConfig.tags[el].length )
			{
				this.tags.push( inConfig.tags[el] );	
			}
		}		
	}

};

Item.prototype.resetConfig = function( inConfig )
{
	this.setConfig( inConfig );

	SButil.EventManager.fireEvent("ItemUpdated", { item:this } );
};

/*
	Function: getConfig
	Gets the basic setup for the Item.
*/
Item.prototype.getConfig = function()
{
	return this.p_config;
};

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

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

/*
	Function: setComments 
	Sets the comment objects for the Item	
*/
Item.prototype.setComments = function( inComments ) 
{
	this.comments = inComments;
};

/*
 	Function: addComment
	Adds a comment dynamically to the comment array
*/
Item.prototype.addComment = function( inComment )
{
	this.comments.push( inComment );
	inComment.item = this;


	inComment.postComment();
};

/*
 	Function: removeComment
	Adds a comment dynamically to the comment array
*/
Item.prototype.removeComment = function( inComment )
{
	for( var i = 0; i < this.comments.length; i++ )
	{	
		if( this.comments[i] == inComment )
		{
			this.comments.splice(i, 1); 
		}
	}
};

/*
	Function: fetchAllComments 
	Sets the comment objects for the Item	
*/
Item.prototype.fetchAllComments = function() 
{
	var sURL = "/comment/getbyitem"
	var config = { success:fetchCommentsCallback, arguments:{ srcobj:this } };
	var data = "item_id="+this.id;
	YUIRequest.asyncRequest("GET", sUrl, config, data);
};

/*
	Function: fetchLast10Comments 
	Sets the comment objects for the Item	
*/
Item.prototype.fetchLast10Comments = function() 
{
	var sURL = "/comment/getbyitem"
	var config = { success:fetchCommentsCallback, arguments:{ srcobj:this } };
	var data = "item_id="+this.id+"&level=0&result=10&oldest=1";	
	YUIRequest.asyncRequest( "GET", sURL, config, data );
};

function fetchCommentsCallback( inReq )
{
	var commentsJSON = YUIJSON.parse(inReq.responseText);
	
	for( el in commentsJSON )
	{
		var c = new Comment();
		c.setConfig( commentsJSON[el] );

		this.comments.push( c );	
	}	
}

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

	toBox - destination box 
*/
Item.prototype.move = function( toBox ) 
{
	if( this.box != toBox )
		toBox.addItemToFront( this );	
};

Item.prototype.setXY = function( x,y )
{
	this.p_config.x = x;
	this.p_config.y = y;
	this.x = x;
	this.y = y;
};

/*
	Function: kill 
	Deletes the Item	
*/
Item.prototype.kill = function() 
{
	this.box.removeItem( this );	

	for( c in this.comments )
       	{ this.comments[c].item = null; }

	this.comments = [];
};



function moveItemSuccess( inObj )
{
	if( inObj.responseText == "1" )
	{
		toBox.addItem( this );	
	}
}

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

})();
