Event.onDOMReady(function() {
	if (window.initTogglingList)
		initTogglingList();

	tableRowHover();
	clickableElements();
	addPopupButtons();
	initCheckAllCheckboxes();
	addButtonHover();
	defaultSearchText();
	initMenu();
	setIeImageCache();
});

function clickableElements() {
	document.getElementsByClassName('clickable').each(function(element) {
		var links = element.getElementsByTagName('a');

		if (links != null && links.length > 0) {
			element.addClassName('is_clickable');
			element.onclick = function() { window.location.href = links[0].href; };

			element.onmouseover = function() {
				element.addClassName('active-clickable');
				if (element.tagName.toLowerCase() == 'tr') adjustCellsBg(element, 'rgb(-19, -2, 8)');
			};

			element.onmouseout = function() {
				element.removeClassName('active');
				if (element.tagName.toLowerCase() == 'tr') adjustCellsBg(element);
			};
		}
	});
}

/**
 * Loops through all cells (td) contained in an element and
 * adjusts their background color. Input amount can be a hex color
 * (#xxxxxx or #xxx) either positive or negative (-#xxxxxx or -#xxx)
 * or an rgb color (rgb(x, x, x) or rgb(-x, x, x) and so on).
 *
 * If the input is null (not provided) and the function has run
 * once, the original background color will be retored. If the amount
 * is null and the function has not been run before, nothing happens.
 */
function adjustCellsBg(element, amount) {
	var cell, el;

	for (var i = 0, td = null; (td = element.getElementsByTagName('td')[i]); i++) {
		if (amount == null) {
			if (td.originalBg) td.style.backgroundColor = td.originalBg;
			continue;
		}

		cell = td, el = td, style = $(el).getStyle('background-color');

		while (style == 'transparent' && el.parentNode != null) {
			el = el.parentNode;
			style = $(el).getStyle('background-color')
		}

		cell.originalBg = style;
		cell.style.backgroundColor = addColors(style, amount);
	}
}

function tableRowHover() {
	$$('table tr').each(function(tr) {
		var row = $(tr);
		var className = 'active';
		var children = row.getElementsByTagName('*');
		var i = 0;
		var cell = children[i++];
		
		// row alternator
		if (!(row.rowIndex % 2)) {
			row.addClassName('alt');
		}

		while(cell.tagName.toLowerCase() != 'td' && cell.tagName.toLowerCase() != 'th')
			cell = children[i++];

		$(cell).addClassName('first');
		i = children.length-1;
		cell = children[i--];

		while(cell.tagName.toLowerCase() != 'td' && cell.tagName.toLowerCase() != 'th')
			cell = children[i--];

		$(cell).addClassName('last');

		if (row.hasClassName('clickable') && row.getElementsByTagName('a').length > 0) {
			className = 'active-clickable';
			tr.onclick = function() { window.location.href = this.getElementsByTagName('a')[0].href; };
		}

		tr.onmouseover = tr.onmouseout = function() {
			row.toggleClassName(className);
		};
	});
}

function addPopupButtons() {
	if (window.parent.location == null || window.parent.currentPopup == null ||
		window.location.href == window.parent.location.href)
		return;

	var confirm = document.getElementsByClassName('confirm');
	var cancel = document.getElementsByClassName('cancel');

	for (var i = 0, element = null; (element = confirm[i]); i++)
		window.parent.currentPopup.registerConfirmButton(element);

	for (var i = 0, element = null; element = cancel[i]; i++)
		window.parent.currentPopup.registerCancelButton(element);

	$$('body form').each(function(form) {
		form.appendChild(HTMLDOMUtil.createElement('input',
												   null, null, null,
												   ['type', 'hidden', 'name', 'layout', 'value', 'lightbox']));
	});

	$$('body a').each(function(link) {
		link.href += link.href.indexOf('?') >= 0 ? '&layout=lightbox' : '?&layout=lightbox';
	});
}

function initCheckAllCheckboxes() {
	$$('#content input[type="checkbox"]').each(function(input) {
		if (input.className.indexOf('check_all;') < 0)
			return;

		var parentScope = input.className.split('check_all;')[1].split(' ')[0];

		Event.observe(input, 'click', function() {
			$$('#' + parentScope + ' input[type="checkbox"]').each(function(checkbox) {
				checkbox.checked = input.checked;
			});
		});
	});
}

function addButtonHover() {
	$$('div.button').each(function(button) {
		var className = '_button';
		if (button.className.indexOf('action') >= 0) className = '_action_button';

		var addFocus = function() { button.addClassName('focused' + className); };
		var removeFocus = function() {
			button.removeClassName('focused' + className);
			button.removeClassName('active' + className);
		};

		Event.observe(button, 'mouseover', addFocus);
		Event.observe(button, 'mousedown', function() { button.addClassName('active' + className); });
		Event.observe(button, 'focus', addFocus);
		Event.observe(button, 'mouseout', removeFocus);
		Event.observe(button, 'mouseup', function() { button.removeClassName('active' + className); });
		Event.observe(button, 'blur', removeFocus);
	});
}

function defaultSearchText() {
	$$('#search .text').each(function(input) {
		input.defaultText = input.value;

		if (input.defaultText == '') return;

		Event.observe(input, 'focus', function(e) { if (input.value == input.defaultText) input.value = ''; });
		Event.observe(input, 'blur', function(e) { if (input.value == '') input.value = input.defaultText; });
	});
}