// <%@page contentType="application/x-javascript; charset=UTF-8" %>

/*
Class: PACUploadList
	Simple list to hold upload instances as PACUploadEntries.
	Convenience-Class to match document list syles.
	
Author: 
	cwittenb

Arguments:
	options - (object) An options object (see below for details).
	
Options:
	
 	el 				- the element for this documentList
	listID 			- list ID, mandatory
	uploadLook 			- the uploadLook of the documentEntries in this list: one of 'LIST', 'PREVIEW', 'DETAIL' (defaults to 'PREVIEW')
 */

PACUploadList = new Class({
	
	options: {
		el: null,				// el can be css-ID or element, mandatory
		listID: null, 			// list ID, mandatory
		documentIDs: null, 		// array of document entries
		uploadLook: "DEFAULT",		//uploadLook of its PACUploadEntries
		// other settings
		contentHashName: 'PACUploadEntry', // the PACHierarchyHash that handles the this' items

		//Settings for upload Entry:
		uploadListClass : "entryList uploadList",
		uploadItemClass : "uploadItem",
		uploadItemCancelClass : "uploadCancel",
		uploadItemBarClass : "uploadBar",
		uploadItemBarRunningClass : "uploadRunning",
		uploadItemBarFailedClass : "uploadFailed",
		uploaderID: "",

		// modes, debug, etc.
		debug: false,			// custom debug mode. will enable more output in cl
		testMode: false,		// custom test mode. will enable js testing
		forceUpdate: false
	},
	
	initialize: function(el, options){
		this.setOptions(options);
		// set some important vars
		this.listID = this.options.listID;
		if($defined(el)){
			this.element = $(el);
		} else {
			this.element = new Element("ul", {
				"class": this.options.uploadListClass
			});
		};	
		this.addEvents();
	},
	
	addEvents: function(){
		this.addEvent("onUploadFileAdded", function(listID, file){
			if((listID == this.listID) && (file.app == this.options.uploaderID)) this.addItem(file);
		}.bind(this));
		this.addEvent("onUploadFileComplete", function(listID, file){
			if(listID == this.listID) this.uploadComplete(file);
		}.bind(this));
		this.addEvent("onUploadProgress", function(listID, progress){
			if(listID == this.listID) this.updateProgress(progress);
		}.bind(this));
		this.addEvent("onUploadFileCanceled", function(listID, file){
			if(listID == this.listID) this.uploadCancel(file);
		}.bind(this));
		this.addEvent("onUploadFileFailed", function(listID, file){
			if(listID == this.listID) this.uploadFail(file);
		}.bind(this));
		this.addEvent("onUploadFileError", function(listID, file){
			if(listID == this.listID) this.uploadFail(file);
		}.bind(this));	
	},
	
	removeEvents: function(){
		this.removeEvent("onUploadFileAdded");
		this.removeEvent("onUploadFileComplete");
		this.removeEvent("onUploadProgress");
		this.removeEvent("onUploadFileCanceled");
		this.removeEvent("onUploadFileFailed");
		this.removeEvent("onUploadFileError");
	},
	
	updateProgress: function(progress) {
		var item = this.getItem(progress.ID);
		if(item) item.updateProgress(progress);
	},
	
	uploadComplete: function(file) {
		var item = this.getItem(file.ID);
		if(item) item.uploadComplete();
		//this depends on uploadLook: remove item, or replace loading with format icon
		if(this.options.uploadLook == "DEFAULT"){
			this.removeItem(file.ID);
		}
	},
	
	uploadFail: function(file) {
		var item = this.getItem(file.ID);
		if(item){ 
			item.uploadFail();
			this.removeItem.delay(1000, this, [file]);
		}
	},
	
	uploadCancel: function(file){
		var item = this.getItem(file.ID);
		if(item){ 
			item.uploadCancel();
			this.removeItem.delay(1000, this, [file]);
		}
	},
	
	updateDocumentAttributes: function(){
		new PACJsonP(sjsoURL, {
			'method': 'setDocumentAttributesByFileName',
			'params' : [PAC.token, this.getItems(), this.listID],	// 4th argument 0 ??
			'onComplete': function(response) {
				if ($defined(response.error)) {
					PAC.notify("Server Error " + response.error.code + ", " + response.error.msg);
				}
				else {
					//fire on context event
				}
			}.bind(this)
		}).request();
	},
	
	/*
	Method: show
	*/
	show: function() {
		if ($defined(this.element)) {
			this.element.show();
		}
	},
	
	/*
	Method: hide
	*/
	hide: function() {
		if ($defined(this.element)) {
			this.element.hide();
		}
	},
	
	/*
	Method: setAttributes
		Mehtod to bulk-edit all stuff in the upload list.
	*/
	setAttributes: function(callback){
		//wait for upload to be finished
		//call the server
		if(callback && $type(callback) == 'function') callback();
	},
	
	createElement: function(){
		this.parent();
		this.messageEl = new Element('div', {'class':'listNotification'}).injectTop(this.element);
	},
	
	getItem: function(elementID){
		return this.getChild(elementID, this.options.contentHashName);
	},
	
	addItem: function(element){
		element.parentObject = this;//PACH. will include this too late, I need it in initialize :(
		if(this.countChildren(this.options.contentHashName) == 0) this.element.empty(); //remove placeholder
		if(!$(element.ID)) this.includeChild(element.ID, this.options.contentHashName, element);
	},
	
	removeItem: function(element){
		return this.removeChild(element.ID || element, this.options.contentHashName);
	},
	
	hasItems: function(){
		return this.hasChildren(this.options.contentHashName);
	},
	
	removeItems: function(){
		return this.removeChildren(this.options.contentHashName);
	},
	
	getItems: function(){
		return this.getChildren(this.options.contentHashName);
	},

	/*
	Method: update
		Update the View with an options object
		
	Arguments: 
		options - (optional) see above
		callback - (optional) a callback function to be executed after the complete update.
	*/
	update: function(options, callback) {
		this.parent(options, callback);
	},
	
	/*
	Method: processOptions
		Parse the options to set the content of the view.
	*/
	processOptions: function() {
	},
	
	/*
	Method: remove
		for PACWidget.destroy()
	*/
	remove: function() {
		this.removeItems();
	}
});

PACUploadList.implement(new Options);
PACUploadList.implement(new PACEvents); // add Event functionality
PACUploadList.implement(new PACHierarchy); // add PACUploadEntry management