<inp2:m_NoDebug/>
<inp2:m_Set skip_last_template="1"/>
<inp2:m_include t="browser/browser_header" nobody='yes'/>


<script language="javascript">

var $current_sort_order = '_asc';
var $current_sort_by = 'name';
var $print_html = '';
var folder_up_exists = false;
var table_footer = false;
var table_header = false;
var table_footer2 = false;
var table_header2 = false;
var output = '';
var output2 = '';

function ShowContextMenu(event) {
	if (event.target) {
		var targ = event.target;
	}
	else if (event.srcElement) {
		var targ = event.srcElement;
	}
	if (targ.nodeType == 3) {
		targ = targ.parentNode;	// defeat Safari bug
	}
	document.getElementById("context_menu").style.display = "";
	document.getElementById("context_menu").style.top = event.clientY;
	document.getElementById("context_menu").style.left = event.clientX;
	id = targ.id;
	document.getElementById("context_menu_file").value = id;
}



	function HideContextMenu() {
		document.getElementById("context_menu").style.display = "none";
	}


	function sortBy(sort_by)
	{
		if (sort_by == $current_sort_by)
			$current_sort_order = ($current_sort_order == '_asc')?'_desc':'_asc';
		else {
			document.getElementById('sort_' + $current_sort_by).src = document.getElementById('_no').src;
			$current_sort_by = sort_by;
			$current_sort_order = '_asc';
		}
		document.getElementById('sort_' + $current_sort_by).src = document.getElementById($current_sort_order).src;
		files_list.getFilesList();
		//alert('GetFoldersAndFiles_'+current_sort_by+current_sort_order);
	}

	function OpenFolder( folderPath , folder )
	{
		document.getElementById('tableFiles').innerHTML = '';
		window.parent.frames['frmFolders'].document.getElementById('tableFiles').innerHTML = '';
		window.top.current_folder = folderPath ;
		window.top.last_folder = folder;
		files_list.getFilesList();
	}

	function OpenFile( fileUrl, fileSize)
	{
		window.top.opener.SetUrl( fileUrl, '', '', '', fileSize);
		window.close() ;
		window.top.close() ;
		window.top.opener.focus() ;
	}

	function HandleCheckboxes() {
		var checked_num = 0, i, count = 0;
		checkboxes = document.getElementsByName('file_checkbox');
		for (var i = 0; i< checkboxes.length; i++)
		{
			if (checkboxes[i].checked && checkboxes[i].value)
				checked_num++;
		}

		if (checked_num == 0) {
			document.getElementById("selector").checked = false;
		}
		if (checked_num > 0) {
			window.parent.frames['frmUpload'].SetElementVisibility("none", "");
			if (checked_num > 1) {
				window.parent.frames['frmUpload'].SetButtonAvalability("rename", false);
			}
			else {
				window.parent.frames['frmUpload'].SetButtonAvalability("rename", true);
			}
		}
		else {
			window.parent.frames['frmUpload'].SetElementVisibility("", "none");
		}
	}

	function RenameCallBack($request, $params, $object)
	{
		iErrorNumber = parseInt($request.responseText);
		switch ( iErrorNumber )
		{
			case 0 :
				window.parent.frames['frmUpload'].SetElementVisibility("", "none");
				files_list.getFilesList();
				break ;
			case 203 :
				alert( '<inp2:m_Phrase label="la_fck_InvalidFileType" js_escape="1"/>' ) ;
				break ;
			case 204 :
				alert( '<inp2:m_Phrase label="la_fck_FileAvailable" js_escape="1"/>' ) ;
				break ;
			default :
				alert( '<inp2:m_Phrase label="la_fck_ErrorFileUpload" js_escape="1"/> ' + errorNumber ) ;
				break ;
		}
	}

	function CreateCallBack($request, $params, $object)
	{
		iErrorNumber = parseInt($request.responseText);
		switch ( iErrorNumber )
		{
			case 0 :
				files_list.getFilesList();
				break ;
			case 101 :
				alert( '<inp2:m_Phrase label="la_fck_FolderAlreadyExists" js_escape="1"/>' ) ;
				break ;
			case 102 :
				alert( '<inp2:m_Phrase label="la_fck_InvalidFolderName" js_escape="1"/>' ) ;
				break ;
			case 103 :
				alert( '<inp2:m_Phrase label="la_fck_NoPermissionsCreateFolder" js_escape="1"/>' ) ;
				break ;
			case 110 :
				alert( '<inp2:m_Phrase label="la_fck_UnknownErrorCreatingFolder" js_escape="1"/>' ) ;
				break ;
			default :
				alert( '<inp2:m_Phrase label="la_fck_ErrorCreatingFolder" js_escape="1"/> ' + iErrorNumber ) ;
				break ;
		}
	}

	function DeleteCallBack($request, $params, $object)
	{
		window.parent.frames['frmUpload'].SetElementVisibility("", "none");
		files_list.getFilesList();
	}


	function Refresh()
	{
		OpenFolder(window.top.current_folder,window.top.last_folder)
		window.parent.frames['frmUpload'].SetElementVisibility("", "none");
	}


	function RenameFile(old_name,new_name)
	{
		if (ValidateFileName(new_name)) {
//			files_list.renameFile(old_name,new_name);
			var $url = rename_url.replace('#OLD_NAME#', old_name);
			var $url = $url.replace('#NEW_NAME#', new_name);
			var $url = $url.replace('#FOLDER#', window.top.current_folder);
			Request.makeRequest($url, null, '', RenameCallBack);
		}
		else
			RenameFromContextMenu(old_name);
	}

	function RenameFromContextMenu(old_name) {
		if (old_name)
			file_name = old_name;
		else
			file_name = document.getElementById("context_menu_file").value;
		f=prompt('Rename file "'+file_name+'"?',file_name);
		if (f) {
			 RenameFile(file_name,f);
		}
		HideContextMenu();
	}

	function ValidateFileName(file_name)
	{
		var sp = file_name.replace(/\\/g, '\/');
		sp=sp.split("/");
		file_name = sp[sp.length-1];
		var re = /^([\/a-zA-Z0-9\(\)_\.-]+)$/

		if (!re.test(file_name)) {
			alert('<inp2:m_Phrase label="la_fck_ErrorFileName" js_escape="1"/>');
			return false;
		} else
			return true;
	}


	function DeleteFiles(confirm)
	{
		var files = GetCheckedFileName('All');
		var $url = delete_url.replace('#FOLDER#', window.top.current_folder);
		var $url = $url.replace('#FILES#', files);
		Request.makeRequest($url, null, '', DeleteCallBack);
	}

	function CreateFolder(folder)
	{
		var $url = create_folder_url.replace('#CURRENT_FOLDER#', window.top.current_folder);
		var $url = $url.replace('#FOLDER#', folder);
		Request.makeRequest($url, null, '', CreateCallBack);
	}

	function DeleteFromContextMenu() {
		file_name = document.getElementById("context_menu_file").value;
		f=confirm('Delete file "'+file_name+'"?',file_name);
		if (f) {
			var $url = delete_url.replace('#FOLDER#', window.top.current_folder);
			var $url = $url.replace('#FILES#', file_name);
			Request.makeRequest($url, null, '', DeleteCallBack);
		}
		HideContextMenu();
	}



	function GetCheckedFileName(method)
	{
		var files = '';
		checkboxes = document.getElementsByName('file_checkbox');
		for (var i = 0; i< checkboxes.length; i++) {
			if (checkboxes[i].checked && checkboxes[i].value) {
				if (method == 'first')
					return checkboxes[i].value;
				if (method == 'cnt') {
					files++;
				} else
					files = files+checkboxes[i].value+'|';
			}
		}
		return files;
	}


	function UpdateDivSize()
	{
/*		table_height = document.getElementById('tableFiles').clientHeight;
		if(is_msie())
			window_height = document.body.clientHeight - 20;
		else
			window_height = window.outerHeight - 165;
		if (document.getElementById('data_div')) {
			document.getElementById('data_div').style.height = window_height;
			if (window_height < table_height) {
				document.getElementById('data_div').scroll = 'yes';
				document.getElementById('sortFiles').style.width = document.getElementById('tableFiles').clientWidth;
			} else {
				document.getElementById('data_div').scroll = 'yes';
				document.getElementById('sortFiles').style.width = document.getElementById('tableFiles').clientWidth;
			}
		}
*/	}


	function ProtectPath(path)
	{
		path = path.replace( /\\/g, '\\\\') ;
		path = path.replace( /'/g, '\\\'') ;
		return path ;
	}

	function AjaxFilesList($url, $input_mask) {
		this.URL = $url;
		this.InputMask = $input_mask;
	}


	AjaxFilesList.prototype.getFilesList = function ()
	{
		var $url = this.URL.replace('#SORT_BY#', $current_sort_by);
		var $url = $url.replace('#ORDER_BY#', $current_sort_order);
		var $url = $url.replace('#FOLDER#', window.top.current_folder);
		Request.makeRequest($url, this.BusyRequest, '', this.successCallback, this.errorCallback, ['getFilesList'], this);
	}

	AjaxFilesList.prototype.renameFile = function (oldName, newName)
	{
		var $url = rename_url.replace('#OLD_NAME#', oldName);
		var $url = $url.replace('#NEW_NAME#', newName);
		var $url = $url.replace('#FOLDER#', window.top.current_folder);
		Request.makeRequest($url, this.BusyRequest, '', RenameCallBack, this.errorCallback, ['getFilesList'], this);
	}

	AjaxFilesList.prototype.deleteFiles = function (oldName, newName)
	{
		var $url = delete_url.replace('#FOLDER#', window.top.current_folder);
		Request.makeRequest($url, this.BusyRequest, '', RenameCallBack, this.errorCallback, ['getFilesList'], this);
	}


	AjaxFilesList.prototype.successCallback = function($request, $params, $object) {
		var $responce = $request.responseText;
		$object.ProcessXMLNode($request.responseXML);
	}


	AjaxFilesList.prototype.GetFolderTR = function(folder, folderPath)
	{

		var sLink = '<a href="#" onclick="OpenFolder(\'' + folderPath + '\',\'' + folder + '\'); return false;">' ;
		output = output + '<tr>'+
					'<td width="25">' +
					'<\/td>' +
					'<td>' +
						'<table cellSpacing="1" cellPadding="0" border="0">' +
							'<tr>'+
								'<td>' +
									sLink +
									'<img alt="" src="img/browser/Folder.gif" width="16" height="16" border="0"><\/a>' +
								'<\/td>' +
								'<td>' +
									sLink +
									folder +
									'<\/a>' +
								'<\/td>' +
							'<\/tr>' +
						'<\/table>' +
					'<\/td>' +
					'<td width="50">&nbsp;' +
					'<\/td>' +
					'<td width="10">&nbsp;'+
					'<\/td>' +
					'<td width="120">&nbsp;'+
					'<\/td>' +
				'<\/td><\/tr>' ;
	}

	AjaxFilesList.prototype.GetFolderTR2 = function(folder, folderPath)
	{
		var sLink = '<a href="#" onclick="OpenFolder(\'' + folderPath + '\',\'' + folder + '\'); return false;">' ;
		output2 = output2 + '<tr>'+
					'<td>' +
						sLink +
						'<img alt="" src="img/browser/Folder.gif" width="16" height="16" border="0"><\/a>' +
					'<\/td>' +
					'<td>' +
						sLink +
						folder +
						'<\/a>' +
					'<\/td>' +
				'<\/tr>';
	}


	AjaxFilesList.prototype.GetFolderTRfile = function(fileName, fileUrl, fileIcon, fileSize, fileDate)
	{
		var sLink = '<a href="#" onclick="OpenFile(\'' + fileUrl + '\',\''+fileSize+'\'); return false;">' ;
		output = output + '<tr>'+
			'<td width="25"><input type="checkbox" onclick="HandleCheckboxes();" value="' + fileName + '" name="file_checkbox"\/><\/td>' +
			'<td>' +
				'<table cellSpacing="1" cellPadding="0" border="0">' +
					'<tr>'+
						'<td>' + sLink + '<img alt="" src="img/browser/icons/' + fileIcon + '.gif" width="16" height="16" border="0"><\/a><\/td>' +
						'<td>' + sLink + '<span id="' + fileName + '" oncontextmenu="ShowContextMenu(event);">' + fileName + '<\/span><\/a><\/td>' +
					'<\/tr>' +
				'<\/table>' +
			'<\/td>' +
			'<td width="50">&nbsp;&nbsp;' + fileSize + ' KB<\/td>' +
			'<td width="10">&nbsp;<\/td>' +
			'<td width="120">'+ fileDate + '<\/td><\/tr>' ;
	}


	AjaxFilesList.prototype.ProcessXMLNode = function($node)
	{
		//document.getElementById('tableFiles').innerHTML = '';
		//window.parent.frames['frmFolders'].document.getElementById('tableFiles').innerHTML = '';
		//folder_up_exists = false;
		//alert(document.getElementById('tableFiles').innerHTML);
		if (!table_header) {
			document.getElementById('tableFiles').innerHTML = '';
			output = '<table border="0" cellSpacing="1" cellPadding="0" width="100%" border="0" style="padding-top:0px;">';
			table_header = true;
		}
		if (!table_header2) {
			window.parent.frames['frmFolders'].document.getElementById('tableFiles').innerHTML = '';
			output2 = '<table border="0" cellSpacing="1" cellPadding="0" width="100%" border="0" style="padding-top:0px;">';
			table_header2 = true;
		}

		for (var i = 0; i < $node.childNodes.length; i++) {
			var $child = $node.childNodes.item(i);
			//alert($child.tagName+' CHILD');
			switch ($child.tagName)
			{
				case 'folder':
					var path = $child.getAttribute('path');
					var resource = $child.firstChild.nodeValue;
					var reg_exp = window.top.current_type + '/' +resource;
					this.GetFolderTR(resource, path);
				break;
				case 'folder2':
					var path = $child.getAttribute('path');
					var resource = $child.firstChild.nodeValue;
					var reg_exp = window.top.current_type + '/' +resource;
					this.GetFolderTR2(resource, path);
				break;
				case 'file':
					this.GetFolderTRfile(
						$child.firstChild.nodeValue,
						$child.getAttribute('url'),
						$child.getAttribute('icon'),
						$child.getAttribute('size'),
						$child.getAttribute('date')
					);
				break;

				case 'content':
					this.ProcessXMLNode($child);
				break;
			}
		}
		document.getElementById('tableFiles').innerHTML = output + '</table>';
		window.parent.frames['frmFolders'].document.getElementById('tableFiles').innerHTML = output2 + '</table>';
		table_header = false;
		table_header2 = false;
		window.parent.frames['frmFolders'].SetVisibleButtonUp();
		window.parent.frames['frmActualFolder'].SetCurrentFolder() ;
	}
//	'admin/index.php?env=-dummy:fck--OnLoadCmsTree---&admin=1'


	var files_list_url = '<inp2:m_Link template="dummy" sort_by="#SORT_BY#" order_by="#ORDER_BY#" folder="#FOLDER#" pass="m,fck" fck_event="OnGetFoldersFilesList" no_amp="1"/>'
	// '<inp2:m_Link template="core/browser/xml/files_list" sort_by="#SORT_BY#" order_by="#ORDER_BY#" folder="#FOLDER#" no_amp="1"/>'
	var files_list = new AjaxFilesList(files_list_url);
	var rename_url = '<inp2:m_Link template="dummy" old_name="#OLD_NAME#" new_name="#NEW_NAME#" folder="#FOLDER#" pass="m,fck" fck_event="OnRenameFile" no_amp="1"/>';
	var delete_url = '<inp2:m_Link template="dummy" files="#FILES#" folder="#FOLDER#" pass="m,fck" fck_event="OnDeleteFiles" no_amp="1"/>';
	var create_folder_url = '<inp2:m_Link template="dummy" current_folder="#CURRENT_FOLDER#" new_folder="#FOLDER#" pass="m,fck" fck_event="OnCreateFolder" no_amp="1"/>';

	window.onload = function()
	{
		files_list.getFilesList();
		window.top.IsLoadedResourcesList = true ;
	}

	function do_nothing() {}

	window.onresize = UpdateDivSize;

</script>
	<body class="FileArea" bottomMargin="10" leftMargin="10" topMargin="3" rightMargin="10" onclick="HideContextMenu();" oncontextmenu="return false;">
	<div id="sort_div" style="padding: 0; border: none; overflow: hidden; width: 100%; position: relative;">
		<table id="sortFiles" border="0" cellSpacing="1" cellPadding="0" width="100%" border="0" style="border-bottom:1px solid #000000; margin-bottom:0px; padding-bottom:0px;">
			<tr>
				<td width="25">
					<input type="checkbox" id="selector" value="unchecked" onclick="ToggleSelectAll();" />
				</td>
				<td>
					<table border="0" cellSpacing="0" cellPadding="0"border="0">
						<tr>
							<td><a href="javascript:sortBy('name');"><img id="sort_name" src="img/browser/sort_asc.gif" alt="" border="0"></a></td>
							<td><a href="javascript:sortBy('name');" class='order-link'>&nbsp;<inp2:m_Phrase label="la_fck_FileName"/></a></td>
						</tr>
					</table>
				</td>
				<td width="50" align="center">
					<table border="0" cellSpacing="0" cellPadding="0"border="0">
						<tr>
							<td><a href="javascript:sortBy('size');"><img id="sort_size" src="img/browser/sort_no.gif" alt="" border="0"></a></td>
							<td><a href="javascript:sortBy('size');" class='order-link'>&nbsp;<inp2:m_Phrase label="la_fck_FileSize"/></a></td>
						</tr>
					</table>
				</td>
				<td width="10"> </td>
				<td width="120">
					<table border="0" cellSpacing="0" cellPadding="0"border="0">
						<tr>
							<td><a href="javascript:sortBy('date');"><img  id="sort_date" src="img/browser/sort_no.gif" alt="" border="0"></a></td>
							<td><a href="javascript:sortBy('date');" class='order-link'>&nbsp;<inp2:m_Phrase label="la_fck_FileDate"/></a></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</div>

	<div id="data_div" style="padding: 0; border: none; overflow: auto; width: 100%; position: relative;">
		<img src="img/browser/s.gif" width="1" height="3"><br />
		<div id="tableFiles" style="width:100%">
		</div>
	</div>
	<div style="display:none">
	<img id='_desc' src="img/browser/sort_desc.gif" alt="" border="0">
	<img id='_asc' src="img/browser/sort_asc.gif" alt="" border="0">
	<img id='_no' src="img/browser/sort_no.gif" alt="" border="0">

	</div>
	<table id="context_menu" cellpadding="0" cellspacing="2" style="position: absolute; background-color: #FFFFFF; border: 1px solid #000000; display: none">
		<tr>
			<td>
				<table id="context_menu" cellpadding="0" cellspacing="0">
					<tr style="height:18px;" onmouseover="document.getElementById('rename').bgColor = '#8f8f73'; document.getElementById('rename_dot').bgColor = '#737357';" onmouseout="document.getElementById('rename').bgColor = '#FFFFFF'; document.getElementById('rename_dot').bgColor = '#E3E3C7';">
						<td id="rename_dot" bgcolor="#E3E3C7" align="center" valign="middle" width="15"><img src="img/browser/blackpix.gif" style="width: 5; height: 5" /></td>
						<td id="rename" align="left" style="font-size: 11px; font-family: 'Microsoft Sans Serif' , Arial, Helvetica, Verdana" onclick="RenameFromContextMenu();">&nbsp;<a href="javascript:do_nothing()" style="text-decoration: none; color: #000000">Rename</a>&nbsp;&nbsp;</td>
					</tr>
					<tr style="height:18px;" onmouseover="document.getElementById('delete').bgColor = '#8f8f73'; document.getElementById('delete_dot').bgColor = '#737357';" onmouseout="document.getElementById('delete').bgColor = '#FFFFFF'; document.getElementById('delete_dot').bgColor = '#E3E3C7';">
						<td id="delete_dot" bgcolor="#E3E3C7" align="center" valign="middle" width="15"><img src="img/browser/blackpix.gif" style="width: 5; height: 5" /></td>
						<td id="delete" align="left" style="font-size: 11px; font-family: 'Microsoft Sans Serif' , Arial, Helvetica, Verdana" onclick="DeleteFromContextMenu();">
							&nbsp;<a href="javascript:do_nothing()" style="text-decoration: none; color: #000000">Delete</a>&nbsp;&nbsp;
							<input type="hidden" value="" id="context_menu_file">
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
<inp2:m_include t="browser/browser_footer" />