
#surgeweb new calendaring implementation


function cloud_action()
{
	x_alert("cal action");
}

function show_cld_ui()	// cf contacts_mgt_sw
{
	sndReqCached('cmd=show&page=~cloud.htm'+'&if=ajax&static=||web_ref_path||&||vz||', function(chan){
#		// Same in manage_folders
		if (chan.status!=200){
			success_string=err_comms(5);
			set_status(success_string,'warning');
			return false;
		}
		node=actionReqSethtml(chan.responseText,'utility');
#		if (global_auth_error){
#			global_auth_error=false;
#			window_manager_restore();
#		}
	},ch[enFG]);
}	

function cloud_lpane_html()
{	
	return  '<div id="cloud_tree" class="xc pt20">My Cloud Files</div>'+\
			'<div class="xc pt20"><input type="button" value="Configure" onclick="manage_calendars(event)"></div>'+\
#			'<div class="xc pt20" style="color:#777">Note: This is not the calendar displayed on the right!</div>'+\
			'<div><br>Other: <ul><li><a href="#" onclick="app_menu(\'photos\')">SurgePlus Photos</a></li><li><a href="#" onclick="app_menu(\'files\')">SurgePlus Files</a></li><li><a href="#" onclick="app_menu(\'blogs\')">Blogs</a></li></ul></div>'+\
			'';
}


function cloud_init()
{
#	dge('cal_groups').innerHTML='foo';
	var q2=dge('lpanel_cloud');
	remove_children(q2);
	new_anchor('div','<div id="cloud_lpane" style="padding:5px">'+cloud_lpane_html()+'</div>',q2);
	cloud_selection_init();
	cloud_sel()
	if(do_fdnd()){
		// msglist_box vs 
		fdnd_init(dge('cloudlist_box'),'copyMove',cdnd_list_hover,cdnd_list_drop);
	}

	return ;
	var i;
	var arr=['<ul id="gtree" class="gtree ftree">'];

	if(abooks.read_books.length==1){
		contacts_output_abooks(arr,'User');
	}else{
		arr.push(cp_format_abook3('$$st_ct_personal$$'));
		contacts_output_abooks(arr,'User')
		arr.push(cp_format_abook3('$$st_ct_directories$$'));
		for(var i=0;i<abooks.read_books.length;i++){
			abook=abooks.read_books[i];
			if(abook=='User') continue;
			contacts_output_abooks(arr,abook,i)		
		}
 	 	arr.push('</ul>');
	}
	dge('cm_groups').innerHTML=arr.join('');
	dge('cm_details').innerHTML="$$st_ct_sel_detail$$";
	cm_reselect_group();
	dnd2_init();
}

function cloud_resize(h,w)
{
	if (sw.active.id!='cloud_panel') return;
	var ww=w-||panel_gap||-2-200;
	var o=dge('cloud_list').parentNode;
	var s=o.style;
	s.height=px(h);
	s.width=px(ww);
	var o2=dge('cloud_details_scrolled');
	var s2=o2.style;
	s2.height=px(h);
	var o3=dge('cloud_list');
	o3.style.height=px(h);
	
	cloud_grid_reposition(ww,h);
	sw.cld_w=ww;
	sw.cld_h=h;
	
#	s.width=px(pref.sizer1*1.2);
#	s=dge('cal_outline').style;
#	o=dge('cal_outline').parentNode;
#	o.style.height=px(h);
#	var o=dge('contacts_info');
#	o.style.height=px(h);
}

function cloud_sel()	//cf account_sel
{
	if(!sw.cld_store) sw.cld_store='lcal';
	sw.cld_path='/';

#	show anything cached we already have
	dge('cloud_tree').innerHTML = 'cached cloud tree info';
#	fire off data request for more	
	var xcmd='cmd=show&page=~cld_result.js&output_folders=true&output_files=true';
	
	show_cloud(sw.cld_path,xcmd,sw.cld_store,U,'none');
}
# show_cld_ui	cf: function F(		cloud_location	function C(


var skip_one=true;
function show_cloud(cld_path, xcmd, cld_store, xcld_path, slide, opt)
{
	if(!opt) opt={};
	var trycache=false;//true;
	if(!cld_store) cld_store=sw.cld_store;
	xcld_store=sw.cld_store;
	xcld_path=sw.cld_path;

	cloud_editing(false);

	if(!slide) slide='right';

	if(slide=='left'){ 
		cloud_selstore()
	}

	cloud_search_fullreset();
	cloudcache_store(xcld_store,xcld_path,U);
	cloud_slide(slide);
	cloud_reset_empty();

	sw.cld_store=cld_store;
	sw.cld_path=cld_path;

	var was_cached = false, cache_entry;
	if(false && /*!refresh && !pref.nocache2 &&*/ trycache && !skip_one){
		cache_entry = cloudcache_find(cld_store,cld_path);
		if(cache_entry) was_cached=true;
#		msgdnd_init();
#		if(o.valid && (sw.search_type=='body' || sw.search_type=='folder')) {
#			sw.search_input.focus();
#		}
	}
	skip_one=false;

	cloud_crumbs('&nbsp;',U,was_cached);
	
	if (was_cached){
		restoreTimer=setTimeout(function(){
			clearTimeout(slideTimer);
			cloudcache_restore(cache_entry);
		}, 300);
	}
	
	if(slide!='left'){
		cloud_preview_none();
	}
	if(was_cached) return;

	var param={};
	var extra='';
#	extra+='&ntest=20';
	ajax_param(param,'cld_store',cld_store);
	ajax_param(param,'cld_path',cld_path);
	
#	var cmd='cmd=show&page=~cld_files.js&cld_store='+xescape(cld_store)+'&cld_path='+cld_path+iid()+extra+'&want_layout='+sw.cld_list.want_layout;
	var cmd='cmd=show&page=~cld_files.js&want_layout='+sw.cld_list.want_layout;
	if(xcmd) cmd=xcmd;
	
#	sndReq(cmd, function(chan){
	ajaxPost(cmd+iid(), param, function(chan){
		clearTimeout(restoreTimer);
		clearTimeout(slideTimer);
		success = actionReqJs(chan);

		if(opt.keep_search){
			cloud_search_restore();
			cloud_search_apply();
		}
		if(slide!='left'){
			cloud_select('none');
			cloud_selectpop();
		}
	});
}
function refresh_cloud()
{
	var cmd='cmd=show&page=~cld_result.js&cld_store='+xescape(sw.cld_store)+'&cld_path='+sw.cld_path+\
			'&output_files=true&sid='+sid+cid();
	sndReq(cmd, function(chan){
		var success=actionReqJsStat(chan);
	});
}


var cloud_sel_old=[];
function cloud_selectpop()
{
	var id=cloud_sel_old.pop();
	gs_select_bymid(sw.cld_list,id,U,false);
}
function cloud_selstore()
{
	var sel=gs_find_sel(sw.cld_list);
	if(sel.length==0) return;
	cloud_sel_old.push(sel[0]);
}


// cloud cache much like folder cache:
var cloud_cache=new Object();
function cloudcache_store(cld_store,cld_path,xfid)	// cf fldcache_store
{
	var el2=dge('cloud_list');
	if(el2 && el2.firstChild){
		var el2b=el2.firstChild.firstChild;
		if(el2b.slide_bad) {
			return;
		}
	}


	var cache_it = true;
	var co = new Object();
	co.list = dge('cloud_list').innerHTML;
	co.nav = dge('cloudlist_extra').innerHTML;
	co.time = time();
#	var sel = find_sel(true,xiid,xfld_id,xfid);
#	if(sel) co.sel = sel;
	cloud_cache[cld_store+"_"+cld_path] = co;
}
function cloudcache_find(cld_store,cld_path)		// cf fldcache_restore
{
	var co = cloud_cache[cld_store+"_"+cld_path];
	if(!co) return null;
	return co;
}
function cloudcache_restore(co)		// cf fldcache_restore
{
	dge('cloud_list').innerHTML = co.list;
	dge('cloudlist_extra').innerHTML = co.nav;
#	do_select('none');
#	if(co.sel){
#		msglist_select(co.sel[0],U,false);
#	}
#	sw.cached_sel = co.sel
	return true;
}
// ***
function set_cloud_folders(txt)
{
	var el=dge('cloud_tree');
	el.innerHTML=txt;
	el.onclick=folder_click;
	clear_folder_menu('folder_menu');
}
function set_cloud_files_js(obj, crumbs, depth, limit)
{
	var layout=sw.cloud_layout;
	var el=dge('cloud_list');
	sw.cld_list.want_layout=layout;
	sw.cloud_cur_limit=limit;
	el.innerHTML=cloud_files_to_html(obj,layout=='grid',sw.cloud_cur_limit);
	sw.cloud_cur_files=obj;
	cloud_crumbs(crumbs);
	sw.cloud_cur_depth=depth;
	cloud_grid_reposition(sw.cld_w, sw.cld_h);	
}
function set_cloud_files(txt, crumbs, depth, grid)
{
	var el=dge('cloud_list');
	el.innerHTML=txt;
	cloud_crumbs(crumbs);
	sw.cloud_cur_depth=depth;
	cloud_grid_reposition(sw.cld_w, sw.cld_h);	
}
function cloud_selection_init()	// cf 
{
	var el=dge('cloud_list');
	var gsel=new Object();
	gs_init(gsel,"sel",el);
	sw.cld_list=gsel;
	cloud_list_callbacks('grid');
}
function cloud_list_callbacks(type)
{
	var gsel=sw.cld_list;
	if (type=='grid'){
		gs_callbacks(gsel,cldlist2_rows,cldlist2_row_to_sel,cldlist2_sel_to_row,cldlist2_sel_to_mid,cldlist2_find_row,cldlist_show_soon);
	}else{
		gs_callbacks(gsel,cldlist_rows,cldlist_row_to_sel,cldlist_sel_to_row,cldlist_sel_to_mid,cldlist_find_row,cldlist_show_soon);
	}
}

function cldlist_rows(o){ return o.getElementsByTagName("tr"); }
function cldlist_row_to_sel(o){ if(!o) return null; sw.cld_sel_col=0; return o.childNodes[sw.cld_sel_col].firstChild;}
function cldlist_sel_to_row(node){ return ancestor_by_tag(node,'tr');}
function cldlist_sel_to_mid(node){ return node.id.substring(4);}
function cldlist_find_row(o,node){ return ancestor_by_tag(node,'tr');}

function cldlist2_rows(o){ return o.getElementsByTagName("li"); }
function cldlist2_row_to_sel(o){ if(!o) return null; return o.firstChild.firstChild.firstChild;}
function cldlist2_sel_to_row(node){ return ancestor_by_tag(node,'li');}
function cldlist2_sel_to_mid(node){ return node.id.substring(4);}
function cldlist2_find_row(o,node){ return ancestor_by_tag(node,'li');}

function cloud_list_mouseup(e){return false;}
function cloud_list_mousedown(e)
{
	if(!cx_menu(e)) return false;
#	if(e.metaKey) return false;
	
#	var t=target(e);
#	var tag=t.tagName.toLowerCase();
#	var row=ml_find_row(sw.ml,t);
#	var td=gs_find_td(sw.ml,t);
#	if(td==row.childNodes[1]){
#		return;
#	}
	gs_mousedown(sw.cld_list,e);

#	if(el_to_labels(t)){
#		labels_cx(e);
#	}else if(td==row.childNodes[0]){
#		do_menu_show('pg_menu_mark',e,undefined,undefined,{cursor:true})
#	}else{
#		do_menu_show('pg_menu_message',e,undefined,undefined,{cursor:true})
#	}
	stop_def_action(e)
	return false;
}
function cloud_list_click(e)	// cf msg_list_click
{
	var t=target(e);
	var tag=t.tagName.toLowerCase();
	var row=gs_find_row(sw.cld_list,t);
	if(!row) {
		cloud_select('none');
		return;
	}
	gs_list_click(sw.cld_list,e);
	cloud_show_preview(row);
}

function cloud_list_keydown(e)	// gs_keydown	// msglist_keydown
{
	var ret=true;
	
	if(is_cloud_content()){
		return cloud_content_keydown(e);
	}
	
	switch (e.keyCode){

#	Stop OSX Delete / backspace based back browsing
	case vkDelete: 
	case vkBspDel:
			if(ignore_done()) break;
			if(e.ctrlKey&&e.altKey) break;
			stop_def_action(e);
			ret=false;
			return ret;
			break;

#	enter - show message (in keydown as keyup received after pressing enter on a dialog)
	case vkEnter:
			cloud_open_cur();
			return ret;
			break;	

	case vkUpArrow:
	case vkLeftArrow:
			if (e.altKey||e.metaKey){
				cloud_browse_up();
#				ret=false;
				return ret;
			}
			break;
	case vkV:
			if(ctl_cmd_key(e)){
				cloud_toggle_view();			
				return ret;
			}
	case vkQ:
	case 191:
			if(ctl_cmd_key(e)) return ret;
			stopEvent(e);
			dge('cld_search').focus();
			ret=false;
			break;
			
	}	

	ret = gs_keydown(sw.cld_list,e);	
	dbg("********* cloud_list_keydown")

	return ret;
}

function show_cld(row,e)	//QQCLD_SHOW
{
	if(row && row.getAttribute('cld_folder')){
		var xstore=row.getAttribute('cld_fstore');
		var xpath=row.getAttribute('cld_fpath')+row.getAttribute('cld_fname');
		show_cloud(xpath,U,xstore,xpath,'left');
	}
}

function cloud_search_do(ctl,e)	// cf: contact_search2_do
{
	setTimeout(function(){cloud_search_real(ctl)},10);
}
function cloud_search_clear(ctl,e)
{
	if(ctl.value.indexOf("$$st_cld_srch_list$$") != -1){
		ctl.value="";
	}
}
function cloud_search_reset(ctl,e)
{
	if(ctl.value==''){
		ctl.value="$$st_cld_srch_list$$";
	}
}
function cloud_search_fullreset()
{
	var ctl=dge('cld_search');
	if(!ctl) return;
	ctl.value="$$st_cld_srch_list$$";
}
function cloud_save_search()
{
	cloud_saved_search=dge('cld_search').value;
}


function cloud_search_restore()
{
	dge('cld_search').value=cloud_saved_search;
	cloud_saved_search='';
}
function cloud_search_apply()
{
	var el=dge('cld_search');
	if (el.value.length==0 || el.value=='$$st_cld_srch_list$$') return;
	cloud_search_real(el);
}

function cloud_search_real(ctl)	// cf: contact_search2_real | search_keyup/apply_quick_search
{
	var count=0;
	var x;
	var match=ctl.value.toLowerCase();
	var nodes= gs_list_rows(sw.cld_list);
	var type=sw.cld_list.want_layout;
# 	normal hide does not seem to work so force it	
	var force=(type=='grid')
	for(var i=0; i < nodes.length; i++){
		var node = nodes[i];
		x=node.outerHTML.toLowerCase();
		if(x.indexOf(match) != -1){
			class_remove(node,'hidden');
			if(force) node.style.display='block';
		}else{
			class_add(node,'hidden');
			if(force) node.style.display='none';
			count++;
		}
	}
	if(force){
		cloud_grid_reposition(sw.cld_w, sw.cld_h);
	}
	return count;
}

function cloud_location(el)
{
	xcld_store=el.getAttribute('cld_lstore');
	xcld_path=el.getAttribute('cld_lpath');
	
	var el=dge('cloud_list');
#	cloud_slide(U,true);

	show_cloud(xcld_path,U,xcld_store,xcld_path,'fade');
}
var slideTimer,restoreTimer;
function cloud_slide(type,notimer)
{	
	if(type=='none') return;
	var el2a=dge('cloud_list')
	if(!el2a || !el2a.firstChild) return;
	el2=el2a.firstChild;
	if(!is_cloud_content) el2=el2.firstChild;
	if(!el2)return;
	el2.slide_bad=true;
	var w=800;
	class_add(el2,'animate_slide');
	if(type=='left'||type=='right'){
#		el2.style.width=px(w);
		if(type=='left'){
			el2.style.right=px(w/2);
		}else{
			el2.style.left=px(w/2);
		}
	}
	if(notimer)return;
	slideTimer=setTimeout(function(){
		dge('cloud_list').innerHTML = '<div style="color:#777;padding:5px;">Loading...</div>';}, 700);
}
function cloud_unslide()
{
	var el2=dge('cloud_list').firstChild.firstChild;
	class_remove(el2,'animate_slide');
	el2.style.right=px(0);
	el2.style.left=px(0);
	
}

function cid()	// cf iid
{
	var res='';
	res+='&cld_store='+xescape(sw.cld_store);
	res+='&cld_path='+xescape(sw.cld_path);
	return res;
}
function param_cid(param)
{
	ajax_param(param,'cld_store',sw.cld_store);
	ajax_param(param,'cld_path',sw.cld_path);
}

function cloud_row(id)
{
	var el=dge('sel_'+id);
	if(!el) return;
	return cldlist2_sel_to_row(el);
}
function cloud_delete()
{
	var sel=gs_find_sel(sw.cld_list);
	x_confirm('Permanently delete '+sel.length+' item(s), are you sure?', function(n){
		if(!n) return;
		cloud_showhide(sel,false);
		var param={}
		param_cid(param);
		param_sel(param,sel);
		ajaxPost('cmd=cld_action,show&page=~cld_result.js&action=delete_file&sid='+sid, param, function(chan){
			success = actionReqJs(chan);
			if(!success){
				cloud_showhide(sel,true);
				set_status('Error deleting items ('+success_string+')','warning');
				return;
			}
			set_status(sel.length+' item(s) deleted','success');
		});
	});
}
function cloud_showhide(sel,show)
{
	if(show){
		for_each(sel,function(id){var el=cloud_row(id); class_remove(el,'action_pending_hide')});
	}else{
		for_each(sel,function(id){var el=cloud_row(id); class_add(el,'action_pending_hide')});
	}
	cloud_grid_reposition(sw.cld_w, sw.cld_h);	
}

function cloud_download()	// cf do_download
{
	var sel=gs_find_sel(sw.cld_list);
	var url=cgibase+'cmd=cld_action&action=download_file&sid='+sid+cid()+'&sel='+sel+'&attachment=true';
	dge('an_iframe').src=url;
}
function cloud_download_tryopen()
{
	var sel=gs_find_sel(sw.cld_list);
	var url=cgibase+'cmd=cld_action&action=download_file&sid='+sid+cid()+'&sel='+sel;
	window.open(url);
}
function cdnd_list_hover(e,t,hovering)
{

}
function cdnd_list_drop(e,t,files)
{
	var i, fld_id=sw.fld_id; 
	for (i=files.length-1;i>=0;i--){
		ajax_upload(files[i],U, function(success,final){ 
				refresh_cloud();
				if (!success || !final) return;
			}, function(bytesLoaded,bytesTotal){
				var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
				set_status('Uploading '+percent+'%','info');
			},U,U,true);
	}
}

function cloud_list_dblclick(e)
{	
	var t=target(e);
	var row=gs_find_row(sw.cld_list,t);
	var valid_row=gs_row_to_sel(sw.cld_list,row);
	if (!valid_row) return;
	var mid=valid_row.id.substring(4);
	
	gs_list_dblclick(sw.cld_list,e);

	cloud_open(row);
}

function cloud_crumbs(val,opt)
{
	if(!opt) opt={};
	var el=dge('cloudlist_extra');
	txt='';
	if(!opt.nolink){
		txt+='<span style="float:right;padding-right:10px;">View: \
				<a href="#" class="link_blue" onclick="cloud_arrange(\'list\')">list</a> | \
				<a href="#" class="link_blue" onclick="cloud_arrange(\'grid\')">grid</a> \
#				<a href="#" class="link_blue" onclick="cloud_arrange(\'media\')">media</a> \
			</span>';
	}
	txt+=val;
	if(!txt){
		class_add(el,'hidden');
	}else{
		class_remove(el,'hidden');
		el.innerHTML=txt;
	}
}
function crumbs_root(el,refresh)
{
	var path=el.getAttribute('cld_path');
	var depth=el.getAttribute('cld_depth');
	var slide='right';
	if(depth<sw.cloud_cur_depth) slide='right';
	if(depth==sw.cloud_cur_depth) slide='fade';
	show_cloud(path,U,U,path,slide,{keep_search:true});
}
function cloud_browse_up()
{
	cloud_search_fullreset();
	var el=dge('cld_browse_up');
	if(!el)return;
	crumbs_root(el);
}
function cloud_browse_here()
{
	var el=dge('cld_browse_here');
	if(!el) el=dge('cld_browse_up');
	if(!el) return;
	crumbs_root(el);
}
function auth_redirect(url)
{
	window.location.href=url;
}
var count=1;
function cldlist_show_soon(file_id)
{
	var o=sw.cld_list;
	node = dge(o.sel_prefix+'_'+file_id);
	if (!node) return;
	
	var row=o.sel_to_row(node);

	cloud_show_preview(row);
}

function jfile_new(row)
{
	var jfile=new Object();
	jfile.is_dir=true;
	if(!row) {
		jfile.name='no name';
		jfile.type='folder';
		return jfile;
	}
	jfile.name=row.getAttribute('cld_fname');
	jfile.type=(row.getAttribute('cld_folder')?'folder':'');
	jfile.mime=row.getAttribute('cld_fmime');
	return jfile;
}	

function cloud_show_preview(row,name)
{
	var sel=gs_find_sel(sw.cld_list);
	var jfile=jfile_new(row);
	var opt=new Object();
	if(name) jfile.fname=name;
	if(!jfile.fname) jfile.fname='';
	jfile.fcontains=123;
	var txt='<div style="padding:15px">\
		<div>'+pht_image(jfile,opt)+'</div>\
		<ul class="cloud_info_list">\
			<li>'+jfile.name+'</li>';
	if(jfile.mime)
		txt+='<li>(mime_type='+jfile.mime+')</li>';
	if(sel.length>1)
		txt+='<li>'+sel.length+' files selected</li>';
			
	if(sel.length>0){
		txt+='<li>&nbsp;</li>\
			<li>Share:</li>\
			<li><div class="pl10">\
				<a class="link_blue" href="#" onclick="cloud_nop()">As link</a> | \
				<a class="link_blue" href="#" onclick="cloud_nop()">Send file</a></div></li>\
			<li><div style="padding-top:5px;">Action:</div></li>\
			<li><div class="pl10">\
				<a class="link_blue" href="#" onclick="cloud_nop()">Edit</a> | \
				<a class="link_blue" href="#" onclick="cloud_open_cur()">View </a> | \
				<a class="link_blue" href="#" onclick="cloud_download()">Download</a></div></li>\
			<li><div style="padding-top:5px;">Manage:</div></li>\
			<li><div class="pl10">\
				<a class="link_blue" href="#" onclick="cloud_tag()">Tag</a> | \
				<a class="link_blue" href="#" onclick="cloud_nop()">Copy</a> | \
				<a class="link_blue" href="#" onclick="cloud_nop()">Move</a> | \
				<a class="link_blue" href="#" onclick="cloud_delete()">Delete</a></div></li>';
	}else{
		txt+='<li>&nbsp;</li>\
			<li>Manage:</li>\
			<li><div class="pl10">\
				<a class="link_blue" href="#" onclick="cloud_new_folder()">New folder</a> | \
				<a class="link_blue" href="#" onclick="cloud_new_file()">New text file</a> \
				</li>';
	}
	
	txt+='\
			<li>&nbsp;</li>\
			<li>Info:</li>\
			<li><div class="pl10">Size / lines</div></li>\
			<li><div class="pl10">42 Versions</div></li>\
			<li><div class="pl10"><canvas id="hist_canvas"></canvas></div></li>\
			<li><div class="pl10">Edited:12 May 2:32am<br> &nbsp;&nbsp;&nbsp; by Marijn Weehuizen</div></li>\
			<li><div class="pl10">Created:10 May 1:39am</div></li>\
			<li><div class="pl10">Viewed 99 times</div></li>\
			<li><div class="pl10">Other important info??</div></li>\
			<li>&nbsp;</li>\
			<li>Four'+(count++)+'</li>\
		</ul>';
	dge('cloud_details').innerHTML=txt;
	clearTimeout(fade_timer);
	cloud_draw_hist();
	fade_timer=setTimeout(cloud_run_fade, 2000);
}
function cloud_preview_none()
{
	dge('cloud_details').innerHTML='';
}

function cloud_file(jfile)
{
	return pht_image(jfile, {'pht_height':100,'want_sel':true, 'h':100});
}

var any_type=1;
function pht_image(jfile, opt)
{	
	var type, random=false, with_old=false, photo_override=false;
	if(!opt) opt={};
#	random=true;
#	with_old=true;
	if(!random){
		if(jfile.ftype=='folder') {
			if(photo_override || jfile.fname.indexOf("Pictures")!=-1 || jfile.fname.indexOf("Photos")!=-1 
							  || jfile.fname.indexOf("Album")!=-1 || jfile.fname.indexOf("Pics")!=-1){
				return pht_image_fld2(jfile,opt);
			}else{
				return pht_image_fld(jfile,opt);
			}
		}else{
			return pht_image_doc(jfile,opt);
		}

		if(jfile.mime && jfile.mime.indexOf('text') != -1){
			type=4;
		}
	}	
	if(random){
		jfile.want_sel=false;
		while(true){
			any_type++;
			if(any_type>5) any_type=0;
			if(!with_old){
				if(any_type==0 || any_type==1 || any_type==2) continue;
			}
			break;
		}
	}
	type=any_type;
	switch (type){
		case 0: return pht_image_album(jfile,opt);
		case 1: return pht_image_h(jfile,opt);
		case 2: return pht_image_v(jfile,opt);
		case 3: return pht_image_fld(jfile,opt);
		case 4: return pht_image_doc(jfile,opt);

		case 5: return pht_image_fld2(jfile,opt);
	}
}

function pht_image_album(jfile,opt)	//
{
	if(!opt) opt={};
	var h=opt.pht_height;
	if(!h) h=128;
	var txt='\
	<span class="pht_node pht_with_img" style="_width: 165px; height: '+h+'px; ">\
#		<span class="pht_sel"><input type="checkbox" id="sel_'+jfile.fname+'"  /></span>\
		<span class="pht_txt_blk pht_with_text" style="width: 165px; height: '+h+'px; ">\
			<span class="pht_txt_blk2" style="">\
				<span class="pht_name_stl2">\
					<span class="pht_name_stl">Album: '+jfile.fname+'</span>\
				</span>\
			</span>\
			<span class="pht_number_stl">'+jfile.fcontains+'</span>\
		</span>\
		<span id="cf1" class="pht_img_blk pht_slow_fade" style="opacity: 1; width: 165px; height: '+h+'px; ">\
			<span class="pht_layout" style="width: 170px; height: '+h+'px; margin-left: -3px; margin-top: 0px; ">\
				<img alt="" title="" src="||www_base||shared/img/investigation/pht_sample.jpg">\
			</span>\
		</span>\
		<span id="cf2" class="pht_img_blk pht_slow_fade" style="opacity: 0; width: 165px; height: '+h+'px; ">\
			<span class="pht_layout" style="width: 170px; height: '+h+'px; margin-left: -3px; margin-top: 0px; ">\
				<img alt="" title="" src="||www_base||shared/img/investigation/pht_sample3.jpg">\
			</span>\
		</span>\
	</span>\
	';
	if(opt.as_link){
#  <a style="top: 0px; width: 227px; height: '+h+'px; left: 0px; " title="Album1" class="up_folderdrop" href="#cid=452A6125FCA37AE2&amp;id=452A6125FCA37AE2%21159&amp;sc=photos">\
		txt='<a style="top: 0px; width: 227px; height: '+h+'px; left: 0px; " title="android" href="#">'+txt+'</a>';
	}
	return txt;
}

function pht_image_hv(jfile,opt)
{
	var txt='';
# list item of some kind :-)	
	txt+='<span class="pht_node pht_with_img" style="width: 165px; height: 128px; ">';

# add a tickbox if we want one
	if(opt.want_sel) 
		txt+='<span class="pht_sel"><input type="checkbox" id="sel_'+jfile.fname+'" /></span>';
		
	txt+='<span class="pht_none_blk"></span>';
	txt+='<span class="pht_layout" style="'+pht_whlt(jfile,opt)+'">\
			<img alt="The text filename" title="" src="'+jfile.thumb_url+'">\
		  </span>';
	txt+='</span>';
	return txt;
}
function pht_whlt(jfile,opt)
{
	if(true||opt.fill){
		return 'width: 171px; height: 128px; margin-left: -3px; margin-top: 0px;';
	}else{
		return 'width: 96px; height: 128px; margin-left: 34px; margin-top: 0px';
	}
}

function pht_image_h(jfile,opt)
{
	return '\
	<span class="pht_node pht_with_img" style="width: 165px; height: 128px; ">\
#		<span class="pht_sel"><input type="checkbox" id="" /></span>\
		<span class="pht_none_blk"></span>\
		<span class="pht_layout" style="width: 171px; height: 128px; margin-left: -3px; margin-top: 0px; ">\
			<img alt="The text filename" title="" src="||www_base||shared/img/investigation/pht_sample.jpg">\
		</span>\
	</span>\
	';
}

function pht_image_v(jfile,opt)
{
	return '\
	<span class="pht_node pht_with_img" style="width: 165px; height: 128px; ">\
#		<span class="pht_sel"><input type="checkbox" id="" /></span>\
		<span class="pht_none_blk"></span>\
		<span class="pht_layout" style="width: 96px; height: 128px; margin-left: 34px; margin-top: 0px; ">\
			<img alt="The text filename" title="" src="||www_base||shared/img/investigation/pht_sample2.jpg">\
		</span>\
	</span>\
	';
}


function pht_image_fld(jfile,opt)
{
	if(!opt) opt={};
	var h=opt.h; if(!h) h=128;
	var txt= '\
	<span class="pht_node" style="_width: 165px !important; height: '+h+'px; ">\
		'+(opt.want_sel?'\
		<span class="pht_sel"><input type="checkbox" id="sel_'+jfile.fname+'" /></span>\
		':'')+'\
		<span class="pht_info_fld">'+hash_tags(jfile)+'</span>\
		<span class="pht_txt_blk pht_with_fld">\
			<span class="pht_fld_blk2">\
				<span class="pht_fld_outer">\
					<span class="pht_fld_gradient">\
						<img src="||www_base||shared/img/fld_gradient.png" class="itemTgrad" width="100%" height="100%">\
					</span>\
					<img class="pht_fld_tab" src="||www_base||shared/img/fld_tab.png">\
				</span>\
			</span>\
			<span class="pht_txt_blk2">\
				<span class="pht_name_stl2">\
					<span class="pht_name_stl" title="'+enc_ifdiff(jfile.fname,jfile.fdisplay)+'">'+ital_ifdiff(jfile.fname,jfile.fdisplay)+'</span>\
				</span>\
			</span>\
			<span class="pht_number_stl">'+jfile.fcontains+'</span>\
		</span>\
	</span>\
	';
	if(opt.as_link){
#		  <a style="top: 0px; width: 227px; height: 128px; left: 0px; " title="Album1" class="up_folderdrop" href="#cid=452A6125FCA37AE2&amp;id=452A6125FCA37AE2%21159&amp;sc=photos">\
		txt='<a style="top: 0px; width: 227px; height: 128px; left: 0px; " title="android" href="#">'+txt+'</a>';
	}
	return txt;
}
function enc_ifdiff(a,b)
{
	if(a==b) return '';
	return encode_quotes(a);
}
function ital_ifdiff(a,b)
{
	if(a==b) return a;
	return '<i>'+b+'</i>';
}
function pht_image_fld2(jfile,opt)
{
	if(!opt) opt={};
	var h=opt.h; if(!h) h=128;
	var txt= '\
	<span class="pht_node" style="_width: 165px !important; height: '+h+'px; ">\
		'+(opt.want_sel?'\
		<span class="pht_sel"><input type="checkbox" id="sel_'+jfile.fname+'" /></span>\
		':'')+'\
		<span class="pht_txt_blk pht_with_fld pht_imgfld">\
			<span class="pht_fld_blk2">\
				<span class="pht_fld_outer">\
# Gradient image covering whole face
\
#					<span id="cf1" class="pht_fld_gradient pht_img_blk pht_slow_fade" style="opacity: 1; width: '+(165-2)+'px; height: '+(h-2)+'px; ">\
#						<span class="pht_layout" style="width: 170px; height: '+h+'px; margin-left: -3px; margin-top: 0px; ">\
#							<img alt="" title="" src="||www_base||shared/img/investigation/pht_sample.jpg" style="visibility:visible">\
#						</span>\
#					</span>\
#					<span id="cf2" class="pht_fld_gradient pht_img_blk pht_slow_fade" style="opacity: 0; width: '+(165-2)+'px; height: '+(h-2)+'px; ">\
#						<span class="pht_layout" style="width: 170px; height: '+h+'px; margin-left: -3px; margin-top: 0px; ">\
#							<img alt="" title="" src="||www_base||shared/img/investigation/pht_sample3.jpg" style="visibility:visible">\
#						</span>\
#					</span>\
\
					<span id="cf1b" class="pht_fld_gradient pht_slow_fade" style="overflow:hidden;">\
						<span class="_pht_layout" style="width: 170px; height: '+h+'px; margin-left: -3px; margin-top: 0px; ">\
							<img src="||www_base||shared/img/investigation/pht_sample.jpg" class="itemTgrad" width="100%" height="100%">\
						</span>\
					</span>\
					<span id="cf2b" class="pht_fld_gradient pht_slow_fade" style="overflow:hidden;">\
						<span class="pht_layout" style="width: 170px; height: '+h+'px; margin-left: -3px; margin-top: 0px; ">\
							<img src="||www_base||shared/img/investigation/pht_sample3.jpg" class="itemTgrad" width="100%" height="100%">\
						</span>\
					</span>\
					<span class="pht_fld_gradient">\
#						<img src="||www_base||shared/img/fld_gradient.png" class="itemTgrad" width="100%" height="100%">\
#	#						<img src="||www_base||shared/img/investigation/pht_sample3.jpg" class="itemTgrad" width="100%" height="100%">\
						<img src="||www_base||shared/img/pht_gradient.png" class="itemTgrad" width="100%" height="100%">\
					</span>\

#					<span class="pht_fld_gradient">\
#						<img src="||www_base||shared/img/investigation/pht_sample3.jpg" class="itemTgrad" width="100%" height="100%">\
#					</span>\
					<img class="pht_fld_tab" src="||www_base||shared/img/fld_tab.png">\

				</span>\
			</span>\
			<span class="pht_txt_blk2">\
				<span class="pht_name_stl2">\
					<span class="pht_name_stl QWHITE">'+jfile.fname+'</span>\
				</span>\
			</span>\
			<span class="pht_number_stl QWHITE">'+jfile.fcontains+'</span>\
		</span>\
	</span>\
	';
	if(opt.as_link){
#		  <a style="top: 0px; width: 227px; height: 128px; left: 0px; " title="Album1" class="up_folderdrop" href="#cid=452A6125FCA37AE2&amp;id=452A6125FCA37AE2%21159&amp;sc=photos">\
		txt='<a style="top: 0px; width: 227px; height: 128px; left: 0px; " title="android" href="#">'+txt+'</a>';
	}
	return txt;
}

var file_count=100;
function hash_tags(jfile)
{
	if(!jfile || !jfile.htags) return '';
	var tags=jfile.htags.split(' ');
	var arr=[];
	for(var i=0; i<tags.length; i++){
		var tag=tags[i];
		if(!tag) continue;
		arr.push('<a href="#" class="link_htag" onclick="hash_link(this)">'+tag+'</a>');
	}
	return arr.join(' ');
	
	file_count++;
	if(jfile.fname.indexOf('A')!=-1) return '#gizmo';
	if(file_count%2==0) return '';
	return '#'+file_count;
}
function pht_image_doc(jfile,opt)
{
	if(!opt) opt={};
	var txt= '\
	<span class="pht_node" style="width: 165px; height: 128px; ">\
		<span class="pht_sel"><input type="checkbox" id="sel_'+jfile.fname+'" search="'+jfile.htags+'" ></span>\
		<span class="pht_info">'+hash_tags(jfile)+'</span>\
		<span class="pht_doc_blk pht_doc_color">\
			<span class="pht_doc_blk2">\
				<span class="pht_doc_outer">\
#					<span class="pht_doc_img" style="height: 48px; width: 48px; left: 12px; margin-top: -24px; ">\
					<span class="pht_doc_img" style="height: 32px; width: 32px; left: 4px; margin-top: -24px; ">\
						<span class="is_c" dir="ltr">\
							<img class="is_img" src="||www_base||shared/img/1pixel_transparent.png" style="width: 32px; height: 32px; background-image: url(||www_base||shared/img/cloud.png); background-position: '+cloud_icons(jfile)+';" alt="android" title="">\
						</span>\
					</span>\
				</span>\
#				<span class="pht_doc_blk3" style="left: 72px; right: 12px; ">\
				<span class="pht_doc_blk3" style="left: 45px; right: 12px; ">\
					<span class="pht_doc_stl1">\
						<span class="pht_name_stl et_main">\
							<span class="et_textContainer">\
								<span class="et_text et_inactive">\
									<span title="'+encode_quotes(jfile.fname)+'">'+jfile.fdisplay+'</span>\
								</span>\
							</span>\
						</span>\
					</span>\
					<span class="pht_doc_stl2">\
						<span>'+jfile.fdate+'</span>\
					</span>\
				</span>\
			</span>\
		</span>\
	</span>\
';
	if(opt.as_link){
		txt='<a style="top: 0px; width: 227px; height: 128px; left: 0px; " title="android" href="#">'+txt+'</a>';
	}
	return txt;
}


var fade_timer;
var fade_item=0;
function cloud_run_fade()
{
	var o1, o2;
	fade_item++;
	if(fade_item%2){
		o1=dge("cf1"); o2=dge("cf2");
	}else{
		o1=dge("cf1b"); o2=dge("cf2b");
	}
	if(!o1 || !o2) return;

	if(!o1.fade) o1.fade=0;
	o1.fade++;
	if(o1.fade%2==0){
		o1.style.opacity=1;
		o2.style.opacity=0;
	}else{
		o1.style.opacity=0;
		o2.style.opacity=1;
	}
	if(isIE){
		// do javascript based fade maybe??	
	}
	fade_timer = setTimeout(cloud_run_fade, 2000);
}

function cloud_preview_folder(name)
{
	cloud_show_preview(U,name);
}

function cloud_toggle_view()
{
	var type=sw.cloud_layout;
	cloud_arrange(type=='grid'?'list':'grid')
}
function cloud_arrange(type)
{
	sw.cloud_layout=type;
	sw.cld_list.want_layout=type;
	var el=dge('cloud_list');
	el.innerHTML=cloud_files_to_html(sw.cloud_cur_files,type=='grid',sw.cloud_cur_limit);
	cloud_list_callbacks(type);
	cloud_grid_reposition(sw.cld_w, sw.cld_h);	
	if(type=='list') sw.cld_list.grid_rows=1;
	cloud_search_apply();
}

function cloud_reset_empty()
{
	var el=dge('x_files_grid_extra');
	if(!el) return;
	el.innerHTML='';
}


function cloud_grid_reposition(w,h)
{
	var el=dge('x_files_grid');
	if(!el) return;
	var els=el.childNodes;
	var totw=w-20;
	var nw = Math.floor(totw/150);
	var ew = totw/nw;
	sw.cld_list.grid_rows=nw;
	var r=0, c=0;
	for(var i=0;i<els.length;i++){
		el=els[i];
		if(class_contains(el,'hidden')||class_contains(el,'action_pending_hide')) continue;
#		el.style.width=px(ew-10);
		el.style.left=px(c*ew);
		el.style.top=px(r*120);
		c++; if(c>=nw) {r++; c=0;}
		
		// need the native width & native height
		
		
		// need the intended cropped or uncropped layout style
		
		// update all the html elements that need their size set
		//find any pht_img_blk -> set width/height
		//find first child (pht_layout) -> set width/height + positions using margin-left, margin-top (-ve)
		var el2=el.firstChild;
		el2.style.width=px(ew-10);
		for(var j=0;j<el2.childNodes.length;j++){
			var el3=el2.childNodes[j];
			if(class_contains(el3,'pht_txt_blk')) {
#				el3.style.width=px(ew-10);
			}
			if(class_contains(el3,'pht_img_blk')) {
				el3.style.width=px(ew-10);
#				el3.firstChild.style.width=px(ew-10);
			}
#			if(class_contains(el2,'pht_node')) {	// folders
#				el3.style.width=px(ew-10);
#				el3.firstChild.style.width=px(ew-10);
#			}
			
		}
	}
//	do_resize_real();
	setTimeout(function(){class_add(dge('x_files_grid'),'grid_animated')},0);
}

function cloud_files_to_html(obj, grid, limit)
{
	var txt='',o;
	if(grid){
		txt+='<div><ul id="x_files_grid" class="grid_dynamic" style="">';
		for(i=0;i<obj.length;i++){
			o=obj[i];
			txt+='<li class="" style="" '+cloud_param(o)+'>'+cloud_file(o)+'</li>';
		}
		txt+='</ul>';
		if(obj.length==0){
			txt+='<div id="x_files_grid_extra" style="padding-top:60px;padding-left:120px;"><div style="color:#555; ||ifdef||cloudhint||text-align:center;||endif||">This folder is empty - <a class="link_blue2" href="#" onclick="cloud_browse_up();">back to parent folder</a> &nbsp;&nbsp; ||ifndef||cloudhint|| <span style="color:#999;">(or Alt-Up Arrow)</span>||endif||</div></div>';
		}
		if(limit){
			txt+='<div id="x_files_grid_extra2" style="padding-top:60px;padding-left:120px;">\
					500 of '+limit+' files shown <a class="link_blue2" href="#" onclick="cloud_nop();">\
					show all</a> (note this may be slow) </div>';
		}
		txt+='</div>';
	}else{
		txt+='<span><table id="x_files_table" class="msgs_table" style="width:100%;table-layout:fixed;">';
		for(i=0;i<obj.length;i++){
			o=obj[i];
			txt+='<tr class="file_row '+(o.fshared?'shared':'')+'" '+cloud_param(o)+'\
#					cld_fstore="'+o.fstore+'" cld_fpath="'+o.fpath+'" cld_fname="'+o.fname+'" \
#					cld_fmime="'+o.fmime+'"cld_folder="'+(o.fdir?'true':'')+'" \
					>\
				  <td class="mi" style="width:25px;"><input type="checkbox" id="sel_'+o.fname+'" /></td>\
				  <td style="width:28px;">'+(o.fdir?'<div class="cloud_folder_icon"></div>':'')+'</td>\
				  <td>'+(o.fsharable?'<div class="file_link"></div>':'')+o.fdisplay+'</td>\
				  <td style="width:50px;text-align:right;">'+o.fsize+'</td>\
				  <td style="width:100px;text-align:center;">'+o.fdate+'</td>\
				</tr>';
		}
		if(obj.length==0){
			txt+='\
			<tr><td colspan="5"><div style="padding:40px;"> \
				<div style="color:#555; ||ifdef||cloudhint||text-align:center;||endif||">This folder is empty - <a class="link_blue2" href="#" onclick="cloud_browse_up();">back to parent folder</a> &nbsp;&nbsp; ||ifndef||cloudhint|| <span style="color:#999;">(or Alt-Up Arrow)</span>||endif||</div>\
			  </div></td>\
			</tr>'
		}
#||define||cloudhint||true||\
#||ifndef||done_some||\
#	<tr><td colspan="5"><div style="padding:40px;"> \
#		<div style="color:#555; ||ifdef||cloudhint||text-align:center;||endif||">This folder is empty - <a class="link_blue2" href="#" onclick="cloud_browse_up();">back to parent folder</a> &nbsp;&nbsp; ||ifndef||cloudhint|| <span style="color:#999;">(or Alt-Up Arrow)</span>||endif||</div>\
#	  </div></td>\
#	</tr>\
#||endif||
#||ifdef||cloudhint||\
#	<tr class="||ifdef||done_some||files_row_dummy||endif||"><td colspan="5"><div style="padding:40px;"> \
#		<div style="color:#999; margin-top:70px; text-align:center;">Navigate using keyboard: Up / Down Arrow, Enter to open, or "Alt - Up Arrow" for parent folder (<a class="link_blue2" href="#">hide</a>)</div>\
#	  </div></td>\
#	</tr>\
#||endif||\
		if(limit){\
			txt+='\
			<tr><td colspan="5"><div style="padding:40px;"> \
				500 of '+limit+' files shown <a class="link_blue2" href="#" onclick="cloud_nop();">\
					show all</a> (note this may be slow) </div>\
			  </div></td>\
			</tr>'
		}
		txt+='</table></span>';
	}
	
	return txt;
}

function cloud_param(o)
{
	return 'cld_fstore="'+o.fstore+'" cld_fpath="'+o.fpath+'" cld_fname="'+o.fname+'" \
					cld_fmime="'+o.fmime+'" cld_folder="'+(o.fdir?'true':'')+'" cld_htags="'+o.htags+'"';
}

function cloud_icons(o)
{
	return '-'+px(o.ficon_x)+' -'+px(o.ficon_y);
}

# Display and editing of caldav event text files

var cloud_content;
var cloud_saved_store,cloud_saved_path;
function is_cloud_content()
{
	return cloud_content;
}
function cloud_save_location()
{
	cloud_saved_store=sw.cld_store;
	cloud_saved_path=sw.cld_path;
}
function cloud_browse_out()
{
	cloud_save()
	cloud_content=false;
	show_cloud(cloud_saved_path,U,cloud_saved_store,U,U,{keep_search:true});
}
function cloud_content_keydown(e)
{
	var ret=true;
	
	switch (e.keyCode){

	case vkUpArrow:
	case vkLeftArrow:
			if (e.altKey||e.metaKey){
				cloud_browse_out();
				return ret;
			}
			break;
	}
	return ret;
}

function set_cloud_file_js(data)
{
	cloud_content=true;
	var el=dge('cloud_list');
	el.innerHTML=data.html;
	var el2=dge('cloud_file_content');
	el2.value=data.file_body;
	el2.file_version=data.file_version;
	el2.file_path=sw.cld_path;
	el2.file_store=sw.cld_store;
	el2.file_name=data.file_name;

	code_mirror_init('cloud_file_content');
	cloud_editing(true);
}

function cloud_editing(val)
{
	if(val){
		class_add(dge('btn_cloud_upload'),'hidden');
		class_remove(dge('btn_cloud_save'),'hidden');
		class_add(dge('btn_cloud_open'),'hidden');
	}else{
		class_add(dge('btn_cloud_save'),'hidden');
		class_remove(dge('btn_cloud_upload'),'hidden');
		class_remove(dge('btn_cloud_open'),'hidden');
	}
}
function cloud_edit_prepare(notimer)
{
	cloud_slide('left',notimer);	
	cloud_crumbs('filename...',{nolink:true});
}
function cloud_edit_file(cld_store, cld_path, cld_item)
{
	cloud_edit_prepare();
	var extra='';
	var cmd='cmd=show&page=~cld_file.js&cld_store='+xescape(cld_store)+'&cld_path='+cld_path+'&sel='+cld_item+iid()+extra+'&want_layout='+sw.cld_list.want_layout;
	sndReq(cmd, function(chan){
		clearTimeout(restoreTimer);
		clearTimeout(slideTimer);
		success = actionReqJs(chan);
	});
}
function cloud_nop()
{
	alert('unimplemented');
}
function cloud_back()
{
	if(is_cloud_content()){
		cloud_browse_out();
	}else{
		cloud_browse_up();
	}	
}
function cloud_open_cur()
{
	var row = gs_cursor_row(sw.cld_list);
	if (!row) return
	cloud_open(row);
#	show_cld(row,e);	
}
function cloud_open(row)
{
	cloud_save_search();
	var xstore=row.getAttribute('cld_fstore');
	if(row.getAttribute('cld_folder')){
		var xpath=row.getAttribute('cld_fpath')+row.getAttribute('cld_fname');	//QQCLD_SHOW
		show_cloud(xpath,U,xstore,xpath,'left');
	}else{
		var file=cloud_current_file();
		if(file && file.editable){
			cloud_save_location();
			cloud_selstore();
			cloud_edit_file(xstore,row.getAttribute('cld_fpath'),row.getAttribute('cld_fname'));
		}else{
			cloud_download_tryopen();
		}
	}
}
function cloud_current_file()
{
	var sel=gs_find_sel(sw.cld_list);
	if(!sel || sel.length>1) return;
	var sel_fname=sel[0];
	for(i=0;i<sw.cloud_cur_files.length;i++){
		var f=sw.cloud_cur_files[i];
		if(f.fname == sel_fname) return f;
	}
}

function cloud_upload()
{
	alert('You can drag and drop images in to the message list to upload (form based uploader not yet implemented)');
}



function code_mirror_init(id)
{
#    <script src="../lib/util/formatting.js"></script>
#    <script src="../mode/css/css.js"></script>
#    <script src="../mode/xml/xml.js"></script>
#    <script src="../mode/javascript/javascript.js"></script>
#    <script src="../mode/htmlmixed/htmlmixed.js"></script>
#    <link rel="stylesheet" href="../doc/docs.css">

	  sw.cm_editor = CodeMirror.fromTextArea(dge(id), {lineNumbers: true});
	  sw.cm_editor.focus();
	  CodeMirror.commands.saveFile=cloud_save;

}

function cloud_new_folder()
{
	x_prompt('Create new folder:','foldername',function(val){
		if (!val) return;
		cal_spinner_start();
		var cmd='cmd=cld_action,show&page=~cld_result.js&action=new_folder&fld_name='+xescape(val)+\
				'&cld_store='+xescape(sw.cld_store)+'&cld_path='+sw.cld_path+'&output_files=true&sid='+sid+cid();
		sndReq(cmd, function(chan){
			cal_spinner_stop();
			var success=actionReqJsStat(chan);
			if(!success){
				set_status('Error creating folder ('+success_string+')','warning');
				return;
			}
		});
	});
}
function cloud_new_file()
{
	x_prompt('Create new file:','foldername',function(val){
		if (!val) return;
		cloud_edit_prepare(true);
		set_cloud_file_js({html:'<div><textarea id="cloud_file_content"></textarea></div>', file_body:''});
	});
}


function cloud_select(type)
{
	gs_select(sw.cld_list,type);
	cloud_preview_folder('should be this folder');
}

function cloud_draw_hist()
{
	var canvas=dge('hist_canvas');
	var ctx=canvas.getContext('2d');
    var pts=[{x:0,y:50},{x:100,y:100},{x:150,y:50},{x:200,y:150},
    	 	 {x:250,y:50},{x:300,y:70},{x:310,y:130},{x:380,y:30}];

    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.scale(1	,1);
	hist_graph(ctx,pts);
}
function hist_graph(ctx,pts)
{
	ctx.save();

    var xs=0.7, ys=1.0;

	var ymax=0, xmax=0;
	for(var i=0;i<pts.length;i++){
		if(pts[i].y>ymax) ymax=pts[i].y;
		if(pts[i].x>xmax) xmax=pts[i].x;
	}

    ctx.lineWidth=4;
	ctx.strokeStyle=hexToCanvasColor("#FF0000",0.75); 
	ctx.fillStyle=hexToCanvasColor("#8dbdff",0.75); 
	ctx.beginPath();
	ctx.moveTo(pts[0].x*xs, pts[0].y*ys);
	for (i = 1; i < pts.length - 2; i ++)
	{
	  var xc = (pts[i].x + pts[i + 1].x) / 2;
	  var yc = (pts[i].y + pts[i + 1].y) / 2;
	  ctx.quadraticCurveTo(pts[i].x*xs, pts[i].y*ys, xc*xs, yc*ys);
	}
	ctx.quadraticCurveTo(pts[i].x*xs, pts[i].y*ys, pts[i+1].x*xs,pts[i+1].y*ys);
	ctx.lineTo(pts[i+1].x*xs,ymax*ys);
	ctx.lineTo(pts[0].x*xs,ymax*ys);
	ctx.lineTo(pts[0].x*xs,pts[0].y*ys);
//	ctx.stroke();
	ctx.fill();
	ctx.closePath();

	ctx.beginPath();
    ctx.lineWidth=2;
	color="#FF0000";
	ctx.strokeStyle=hexToCanvasColor(color,0.75); 
	ctx.moveTo(xmax*(1/3)*xs,ymax*0.2*ys);
	ctx.lineTo(xmax*(1/3)*xs,ymax*ys);
	
	ctx.moveTo(xmax*(2/3)*xs,ymax*0.2*ys);
	ctx.lineTo(xmax*(2/3)*xs,ymax*ys);
	ctx.stroke();
	ctx.closePath();

	ctx.restore();
}

function cloud_save()
{
	var el=dge('cloud_file_content');
	if(!el) return;
	
	var value=sw.cm_editor.getValue();
	if(el.value==value) return;
	el.value=value;

	var file={};
	file.name=el.file_name;
	file.store=el.file_store;
	file.path=el.file_path;
	file.version=el.file_version;
	file.body=el.value;
	ajax_save_file(file,U, function(success,final){ 
//			refresh_cloud();
//			if (!success || !final) return;
//		});
	});
}

function cloud_share()
{
	x_confirm('Sorry sharing of files not available on this storage location, upload this file to dropbox and share that?', function(n){
		if(!n) return;
	});
}

function cloud_tag()
{
	var sel=gs_find_sel(sw.cld_list);
	x_prompt('Tag files with:','tagname',function(val){
		if (!val) return;
		var param={}
		param_cid(param);
		param_sel(param,sel);
		ajax_param(param,'tag_delta',val);
		ajaxPost('cmd=cld_action,show&page=~cld_result.js&action=change_tags&sid='+sid, param, function(chan){
			success = actionReqJs(chan);
			if(!success){
				cloud_showhide(sel,true);
				set_status('Error tagging items ('+success_string+')','warning');
				return;
			}
			set_status(sel.length+' item(s) tagged','success');
		});
	});
}
function hash_link(link)
{
	var ctl=dge('cld_search');
	ctl.value=link.innerText;
	cloud_search_real(ctl);
}