/**
* remoteCP 4
* ütf-8 release
*
* @package remoteCP
* @author hal.sascha
* @copyright (c) 2006-2008
*/
Modal = new Class(
{
	ajax: false,

	initialize: function()
	{
		// Set close Modal events
		$('modalcloseA').addEvent('click', function()
		{
			this.Disable();
		}.bind(this));

		$('modalbg').addEvent('click', function()
		{
			this.Disable();
		}.bind(this));

		// Set all class="modal" links
		this.Linkinizer();
	},

	Disable: function()
	{
		$('modal').setStyle('display', 'none');
		$('modal_').setStyle('width', 0);
		$('modal_').setStyle('height', 0);
		$('modalbg').setStyle('display', 'none');
	},

	Enable: function(boxsx, boxsy)
	{
		// options
		var margin = 50;

		// get window sizes
		var windowsx = window.getWidth();
		var windowsy = window.getHeight();
		var windowss = window.getScrollHeight();

		// set block elements
		$('modal').setStyle('display', 'block');
		$('modalbg').setStyle('display', 'block');
		$('modal_').setStyle('overflow', 'visible');

		// set modal-box size (if specified)
		boxsx = ((boxsx < 0) || (boxsx > windowsx)) ? windowsx - margin : boxsx;
		boxsy = ((boxsy < 0) || (boxsy > windowss)) ? windowss - margin : boxsy;
		if(boxsx > 0) $('modal_').setStyle('width', boxsx+'px');
		if(boxsy > 0) $('modal_').setStyle('height', boxsy+'px');

		// get modal-box size
		boxsx = $('modal').getSize().size.x;
		boxsy = $('modal').getSize().size.y;
		if(boxsy > windowss)
		{
			boxsy = windowss - (margin*2);
			$('modal_').setStyle('height', boxsy+'px');
			$('modal_').setStyle('overflow', 'scroll');
		}

		// set modal-box position
		var boxpx = Math.floor((windowsx - boxsx) / 2);
		var boxpy = Math.floor((windowsy - boxsy) / 2);
		boxpx = ((boxpx < 0) || (boxpx > windowsx)) ? 0 + margin : boxpx;
		boxpy = ((boxpy < 0) || (boxpy > windowsy)) ? 0 + margin : boxpy;
		$('modal').setStyle('left', boxpx);
		$('modal').setStyle('top' , boxpy);

		// display bg
		$('modalbg').setStyle('width', windowsx);
		$('modalbg').setStyle('height', windowss);
		$('modalbg').setStyle('visibility', 'visible');

		// display modal
		$('modal').setStyle('visibility', 'visible');
	},

	Linkinizer: function()
	{
		$$('a[class="modal"]').each(function(el)
		{
			el.removeEvents('click');
			el.addEvent('click', function(e)
			{
				var img, options, size;

				// create new link event
				e = new Event(e);
				e.stop();

				// cleanup container
				$('modal_').empty();

				// set modal title
				$('modaltitle').setHTML(el.title);

				// get height/width options
				size = new Array(0,0);
				if('rel' in el)
				{
					options = el.getProperty('rel');
					if(options)
					{
						size = options.split(';');
						size[0] = size[0] ? size[0] : 0;
						size[1] = size[1] ? size[1] : 0;
					}
				}

				// set container
				this.ajax = new Ajax(el.href,
				{
							method: 'get',
							update: $('modal_'),
							onRequest: function()
							{
								$('modal_').addClass('loading');
								this.Enable(size[0],size[1]);
							}.bind(this),
							onComplete: function()
							{
								$('modal_').removeClass('loading');
								this.Enable(size[0],size[1]);
								rcp_Actions.Linkinizer();
							}.bind(this),
							onFailure: function()
							{
								$('modal_').removeClass('loading');
								$('modal_').setHTML('failure @ ajax request');
								this.Enable(size[0],size[1]);
							}.bind(this)
				});
				this.ajax.request();
			}.bind(this));
		}.bind(this));
	}
});
