var ConfirmDelete = {

	init: function () {
		
		var popup = new Element('div', {
			'class': 'confirm-popup'
		});
		
		var form = new Element('form', {
			'action': '',
			'method': 'post'
		});
		popup.adopt(form);

		form.adopt(new Element('input', {
			'type': 'hidden',
			'name': 'delete_confirm[return_url]',
			'value': window.location.href
		}));
		form.adopt(new Element('label', {
			'for': 'delete_confirm[submit]',
			'text': 'Er du sikker?'
		}));
		form.adopt(new Element('input', {
			'type': 'submit',
			'name': 'delete_confirm[submit]',
			'value': 'OK'
		}));
		
		$(document.body).adopt(popup);
		
		ConfirmDelete.popup = popup;
		ConfirmDelete.popupform = form;
		
		
		popup.addEvent('click', function (e) {
			e.stopPropagation();
		})
		
		$$('a.delete').addEvent('click', function (e) {
			e.stop();
			x_displacement = $(this).hasClass('modal-right') ? 10 : -95 ;
			ConfirmDelete.display_popup(this.href, e.page.x, e.page.y, x_displacement);
		});
	},
	
	display_popup: function (url, x, y, x_displacement) {
		ConfirmDelete.popupform.action = url;
		ConfirmDelete.popup.setStyle('top', (y - 20));
		ConfirmDelete.popup.setStyle('left', (x + x_displacement));
		ConfirmDelete.popup.setStyle('display', 'block');
		
		$(document.window).addEvent('click', ConfirmDelete.hide_popup_event_handler);
	},
	
	hide_popup: function () {
		ConfirmDelete.popup.style.display = 'none';
		
		$(document.window).removeEvent('click', ConfirmDelete.hide_popup_event_handler);
	},
	
	hide_popup_event_handler: function (e) {
		e.stopPropagation();
		ConfirmDelete.hide_popup();
	}
	
};

window.addEvent('domready', ConfirmDelete.init);
