var v_open_menu=null;
function close_menu(evt)
{
	if (!v_open_menu) {
		return;
	}
	if (evt != 'manual')  {
		evt = (evt) ? evt : ((event)? event : null);
		var related_el=evt.toElement?evt.toElement:evt.relatedTarget;
		var source_el=evt.srcElement?evt.srcElement:evt.target;
		if (is_child(v_open_menu,related_el)) {
			return;
		}
//		if (!source_el.id || !(source_el.id == v_open_menu.id || source_el.id == 'cp_menu_catcher_div' || source_el_id == 'cp_menu_closer_div') ) {
//			return;
//		}
	} 

	v_open_menu.style.visibility='hidden';
	v_open_menu=undefined;
}               
function open_menu(evt)
{                   
	evt = (evt) ? evt : ((event)? event : null);
	var related_el=evt.fromElement?evt.fromElement:evt.relatedTarget;
	var source_el=evt.srcElement?evt.srcElement:evt.target;

	var menubody_id=source_el.getAttribute('target');

    close_menu('manual'); 
    var menubody=document.getElementById(menubody_id);
    menubody.style.visibility='visible';
    v_open_menu=menubody;
}                   
// parent must have an id (why is object comparison impossible in js? maybe its not , but i cant find it)


// id - an elment id which the menu will replace the contents of
// whole_menu - a structure, where:
// whole_menu.height - the menu bar, not the individual menus
// whole_menu.width - the whole menu
// whole_menu.pad_top
// whole_menu.pad_bottom
// whole_menu.pad_left
// whole_menu.pad_right
// whole_menu.menus is an array of menu structures where:
//
// menu.width is the width of the menu
// menu.menuhead is the title of the menu
// menu.menubody is the body of the menu
   
function cp_menu(id,whole_menu)
{
	// defaults
	if (!whole_menu.height) {whole_menu.height=16};
	if (!whole_menu.catcher_height) {whole_menu.catcher_height=10}; // a amount of spacing on either side of the menu, above is used to catch mouseovers to close menus
	if (!whole_menu.width) {
		if (document.body.parentNode.clientWidth) {
			whole_menu.width=document.body.parentNode.clientWidth-32;
			whole_menu.width=944
		} else {
			whole_menu.width=944
		};
	}
	if (!whole_menu.pad_top) {whole_menu.pad_top=2};
	if (!whole_menu.pad_bottom) {whole_menu.pad_bottom=2};
	if (!whole_menu.pad_left) {whole_menu.pad_left=5};
	if (!whole_menu.pad_right) {whole_menu.pad_right=10};
	if (!whole_menu.backgroundColor) {whole_menu.backgroundColor='white'};

    var i;
    var repl=document.getElementById(id);
    var node;
    for (i=0;i<repl.childNodes.length;i++){
        repl.removeChild(repl.childNodes[i]);
    }
    var catcher_div=document.createElement('div');
    catcher_div.style.position='relative';
    catcher_div.style.height=whole_menu.catcher_height+'px';
    catcher_div.style.width=whole_menu.width+'px';
	catcher_div.id='cp_menu_catcher_div';
	catcher_div.onmouseover=close_menu;
	repl.appendChild(catcher_div);

    var outer_div=catcher_div.cloneNode(1);
	outer_div.style.height=whole_menu.height+(whole_menu.catcher_height*2)+'px'; // the 8 is twice the catcher height, for equal spacing
	outer_div.id='cp_menu_outer_div';
	outer_div.className='cp_menu';
	repl.appendChild(outer_div);
	var cursor=0;

	var section_div=document.createElement('div');
    section_div.style.height=whole_menu.height+'px';
	section_div.style.position='absolute';
	section_div.style.padding=sprintf("%dpx %dpx %dpx %dpx",whole_menu.pad_top,whole_menu.pad_right,whole_menu.pad_bottom,whole_menu.pad_left);
	section_div.style.border="1px solid black";
	section_div.style.backgroundColor=whole_menu.backgroundColor
	section_div.style.visibility='visible';
	section_div.style.overflow='hidden';

    for (i=0;i<whole_menu.menus.length;i++) {
		var menu=whole_menu.menus[i];	
		var menuhead_div=section_div.cloneNode(1);
		menuhead_div.style.width=menu.width+'px';
		menuhead_div.style.left=cursor+'px';
		menuhead_div.id='cp_menuhead_'+i;
		menuhead_div.setAttribute('target','cp_menubody_'+i);
		menuhead_div.onmouseover=open_menu;

		var menubody_div=menuhead_div.cloneNode(1);
		menubody_div.style.height=null;
		menubody_div.style.visibility='hidden';
		menubody_div.style.left=cursor+'px';
		menubody_div.setAttribute('id','cp_menubody_'+i);
		menubody_div.onmouseout=close_menu;
		//menubody_div.style.top=+(menuhead_div.style.height.replace("px",""))+1+"px";
		menubody_div.style.top=whole_menu.height+1+whole_menu.pad_bottom+whole_menu.pad_top+'px'

		menuhead_div.innerHTML=menu.menuhead;
		menu.menubody && (menubody_div.innerHTML=menu.menubody);
		outer_div.appendChild(menuhead_div);
		outer_div.appendChild(menubody_div);
		cursor+=menu.width+whole_menu.pad_right+whole_menu.pad_left+1;
	}
	if (menu_about.width) {
		whole_menu.about_width=menu_about.width;
	}
	var menucloser_div=section_div.cloneNode(1);
	menucloser_div.style.left=cursor+'px';
	menucloser_div.style.width=whole_menu.width-cursor-whole_menu.about_width+'px';
	menucloser_div.onmouseover=close_menu;
//	menucloser_div.id='cp_menu_closer_div';
	outer_div.appendChild(menucloser_div);

	var menuabout_div=section_div.cloneNode(1);
	menuabout_div.style.left=whole_menu.width-whole_menu.about_width+'px';
	menuabout_div.style.width=whole_menu.about_width+'px';

	var about_text="<a href=# onclick='close_menu(\"manual\");alert(\""+whole_menu.about_message+"\");return false'>About Childsplay</a>";
	if (!menu_about.menubody) {
		menuabout_div.innerHTML=about_text;
		menuabout_div.onmouseover=close_menu;
		outer_div.appendChild(menuabout_div);
	} else {
		menuabout_div.onmouseover=open_menu;

		var menuaboutbody_div=menuhead_div.cloneNode(1);
		menuaboutbody_div.style.height=null;
		menuaboutbody_div.style.visibility='hidden';
		menuaboutbody_div.style.left=menuabout_div.style.left;
		menuaboutbody_div.style.width=menuabout_div.style.width;
		menuaboutbody_div.setAttribute('id','cp_menuaboutbody');
		menuaboutbody_div.onmouseout=close_menu;
		menuabout_div.setAttribute('target','cp_menuaboutbody');
		//menuaboutbody_div.style.top=+(menuhead_div.style.height.replace("px",""))+1+"px";
		menuaboutbody_div.style.top=whole_menu.height+1+whole_menu.pad_bottom+whole_menu.pad_top+'px'

		menuabout_div.innerHTML=menu_about.menuhead;
		menuaboutbody_div.innerHTML=menu_about.menubody+about_text;
		outer_div.appendChild(menuabout_div);
		outer_div.appendChild(menuaboutbody_div);
	}
	
}

function is_child(parent,child)
{
	if (child.id && parent.id == child.id) {
		return 1;
	}
	if (!child.parentNode) {
		return 0;
	}
	return is_child(parent,child.parentNode);
}

// from http://jan.moesen.nu/code/javascript/sprintf-and-printf-in-javascript/
function sprintf()
{
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
				   if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
				   if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
				   if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
				   if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
				   if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
		return str;
	}
	
function debug(msg)
{
	var debug=document.getElementById('debug');
	if (debug) {
		debug.appendChild(document.createTextNode(msg));
		debug.appendChild(document.createElement('br'));
	}
}

function cp_object_editor_action_dropdown(thos)
{
	var i;
 	var reset=1;
	for (i=0;i<thos.options.length;i++) {
		if (thos.options[i].selected) {
			if (thos.options[i].value == 'delete') {
				var to_delete=0;
				for (j=0;j<thos.form.elements.length;j++) {
					if (thos.form.elements[j].name == 'action_object_id' && thos.form.elements[j].type == 'checkbox') {
						if (thos.form.elements[j].checked) to_delete++;
					}
				}
				if (to_delete) {
					if (confirm('This will delete ' + to_delete + ' objects. Are you sure?')) {
						reset=0;
						thos.form.submit();
					} 
				} else {
					alert('Nothing selected for deletion.');
					thos.selectedIndex=0;
				}
			} else if (thos.options[i].value == 'select_all' || thos.options[i].value == 'unselect_all') {
				var j;
				for (j=0;j<thos.form.elements.length;j++) {
					if (thos.form.elements[j].name == 'action_object_id' && thos.form.elements[j].type == 'checkbox') {
						thos.form.elements[j].checked=thos.options[i].value == 'select_all'?true:false;
					}
				}
			} else if (thos.options[i].value.substring(0,12)=='add_to_group' || thos.options[i].value.substring(0,17)=='remove_from_group') {
				var to_add=0;
				for (j=0;j<thos.form.elements.length;j++) {
					if (thos.form.elements[j].name == 'action_object_id' && thos.form.elements[j].type == 'checkbox') {
						if (thos.form.elements[j].checked) to_add++;
					}
				}
				if (to_add) {
					if (thos.options[i].value.substring(0,3)=='add') {
						thos.form.elements['add_to_group_id'].value=thos.options[i].value.substring(13);
					} else {
						thos.form.elements['add_to_group_id'].value=thos.options[i].value.substring(18);
					}
					reset=0;
					thos.form.submit();
				} else {
					alert('Nothing selected for deletion.');
					thos.selectedIndex=0;
				}
			}
		}
	}
	if (reset) {
		thos.options[0].selected=1;
	}
}

var popupmenu_open_name='';
var popupmenu_open_timer='';

function open_popupmenu(name,id,evt,type)
{
	if (type == undefined) {
		type='cursor';
	} // else it should be dropdown, nothing else defined yet

	mouseover_popupmenu(name);
	if (popupmenu_open_name) {
		close_popupmenu(popupmenu_open_name);
	}
	popupmenu_open_name=name;
	var popupmenu=document.getElementById(name+"_popupmenu");
	if (evt.pageX) { //dom
		if (type != 'cursor') {
			popupmenu.style.left=evt.target.offsetLeft+'px';
			popupmenu.style.top=evt.target.offsetTop+'px';
		} else {
			popupmenu.style.left=(evt.pageX-10)+"px";
			popupmenu.style.top=(evt.pageY-10)+"px";
		}
	} else { //ie5+ (or breakage if something else)
		if (type != 'cursor') {
			popupmenu.style.left=evt.srcElement.offsetLeft+evt.srcElement.offsetParent.offsetLeft;
			popupmenu.style.top=evt.srcElement.offsetTop+evt.srcElement.offsetParent.offsetTop;
		} else {
			popupmenu.style.left=(evt.x-10+document.body.parentNode.scrollLeft)+"px";
			popupmenu.style.top=(evt.y-10+document.body.parentNode.scrollTop)+"px";
		}
	}

	popupmenu.style.visibility='visible';
	//mouseout_popupmenu(name); 
	if (id != null) {
		var popupmenu_id=document.getElementById(name+"_popupmenu_id");
		popupmenu_id.value=id;
	}
}
function close_popupmenu(name)
{
	if (popupmenu_open_name && popupmenu_open_name==name) {
		mouseover_popupmenu(name);
		popupmenu_open_name=popupmenu_open_timer='';

		var popupmenu=document.getElementById(name+"_popupmenu");

		popupmenu.style.visibility='hidden';
	}
}
function fade_menu(name)
{
	//if (popupmenu_open_name && popupmenu_open_name==name) {
	//	return;
	//}
	var popupmenu=document.getElementById(name+"_popupmenu");
	if (popupmenu.style.opacity > 0) {
		popupmenu.style.opacity-=0.2; // Mozilla
		popupmenu.style.filter='alpha(opacity="'+popupmenu.style.opacity*100+'")'; //IE
		popupmenu.fade_timer=setTimeout('fade_menu(\"'+name+'\")',50);
	} else {
		popupmenu.fade_timer='';
		close_popupmenu(name);
	}
}

function mouseout_popupmenu(name)
{
	var popupmenu=document.getElementById(name+"_popupmenu");

	popupmenu.style.opacity=1; // Mozilla
	popupmenu.style.filter='alpha(opacity="'+popupmenu.style.opacity*100+'")'; // IE

	popupmenu_open_timer=setTimeout('fade_menu(\"'+name+'\")',50);
}

function mouseover_popupmenu(name)
{
	var popupmenu=document.getElementById(name+"_popupmenu");

	if (popupmenu.style.opacity) {
		popupmenu.style.opacity=1; // Mozilla
		popupmenu.style.filter='alpha(opacity="'+popupmenu.style.opacity*100+'")'; // IE
	}
	if (popupmenu.fade_timer) {
		clearTimeout(popupmenu.fade_timer);
		popupmenu.fade_timer='';
	}

	if (popupmenu_open_timer) {
		clearTimeout(popupmenu_open_timer);
		popupmenu_open_timer='';
	}
}

