var lightbox;
var loginbox;
var defaultUI;
var currentProject;
var dbg;

function projectLogin(project)
{
	$('ProjectId').value = project;
	currentProject = project;
	lightbox.show();
	loginbox.show();
}

function closeProjectLogin()
{
	loginbox.hide();
	lightbox.hide();
	logininfo.update(defaultUI);
}

function doLogin(form)
{
	data = form.serialize();
	var url = '/upcoming_projects/ajax_authenticate';
	logininfo.update('Please wait while we check authentication...<br /><img src="/img/ajax-loading.gif" alt="Loading ..." />');
	new Ajax.Request(url, {
		parameters: data,
		method: 'post',
		onSuccess: function(tx) {
			parseResponse(tx.responseXML);
		},
		onFailure: function(tx) {
			logininfo.update(defaultUI);
			$('project-files-login-errors').update('Checking login failed.  Please try again later.').show();
		}
	});
}

function parseResponse(xml)
{
	dbg = xml.documentElement;
	xml = xml.documentElement;

	if(xml.getElementsByTagName('code')[0].childNodes[0].nodeValue == '0')
	{
		logininfo.update(defaultUI);
		$('ProjectId').value = currentProject;
		$('project-files-login-errors').update(xml.getElementsByTagName('message')[0].childNodes[0].nodeValue);
		$('project-files-login-errors').show();
		bindForm();
	}
	else
	{
		var files = $H({});
		var resp = xml.getElementsByTagName('files');
		if(resp.length > 0)
		{
			resp = resp[0].getElementsByTagName('file');
			for(var i=0; i<resp.length; i++)
			{
				node = resp[i];

				var file = $H({
					'size': node.getAttribute('size'),
					'last_modified': node.getAttribute('modified'),
					'url': node.getElementsByTagName('url')[0].childNodes[0].nodeValue,
					'link': node.getElementsByTagName('name')[0].childNodes[0].nodeValue
				});

				files.set(i, file);
			}
		}
dbg = files;
		showFiles(files);
	}
}

function showFiles(files)
{
	var elms = $$('div.project-files > div#'+currentProject);
	var elm = elms[0];

	var totalFiles = files.keys().length;

	var table = new Element('table', { 'class': 'filelist' });
	var thead = new Element('thead');
	var header = new Element('tr');
	var hf = new Element('th').update('Filename');
	var lm = new Element('th').update('Last Modified Date');
	var fs = new Element('th').update('File Size');
	
	header.update(hf).insert(lm).insert(fs);
	table.update(thead.update(header));

	var tbody = new Element('tbody').update();

	if(totalFiles > 0)
	{
		files.each(function(pair) {
			var elm = pair.value;
			
			var row = new Element('tr');
			var linkc = new Element('td');
			var link = new Element('a', { href: elm.get('url'), target: '_blank', rel: 'external' }).update(elm.get('link'));
			var modifiedc = new Element('td').update(elm.get('last_modified'));
			var sizec = new Element('td').update(elm.get('size'));

			linkc.update(link);
			row.update(linkc).insert(modifiedc).insert(sizec);
			tbody.insert(row);
		});
	}
	else
	{
		var row = new Element('tr');
		var tc = new Element('td', { colspan: 3 }).update('No files currently associated with this project');

		row.update(tc);
		tbody.insert(row);
	}
	table.insert(tbody);

	elm.update('<p>'+totalFiles+' Files Found</p>').insert(table);

	closeProjectLogin();
}

function bindForm()
{
	$('projectLoginForm').observe('submit', function() {
		doLogin(this);
		return false;
	});
}

document.observe('dom:loaded', function() {
	lightbox = $('project-files-lightbox');
	loginbox = $('project-files-login-box');
	logininfo = $('project-login-info');
	defaultUI = logininfo.innerHTML;

	bindForm();
});
