/**
 * AS4ResourceLibrary Component Controller
 *
 * The resource library is a subclass of the resource browser designed solely for browsing
 * resources and allowing them to be dragged to external droppable receivers.
 */
if(typeof(AS4ResourceBrowser) == 'undefined')
	throw('AS4ResourceLibrary requires AS4ResourceBrowser');

var AS4ResourceLibrary = Class.create(AS4ResourceBrowser, {
	
	/**
	 * @constructor
	 */
	initialize: function($super)
	{
		$super;

 		this.iconDisplayMode = AS4ResourceBrowser.DISPLAY_MODE_THUMBNAIL;
		this.currentFolderId = AS4ResourceBrowser.MY_RESOURCES_PATH;
		
		// Cached event listeners
		this.cachedOnExpanderClicked = this.onExpanderClicked.bindAsEventListener(this);
		this.cachedOnLeafClicked = this.onLeafClicked.bindAsEventListener(this);

		// Default pagination options
		this.resourcesPerPage = 10;

		// Media browsers show only public, approved and unhidden resources
		this.isMediaBrowser = true;

		// Hide the global ajax status indicator
		this.ajaxUpdateOptions = { showIndicator: false };

		// Default do not filter by type
		this.searchTypes = $A([]);
	},
	
	/**
	 * Override the fetchResources method to forward to fetchFolders if the currently selected folder is Channels
	 * and Folders
	 * 
	 * @param {string} [path] The location to load. If numeric, the corresponding folder id will be retrieved. If null, the currently displayed path will be used.
	 * @param {target} [target] The node element to update with the returned content. If undefined, $('folders_tree') will be used.
	 */
	fetchResources: function($super, path, target)
	{
		var path = (path ? path : this.currentFolderId);

		if(path == 'folders')
			this.fetchFolders();	
		else
			$super(path, target);
	},

	/**
	 * Override the fetchFolders method to return all folders managable by the current user
	 *
	 */
	fetchFolders: function()
	{
		var target = $('files_list');
		var url = "/resource_browser/fetch_folders";
		var options = Object.extend({}, options);

		this.onBeginFetchResources();

		var params = {
			renderMode: 'update',
			path: AS4ResourceBrowser.ALL_FOLDERS_PATH, 
			options: $H(options).toJSON(),
			icon_display_mode: this.iconDisplayMode,
			is_media_browser: this.isMediaBrowser,
			folderPage: this.resourcesPage,
			folderLimit: this.resourcesPerPage,
			terms: this.searchTerms
		};

		AS4Shell.getInstance().ajaxUpdate(url, params, target, $A([this.onFetchFoldersSuccess.bind(this)]), { showIndicator: false } );
	},

    
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
	// AJAX Callback Methods
	
	/**
	 * Callback handler triggered by a resource being clicked within the library. Can be overridden
	 * by the page controller for custom functionality
	 *
	 * options can be 
	 * {
	 *  title: string Title of the selected resource
	 *  [type]: {string} One of 'resource' or 'folder'. Default is resource.
	 * }
	 *
	 * @param {integer} id Id of the resource to insert
	 * @param {Hash} type Type of the resource, either 'resource' or 'folder'. Default is 'resource'.
	 */
	onResourceClicked: function(resource_id, options)
	{
		options = Object.extend({
				title: null,
				type: 'resource',
        event: null
			},
			options || {});
	},

	onBeginFetchResources: function($super)
	{
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		// Update spinners
		$('files_list').update('<div id="ml_status">' +
		'<img src="/app/common/assets/images/ajax-loader.gif" alt="Loading..>" width="24" height="24" /><br />...Loading Media...' +
		'</div>');
	},

    onBeginFetchResources_thumbnail: function($super)
	{
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Update spinners
		$('files_list_thumbnail').update('<div id="ml_status">' +
		'<img src="/app/common/assets/images/ajax-loader.gif" alt="Loading..>" width="24" height="24" /><br />...Loading Media...' +
		'</div>');
	},
    
	onFetchResourcesSuccess: function(transport, target)
	{
		// Ignore exceptions
		if(transport.headerJSON && transport.headerJSON.exception)
			return;

		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Make the newly loaded resources draggable
		this.draggableResources.invoke('dispose');
		
		$$('#files_list &gt; .list_item .icon').each(function(element)
		{
			//element.observe('click',console.log(element));
			// Normal Scriptac. draggables can't be dragged outside a scrollable (overflow:scroll/auto) container, as they just cause
			// the container to grow. So, rather than directly dragging the icon, we drag a clone of it that is injected at the document
			// top-node level. Hence, a super-draggable!
		//	element.observe('click',resourceLibrary.onResourceClicked(1844, {title: '30000-random-numbers.png'}));
			strResourceId = element.up().readAttribute('id');
			intResourceId = strResourceId.substr(9);
			strResourceName = element.down().readAttribute('alt');
			
			if(Prototype.Browser.IE)
			{
				//element.observe('click',function(){resourceLibrary.onResourceClicked(1844, {title: '30000-random-numbers.png'});});
				element.observe('mousedown', AS4Shell.getInstance().makeSuperDraggable.bindAsEventListener(this, element));
				element.observe('mouseup', function() {
				if(this.draggableClone) {
					$(this.draggableClone).remove();
				}
				}.bindAsEventListener(this));
			}
			else
			{
				element.observe('mousedown', AS4Shell.getInstance().makeSuperDraggable.bindAsEventListener(this, element));
				element.observe('mouseup', function() {
				if(this.draggableClone) {
					$(this.draggableClone).remove();
				}
				}.bindAsEventListener(this));
			}
			
		}.bind(this));
	},

   

 	onFetchFoldersSuccess: function(transport, target)
	{
		// Ignore exceptions
		if(transport.headerJSON && transport.headerJSON.exception)
			return;

		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Make the newly loaded resources draggable
		this.draggableResources.invoke('dispose');

		$$('#files_list &gt; .list_item .icon').each(function(element)
		{
			// Normal Scriptac. draggables can't be dragged outside a scrollable (overflow:scroll/auto) container, as they just cause
			// the container to grow. So, rather than directly dragging the icon, we drag a clone of it that is injected at the document
			// top-node level. Hence, a super-draggable!
			element.observe('mousedown', AS4Shell.getInstance().makeSuperDraggable.bindAsEventListener(this, element));
			element.observe('mouseup', function() {
				if(this.draggableClone) {
					$(this.draggableClone).remove();
				}
			}.bindAsEventListener(this));
		}.bind(this));
	}
});
