/**
 * AS4Channel Component
 */
 
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
// Document-load events

document.observe('dom:loaded', function() 
{
	// If we are not a lightbox!
	if($('resource_manager'))
		AS4ResourceManager.getInstance().refresh();

	// If an all resources part exists
	if($('all_resources'))
	{
		AS4AllResourcesPart.getInstance().channelController = AS4Channel.getInstance();
		AS4AllResourcesPart.getInstance().element = $('all_resources');

		AS4FormMethods.applyPlaceholders(null, $A([$('pagination_search_terms')]));
	}

},null);


// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
// Class declaration

var AS4Channel = Class.create({
	
	/**
	 * @type {integer} The id of the currently loaded channel or folder
	 */
	channelId: null,

	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

	/**
	 * @constructor
	 */
	initialize: function() 
	{},

	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
	// AJAX methods
	
	/**
	 * Begins an AJAX request for the given resource to be injected into the element content container
	 * 
	 * @param integer id Id of the resource to be loaded inline
	 * @param Element element The element into which the resource will be injected
	 */
	loadInlineResource: function(id, element)
	{
		var url = '/resources/view';
		
		var params = {
			id: id,
			inline: 'true'
		};
		
		AS4Shell.getInstance().ajaxUpdate(url, params, element, null);
	},
	
	/**
	 * Begins an AJAX request for the given resource to be injected into the element content container
	 * as HTML text. I.e. use the urlFor method.
	 * 
	 * Options may be:
	 * 
	 * scaling: true|false	- if true when rendering a media item, the image will be scaled to a width of 150px
	 * embed: true|false	- if true, the returned data will be wrapped in HTML tags.
	 * 
	 * @param {integer} id Id of the resource to be loaded inline
	 * @param {Object} options Hash of options as above
	 * @param {Element} element The element into which the resource will be injected
	 */
	urlForResource: function(id, options, element)
	{
		var url = '/resources/urlFor';
		
		var params = {
			id: id,
			inline: 'true',
			scaling: ( options.scaling ? options.scaling : false),
			embed: (options.embed ? options.embed : false)
		};
		
		AS4Shell.getInstance().ajaxUpdate(url, params, element, null);
	},

	/**
	 * @depracated - use Element.insertSWFObject
	 */
	insertFlashMovie: function(UFO,websiteDomain,resourceId,element)
	{	
			var resourceUrl = escape(websiteDomain+"/resources/view/id/"+resourceId+".flv");
		
			var FO = {
				movie:"/app/common/assets/flash/flvplayer.swf",
				width: "620",
				height: "400",
				majorversion:"7",
				build:"0",
				bgcolor:"#FFFFFF",
				flashvars:"file="+resourceUrl
			};

			UFO.create( FO, "player"+resourceId);
	},

	/**
	 * Set the specified resource's approval status
	 */
	setApproval: function(resourceId)
	{
		var url = '/channel/set_resource_approval';
		
		var params = {
			resource_id: resourceId
		};
		
		AS4Shell.getInstance().ajaxUpdate(url, params, null, $A([this.onSetApprovalComplete.bind(this)]));
	},
	
	/**
	 * Callback updates the approval status based on database result
	 */
	onSetApprovalComplete: function(transport, target)
	{
		if(!transport.headerJSON)
			return;
		this.approvalReload();
	},
			/**
	 * Create a new folder from the information in the create_folder_panel
	 */
	approvalReload: function()
	{
		// this will refresh the panel eventually
		// Redirect to the upload form
		top.location = self.location;
		
	}
	
});


// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
// Static Methods & Properties

// Static instance of the AS4Channel object
AS4Channel.instance = null;

/**
 * Return a singleton instance of the AS4Channel object
 * @return AS4Channel
 */
AS4Channel.getInstance = function()
{
	if(!AS4Channel.instance)
		AS4Channel.instance = new AS4Channel();
		
	return AS4Channel.instance;
}
 
