var RedirectBox = {
	Show: function(settings) {
		Object.extend(RedirectBox.settings, settings);
		RedirectBox.Hide();
		RedirectBox.timeout = setTimeout(function() { RedirectBox.createDialog(); }, 50);
		document.observe('keyup', function(e) {
			e = e || event;
			if (e.keyCode == 13) {
				location.href = RedirectBox.settings.redirectURL;
			}
		});
	},

	Hide: function() {
		if (RedirectBox.element) {
			RedirectBox.timerStop();
			RedirectBox.element.remove();
			RedirectBox.element = null;
			if (RedirectBox.timeout) {
				clearTimeout(RedirectBox.timeout);
				RedirectBox.timeout = null;
			}
		}
	},

	settings: {
		width: 150,
		redirectURL: '',
		ticksToRedirect: 3,
		text: '',
		stayButtonText: '',
		redirectButtonText: '',
		timerDelay: 1000,
		timerID: 0,
		timeout: null
	},

	createDialog: function() {
		RedirectBox.element = new Element('div').setStyle({
			position: 'absolute',
			width: RedirectBox.settings.width + 'px',
			zIndex: 9999
		});

		var infobox = new InfoBox('', RedirectBox.getBody(), { width: RedirectBox.settings.width + 48 + 'px', disableHeader: true, className: 'shadowBox' });
		//RedirectBox.element.insert(RedirectBox.getIFrameHack());
		RedirectBox.element.insert(infobox.getInfoBox());
		$(document.body).insert(RedirectBox.element);

		var locationDiv = $('BattleBoxDiv');
		RedirectBox.element.setStyle({
			left: ((locationDiv.cumulativeOffset().left + (locationDiv.getWidth() / 2)) - (RedirectBox.element.getWidth() / 2)) - 34 + 'px',
			top: (locationDiv.cumulativeOffset().top + 50) + 'px'
		});

		RedirectBox.timeout = null;

		RedirectBox.timerStart();
	},

	getBody: function() {
		var element = new Element('div').setStyle({ padding: '0px', width: RedirectBox.settings.width + 'px' });
		element.insert(
			new Element('table').insert(
				new Element('tbody').insert(
					new Element('tr').insert(
						new Element('td').update(RedirectBox.settings.text).setStyle({ textAlign: 'center' })
					)
				).insert(
					new Element('tr').insert(
						new Element('td').insert(
							new Element('div', { id: 'RedirectBoxCounterDiv' }).update(RedirectBox.settings.ticksToRedirect)
						).setStyle({ fontSize: '16px', fontWeight: 'bold', textAlign: 'center', padding: '5px 0px 5px 0px' })
					)
				).insert(
					RedirectBox.getButtonRedirect()
				).insert(
					RedirectBox.getButtonStay()
				)
			).setStyle({ width: RedirectBox.settings.width + 'px' })
		);
		return element;
	},

	getButtonRedirect: function() {
		if (RedirectBox.settings.redirectButtonText != '') {
			var tr = new Element('tr');
			var td = new Element('td');
			var buttonOptions = {
				buttonClass: 'LargeGreen',
				width: '100%',
				onClick: function() {
					location.href = RedirectBox.settings.redirectURL;
				}
			};
			var button = new ArtoButton(RedirectBox.settings.redirectButtonText, buttonOptions);
			return tr.insert(td.update(button));
		}
	},

	getButtonStay: function() {
		if (RedirectBox.settings.stayButtonText != '') {
			var tr = new Element('tr');
			var td = new Element('td');
			var buttonOptions = {
				buttonClass: '',
				width: '100%',
				onClick: function() {
					RedirectBox.Hide();
				}
			};
			var button = new ArtoButton(RedirectBox.settings.stayButtonText, buttonOptions);
			return tr.insert(td.update(button));
		}
	},

	getIFrameHack: function() {
		return new Element('iframe', { scrolling: 'no', frameborder: 0 }).setStyle({
			position: 'absolute',
			top: '24px',
			left: '24px',
			zIndex: 9998,
			width: RedirectBox.settings.width + 'px',
			height: 10 + 'px'
		});
	},

	timerStart: function() {
		RedirectBox.settings.timerID = setTimeout(function() { RedirectBox.timerTick(); }, RedirectBox.settings.timerDelay);
	},

	timerStop: function() {
		clearTimeout(RedirectBox.settings.timerID);
	},

	timerTick: function() {
		if (RedirectBox.settings.ticksToRedirect > 1) {
			RedirectBox.settings.ticksToRedirect--;
			$('RedirectBoxCounterDiv').update(RedirectBox.settings.ticksToRedirect);
			RedirectBox.timerStart();
		} else {
			location.href = RedirectBox.settings.redirectURL;
		}
	}
}
try {
} catch (e) {}
