(function(){

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

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

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

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

	inConfig.id = "sb_ui_free_item"; 
	this.item_obj = inConfig.item_obj;

	this.wasMoving = false;

	var tmpHtmlObj = YUIDom.get( inConfig.id );
	if( tmpHtmlObj )
	{
		this.htmlObj = tmpHtmlObj.cloneNode( true );
		this.htmlObj.id += "_"+inConfig.item_obj.item_id;
	
		var arr = YUIDom.getElementsByClassName( "sb_free_item_image", "img", this.htmlObj );
		if( arr[0] )
		{
			this.itemImg = arr[0];
			this.itemImg.style.msInterpolationMode = "bicubic";
			this.resizeImg(inConfig.zoom);
	
			if( inConfig.url.match(/http/) )
				this.itemImg.src = inConfig.url;
			else
				this.itemImg.src = "/item/image?id="+this.item_obj.id;
		
			YUIEvent.addListener( this.itemImg, "click", this.showLightBox, this, true );
		}

		var arr = YUIDom.getElementsByClassName( "sb_box_free_view", "div", this.htmlObj );
		if( arr[0] ) 
		{ 
			arr[0].style.left = inConfig.x+"px";
			arr[0].style.top = inConfig.y+"px";
			this.x = parseInt(inConfig.x);
			this.y = parseInt(inConfig.y);


			this.dd = new FreeViewItemDD( arr[0], this );
		}	

	}
	

};

FreeViewItem.prototype.showLightBox = function()
{
	if( this.wasMoving ) { this.wasMoving = false; return;  }

	var dlg = window['SB']['ui']['LightBoxDialog'];
	dlg.setConfig( { item:this.item_obj } );
};

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

	toBox - destination box 
*/
FreeViewItem.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)	
*/
FreeViewItem.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 );
};


function deleteItemSuccess( inObj )
{
	if( inObj.responseText == "1" )
	{
		var item = this.arguments.item;
		SButil.EventManager.fireEvent( "ItemDeleted", item );

		item.item_obj.kill();
		item.destroy();
	}
}

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

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

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

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

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

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

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

/*---------------------------------------------*/
FreeViewItem.prototype.resizeImg = function(scaleFactor)
{
	this.itemImg.style.width = (this.p_config.width * scaleFactor)+ "px";
	this.itemImg.style.height = (this.p_config.height * scaleFactor)+ "px";
};

FreeViewItem.prototype.saveImgLayout = function(scaleFactor)
{
//	this.p_config.x = YUIDom.getRegion(this.itemImg).left;
//	this.p_config.y = YUIDom.getRegion(this.itemImg).top;
//	this.p_config.zoom = scaleFactor;
};
/*------------------------------------------------*/

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

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

	var masthead = YUIDom.get("masthead");
	this.yCons = 0;
	if( masthead )
		this.yCons = masthead.offsetHeight + 15;

	var el = this.getDragEl(); 
	inObj.objlink = srcObj;
	YUIDom.setStyle(el, "opacity", 1); // The proxy is fully opaque
}

//FreeViewItemDD extends YUIDD.  
YAHOO.lang.extend( FreeViewItemDD, YUIDDProxy, {

	onDrag: function( e )
	{
		var srcEl = this.getEl();
		if( parseInt(srcEl.style.left) < 0 ) srcEl.style.left = "0px";
		if( parseInt(srcEl.style.top) < this.yCons) srcEl.style.top= this.yCons+"px";
	},

	startDrag: function( x, y )
	{
		// make the proxy look like the source element 
		var srcEl = this.getEl();
	        YUIDom.setStyle(srcEl, "visibility", "hidden"); 
	        var dragEl = this.getDragEl(); 
		dragEl.innerHTML = srcEl.innerHTML;

		this.getEl().objlink.item_obj.pos = 0;

		this.getEl().objlink.wasMoving = true;
	},

	endDrag: function( x,y )
	{
		
	        var srcEl = this.getEl(); 
	        var dragEl = this.getDragEl(); 
		var item = srcEl.objlink.item_obj;
		item.box.item_list.remove(item); 
		item.box.item_list.insertAt( item, 0 );

	        YUIDom.setStyle(srcEl, "visibility", ""); 

		srcEl.style.top = (parseInt(dragEl.style.top) - this.yCons) + "px";
		srcEl.style.left = dragEl.style.left; 
		
		if( !srcEl.objlink.p_config.disable_drag )
		{
			if( !this.didmove )
			{
				var id = srcEl.objlink.item_obj.item_id;
				var pd = "id="+id;
				    pd += "&x="+parseInt(srcEl.style.left);
				    pd += "&y="+parseInt(srcEl.style.top);
				    pd += "&pos="+srcEl.objlink.item_obj.pos;

				var x = parseInt(srcEl.style.left);
				var y = parseInt(srcEl.style.top);

				srcEl.objlink.item_obj.setXY( x, y );

				YUIRequest.asyncRequest( "POST", "/item/update", null, pd);
				this.didmove = null;
			}
		}
	},

	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 ) )
		{
	        	var dragEl = this.getDragEl(); 
			YUIDom.setStyle(dragEl,"opacity","0.5");
			srcEl.objlink.drag_over = destObj;
		}
	     }
	},

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

	     var destObj = destEl.objlink;	

	     var dragEl = this.getDragEl(); 
	     YUIDom.setStyle(dragEl,"opacity","1");

	     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 ) { }  


	});

})();
