(function(){

/*
	Class: ContainerItem
	*Subclass of <BarItem>*

	Description:
	The classic popped up view focused on a singe item
*/
function ContainerItem()
{
	this.p_item = null;
	this.box_view_list = new window["SB"]["util"]["List"];
	
	SButil.EventManager.addListener("BoxDeleted", onBoxDeleted, this);
	
	SBui.ContainerItem.superclass.constructor.call(this);
}

//ContainerItem extends BarItem.  
YAHOO.lang.extend( ContainerItem, window['SB']['ui']['BarItem']);

ContainerItem.prototype.toString = function() { return "[SB.ui.ContainerItem]"; };


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

	Configuration Array Options:
	id - the id of the div to use as a template (default: sb_ui_baritem)
*/
ContainerItem.prototype.setConfig =  function( inConfig )
{
	//Make sure to zero this out, we shouldn't directly set an action
	inConfig.action = null;
	
	this.container_id = inConfig.container_id = inConfig.pallet_id;
	this.container_obj = inConfig.container_obj;

	SBui.ContainerItem.superclass.setConfig.call(this, inConfig);

	this.htmlObj.id = "container_"+this.container_obj.id;

	if( this.name )
	{
		var arr = YUIDom.getElementsByClassName( "sb_container_name", "div", this.htmlObj );
		for(var i = 0; i < arr.length; i++) arr[i].innerHTML = this.name;
	}

	var arr = YUIDom.getElementsByClassName( "containeritem", "div", this.htmlObj );
	if( arr[0] ) 
	{ 	
		this.dd = new ContainerItemDD( arr[0], this );	
		YUIEvent.addListener( arr[0], "click", this.loadBoxes, this, true );
	}

	this.preloadBoxes();

	SButil.EventManager.addListener("ContainerDeleted", onContainerDeleted, this);
};

/*
*	Function rename 
*	Updates the name of the container
*/
ContainerItem.prototype.rename = function( inName )
{
	this.name = inName;
	var containerName = this.name.truncate( this.getWidth(), this.getHeight(), false );  
	this.container_obj.name = containerName;

	var arr = YUIDom.getElementsByClassName( "sb_container_name", "div", this.htmlObj );
	for(var i = 0; i < arr.length; i++) arr[i].innerHTML = inName;
};

ContainerItem.prototype.attach = function()
{
	if( this.name )
	{
		var containerName = this.name.truncate( 240, this.getHeight(), false );  

		var fullName = this.name;

			var elm = this.htmlObj;
			elm.tooltipText = fullName;
		
			YUIEvent.addListener( this.htmlObj, "mouseover", 
				function( ev ) { SBui.Tooltip.show( ev, elm );  } );
			YUIEvent.addListener( this.htmlObj, "mouseout", 
				function( ev ) { SBui.Tooltip.hide() } );

		YUIDom.getElementsByClassName( "sb_container_name", "div", this.htmlObj,
			function( el ) { el.innerHTML = containerName; });
	}
};



/*
*	Function trash 
*	Calls the underlying container core object which calls to the server to delete
*/
ContainerItem.prototype.trash = function()
{
	if( window["dlgManager"] )
		window["dlgManager"].showDeleteContainerDialog( this );
};

/*
	Function: preloadBoxes
	Refreshes the <BoxBar> with the contents of the container
*/
ContainerItem.prototype.preloadBoxes = function()
{
	var i =  0;
	for( ;i < this.container_obj.box_list.size(); i++ ) 
	{
		var box = this.container_obj.box_list.get(i);
		var boxItem = new SBui.BoxItem();

		boxItem.setConfig( {  id:"sb_ui_baritem_box", 
				   title: box.name,
				 box_obj: box
				 } );

		this.addBoxItem( boxItem );
	}
};

/*
	Function: loadBoxes
	Refreshes the <BoxBar> with the contents of the container
*/
ContainerItem.prototype.loadBoxes = function()
{
	if( !this.is_active )
	{
		this.bar.boxbar.removeAllItems();			
	
		//Make this ContainerItem active
		this.setActive( true );
	
		//Broadcast the event, and add a listener for a change in active status
		SButil.EventManager.fireEvent("ContainerItemActivated", this );
		SButil.EventManager.addListener("ContainerItemActivated", onActivationChanged, this );
	
		this.bar.boxbar.setActive( true );

		var i =  0;
		for( ;i < this.box_view_list.size(); i++ ) 
		{
			var boxItem = this.box_view_list.get(i);

			if( SButil.StateManager.currentBox &&
			    boxItem.box_obj.id == SButil.StateManager.currentBox.id )
			{
				boxItem.activate( false );
			}

			this.bar.boxbar.addItem( boxItem );
		}
	}
};

function onBoxDeleted(inEvent, inArgs )
{
	if( inArgs[0].container.id == this.container_obj.id )
	{
		this.box_view_list.remove( inArgs[0].box );
	}	
}

ContainerItem.prototype.addBoxItem = function( inBoxItem )
{
	inBoxItem.containerView = this;
	this.box_view_list.append( inBoxItem );
};

ContainerItem.prototype.findBoxItem = function( inId )
{
		var i =  0;
		for( ;i < this.box_view_list.size(); i++ ) 
		{
			if( this.box_view_list.get(i).box_obj.id == inId )
				return this.box_view_list.get(i);
		}

		return null;
};

/*
	Function: acceptsDrop
	Standard entry point to check if a drag/drop operation on this object would be valid.

	NOTE: This should be part of some kind of dragndrop interface

	Returns - Boolean ( true if ContainerItem will accept a drop from the obj )
*/
ContainerItem.prototype.acceptsDrop = function( inObj )
{
	if( inObj.toString() === "[SB.ui.BoxItem]" ) 
		return true;

	return false;
};

/*
	Function: handleDrop
	Process Drop action	

	NOTE: This should be part of some kind of dragndrop interface
*/
ContainerItem.prototype.handleDrop = function( e, inObj )
{
	if( inObj.toString() === "[SB.ui.BoxItem]" ) 
	{
		inObj.move( this );
	}

	document.body.removeChild( this.arrow );
	this.arrow = null;
};

/*
	Function: handleDragEnter
	Process DragEnter action	

	NOTE: This should be part of some kind of dragndrop interface
*/
ContainerItem.prototype.handleDragEnter = function( e, inObj )
{

	var reg = YAHOO.util.Region.getRegion( this.htmlObj );
	var rarrow = YUIDom.get("sb_ui_box_drop_indicator");
	this.arrow = rarrow.cloneNode( true );
	this.arrow.style.left = (reg.left + 25) + "px";
	this.arrow.style.top = (reg.top - 70) + "px";

	document.body.appendChild( this.arrow );

	return true;
};

/*
	Function: handleDragOut
	Process DragOut action	

	NOTE: This should be part of some kind of dragndrop interface
*/
ContainerItem.prototype.handleDragOut = function( e, inObj )
{
	document.body.removeChild( this.arrow );
	this.arrow = null;
};


/*
	Function: onContainerDeleted
	Callback for when a container is removed.  We need to check to see if it was us.
*/
function onContainerDeleted(inEvent, inArgs )
{
	if( this.container_obj.container_id == inArgs[0].container_id ) 
	{
		this.bar.removeItem( this );	
	}		
}


/*
	Function: onActivationChanged
	Called when the container activation changes 
*/
function onActivationChanged( inEvent, inArgs )
{
	if( inArgs[0] != this )
	{
		this.setActive( false );	
		SButil.EventManager.removeListener("ContainerItemActivated", onActivationChanged, this );
		/*
		 * move scrollBar to start position   
		 */
		this.bar.boxbar.contentHtmlObj.style.marginLeft = 
			this.bar.boxbar.pageLeftHtmlObj.offsetLeft + 
			this.bar.boxbar.pageLeftHtmlObj.offsetWidth + 3+"px";
			
	}
}


window['SB']['ui']['ContainerItem'] = ContainerItem;

function ContainerItemDD( inObj, srcObj )
{
	ContainerItemDD.superclass.constructor.call(this, inObj, null, { scroll:false } );

	var el = this.getDragEl(); 
	inObj.objlink = srcObj;
	YUIDom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent 
}



//ContainerItemDD extends YUIDD.  
YAHOO.lang.extend( ContainerItemDD, YUIDDProxy, 
{
	startDrag: function( x, y )
	{
		this.isReorder = false;

		// make the proxy look like the source element 
	        var dragEl = this.getDragEl(); 
	        var clickEl = this.getEl(); 
	        YUIDom.setStyle(clickEl, "visibility", "hidden"); 
	 
	        dragEl.innerHTML = clickEl.innerHTML; 
	 
	        YUIDom.setStyle(dragEl, "color", "#000"); 
	        YUIDom.setStyle(dragEl, "backgroundColor", "transparent"); 
	        YUIDom.setStyle(dragEl, "backgroundImage", "none"); 
	        YUIDom.setStyle(dragEl, "border", "none"); 
	},

	endDrag: function( x,y )
	{
	        var srcEl = this.getEl(); 
	        var proxy = this.getDragEl(); 
	 
	        // Show the proxy element and animate it to the src element's location 
	        YUIDom.setStyle(proxy, "visibility", ""); 
	        var a = new YAHOO.util.Motion(  
	            proxy, {  
	                points: {  
	                    to: YUIDom.getXY(srcEl) 
	                } 
	            },  
	            0.2,  
	            YAHOO.util.Easing.easeOut  
	        ) 
	        var proxyid = proxy.id; 
	        var thisid = this.id; 
	 
	        // Hide the proxy and show the source element when finished with the animation 
	        a.onComplete.subscribe(function() { 
	                YUIDom.setStyle(proxyid, "visibility", "hidden"); 
	                YUIDom.setStyle(thisid, "visibility", ""); 
	            }); 
	        a.animate(); 

		if( this.isReorder )
		{
		        var srcEl = this.getEl(); 
	
			//Determine the new position
			var containing_div = srcEl.parentNode.parentNode;	
			var parent_node = srcEl.parentNode;	
	
			if( !containing_div || !parent_node ) return;

			for( var i = 0; i < containing_div.childNodes.length; i++ )
			{
				if( containing_div.childNodes[i] == parent_node )
				{
					var pid = srcEl.objlink.container_obj.container_id;
					var pd = "id="+pid+"&pos="+(i-1);
					YUIRequest.asyncRequest( "POST", "/container/update/", null, pd);
	
					break;
				}
			}	
		}

	},

	onDrag: function(e) 
	{ 
	     // Keep track of the direction of the drag for use during onDragOver 
	     var y = YUIEvent.getPageY(e); 
	 
	     if (y < this.lastY) { 
	         this.goingUp = true; 
	     } else if (y > this.lastY) { 
	         this.goingUp = false; 
	     } 
	 
	     this.lastY = y; 
	}, 
	 
	onDragOver: function(e, id) 
	{ 
	     var srcEl = this.getEl(); 
	     var destEl = YUIDom.get(id); 
	 
	     // We are only concerned with list items, we ignore the dragover 
	     // notifications for the list. 
	     if (destEl.className == srcEl.className) 
	     { 
		this.isReorder = true;

	         var orig_p = srcEl.parentNode; 
	         var p = destEl.parentNode.parentNode; 
	 
	         if (this.goingUp) { 
	             p.insertBefore(orig_p, destEl.parentNode); // insert above 
	         } else { 
	             p.insertBefore(orig_p, destEl.parentNode.nextSibling); // insert below 
	         } 
	 
	         YAHOO.util.DragDropMgr.refreshCache(); 
	     } 
	},

	onDragEnter: function( e, id )
	{
	     var srcEl = this.getEl(); 
	     var destEl = YUIDom.get(id); 

	     var destObj = destEl.objlink;	

	     if ( destObj &&
		       destObj.acceptsDrop &&
		       destObj.acceptsDrop( srcEl.objlink ) )
	     {
		destObj.handleDragEnter( srcEl.objlink );	
	     }
	},


	onDragOut: function( e, id )
	{
	     var srcEl = this.getEl(); 
	     var destEl = YUIDom.get(id); 

	     var destObj = destEl.objlink;	

	     if ( destObj &&
		       destObj.acceptsDrop &&
		       destObj.acceptsDrop( srcEl.objlink ) )
	     {
		destObj.handleDragOut( srcEl.objlink );	
	     }
	},

 	onDragDrop: function( e, id )
	{
	     var srcEl = this.getEl(); 
	     var destEl = YUIDom.get(id); 

	     var destObj = destEl.objlink;	

	     if ( destObj &&
		       destObj.acceptsDrop &&
		       destObj.acceptsDrop( srcEl.objlink ) )
	     {
		destObj.handleDrop( e, srcEl.objlink );	
	     }
	}
});




})();
