var Windows = Class.create(
{
	initialize: function()
	{
		this.options = Object.extend({
			title:			'',
			message:		'',
			okLabel:		'',
			cancelLabel:	'',
			parent:			document.body,
			onLoad:			Prototype.emtyFunction,
			onOk:			''});
	},

	confirm: function(parameters)
	{
		this.options['title'] = parameters['title'];
		this.options['message'] = parameters['msg'];
		this.options['okLabel'] = parameters['okLabel'];
		this.options['cancelLabel'] = parameters['cancelLabel'];
		this.options['onOk'] = parameters['onOk'];

		this.element = this.createWindow(); 

		// Now position it
		h = this.element.getHeight();
		w = this.element.getWidth();
		
		this.position(w, h);  
		
		//we _WANT_ to steal focus from element which launched this 
		//window (prevent user from executing multiple confirmations)
		if($('confirm_cancel'))
		{
			$('confirm_cancel').focus();
		}
	},

	createWindow: function()
	{
		// Create modal container
		this.createModal();

		// Create the box
		new Element.insert(this.modal, '<div id="window-body"></div>');
		this.winBody = $('window-body');

		new Element.insert(this.winBody, '<div id="window-title">' + this.options['title'] + '</div>');
		new Element.insert(this.winBody, '<div id="window-content"><img src="' + theme_path + 'images/icons/warning.png" alt="" /><div>' + this.options['message'] + '</div></div>');
		new Element.insert(this.winBody, '<div id="window-buttons"></div>');
		
		if (this.options['okLabel'])
		{
			new Element.insert($('window-buttons'), '<input type="button" name="confirm_submit" id="confirm_submit" class="submit" value="' + this.options['okLabel'] + '" />');
			
			if (this.options['onOk'])
			{
				$('confirm_submit').onclick = this.handle_submit.bindAsEventListener(this);
			}
			else
			{
				$('confirm_submit').onclick = this.handle_cancel.bindAsEventListener(this);		
			}
		}
	    
	    if (this.options['cancelLabel'])
	    {
	    	new Element.insert($('window-buttons'), ' <input type="button" name="confirm_cancel" id="confirm_cancel" value="' + this.options['cancelLabel'] + '" />');
	    	$('confirm_cancel').onclick = this.handle_cancel.bindAsEventListener(this);
	    }

		return this.winBody;
	},
	
	createModal: function()
	{
		// Create the object
		var body = $$('body').first();
		
		// Insert a div into it
		new Element.insert(body, '<div id="window-modal" style="z-index:1000;"></div>');
		this.modal = $('window-modal');
	},
	
	position: function(box_w, box_h)
	{
		var win_w = document.documentElement.clientWidth;
		var win_h = document.documentElement.clientHeight;
	
		this.element.setStyle({
			top: ((win_h - box_h) / 2) + 'px'
		});
	},
	
	handle_cancel: function()
	{
		this.modal.remove();
	},
	
	handle_submit: function()
	{
		window.location = this.options['onOk'];
	}
});
