//========================================================================================
function COLL_Edit( obj )
{
	new Ajax.Request( '/collection/edit',
	{
		postBody:   'ajax=1&entityid='+obj.entityid+'&collectionid='+obj.collectionid+'&onsave_callback='+obj.onsave_callback,
		onComplete: function( t )
		{ 
			modalreturn = Modalbox.show( t.responseText, {width: '600', height: '400', title: 'Edit Review' } );	
			return modalreturn;
		}
	} );
}

//==========================================================================================
function COLL_RefreshRow( collectionid )  
{
	new Ajax.Request( '/collection/row/' + collectionid,
	{
		onComplete: function( t )
		{
			var item = $('collectionitem_'+collectionid);
			item.innerHTML = t.responseText;
			new Effect.Highlight( item, { duration: 10.0 } );
		}
	} );
}

//========================================================================================
function COLL_Save( obj )
{	
	Modalbox.hide();
	
	var args = obj.form.serialize();
	new Ajax.Request( '/collection/save',
	{
		postBody:   'ajax=1&entityid='+obj.entityid+'&collectionid='+obj.collectionid+'&onsave_callback='+obj.onsave_callback+args,
		onComplete: function( t )
		{ 	
			var fn = obj.onsave_callback + '(' + obj.collectionid + ')'; 			
			setTimeout( fn, 0 );
			return modalreturn;
		}
	} );

}

//========================================================================================
function COLL_Delete( obj )
{
	new Ajax.Request( '/collection/delete',
	{
		postBody:   'collectionid='+obj.collectionid,
		onComplete: function( t )
		{ 
			$('collectionitem_' + obj.collectionid ).innerHTML = "";
		}
	} );
}

//========================================================================================
function COLL_Refresh() 
{
	new Ajax.Request( '/collection/refresh',
	{
		postBody: '',
		onComplete: function( t )
		{
			$('collection_items').innerHTML = t.responseText;
			new Effect.Highlight( 'collection_items', { duration: 0.5 } );
		}
	} );
}

//========================================================================================
function callCollEdit( text, li )
{
	var targetElement = $("gearname");
	var textvalue     = targetElement.value;
	
	if ( textvalue == '' )
		return 0;
	
	if ( !confirm( "This item does not exist in our database, would you like to create it?" ) )
		return;			
				
	new Ajax.Request( '/gear/addnew', 
	{
		postBody:   'ajax=1&gearname='+ escape( textvalue ),
		onComplete: function( t ) 
		{
			if ( t.responseText )
			{
				COLL_Edit( { entityid: t.responseText, collectionid: '' } );
				targetElement.value 	= '';
			}
			else 
			{
				alert( "Failed to create new gear item. Please try again." );
			}
		}
	} );
}

