
/*change directory or open new tab*/
function cd(path, newtab, title) {
	tb = AfianHandler.activeTab;
	path = path?path:gluePath([tb.basepath, tb.selectedItem().input_value]);

	if (AfianHandler.shiftpressed) {
		newtab = true;
	}
	
	if (newtab) {
		title = title?title:tb.selectedItem().icon_title;
		newTab = new Afian_Tab(title, path);
		newTab.section = 'myfiles';
		newTab.viewMode = tb.viewMode;
		newTab.onload = MyFilesOnLoad;
		newTab.detailHTML = AfianHandler.MyFilesDetailsTemplate;
		if (!AfianHandler.shiftpressed) {
		newTab.load();
		}
		newTab.MyFiles_Browse(encodeURIComponent(path));
		newTab.onselect = MyFilesOnSelect;
	} else {
		if (!tb.isBusy()) {
			tb.MyFiles_Browse(encodeURIComponent(path));
			/*tabStartTimer(tb); ????*/
		}
	}
}

function MyFilesFolderNA(tab, path) {
AfianAlert(AfianMessages['Folder \'%1\' does not exist any more'].replace('%1', path));
	if (tab.basepath != '/') {
	if (AfianUserPermissions['ui_has_tree']) {
		reloadTreeByPath('/');
	}
	tab.MyFiles_Browse('/');
	}
}

function MyFilesOnLoad() {
	if (AfianHandler.activeTab == this) {
	/* show the menu */
	AfianHandler.showBaseMenu('MyFilesBaseMenu');
	AfianHandler.hideBaseMenu('TrashBaseMenu');
	AfianHandler.hideBaseMenu('SearchBaseMenu');
	AfianHandler.hideBaseMenu('MySharesBaseMenu');
	AfianHandler.hideBaseMenu('BShareBaseMenu');
	AfianHandler.hideBaseMenu('BrowseZipBaseMenu');
	/*admin stuff*/
	AfianHandler.hideBaseMenu('AdmGroupsBaseMenu');
	AfianHandler.hideBaseMenu('AdmUsersBaseMenu');
	AfianHandler.hideBaseMenu('AdmLogsBaseMenu');

		if (activeMenu) {
			hideMenu(activeMenu);
		}
		if (AfianHandler.components['MyFilesBaseMenu']['loaded']) {
	getById('MyFilesBreadcrumbs').innerHTML=getBreadCrumbs('<a href="javascript:;" onclick="cd(\'PATH\')" title="Click to browse \'PATH\'">', AfianHandler.activeTab.basepath, '</a>');
			/* highlight the toggle */
			toggleView(this.viewMode);
			if (clipboard.items.length > 0) {
				getById('MyFilesPasteButton').style.visibility='visible';
			} else {
				getById('MyFilesPasteButton').style.visibility='hidden';
			}
		}
	/* toggle the tree */
	if (AfianUserPermissions['ui_has_tree']) {
		toggleTreeByPath(this.basepath);
	}
	setCookie('AfianLastFolder', this.basepath);
	/* change the title */
	AfianHandler.titleBar.innerHTML = this.title;
		if (!this.showHG) {
		MyFilesStatusBar();
		} else {
		this.showHourGlass();
		}
	}
}

function MyFilesStatusBar() {
	tb = AfianHandler.activeTab;
	if (tb.countSelected == 0) {
		if (tb.countAll == 0) {
			if (tb.browsed) {
			AfianHandler.statusBar.innerHTML = AfianMessages['Folder empty'];
			} else {
			AfianHandler.statusBar.innerHTML = AfianMessages['Accessing folder...'];
			}
		} else {
			if (tb.countAll == 1) {
		AfianHandler.statusBar.innerHTML = AfianMessages['One object (%1)'].replace('%1', formatFileSize(AfianHandler.activeTab.totalSize));

			} else {
		AfianHandler.statusBar.innerHTML =  AfianMessages['%1 objects (%2)'].replace('%1', tb.countAll).replace('%2', formatFileSize(AfianHandler.activeTab.totalSize));
			}
		}
	} else if (tb.countSelected == 1) {
		AfianHandler.statusBar.innerHTML = AfianMessages['One object selected (%1)'].replace('%1', formatFileSize(tb.selSize));
	} else {
		AfianHandler.statusBar.innerHTML =  AfianMessages['%1 objects selected (%2)'].replace('%1', tb.countSelected).replace('%2', formatFileSize(tb.selSize));
	}
}

function MyFilesOnSelect() {
	showfuncname = showMenu;
	hidefuncname = hideMenu;
	
	if (this.countSelected == 0) {
	
	if (AfianHandler.components['MyFilesContextMenus'].loaded) {
		hidefuncname('afian_context_dir');
		hidefuncname('afian_context_file');
		hidefuncname('afian_context_multiple');
	}
		
	} else if (this.countSelected == 1) {
		sel = this.selectedItem();
		if (AfianHandler.components['MyFilesContextMenus'].loaded) {
		if (sel.isdir) {
		showfuncname('afian_context_dir');
		hidefuncname('afian_context_file');
		hidefuncname('afian_context_multiple');
		} else {
		unzipcontextoption = getById('MyFilesContextUnzipOption');
		zipcontextoption = getById('MyFilesContextZipOption');

		if (sel.iszip) {
			unzipcontextoption.style.display='inline';
			zipcontextoption.style.display='none';
		} else {
			unzipcontextoption.style.display='none';
			zipcontextoption.style.display='inline';
		}
		
		showfuncname('afian_context_file');
		hidefuncname('afian_context_dir');
		hidefuncname('afian_context_multiple');
		}
		}
	} else {
	
		if (AfianHandler.components['MyFilesContextMenus'].loaded) {
		showfuncname('afian_context_multiple');
		hidefuncname('afian_context_dir');
		hidefuncname('afian_context_file');
		}
	}
	MyFilesStatusBar();
}

/*download*/
function get(filename) {
	if(!AfianUserPermissions['download']){
		AfianAlert(AfianMessages['You are not allowed to download files.']);
		return false;
	}
	
	filename = (filename)?filename:tb.selectedItem().input_value;
	tb = AfianHandler.activeTab;
	document.hiddenForm.action = '?file=myfilesacting';
	document.hiddenForm.action += '&act=download';
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.hiddenForm.action += '&filename='+encodeURIComponent(filename);
	document.hiddenForm.target = 'hifrm_'+tb.id;
	document.hiddenForm.submit();
	tb.unselectAll();
}

function multiget() {
	if(!AfianUserPermissions['download']){
		AfianAlert(AfianMessages['You are not allowed to download files.']);
		return false;
	}
	
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	document.hiddenForm.action = '?file=myfilesacting';
	document.hiddenForm.action += '&act=multidownload';
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.hiddenForm.action += '&fromtabid='+tb.id;
	fillHiddenFormItemList();
	document.hiddenForm.target = 'hifrm_'+tb.id;
	tb.unselectAll();
	if (document.hiddenForm.itemlist.value.length > 1) {
		document.hiddenForm.submit();
	}
	}
}


/*upload*/
function Mput() {
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	document.afian_upload_form.action = '?file=myfilesacting&act=upload&tofolder='+encodeURIComponent(tb.basepath)+'&totabid='+tb.id;	
	/*
	hiframeid = 'action_hifrm_upload_'+new Date().getTime();
	newHiddenIframe(hiframeid);
	document.afian_upload_form.target = hiframeid;
	alert('se trimite catre frame-ul:'+hiframeid);
	*/
	document.afian_upload_form.target = 'hifrm_'+tb.id;
	document.afian_upload_form.submit();
	hideMenu('popmenu-afian_upload');
	/*document.afian_upload_form.reset(); //MSIE sux bigtime*/
	tb.showHourGlass();
	
	/* timers */
	tabStartTimer(tb);
	}
}


function MyFilesShowUpload() {
	upload2share=false;
	centeredMenu('popmenu-afian_upload');
	JAVAUploadSettings['tabId'] = AfianHandler.activeTab.id;
	if (AfianHandler.uploadType) {
		initJUploadApplet();
	}
}

function switchUploadType(type) {
	if (upload2share) {
		AfianBubbleMessage('You cannot use drag&drop mode in a shared folder.');
		return false;
	}
	setCookie('upload_type', type);
	if (type == 'java') {
		getById('clasic_upload_btn').style.display = 'none';
		getById('afian_jupload').style.display = 'block';
		getById('afian_upload_clasic').style.display='none';
		initJUploadApplet();
		AfianHandler.uploadType = 'java';
	} else {
		getById('clasic_upload_btn').style.display = 'block';
		getById('afian_jupload').style.display = 'none';
		getById('afian_upload_clasic').style.display='block';
		AfianHandler.uploadType = 'clasic';
	}
}

function initJUploadApplet() {
	var tb = AfianHandler.activeTab;
	postURL = jUploadURL+'&path='+encodeURIComponent(tb.basepath);
	getById('juploadHTMLCode').innerHTML = '';
	insertJavaApplet(getById('juploadHTMLCode'), postURL)
}

function MyFilesNewFolder() {
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	document.mkdirForm.action='?file=myfilesacting';
	document.mkdirForm.action += '&act=newfolder';
	document.mkdirForm.action += '&totabid='+tb.id;
	document.mkdirForm.action += '&path='+encodeURIComponent(tb.basepath);
	document.mkdirForm.target = 'hifrm_'+tb.id;
	document.mkdirForm.submit();
	hideMenu('popmenu-afian_mkdir');
	document.mkdirForm.dirName.value='Folder Name';
	tb.showHourGlass();
	/* timers */
	tabStartTimer(tb);
	}
}

function MyFilesDel() {
	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to delete files.']);
		return false;
	}
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	document.hiddenForm.action='?file=myfilesacting';
	document.hiddenForm.action += '&act=delete';
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.hiddenForm.action += '&fromtabid='+tb.id;
	fillHiddenFormItemList();
	document.hiddenForm.target = 'hifrm_'+tb.id;
	tb.unselectAll();
	if (document.hiddenForm.itemlist.value.length > 1) {
		document.hiddenForm.submit();
		tb.showHourGlass();
		/* timers */
		tabStartTimer(tb);
	}
	}
}

function MyFilesPaste() {
	getById('MyFilesPasteButton').style.visibility='hidden';

	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to move or copy files.']);
		return false;
	}

	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	clipboard.totabid = tb.id;
	clipboard.totabbasepath = tb.basepath;
	document.hiddenForm.action='?file=myfilesacting';
	document.hiddenForm.action += '&act='+clipboardAction;
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(clipboard.fromtabbasepath);
	document.hiddenForm.action += '&tofolder='+encodeURIComponent(clipboard.totabbasepath);
	document.hiddenForm.action += '&fromtabid='+clipboard.fromtabid;
	document.hiddenForm.action += '&totabid='+clipboard.totabid;
	if (clipboard.fromuid) {
	document.hiddenForm.action += '&fromuid='+clipboard.fromuid;
	}
	document.hiddenForm.itemlist.value = '';
	for (var i = 0 ; i < clipboard.items.length; i++) {
		if ((clipboard.fromtabbasepath != clipboard.totabbasepath) || clipboard.fromuid != false) {

src = gluePath([clipboard.fromtabbasepath,'/',clipboard.items[i]]);
			if (clipboard.totabbasepath.substring(0,src.length) != src) {
				document.hiddenForm.itemlist.value += '|'+encodeURIComponent(clipboard.items[i]);
			} else {
				AfianAlert(AfianMessages['You cannot move or copy a folder under itself']);
			}
			
		}
	}
	document.hiddenForm.target = 'hifrm_'+clipboard.totabid;
	if (document.hiddenForm.itemlist.value.length > 1) {
	document.hiddenForm.submit();
	tb.showHourGlass();
	/* timers */
	tabStartTimer(tb);
	}
		resetClipboard();
	/*
	if (AfianHandler.allTabs[clipboard.fromtabid] != undefined) {
		AfianHandler.allTabs[clipboard.fromtabid].showHourGlass();
	}
	*/
	
	}
}



/*
//use this to see what's in the clipboard

function clipboardTip() {
	if (clipboardAction == 'copy') {
	} else if (clipboardAction == 'cut') {
	} else {
	}
	for (var i = 0 ; i < clipboard.items.length; i++) {
		gluePath([clipboard.fromtabbasepath,clipboard.items[i]])
	}
	return str;
}
*/



function MyFilesSync(tab) {
	if (!tab.isBusy()) {
	tab.showHG = true;
	tab.showHourGlass();
	document.hiddenForm.itemlist.value = '';
	document.hiddenForm.action='?file=myfilesacting&act=sync&tabid='+tab.id+'&path='+encodeURIComponent(tab.basepath);
	for (var i = 0 ; i < tab.items.length; i++) {
	if (tab.items[i] != undefined) {
		document.hiddenForm.itemlist.value += '|'+encodeURIComponent(tab.items[i].input_value);
	}
	}
	document.hiddenForm.target = 'hifrm_'+tab.id;
	document.hiddenForm.submit();
	/* timers */
	tabStartTimer(tb);
	/*reloadTreeByPath(tab.basepath);*/
	}
}



function MyFilesRename() {

	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to rename files.']);
		return false;
	}

	tb = AfianHandler.activeTab;
	var newform='';
	for (var i=0; i < tb.items.length; i++) {
	if (tb.items[i] != undefined) {
		if (tb.items[i].selected) {
		newform += '<input type="text" name="newName[]" value="'+tb.items[i].icon_title+'"><input type="hidden"  value="'+encodeURIComponent(tb.items[i].input_value)+'" name="oldName[]"><br><br>';
		}
	}
	}
	getById('renameCust').innerHTML = newform;
	centeredMenu('popmenu-afian_rename');
	/*tb.unselectAll();*/
		hideMenu('afian_context_file');
		hideMenu('afian_context_dir');
		hideMenu('afian_context_multiple');
}

function MyFilesRenameSubmit() {
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	tb.unselectAll();
	document.renameForm.action='?file=myfilesacting';
	document.renameForm.action += '&act=rename';
	document.renameForm.action += '&fromtabid='+tb.id;
	document.renameForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.renameForm.target = 'hifrm_'+tb.id;
	document.renameForm.submit();
	hideMenu('popmenu-afian_rename');
	document.renameForm.reset();
	tb.showHourGlass();
	/* timers */
	tabStartTimer(tb);
	}
}

function MyFilesZip() {
	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to zip files.']);
		return false;
	}
	hideMenu('afian_context_file');
	hideMenu('afian_context_dir');
	hideMenu('afian_context_multiple');
	centeredMenu('popmenu-afian_zip');
}
function MyFilesUnZip() {
	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to unzip files.']);
		return false;
	}
	hideMenu('afian_context_file');
	hideMenu('afian_context_dir');
	hideMenu('afian_context_multiple');
	centeredMenu('popmenu-afian_unzip');
}

function MyFilesUnZipSubmit() {
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	selected = tb.selectedItem();
	fromtabid = tb.id;
	document.hiddenForm.action='?file=myfilesacting';
	document.hiddenForm.action += '&act=unzip';
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.hiddenForm.action += '&fromtabid='+fromtabid;
	document.hiddenForm.action += '&filename='+encodeURIComponent(selected.input_value);
	document.hiddenForm.action += '&foldername='+encodeURIComponent(document.unzipForm.foldername.value);
	document.hiddenForm.target = 'hifrm_'+fromtabid;
	tb.unselectAll();
	document.hiddenForm.submit();
	tb.showHourGlass();
	hideMenu('popmenu-afian_unzip');
	document.unzipForm.reset();
	/* timers */
	tabStartTimer(tb);
	}
}

function MyFilesZipSubmit() {
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	fromtabid = tb.id;
	document.hiddenForm.action='?file=myfilesacting';
	document.hiddenForm.action += '&act=zip';
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.hiddenForm.action += '&fromtabid='+fromtabid;
	document.hiddenForm.action += '&zipname='+encodeURIComponent(document.zipForm.zipname.value);
	fillHiddenFormItemList();
	document.hiddenForm.target = 'hifrm_'+fromtabid;
	hideMenu('popmenu-afian_zip');
		tb.unselectAll();
	if (document.hiddenForm.itemlist.value.length > 1) {
		document.hiddenForm.submit();
		tb.showHourGlass();
		/* timers */
		tabStartTimer(tb);
		document.zipForm.reset();
	}
	}
}


function MyFilesEditText(newfile, confirm) {
	hideMenu('afian_context_file');
	hideMenu('afian_context_dir');
	hideMenu('afian_context_multiple');

	tb = AfianHandler.activeTab;
	if (newfile == "1") {
popup(baseURL+'?file=edittext&tabid='+tb.id+'&path='+encodeURIComponent(tb.basepath)+'&filename=newfile.txt&new=1', 'editastext'+Math.round(Math.random()), '460', '650', 'yes');
	} else {
		if (tb.countSelected == 0) {
			AfianAlert(AfianMessages['Please select a file to edit.']);
			return false;
		}
	selected = tb.selectedItem();
	istext = selected.txt;
	

	if (istext == false) {
		if (!confirm){
		b = new Array;
		b[0] = new Array;
		b[0]['title'] = 'Yes';
		b[0]['onclick'] = 'MyFilesEditText(\''+((newfile)?'true':'false')+'\', true);hideMenu(\'popmenu-prompt\')';
		b[1] = new Array;
		b[1]['title'] = 'Cancel';
		b[1]['onclick'] = 'hideMenu(\'popmenu-prompt\');AfianHandler.activeTab.onselect()';
		AfianPrompt(AfianMessages['This doesn\'t seem to be a text file.<br>Edit anyway?'], b);
		return false;
		}
	}
	popup(baseURL+'?file=edittext&tabid='+tb.id+'&path='+encodeURIComponent(tb.basepath)+'&filename='+encodeURIComponent(selected.input_value), 'editastext'+Math.round(Math.random()), '460', '650', 'yes');
	tb.unselectAll();
	}
}

function MyFilesByMail() {
	if (AfianUserPermissions['byemail'] == false) {
		AfianAlert(AfianMessages['You are not allowed to send files by email.']);
		return false;
	}
	tb = AfianHandler.activeTab;
	if (tb.selHasOnlyDirs()) {
		AfianAlert(AfianMessages['Please select at least one file.']);
	} else {
		document.hiddenForm.action='?file=byemail';
		document.hiddenForm.action += '&path='+encodeURIComponent(tb.basepath);
		document.hiddenForm.action += '&tabid='+tb.id;
		document.hiddenForm.itemlist.value = '';
		fillHiddenFormItemList();
		pop = popup('javascript:;', 'bymail', '400', '454', 'no');
		document.hiddenForm.target = 'bymail';
		if (document.hiddenForm.itemlist.value.length > 1) {
			if (pop) {
			document.hiddenForm.submit();
			}
		}
	}
}

function MyFilesComment(){
	/*
	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to add comments to files.']);
		return false;
	}
	*/

	tb = AfianHandler.activeTab;
	popup(baseURL+'?file=comments&path='+encodeURIComponent(tb.basepath)+'&object='+encodeURIComponent(tb.selectedItem().input_value), 'comments', '250', '544', 'yes');
}

function MyFilesShare(){

	if (AfianUserPermissions['share'] == false) {
		AfianAlert('You are not allowed to share folders.');
		return false;
	}
	tb = AfianHandler.activeTab;
	popup(baseURL+'?file=share&path='+encodeURIComponent(tb.basepath)+'&object='+encodeURIComponent(tb.selectedItem().input_value), 'share', '360', '514', 'no');
}


function MyFilesEndAction(tb) {
	if (tb != undefined) {
		if (tb.active) {
			MyFilesStatusBar();
		}
		tb.hideHourGlass();
		cancelTimeOut(tb.id);
	}
}

function QuickSelectStatus() {
count = AfianHandler.activeTab.countSelected;
if (count > 0) {
getById('quickselcount').innerHTML=AfianMessages['%1 matches'].replace('%1', count);
} else {
getById('quickselcount').innerHTML=AfianMessages['No matches'];
}
}

function MyFilesDropDrag(toPath) {
	if(AfianUserPermissions['readonly']){
		AfianAlert(AfianMessages['You are not allowed to move files.']);
		return false;
	}
	toPath = addSingleSlashess(toPath);
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	document.hiddenForm.action='?file=myfilesacting';
	document.hiddenForm.action += '&act=dropdrag';
	document.hiddenForm.action += '&fromfolder='+encodeURIComponent(tb.basepath);
	document.hiddenForm.action += '&tofolder='+encodeURIComponent(toPath);
	document.hiddenForm.action += '&fromtabid='+tb.id;
	document.hiddenForm.action += '&totabid='+tb.id;
	/*
	if (clipboard.fromuid) {
	document.hiddenForm.action += '&fromuid='+clipboard.fromuid;
	}
	*/
	document.hiddenForm.itemlist.value = '';
	for (var i = 0 ; i < tb.items.length; i++) {
		if (tb.items[i] != undefined) {
		if (tb.items[i].selected) {
			src = gluePath([tb.basepath,'/',tb.items[i].input_value]);
			if (toPath.substring(0,src.length) == src) {
			AfianBubbleMessage(AfianMessages['You cannot move or copy a folder under itself.']);
			} else if (tb.basepath == toPath) {
			/*AfianBubbleMessage('The folder you want to move is already there.');*/
			} else {
				document.hiddenForm.itemlist.value += '|'+encodeURIComponent(tb.items[i].input_value);
			}
		}
		}
	}
	document.hiddenForm.target = 'hifrm_'+tb.id;
	if (document.hiddenForm.itemlist.value.length > 1) {
	document.hiddenForm.submit();
	tb.showHourGlass();
	/* timers */
	tabStartTimer(tb);
	tb.unselectAll();
	}
	}
}


function MyFilesStartSlideShow() {
	tb = AfianHandler.activeTab;
	popup(baseURL+'?slideshow=1&path='+encodeURIComponent(tb.basepath), 'slideshow', '550', '700', 'no');
}
