/**
 * Copyright (c) 2005 Margaret Early, The Multiliteracy Project.
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.txt.
 *
 * fileManager.js
 *
 * $Id: fileManager.js,v 1.18 2006/05/26 18:13:31 vera Exp $
 */

var selectedFiles = new Array(); //array of fileIds

var popup = '';
var selectedFilesField = '';
var selectedString = '';

function getSelectionCount() {
	return selectedFiles.length;
}

/**
 * Function to list selected files
 */
function listFiles(){
	alist = "";

	for(var i=0;i<selectedFiles.length;i++){
		if((i+1)==selectedFiles.length){
			alist+=selectedFiles[i];
		}
		else{
			alist+=selectedFiles[i]+", ";
		}
	}
	alert(alist);
}

/**
 * checks if something is an object
 */
function isObject(a){
	return (a && typeof a =='object');
}	

/**
 * Open File Manager Popup window to select Files
 * fieldId: the field you want to store the list of selected files in
 * append: if true, the selected files will be appended to the
 * existing values in the input field, otherwise the values in the 
 * input field will be overwritten.
 * singleItem: if true, only one file can be selected at a time.
 * types: type(s) of files you want appearing in the manager popup (images, video, audio, or documents).
 * select: if true the current file needs to be shown selected, else not
 * If want to show more than one type, pass in an array e.g. new Array('images', 'video').
 */
function openFileManagerPopup(baseUrl,projectId,fieldId,append,singleItem,types,select){
	selectedFilesField = fieldId;
	if(append){
		selectedString = document.getElementById(fieldId).value;
	}
	else{
		document.getElementById(fieldId).value = '';
	}

	url = baseUrl+'/file/showFileManager/popup/'+projectId+'/'+(singleItem ? '1' : '0')+'/'+select;

	if(types){
		if(isObject(types)){ 	//check if types is an array
			url += (types.length > 0 ? '/' : '') + types.join('+');
		}
		else{
			url += '/' + types;
		}
	}

	if(!popup.closed && popup.location){
		popup.location.href = url;
	}
	else{
		popup = window.open(url,"fileManager","width=620,height=500,scrollbars=no,menubar=no");
		if(!popup.opener) popup.opener = self;
	}

	return popup;
}

/**
 * Function to call when user clicks ok in file manager popup
 * sets the field passed in from openFileManagerPopup()
 */
function popupOkClick(){
	opener.selectedFiles = selectedFiles;
	flist = '';

	for(var i=0;i<selectedFiles.length;i++){
		if((i+1) == selectedFiles.length){
			flist+=selectedFiles[i];
		}
		else{
			flist+=selectedFiles[i]+',';
		}
	}

	if(opener.selectedString != "")
		opener.document.getElementById(opener.selectedFilesField).value=opener.selectedString+","+flist;
	else
		opener.document.getElementById(opener.selectedFilesField).value=flist;

	window.close();
}

/**
 * Function to call when user clicks on a button that
 * needs a list of the file selections. Sets the field
 * who's id was passed in with the list of fileIds.
 * This is to be called when the file manager is not in a popup
 */
function setSelections(fieldId){
	flist = '';

	//selectedFiles.length returns key of last element
	//find the length of the associative array
	for(var i = 0; i < selectedFiles.length; i ++) {
		flist += selectedFiles[i] + (i == selectedFiles.length-1 ? '' : ',');
	}
	document.getElementById(fieldId).value=flist;
	return selectedFiles.length;
}

/**
 * Function to clear selected files
 */
function clearSelectedFiles(fieldId){
	selectedFiles = new Array();
}
