
function openShare(username, uid, path) {
	if (!AfianHandler.components['BShareBaseMenu'].loaded) {
		loadComponentData('BShareBaseMenu');
	}
	if (!AfianHandler.components['BShareContextMenus'].loaded) {
		loadComponentData('BShareContextMenus');
	}
	if (AfianHandler.activeTab.section == 'browseshare') {
		AfianHandler.activeTab.uid = uid;
		BShareCD(path);
	} else {
		bshare = new Afian_Tab(username+': '+path, path);
		bshare.section = 'browseshare';
		bshare.onload = BShareOnLoad;
		bshare.username = username;
		bshare.uid = uid;
		bshare.canwrite = false;
		bshare.onselect = BShareOnSelect;
		bshare.baseMenu = 'BShareBaseMenu';
		bshare.detailHTML = AfianHandler.BShareDetailsTemplate;
		bshare.load();
		bshare.BShare_Browse();
	}
}

function BShareOnLoad() {
	if (AfianHandler.activeTab == this) {
	AfianHandler.showBaseMenu('BShareBaseMenu');
	AfianHandler.hideBaseMenu('MySharesBaseMenu');
	AfianHandler.hideBaseMenu('MyFilesBaseMenu');
	AfianHandler.hideBaseMenu('TrashBaseMenu');
	AfianHandler.hideBaseMenu('SearchBaseMenu');
	AfianHandler.hideBaseMenu('BrowseZipBaseMenu');
	
		/*admin stuff*/
	AfianHandler.hideBaseMenu('AdmGroupsBaseMenu');
	AfianHandler.hideBaseMenu('AdmUsersBaseMenu');
	AfianHandler.hideBaseMenu('AdmLogsBaseMenu');
	
	if (activeMenu) {
		hideMenu(activeMenu);
	}
	
	if (AfianHandler.components['BShareBaseMenu']['loaded']) {
	document.getElementById('BShareBreadcrumbs').innerHTML = this.username+': '+ getBreadCrumbs('<a href="#" onclick="BShareCD(\'PATH\')">',this.basepath,'</a>');
	/* highlight the toggle */
	toggleView(this.viewMode);
			if (clipboard.items.length > 0) {
				if (this.canwrite) {
				document.getElementById('BSharePasteButton').style.visibility='visible';
				} else {
				document.getElementById('BSharePasteButton').style.visibility='hidden';
				}
			} else {
				document.getElementById('BSharePasteButton').style.visibility='hidden';
			}
	}
	toggleTreeByPath(this.username+'::'+this.basepath);
	AfianHandler.titleBar.innerHTML = this.title;
		if (!AfianHandler.activeTab.showHG) {
			BShareStatusBar();
		} else {
			this.showHourGlass();
		}
	}
	
	
	
}


function BShareStatusBar() {
	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 {
		AfianHandler.statusBar.innerHTML = AfianMessages['%1 objects (%2)'].replace('%1', tb.countAll).replace('%2',formatFileSize(tb.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 BShareOnSelect() {
	showfuncname = showMenu;
	hidefuncname = hideMenu;

	if (this.countSelected == 0) {
	
	if (AfianHandler.components['BShareContextMenus'].loaded) {
		hidefuncname('afian_bshare_context_dir');
		hidefuncname('afian_bshare_context_file');
		hidefuncname('afian_bshare_context_multiple');
	}
		
	} else if (this.countSelected == 1) {
		sel = this.selectedItem();
		if (AfianHandler.components['BShareContextMenus'].loaded) {
		if (sel.isdir) {
		showfuncname('afian_bshare_context_dir');
		hidefuncname('afian_bshare_context_file');
		hidefuncname('afian_bshare_context_multiple');
		} else {
		showfuncname('afian_bshare_context_file');
		hidefuncname('afian_bshare_context_dir');
		hidefuncname('afian_bshare_context_multiple');
		}
		}
	} else {
	
		if (AfianHandler.components['BShareContextMenus'].loaded) {
		showfuncname('afian_bshare_context_multiple');
		hidefuncname('afian_bshare_context_dir');
		hidefuncname('afian_bshare_context_file');
		}
	}
	BShareStatusBar();
}

function BShareSync(tab) {
	document.hiddenForm.itemlist.value = '';
	document.hiddenForm.action='?file=bshareacting&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 += '|'+tab.items[i].input_value;
	}
	}
	document.hiddenForm.target = 'hifrm_'+tab.id;
	document.hiddenForm.submit();
	tab.showHourGlass();
}


function BShareCD(path, newtab, title) {
	tb = AfianHandler.activeTab;
	selected = tb.selectedItem();
	if (path == '/') {
		/*AfianAlert('You are browsing the top folder.<br>You can select another shared folder (if aplicable) from the tree menu.');*/
		return true;
	}
	path = path?path:gluePath([tb.basepath, selected.input_value]);
	if (newtab) {
		title = title?title:selected.icon_title;
		newTab = new Afian_Tab(title, path);
		newTab.section = 'myfiles';
		newTab.viewMode = tb.viewMode;
		newTab.onload = BShareOnLoad;
		newTab.detailHTML = AfianHandler.MyFilesDetailsTemplate;
		newTab.load();
		newTa.basepath = path;
		newTab.BShare_Browse();
		newTab.onselect = BShareOnSelect;
	} else {
		if (!tb.isBusy()) {
		tb.basepath = path;
		tb.BShare_Browse();
		}
	}
	tb.showHourGlass();
}

function BShareGet(filename) {
	tb = AfianHandler.activeTab;
	filename = (filename)?filename:tb.selectedItem().input_value;
	document.hiddenForm.action = '?file=bshareacting';
	document.hiddenForm.action += '&act=download';
	document.hiddenForm.action += '&path='+tb.basepath;
	document.hiddenForm.action += '&uid='+tb.uid;
	document.hiddenForm.action += '&filename='+filename;
	document.hiddenForm.target = 'hifrm_'+tb.id;
	document.hiddenForm.submit();
	AfianHandler.activeTab.unselectAll();
}

function BSharePut() {
	tb = AfianHandler.activeTab;
	if (!tb.isBusy()) {
	document.afian_upload_form.action = '?file=bshareacting';
	document.afian_upload_form.action += '&act=upload';
	document.afian_upload_form.action += '&path='+tb.basepath;
	document.afian_upload_form.action += '&uid='+tb.uid;
	document.afian_upload_form.action += '&totabid='+tb.id;
	document.afian_upload_form.target = 'hifrm_'+tb.id;
	document.afian_upload_form.submit();
	hideMenu('popmenu-afian_upload');
	document.afian_upload_form.reset();
	tb.showHourGlass();
	}
}

function BShareEndAction(tb) {
	if (tb != undefined) {
		tb.showHG = false;
		
		if (tb.active) {
			BShareStatusBar();
		}
		tb.hideHourGlass();
		cancelTimeOut(tb.id);
	}
}

function BShareShowUpload() {
	if (AfianHandler.activeTab.canwrite) {
		switchUploadType('clasic');
		upload2share=true;
		centeredMenu('popmenu-afian_upload');
	} else {
	AfianAlert(AfianMessages['This shared folder has "read-only" access']);
	}
}

function BSharePaste() {
	tb = AfianHandler.activeTab;
	if (clipboard.fromuid == tb.uid) {
		AfianAlert(AfianMessages['You cannot copy files or folders from and to the same user.']);
		return true;
	}
	
	if (!tb.isBusy()) {
	document.hiddenForm.action = '?file=bshareacting';
	document.hiddenForm.action += '&act=copy';
	document.hiddenForm.action += '&fromfolder='+clipboard.fromtabbasepath;
	document.hiddenForm.action += '&tofolder='+tb.basepath;
	document.hiddenForm.action += '&uid='+tb.uid;
	document.hiddenForm.action += '&fromtabid='+clipboard.fromtabid;
	document.hiddenForm.action += '&totabid='+tb.id;
	document.hiddenForm.itemlist.value = '';
	
	for (var i = 0 ; i < clipboard.items.length; i++) {
		document.hiddenForm.itemlist.value += '|'+clipboard.items[i];
	}
	
	document.hiddenForm.target = 'hifrm_'+tb.id;
	if (document.hiddenForm.itemlist.value.length > 1) {
	document.hiddenForm.submit();
	tb.showHourGlass();
	/* timers */
	tabStartTimer(tb);
	}
	document.getElementById('BSharePasteButton').style.visibility='hidden';
	resetClipboard();
	}
}

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