function checkWidth()
{
	if(parseInt(document.body.clientWidth) < 961)
		document.getElementById('page').style.width = '960px';
	else
		document.getElementById('page').style.width = 'auto';
}

function attach()
{
	window.open('/browse/', 'attachment', 'width=800,height=600,resizable=0');	
}

function onLoad(folder)
{
	if(folder != '')
		openFolder(folder);
}

function deselectFolders()
{
	var folders = $$('.folder');
	
	for(i = 0; i < folders.length; i++)
	{
		$(folders[i]).style.backgroundImage = 'url(/images/icons/folder.png)';	
	}
}

function selectFolder(id)
{
	var folder = $('folder-item-' + id);
	
	folder.style.backgroundImage = 'url(/images/icons/folder_view.png)';
	
	var childFolders = $(folder).childNodes[2];
	
	if(typeof(childFolders) != 'undefined')
	{
		if(childFolders.style.display == '')
			childFolders.style.display = 'none';
		else
			childFolders.style.display = '';
	}
}

function openFolder(id)
{
	if(id == '')
		return;
		
	if($('active-folder'))
		$('active-folder').value = id;
	
	if($('upload-folder'))
		$('upload-folder').value = id;
	
	new Ajax.Updater('contents', '/browse/' + id + '/');
	
	$('folder-name').innerHTML = $('folder-' + id).innerHTML;
	$('new-folder').innerHTML = 'in ' + $('folder-' + id).innerHTML;
	
	deselectFolders();
	selectFolder(id);
	
	$('file-info').style.display = 'none';
	$('upload-file').style.display = '';
}

function makeFolder()
{
	var folderName = prompt('Folder Name', 'New Folder');
	
	if(folderName.length)
	{
		$('command').value = 'newfolder';
		$('value').value = folderName;
		$('value2').value = $F('active-folder');
		$('form').submit();
	}
}

function deleteFolder()
{
	if($F('active-folder') == '2')
		return;
		
	if(confirm('Are you sure you want to delete this folder?\n\nThe folder\'s contents (if any) will be moved to the parent folder.'))
	{
		$('command').value = 'deletefolder';
		$('value').value = $F('active-folder');
		$('form').submit();	
	}
}

function renameFile()
{
	var id = $F('active');
	
	var fileName = prompt('New File Name', $('file-' + id).innerHTML);
	
	if(fileName.length)
	{
		new Ajax.Request('/browse/', {parameters: 'command=renamefile&value=' + id + '&value2=' + fileName, onComplete: function() {new Ajax.Updater('contents', '/browse/' + $F('active-folder') + ' /');}} );
	}
}

function downloadFile()
{
	if($('active'))
	{
		var active = $F('active');
		
		if(active.length)
			document.location.href = '/download/' + $F('active') + '/';
	}
}

function deleteFile()
{
	var id = $F('active');	
	
	var name = $('file-' + id).innerHTML;
	
	name = name.substring(1, name.length);
	
	if(confirm('Are you sure you wish to delete the file \'' + name + '\' ?\n\nIf this file is attached it any items such as Events or Notices it will be removed from those also.'))
	{
		new Ajax.Request('/browse/', {parameters: 'command=deletefile&file=' + id, onComplete: function() 
		{
			new Ajax.Updater('contents', '/browse/' + $F('active-folder') + '/');
		}
		});
	}
}

function attachFile()
{
	var id = $F('active');
	
	window.opener.document.getElementById('attachment').value = id;
	window.opener.document.getElementById('attach-button').innerHTML = '<img src="/images/icons/attach.png" width="16" height="16" />  Change File';
	
	window.opener.document.getElementById('attachment-name').innerHTML = $('file-' + id).innerHTML;
	
	window.opener.document.getElementById('attachment-name').style.backgroundImage = $('file-' + id).parentNode.style.backgroundImage;
	
	window.close();
}

function details(id)
{
	if(id == '')
		return;
		
	if($('active'))
		$('active').value = id;
		
	if($('move-file'))
		$('move-file').value = id;
	
	$('file-name').innerHTML = $('file-' + id).innerHTML;
	
	$('file-info').style.display = '';
	$('upload-file').style.display = 'none';
	
	new Ajax.Updater('preview', '/browse/preview/' + id + '/');
}

function clearAttachment()
{
	$('attachment').value = '';
	$('attachment-name').innerHTML = 'None';
	$('attachment-name').style.backgroundImage = 'url(/images/icons/mime/text.png)';
	$('attach-button').innerHTML = '<img src="/images/icons/attach.png" width="16" height="16" />  Attach File';
}

function moveFile()
{
	var file = $F('move-file');
	var folder = $F('move-folder');

	new Ajax.Request('/browse/', {parameters: 'command=Move&move=' + file + '&folder=' + folder, onComplete: function() 
	{
		if($F('active-folder') == '' || folder == '0')
			document.location.href = '/browse/';
		else
			new Ajax.Updater('contents', '/browse/' + $F('active-folder') + '/');
	}
	});
		
	return false;
}

function moveFolder()
{
	var folder = $F('active-folder');
	var parent = $F('move-parent');

	new Ajax.Request('/browse/', {parameters: 'command=MoveFolder&move=' + folder + '&folder=' + parent, onComplete: function(result) 
	{
		document.location.href = '/browse/';
	}
	});
		
	return false;
}

function showMonth(region)
{
	var year = $F('calendar-year');
	var month = $F('calendar-month');
	
	if(region == 'all')
		document.location.href = '/events-calendar/' + year + '/' + month + '/';
	else
		document.location.href = '/regions/' + region + '/events/' + year + '/' + month + '/';
	
	return false;
}

function check(url)
{
	if(confirm('Are you sure you want to delete this item?'))
	{
		document.location.href = url;
	}
}