mpi = Infinity;

createItemsConfig = function(arr){
	if(arguments.length>1){
		arr._config = [];
		for(i=1; i<arguments.length; i++){
			arr._config.push(arguments[i]);
		}
		return arr;
	}
}

appendCollapsedMenuItem = function(id, pid, label){
	if(!this.count) this.count = 0;
	this[id] = {};
	this[id].id = id;
	this[id].pid = pid;
	this[id].label = label;
	this[id]._pos = this.count;
	this.count++;
/*
	if(arguments[3]) this[id].uri = arguments[3]; 
	if(arguments[4]) this[id].onclick = arguments[4]; 
*/
	for(i=3; i<arguments.length; i++){
		eval("this[id]."+this._config[i-3]+"= arguments[i]");
	}
}

createCollapsedMenu = function(nodeId, data){
	if(!data.collapsed)	data = collapseMenuData(data);

	activeItem = arguments[2];

	if(activeItem){ 
		if(data[activeItem]) data[activeItem].active = true;
		data = setMenuDNC(data, activeItem);
	};

	_data = [];
	for(i=0; i<data.length; i++){
		if(data[i]){
			if(data[i].pid == mpi) _data.push(data[i]);
			if(data[i].active && activeItem != i) delete data[i].active;
		}
	}

	if(DOM){
//		showTitles = data.showTitles;
		createMenuItems(nodeId, _data, nodeId);
	}
}

collapseMenuData = function(data){
	data.collapsed = true;
	for(i=0; i<data.length; i++){
		if(data[i]){
			_parent = data[data[i].pid];
			if(_parent){
				if(!_parent._children){ _parent._children = [] };
				_parent._children.push(data[i]);
			}
			mpi = Math.min(mpi, data[i].pid);
		}
	}
	return data;
}

setMenuDNC = function(data, id){
	if(data[id]){
		if(!data[id].active) data[id].dnc = true;
		if(data[data[id].pid]){
			setMenuDNC(data, data[id].pid);
		}
	}
	return data;
}


createMenuItems = function(nodeId, data, name){
	sortDataByPos = function(a,b){
//		if(a._pos || b._pos) return 0;
		return a._pos - b._pos;
	}

	data.sort(sortDataByPos); 

	var container = document.getElementById(nodeId);
	if(container){
		for(var i=0; i<data.length; i++){
			if(data[i]){
				var itemDiv = document.createElement("DIV");
				itemDiv.className =  "mItem";

				if(data[i]._children){
					itemDiv.className += (data[i].dnc)? " mOpenChildren" : " mClosedChildren";
					var childContainer = document.createElement("DIV");
					childContainer.className = "mItemContainer";
					childContainer.id = name + "_container_" + data[i].id;
					childContainer.style.display = (data[i].dnc)? "block" : "none";
				}else{
					if(childContainer) childContainer = null;
				}

				var linkNode = document.createElement("A");
				linkNode.href = (data[i].uri)? data[i].uri : "#";
				linkNode.className = "mItemLabel";
				if(data[i].active) linkNode.className += " active";
				if(data[i].stat) linkNode.className += " "+data[i].stat;
				if(data[i].title) linkNode.title = data[i].title;
				linkNode.container = name;
				linkNode._id = data[i].id;
				if(data[i].node_id)	linkNode.id = name + convertPathToId(data[i].node_id);

				if(data[i].onclick) linkNode._onclick = data[i].onclick;
				linkNode.onclick = function(e){
					stopBubble((e)? e : event);
					if(this._onclick) this._onclick();
				}
				
				linkNode.appendChild(document.createTextNode(data[i].label));

				itemDiv.appendChild(linkNode);
				if(childContainer){
					itemDiv.appendChild(childContainer);
					itemDiv.containerId = childContainer.id;
					itemDiv.onclick = function(e){
						menuExpand(this, this.containerId);
						stopBubble((e)? e : event)	
					}
				}
				container.appendChild(itemDiv);

				if(childContainer) createMenuItems(childContainer.id, data[i]._children, name);
			}
		}
	}
}

stopBubble = function(e){
	if(!e) e = event;
	e.cancelBubble = true;
	if(e.stopPropagation){
		e.stopPropagation();
	}
}
menuExpand = function(node, id){
	var container = document.getElementById(id);
	if(container){
		_display = container.style.display;
		container.style.display = (_display == "block")? "none" : "block";
		node.className = "mItem " + ((_display != "block")? "mOpenChildren" : "mClosedChildren");
	}
}

loadFileList = function(uri){
	if(DOM){
		if(parent){
			if(parent != self){
				container = parent.document.getElementById("_fileListContainer_");
				if(container){
					container.src = uri;
				}
			}
		}
	}
}

changeCheckState = function(id,state){
	if(DOM){
		container = document.getElementById(id);
		if(container){
			elements = container.getElementsByTagName("INPUT");
			for(i=0; i<elements.length; i++){
				element = elements.item(i);
				if(element.type == "checkbox"){
					element.checked = state;
				}
			}
		}
	}
}

setActiveTreeItem = function(node){
	if(DOM){
		if(arguments[1]){
			container = document.getElementById(arguments[1]);
		}else{
			container = document.getElementById(node.container);
		}

		if(container){
			nodeList = container.getElementsByTagName("A");
			for(i=0; i<nodeList.length; i++){
				nodeList.item(i).className = "mItemLabel";
				if(node){
					if(nodeList.item(i)._id == node._id) nodeList.item(i).className += " active";
				}
			}
		}
	}
}
fm_clearActivity = function(fid, tid){
	if(DOM){
		fmfc = document.getElementById(fid);
		if(fmfc){
			fmfc.contentWindow.setActiveTreeItem(null,tid);
		}
	}
}


fm_showHideUploadForm = function(){
	if(DOM){
		fmfc = document.getElementById("_fileListContainer_");
		if(fmfc){
			fmfc.contentWindow._shuf_();
		}
	}
}

fm_delSelectedFiles = function(){
	if(DOM){
		fmfc = document.getElementById("_fileListContainer_");
		if(fmfc){
			fmfc.contentWindow.del_multiple();
		}
	}
}

function popup(src){

	w = 500;
	h = 400;

	type = arguments[1];
	if(type == "image"){
		iw = (arguments[2])? arguments[2]+36 : w;
		if(iw > screen.width) iw = screen.width - 36;
		ih = (arguments[3])? arguments[3]+36 : h;		
		if(ih > screen.height) ih = screen.height - 36;
		param = "width=" + iw + ",height=" + ih + ",resizable=yes,scrollbars=yes";
		nw = window.open(src,'',param);
		return;
	}

	if(type){
		_popup_ = window.open(src,'',type);
	}else{
		_popup_ = window.open(src,'','width=' + w + ',height=' + h + ',resizable=yes,menubar=yes,status=yes,scrollbars=yes');
	}

}

function setEvents(id){
   var obj = document.getElementById(id);
	if(obj){
	   if(IE){
	     obj.fireEvent("onclick");
	   }else{
	     initEvents("click",obj);
	   }
	}
}

function initEvents(type,obj){
   var e = document.createEvent("MouseEvents");
         e.initMouseEvent(type, true,false,null,
                           0, 0, 0, 0, 0,
                           false, false, false, false,
                           0, obj);
   obj.dispatchEvent(e);
}

fm_loadDirById = function(id,path){
	if(DOM){
		fmtc = document.getElementById("_dirTreeContainer_");
		if(fmtc){
			fmtc.contentWindow.setEvents(id+convertPathToId(path));
		}
	}
}

convertPathToId = function(st){
	re = /(\/)+/gi;
	return st.replace(re,"_");
}