//<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">

function cleardebug() {
	var e=xget("log");
	if (e) e.innerHTML = "";
}
function debug(s, nobr) {
	var e=xget("log");
	if (e) e.innerHTML += s + (nobr ? "" : "<br/>");
}
function debug_xloader(module, s, severity) {
	debug('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + module + ':' + severity + ': ' + s);
}


var treeNodeCache = new Array();

function init() {
	debug("<b>init()</b> started, xloader=" + xloader);
	// Удаляем из URL GET-параметры и хвост своего адреса после последнего слеша, и считаем это базой для передаваемых в xloader относительных URL.
        var s = document.location.href, p = s.indexOf("?");
	if (p >= 0) s = s.substring(0, p);
	    p = s.lastIndexOf("/");
	if (p >= 0) s = s.substring(0, p+1);
			
	debug("<b>init()</b> url, url=" + s);
	
	xloader.base_url = s;
	xloader.debug_callback = debug_xloader;
	xloader.debug_severity = 0;
	xloader.register_frame(xget("iframe1"));
	xloader.register_frame(xget("iframe2"));
	xloader.register_frame(xget("iframe3"));
	xloader.register_frame(xget("iframe4"));
	
	treeNodeCache[0] = new Image();  treeNodeCache[0].src = "/images/tv_empty.gif";
	treeNodeCache[1] = new Image();  treeNodeCache[1].src = "/images/tv_open.gif";
	treeNodeCache[2] = new Image();  treeNodeCache[2].src = "/images/tv_closed.gif";
	treeNodeCache[3] = new Image();  treeNodeCache[0].src = "/images/tv_empty_s.gif";
	treeNodeCache[4] = new Image();  treeNodeCache[1].src = "/images/tv_open_s.gif";
	treeNodeCache[5] = new Image();  treeNodeCache[2].src = "/images/tv_closed_s.gif";	
	debug("<b>init()</b> finished<br/>");
}


function treeNodeClickMove(span) {
	var li = span.parentNode, 
		id = xattr(li, "xid");
	// li.xstate: 0=not loaded, 1=loading, 2=loaded.
	
	if (typeof(li.xloaderstate) != 'number')
		li.xloaderstate = xattr(li, "xloaded") == "false" ? 0 : 2;

	if (li.className == 'tv_open' || li.className == 'tv_open_s') {
		treeNodeSetOpen(li, false);
		if (li.xloaderstate == 1)
			xloader.cancel_request(id);
	}
	else if (li.className == 'tv_closed' || li.className == 'tv_closed_s') {
		treeNodeSetOpen(li, true);
		if (li.xloaderstate == 0) {
			var div = xget("d" + id);
			if (div) {
				div.innerHTML = "<div class='wait'>Идет загрузка... <a href='javascript:treeNodeCancel(" + id + ")'>Отменить</a> <a href='javascript:treeNodeReload(" + id + ")'>Повторить запрос</a></div>";
				xloader.new_request({id:id, tag:li, callback:treeNodeLoaded, subrequests:[{url:"iframe2?id=" + id, target:div}]});
			}
		}
	}
}

function GetFiles(idfolder,page,ord,direct)
{
		var chkAllFiles = document.getElementById('listType');
		if(chkAllFiles != null && chkAllFiles.checked)
		{
			idfolder = '-1';
		}
		
		if (page == undefined) page = 1;
		if (ord == undefined) ord = '';
		if (direct == undefined) direct = '';
		var div = xget("files_div");
		var tmp = xget("temp");
		if (typeof(tmp.xloaderstate) != 'number')
		tmp.xloaderstate = xattr(tmp, "xloaded") == "false" ? 0 : 2;
		//if (tmp.xloaderstate == 1)	xloader.cancel_request(1111);
		var result = Math.floor(Math.random()*(1000-0+1))+0;		
		if (tmp.xloaderstate == 0) {
			if (div) {
				
				div.innerHTML = "<div class='wait'>Идет загрузка... </div>";
				xloader.new_file_request({id:1, tag:tmp,  callback:filesLoaded, subrequests:[{url:"iframe3?tt="+result+"&folder_id=" + idfolder + "&page=" + page + "&o=" + ord + "&direct=" + direct, target:div}]});
			}
		}		
}

function treeNodeClick(span) {
	var li = span.parentNode, 
		id = xattr(li, "xid");
		
	// li.xstate: 0=not loaded, 1=loading, 2=loaded.
	
	if (typeof(li.xloaderstate) != 'number')
		li.xloaderstate = xattr(li, "xloaded") == "false" ? 0 : 2;

	if (li.className == 'tv_open' || li.className == 'tv_open_s') {
		treeNodeSetOpen(li, false);
		if (li.xloaderstate == 1)
			xloader.cancel_request(id);
	}
	else if (li.className == 'tv_closed' || li.className == 'tv_closed_s') {
		treeNodeSetOpen(li, true);
		if (li.xloaderstate == 0) {
			var div = xget("d" + id);
			if (div) {
				div.innerHTML = "<div class='wait'>Идет загрузка файлов... <a href='javascript:treeNodeCancel(" + id + ")'>Отменить</a> <a href='javascript:treeNodeReload(" + id + ")'>Повторить запрос</a></div>";
				xloader.new_request({id:id, tag:li, callback:treeNodeLoaded, subrequests:[{url:"iframe?id=" + id, target:div}]});
			}
		}
	}
}

function treeNodeReload(id) {
	xloader.reload_request(id);
}

function treeNodeCancel(id) {
	xloader.cancel_request(id);
}

function filesLoaded(request, code)
{
	if (code == 1)
		return true;
}

function treeNodeLoaded(request, code) {
	if (code == 1)
		return true;
	var li = request.tag;
	li.xloaderstate = code;  // 0 or 2
	if (!code)
		treeNodeSetOpen(li, false);
}

function treeNodeSetOpen(li, isopen) {
	var id = xattr(li,"xid"), s = isopen ? "tv_open" : "tv_closed", x;
	var re = /_s/;
	if(li.className.match(re))
	{
		s = s+'_s';
	}
		
	li.className = s;
	x = xget("i" + id);
	if (x)  
		x.src = "/images/" + s + ".gif";
	x = xget("d" + id);
	if (x)  
		x.style.display = isopen ? "block" : "none";
}


function dodump() {
	xloader.debug_dump();
}

//--------------------------------------------------------------------------------------------------------------------------

function xget(id, doc) {
	if(!doc) doc=document;
	if(doc.getElementById) return doc.getElementById(id);
	else if(doc.all) return doc.all[id];
	return null;
}

//--------------------------------------------------------------------------------------------------------------------------

xbrowser = new function() {
	this.isIE = (navigator.appName.indexOf('Microsoft') >= 0);
	this.isN = (navigator.appName.indexOf('Netscape') >= 0);
	this.isO = (navigator.appName.indexOf('Opera') >= 0);
	var i = -1;
	if (this.isIE) {
        i = navigator.appVersion.indexOf('MSIE ');
        i = (i >= 0) ? (i + 5) : 0;
	}
	else if (this.isN || this.isO)
		i = 0;
	this.version = (i >= 0) ? parseFloat(navigator.appVersion.substring(i)) : 0;
	//this.supported = ((this.isIE && this.version >= 4) || (this.isN && this.version >= 4) || (this.isO && this.version >= 6));
	this.caps = {
		cssPositionFixed: (this.isN || this.isO),  // IE5 doesn't support style="position:fixed"
		attrAccessMode: ((this.isIE && this.version < 6) ? 'ie' : 'dom')  // used by xattr
	};
};

xattr = function(elem, name) {
	if (elem) {
		switch(xbrowser.caps.attrAccessMode) {
			case 'ie': {
				var a = elem[name];
				return (typeof(a) == 'string') ? a : null;
			}
			case 'dom': {
				if (elem.attributes) {
					var a = elem.attributes.getNamedItem(name);
					if (a) return a.nodeValue;
				}
			}
		}
	}
	return null;
};

