// file lists
document.observe('dom:loaded', function() {
	var table = $$('table.filelist')[0];

	if (!table) {
		return;
	}

	var headers = table.select('th');
	var rows = table.select('tbody tr');

	if (table.down('input[type=checkbox]') || table.down('input[type=radio]')) {
		var created_select_all = false;
		var select_all;
		var checkboxes = table.select('td input.chkbox');
		rows.each(function(row) {
			// create the click event to select the radio or checkbox
			row.observe('click', function(event) {
				var el = Event.element(event).up('tr').down('input[type=checkbox]');
				if (!el) {
					el = Event.element(event).up('tr').down('input[type=radio]');
				}

				if (el) {
					el.checked = !el.checked;
					select_all.checked = countChecked(checkboxes) == checkboxes.length;
				}
			});

			if (!created_select_all) {
				var cb = row.down('input[type=checkbox]');
				if (cb) {
					// find which column this is so we can insert the
					// select all button in the header
					var cell = cb.up('td');
					var pos = 0;
					while (cell = cell.previous()) {
						pos++;
					}

					select_all = $(document.createElement('input'));
					select_all.setAttribute('type', 'checkbox');

					headers[pos].appendChild(select_all);

					$(select_all).observe('click', function(event) {
						checkboxes.each(function(el){
							el.checked = select_all.checked;
						});
					})

					created_select_all = true;
				}
			}
		});

		// add a binding to the checkboxes to allow checking whether
		// all/none already checked
		function countChecked(checkboxes) {
			var c = 0;
			for (var i = 0; i < checkboxes.length; i++) {
				if (checkboxes[i].checked) {
					c++;
				}
			}

			return c;
		}

		// replicate the action
		table.select('td input.chkbox').each(function(el){
			el.observe('click', function(event) {
				var chk = Event.element(event);
				chk.checked = !chk.checked;
			});
		});
	}

	function pending()
	{
		if ( typeof pending.counter == 'undefined' ) {
			pending.counter = 0;
		}
		var id;
		var divs = $H();
		$$('.pending').each(function(el){
			id = el.down('input').value;
			divs.set(id, el);
		});
		new Ajax.Request('/c/ajax/pending_files.php',
			{
				method: 'post',
				parameters: {'files[]': divs.keys()},
				onSuccess: function(response){
					if (200 == response.status && response.responseText){
						var still_pending = response.responseText.evalJSON(true);
						var queue = divs.keys();
						var size = still_pending.shift();
						if (size == queue.length){
							while (id = queue.pop()) {
								if (still_pending.indexOf(id) == -1){
									var d = divs.unset(id);
									if (d){
										d.hide();
										d.next().show();
										d.removeClassName('pending');
									}
								}
							}
						}
						if (divs.keys().length && pending.counter++ < 20) {
							pending.delay(5 * pending.counter);
						}
					}
				},
				evalJSON: true
			}
		);
	}
	pending.delay(2);

});

$jq(document).ready(function() {
	$jq('.fvcontrols').click(function(event) {
		var el = $jq(event.target);

		if (el[0].tagName == 'DIV') {
			var div = el;
		} else if (el[0].tagName == 'LABEL') {
			var div = el.parent('div');
		} else {
			// do nothing, clicked on the checkbox
			return;
		}

		div.find('.chkbox').click();
	});
});

