# *** Generic menu related stuff ***
# This menu implementation is a bit verbose and incomplete, needs more work
var global_active_menu;
function do_menu_show(id,e,own_calc,fn,posn)
{
	dbg("menu show "+id);
	mnu=document.getElementById(id);
	if(fn) mnu.cb=fn;
	var t;
	
	if(e) {
		t=target(e);
		if (t.tagName.toLowerCase()=='img') 
			t=t.parentNode;
	}
	if(posn && posn['t'])
		t=posn.t;

	if (menu_timer)	clearTimeout(menu_timer);
	if (global_active_menu && mnu!=global_active_menu)
		menu_hide2(global_active_menu);
	if(posn && posn['cursor'])
		menu_position_cursor(e,mnu,posn);
	else
		menu_position(t,mnu,posn);

#	Ajax only interface functionality
	if(sw.ajax){
		shadofy(mnu);
		mnu.fresh=true;
	}
	class_remove(mnu,'hidden');
	global_active_menu=mnu;
	if(e) return stopEvent(e);
}
function do_menu_show2(mnu,el,posn)
{	
	dbg("menu show2 "+el.id);	
	if (menu_timer)	clearTimeout(menu_timer);
	if (global_active_menu && mnu!=global_active_menu)
		menu_hide2(global_active_menu);
	menu_position(el,mnu,posn,sw.active.field_eframe);

	class_remove(mnu,'hidden');
	global_active_menu=mnu;
}

function menu_position_old(ctl,mnu,posn)
{
	if(true){
		var x=0;
		for(var p=ctl;p.offsetParent;p=p.offsetParent){
			x+=p.offsetLeft;
		}
		if(posn&&posn.xoffset) x+=posn.xoffset;
		if(posn&&posn.right)
			mnu.style.left=px(x+ctl.offsetWidth-mnu.offsetWidth);
		else
			mnu.style.left=px(x);
	}else{
		mnu.style.left=px(ctl.offsetLeft);
	}
	if(true){
		var x=0;
		for(var p=ctl;p.offsetParent;p=p.offsetParent){
			x+=p.offsetTop;
		}
		if(posn&&posn.yoffset) x-=posn.yoffset;
		if(posn&&posn.upwards)
			mnu.style.top=px(x-mnu.offsetHeight);
		else
			mnu.style.top=px(x+ctl.offsetHeight-2);
	}else{
		mnu.style.top=ctl.offsetTop+ctl.offsetHeight+'px';	
	}
}

function menu_position(ctl,mnu,opt,ctl2)
{
	var pos = posn_offset(ctl);
	if(ctl2) pos = posn_offset(ctl2,pos);

	if(!opt) opt={};
	if(opt.xoffset) pos[0]+=opt.xoffset;
	if(opt.right) pos[0]+=ctl.offsetWidth-mnu.offsetWidth;

	if(opt.yoffset) pos[1]-=opt.yoffset;
	if(opt.upwards)
		pos[1]-=mnu.offsetHeight;
	else
		pos[1]+=ctl.offsetHeight-2;	
	position_node(mnu,pos);
}
function menu_position_cursor(e,mnu,opt)
{
	var pos = [mouseX(e),mouseY(e)]
	position_node(mnu,pos);
}
function posn_offset(ctl,pos)
{
	if (!pos) pos=[0,0];
	for(var p=ctl;p.offsetParent;p=p.offsetParent) pos[0]+=p.offsetLeft;
	for(var p=ctl;p.offsetParent;p=p.offsetParent) pos[1]+=p.offsetTop;
	return pos;
}
function position_node(mnu,pos)
{
	mnu.style.left=px(pos[0]);
	mnu.style.top=px(pos[1]);
}



var menu_timer;
function x_hide_menu_soon(txt)
{
	if (menu_timer)	clearTimeout(menu_timer);
	menu_timer=setTimeout(function(){
		menu_hide(txt);
	}
	,400);
}
function menu_mouseup(id)
{
	var o=dge(id);
	if(!o) return;
	if(o.fresh) {
		stopEvent(e);
		return;
	}
	menu_hide(id);
}
function menu_hide_ex(e,id,param)
{
	t=target(e);
	if(class_contains(t,'shadow')) {
#		Hack for IE not catching events properly	
		if (isIE||isChrome) ignore_one_hide=true;	
		return;
	}
	return menu_hide(id,param);
}
function menu_hide(id,param,now)
{
	dbg('menu_hide id='+id+' now='+now)
	if(!now){
#		This is to try and correct for different browsers sending oncontextmenu and onmouseup events in different orders :-((
		setTimeout(function(){menu_hide(id,param,true)},10);
		return;
	}
	dbg('*** menu_hide')
	var o=dge(id);
	if(!o) return;
	class_add(o,'hidden');
	global_active_menu=null;
	if (o.cb)
		o.cb('hide',param);
}
function menu_hide2(mnu,now)
{
	if(!now){
		setTimeout(function(){menu_hide2(mnu,true)},10);
		return;
	}
	if (mnu.cb)
		mnu.cb('hide');
	class_add(mnu,'hidden');
	global_active_menu=null;
}
function menu_delete(id)
{
	if (menu_timer)	clearTimeout(menu_timer);
	var o=dge(id);
	remove_node(o);
	global_active_menu=null;
	if (o.cb)
		o.cb('hide');
}


function menu_action_mouseout(e,txt){
	if (!e) e = window.event;
	var t = target(e);
	if (class_contains(t,'shadow')){
		var el=ancestor_by_class(t,'xmenu');
		if(el.fresh) {
			return;
		}
	}
	if(pref['sticky_menus']) return;
	x_hide_menu_soon(txt);
}
function menu_action_mouseover(e,txt){
	if (!e) e = window.event;
	var t = target(e);
	if(!class_contains(t,'shadow')){
		var el=ancestor_by_class(t,'xmenu');
		if(el) el.fresh=false;
	}
	if (menu_timer)	clearTimeout(menu_timer);
}

var defer_items = ['$$st_defer_time_today$$', '$$st_defer_time_evening$$', '$$st_defer_time_tomorrow$$', '$$st_defer_time_weekend$$', '$$st_defer_time_week$$', '$$st_defer_time_month$$', '$$st_defer_time_sometime$$', '<hr>', '$$st_defer_time_pick$$','<hr>', '$$st_defer_time_inbox$$'];
var defer_types = ['later', 'evening', 'tomorrow', 'weekend', 'week', 'month', 'sometime', 'none', 'custom', 'none', 'inbox'];
function menu_get_html(type,del_menu)
{
	var menu_html,id;
	var d=(sw.is_popup?sw_win.document:document);
	if(type=='qpick'){
		menu_html=['<ul class="xtree">\
			<li labelid="remind"><div>Later to self</div></li>\
			<li labelid="note"><div>Note to self</div></li>\
			<li labelid="none" class="menu_spacer"><hr style="margin:0;"></li>'];
		var qpick = sw.picklist_raw.split('\n');
		for(i=0;i<qpick.length;i++){
			var txt = qpick[i];
			menu_html.push('<li labelid="'+txt+'"><div>'+txt+'</div></li>');
		}
		if(qpick.length>0){
			menu_html.push('<li labelid="none" class="menu_spacer"><hr style="margin:0;"></li>');
		}
		menu_html.push('<li labelid="manage"><div>Edit quick pick list</div></li></ul>');
	}else if(type=='defer'){
		menu_html=['<ul class="xtree">'];
		for(i=0;i<defer_items.length;i++){
			if(defer_items[i]=='<hr>'){
				menu_html.push('<li class="menu_spacer"><hr style="margin:0;"></li>');			
			}else{
				menu_html.push('<li labelid="'+defer_types[i]+'"><div>'+defer_items[i]+'</div></li>');
			}
		}
		menu_html.push('</ul>');
	}else if(type=='folder'){
		var src=d.getElementById("folders");
		menu_html=[src.innerHTML.replace(/<b.*?>.*?<\/b>/ig,'')];
	}else{
		var src=d.getElementById("folders");
		var links = src.getElementsByTagName('li');
		menu_html=['<ul class="ltree qlabel" style="color:green;" onmouseup="ltree_mouseup(event)">'];
		for (i=0;i<links.length;i++){
			var icon=(links[i].getAttribute('label_type')=='imap'?'label_icon':'label_icon3');
			if(del_menu) icon='label_icon2';
			id=links[i].getAttribute('labelid');
			if(id){
				menu_html.push('<li class="qlabel" labelid="'+id+'"><div class="full_cursor"><div class="tree_icons" style="float:left;width:20px;margin-left:5px;"><div class="'+icon+'" style="width:20px;"></div></div><div class="part_cursor label" style="margin-left:21px;">'+id+'</div></div></li>');
			}
		}		
		menu_html.push('</ul>');
	}
	return menu_html;
}
function menu_dom_trim(type,list,old){
	var node, link_text, fname;
	if(type=='folder'){
		var oRegex1, oRegex2, oRegex2b;
#		find the first link text
		oRegex1 = new RegExp('<a.*?>','i') ;
#		Find the quoted text 2=IE&FF 2b=Safari
		oRegex2 = new RegExp('F\\("(.*?)"','i') ;
		oRegex2b = new RegExp('F\\(&quot;(.*?)&quot;','i') ;

		var links = list.getElementsByTagName('a');
		for (i=0;i<links.length;i++){
			node=links[i];
			if (class_contains(node,'flist_link')){
				node.parentNode.removeChild(node);
				i--;
			}else if (old){
#				First link "<a ...>" text then extract the onclick foldername and blank out the function
				link_text = oRegex1.exec(node.parentNode.innerHTML);
#				dbg(('link_text='+link_text.length+' [['+link_text.join('||')+']]').replace(/</g, "&lt;").replace(/>/g, "&gt;"));
#				dbg('link_text[0]='+link_text[0].replace(/</g, "&lt;").replace(/>/g, "&gt;"));
				
				var fname = oRegex2.exec(link_text[0]) ;
				if(!fname) fname=oRegex2b.exec(link_text[0]) ;
				if (fname) {
					node.fname = fname[1];
					node.className='';
					dbg("fname="+node.fname);
				}
				node.onclick=function(){};
			}
		}
		
#		Remove undesired parts of tree & the flag causing preferences save	
		var nodes=list.firstChild.childNodes;
		for(var i=nodes.length-1;i>=0;i--){
			if (class_contains(nodes[i],'myfld')) 
				class_remove(nodes[i],'myfld');
			if (class_contains(nodes[i],'ltree') || class_contains(nodes[i],'stree') || class_contains(nodes[i],'fld_text_spacer')) 
				remove_node(nodes[i]);
		}		
#		Now fix selection & toggles
		var nodes=list.getElementsByTagName('div');
		for(var i=0;i<nodes.length;i++){
			if (class_contains(nodes[i],'selected')) 
				class_remove(nodes[i],'selected');
			if (class_contains(nodes[i],'tog')) 
				nodes[i].onmouseup=function(e){if(!e)e=window.event;stopEvent(e)};
		}

	}else{
	
	}
}


# *****

function menu_setsize(mnu)
{
	var h=ClientHeight()/2;
	if (h<200) h=200;
	if (h>400) h=400;
	mnu.menu_list.style.height=px(h+18);
}	


var filter_menus=true;
function do_generic_menu_setup(type,id,del_menu,opt)
{
	if(!opt) opt={};
	var mnu=document.getElementById(id);
	var anchor=mnu.firstChild;
	anchor.innerHTML='';
	mnu.del_menu=del_menu;
	if (anchor.childNodes.length==0){
		var menu_html=menu_get_html(type,del_menu);
		filter_menus=sw.ajax&&(pref.no_filtermenus!=true);
		if(opt.cursor || opt.nofilter){
			mnu.search_attrib='labelid';
			anchor.innerHTML='<div class="menu_scrolling">'+menu_html.join('')+'</div>';
			var list_id=0;
		}else if(filter_menus){
			var new_item = type=='folder' ? '$$st_fldmng_newfld2$$' : '$$st_lblmng_newlbl2$$';
			var extra='';
			if(pref.pickmenu_create) extra='<a href="#" id="filtermenu_newitem_'+type+'" onmouseup="filtermenu_newitem(event,\''+type+'\')">'+new_item+'</a>';
			new_item = type=='folder' ? '$$st_fldmng_manage2$$' : '$$st_lblmng_manage2$$';
			if(!sw.is_popup) extra+='<a href="#" onmouseup="manage_'+type+'s(event)">'+new_item+'</a>';
			if(opt.nomanage) extra='';
			anchor.innerHTML=filtermenu_new(menu_html.join(''),'<div>'+extra+'</div>', opt)
			mnu.create_item=dge('filtermenu_newitem_'+type);
			mnu.menu_type=type;
			mnu.search_ctl=menu_to_ctl(anchor);
			mnu.search_attrib=(type=='folder'?'fld_id':'labelid');
			mnu.search_ctl.filtermenu = mnu;
			var list_id=1;
		}else{
			anchor.innerHTML='<div class="menu_scrolling">'+menu_html.join('')+'</div>';
			var list_id=0;
		}	
		mnu.menu_list=anchor.childNodes[list_id];
		menu_dom_trim(type,mnu.menu_list)
		xmenu_register_jshover(anchor);
		menu_setsize(mnu);
	}else{
		if(filter_menus){
			mnu.search_ctl.value='';
			filtermenu_search_real(mnu.search_ctl);
			filtermenu_search_clear(mnu.menu_list)
		}
		if(type=='label') mnu.menu_list.innerHTML=menu_get_html(type,del_menu).join('');
	}
	setTimeout(function(){
#		// if timer is not used it does not get applied in FF
		scroll_top(mnu.menu_list);
		if (mnu.search_ctl)	mnu.search_ctl.focus();
	},1);
	
}

function do_folder_menu_setup(e,id,old)
{
	return do_generic_menu_setup('folder',id,(e&&e.shiftKey));
}
function do_label_menu_setup(e,id,old)
{
	return do_generic_menu_setup('label',id,(e&&e.shiftKey));
}


function xmenu_register_jshover(ctl){
	add_event(ctl,'mouseover',function(e){
			var el=target(e);
			el=class_contains(el.parentNode,'xmenu')?el:ancestor_by_class(target(e),'full_cursor');
			if(el)class_add(el,'hover_js')
		});
	add_event(ctl,'mouseout',function(e){
			var el=target(e);
			el=class_contains(el.parentNode,'xmenu')?el:ancestor_by_class(target(e),'full_cursor');
			if(el)class_remove(el,'hover_js')
		});
}

function do_folder_menu(e,el)
{
	if (!e) e = window.event;
	var t = target(e);

	var row=ancestor_by_tag(t,'li');
	var fld_id=row.getAttribute('fld_id')

	if (class_contains(row,'fake')){
		fld_expand(undefined,undefined,row);
		stopEvent(e)
		return;
	}	
	menu_hide('folder_menu',fld_id);
	var o=dge('folder_menu');
	class_add(o,'hidden');

	if (!fld_id) return;
#	>> do_copy_show / do_move_show cb
	if (o.cb)
		o.cb('ok',fld_id);
}
