(function(){

/*
	Class: ItemView 
	The base class for a collection of navigation components 
*/
function ItemView()
{
	this.htmlObj = null;
}

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

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

	Configuration Array Options:
	id - the id of the div to use as a template (default:none)
*/
ItemView.prototype.setConfig = function( inConfig )
{
	this.p_config = inConfig;
	this.item_obj = inConfig.item_obj;

	var tmpHtmlObj = YUIDom.get( inConfig.id );
	if( tmpHtmlObj )
	{
		this.htmlObj = tmpHtmlObj.cloneNode( true );
	
		if( !inConfig.disable_drag )
		{
			var arr = YUIDom.getElementsByClassName( inConfig.dd, "div", this.htmlObj); 
			if( arr[0] )
			{
				this.dd = new ItemViewDD( arr[0], this ); 
                         	this.dd.addInvalidHandleType("input");  	 
			        this.dd.addInvalidHandleType("a"); 	 
				this.dd.addInvalidHandleType("textarea"); 	 
				this.dd.addInvalidHandleClass("sb_box_list_view_comment_item"); 	 
				this.dd.addInvalidHandleClass("sb_box_list_view_comment_item_text"); 	 
				this.dd.addInvalidHandleClass("sb_listiteminfocomments_2"); 	 
				this.dd.addInvalidHandleClass("sb_box_list_view_comments_area"); 
			}

		}
	}
};

/*
	Function: getConfig
	Returns a configuration array for the ItemView.

	See <setConfig> for configuration array definition
*/
ItemView.prototype.getConfig = function()
{
	return this.p_config;
};

/*
	Function: getWidth
	Returns the dynamic width for the DOMElement
*/
ItemView.prototype.getWidth = function()
{
	return this.htmlObj.offsetWidth;
};

/*
	Function: getHeight
	Returns the dynamic height for the DOMElement
*/
ItemView.prototype.getHeight = function()
{
	return this.htmlObj.offsetHeight;
};

/*
	Function: getX
	Returns the dynamic horizontal position for the DOMElement
*/
ItemView.prototype.getX = function()
{
	return this.htmlObj.offsetLeft;
};

/*
	Function: getY
	Returns the dynamic vertical position for the DOMElement
*/
ItemView.prototype.getY = function()
{
	return this.htmlObj.offsetTop;
};

/*
	Function: destroy 
	Cleans up and frees any data contained by the object	
*/
ItemView.prototype.destroy = function()
{	
	if( this.dd ) 
	{
		this.dd.endDrag( 0, 0 );

		this.dd.getEl().objlink = null;	
		free( this.dd );
	}

	if( this.htmlObj )
	{
		this.htmlObj = null;
	}

	free( this );
};

function free( obj )
{
	delete obj;
	obj = null;
}

ItemView.prototype.getHtmlObj = function()
{
	return this.htmlObj;
};

ItemView.prototype.showLightBox = function()
{
	var dlg = window['SB']['ui']['LightBoxDialog'];
	dlg.setConfig( { item:this.item_obj, from:this.from } );
};


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

	toBox - destination box 
*/
ItemView.prototype.move = function( toBox ) 
{
	if( this.box != toBox )
	{
		this.htmlObj.style.display = "none";

		var req = "/item/move/";
		var pd = "id="+this.item_obj.item_id+"&box_id="+toBox.box_obj.box_id;
		var cb = { success:moveItemSuccess, 
		  	arguments:{ item:this, box:toBox, old_box:this.item_obj.box} };

		YUIRequest.asyncRequest( "POST", req, cb, pd );
	}
};


function moveItemSuccess( inObj )
{
	var item = this.arguments.item;
	var box = this.arguments.box;
	var old_box = this.arguments.old_box;

	item.item_obj.move( box.box_obj );

	var ctx = { item:item, from:old_box, to:box};
	SButil.EventManager.fireEvent( "ItemMoved", ctx );
}

/*
	Function: trash 
	Removes the Item (locally and on the server)	
*/
ItemView.prototype.trash = function() 
{
	this.htmlObj.style.display = "none";

	var req = "/item/remove";
	var pd = "id="+this.item_obj.item_id;
	var callback = { success:deleteItemSuccess, 
		  	arguments:{ item:this } };
	YUIRequest.asyncRequest( "POST", req, callback, pd );

	SButil.EventManager.fireEvent( "ItemDeleted", this );

	this.item_obj.kill();
	this.destroy();

};


function deleteItemSuccess( inObj )
{
	if( inObj.responseText == "1" )
	{
		var item = this.arguments.item;
	}
}


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


function ItemViewDD( inObj, srcObj )
{
	ItemViewDD.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 

}

//ItemViewDD extends YUIDD.  
YAHOO.lang.extend( ItemViewDD, YUIDDProxy, 
{
	startDrag: function( x, y )
	{
		// make the proxy look like the source element 
	        var dragEl = this.getDragEl(); 
	        var clickEl = this.getEl(); 
	        YUIDom.setStyle(clickEl, "visibility", "hidden"); 

		if( clickEl.objlink.dragStart )
			clickEl.objlink.dragStart();
	 
	        dragEl.innerHTML = clickEl.innerHTML; 
	 
	        YUIDom.setStyle(dragEl, "color", YUIDom.getStyle(clickEl, "color")); 
	        YUIDom.setStyle(dragEl, "backgroundColor", YUIDom.getStyle(clickEl, "backgroundColor")); 
	        YUIDom.setStyle(dragEl, "border", "2px solid gray"); 
	        YUIDom.setStyle(dragEl, "backgroundImage", "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.didmove )
		{
			//Determine the new position
			var containing_div = srcEl.parentNode.parentNode;	
			var parent_node = srcEl.parentNode;	
			var childNodes = YUIDom.getChildrenBy( containing_div, function(el) { return (el.tagName == "DIV"); } );	

			for( var i = 0; i < childNodes.length; i++ )
			{
				if( childNodes[i] == parent_node )
				{
					var iid = srcEl.objlink.item_obj.item_id;
					var pd = "id="+iid+"&pos="+i;
					YUIRequest.asyncRequest( "POST", "/item/update/", null, pd);

					var boxItemList = srcEl.objlink.item_obj.box.item_list;
					boxItemList.remove( srcEl.objlink.item_obj );
					boxItemList.insertAt( srcEl.objlink.item_obj, i ); 		
					break;
				}
			}	

			this.didmove = null;
		}

	},

	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) 
	     { 
	         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 ) )
	     {
		//If Enter is handled, prevent further bubbling
		//HACK: Trash overrides all
		if( ( 	srcEl.objlink.drag_over == null || 
			destObj.toString() == "[SB.ui.Trash]") && 
		    destObj.handleDragEnter( e, srcEl.objlink ) )
		{
			srcEl.objlink.drag_over = destObj;
		}
	     }
	},

	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( e, srcEl.objlink );	

		if( srcEl.objlink.drag_over == destObj )
			srcEl.objlink.drag_over = null;
	     }
	},

 	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 ) &&
		  srcEl.objlink.drag_over == destObj )
	     {
		this.didmove = true;
		destObj.handleDrop( e, srcEl.objlink );	
	     }

	},

	onInvalidDrop: function( e ) { }  

}); 

})();
