/**
* remoteCP 4
* ütf-8 release
*
* @package remoteCP
* @author hal.sascha
* @copyright (c) 2006-2008
*/
Actions = new Class(
{
	ajax: false,
	fxdefstyle: false,

	initialize: function()
	{
		this.Linkinizer();
	},

	Linkinizer: function()
	{
		// post method
		$$('form[class="postcmd"]').each(function(form)
		{
			form.removeEvents('submit');
			form.addEvent('submit', function(e)
			{
				e = new Event(e);
				e.stop();
				this.PostLoad(form);
			}.bind(this));
		}.bind(this));

		$$('form[class="postcmdc"]').each(function(form)
		{
			form.removeEvents('submit');
			form.addEvent('submit', function(e)
			{
				e = new Event(e);
				e.stop();
				if(confirm('Are you sure?')) this.PostLoad(form);
			}.bind(this));
		}.bind(this));

		// get method
		$$('a[class="getcmd"]').each(function(link)
		{
			link.removeEvents('click');
			link.addEvent('click', function(e)
			{
				e = new Event(e);
				e.stop();
				this.GetLoad(link);
			}.bind(this));
		}.bind(this));

		$$('a[class="getcmdc"]').each(function(link)
		{
			link.removeEvents('click');
			link.addEvent('click', function(e)
			{
				e = new Event(e);
				e.stop();
				if(confirm('Are you sure?')) this.GetLoad(link);
			}.bind(this));
		}.bind(this));

		$$('select[class="getcmd"]').each(function(select)
		{
			select.removeEvents('change');
			select.addEvent('change', function(e)
			{
				e = new Event(e);
				e.stop();
				element   = $(select);
				var url   = element.getAttribute('href');
				var value = element.options[element.selectedIndex].value;
				element.href = url+value;
				this.GetLoad(element);
				element.href = url;
			}.bind(this));
		}.bind(this));
	},

	onCompleteFx: function(rel)
	{
		if(!this.fxdefstyle) this.fxdefstyle = rel.getStyle('background-color');
		var fx = new Fx.Style(rel, 'background-color',
		{
			duration: 800,
			transition: Fx.Transitions.Quad.easeOut
		}).start('#000000', this.fxdefstyle);
	},

	PostLoad: function(element)
	{
		element	= $(element);
		var id  = element.getAttribute('id');
		var rel = element.getAttribute('rel');
			rel = rel.split(':');

		// AJAX Powaaah!
		this.ajax = $(id).send(
		{
			update: $(rel[0]),
			onRequest: function() { }.bind(this),
			onComplete: function()
			{
				this.onCompleteFx($(rel[0]));
				this.Linkinizer();
				rcp_Modal.Linkinizer();

				// call callback function
				if(rel[1])
				{
					if(typeof window[rel[1]] == 'function') window[rel[1]]();
				}
			}.bind(this),
			onFailure: function() { }.bind(this)
		});
	},

	GetLoad: function(element)
	{
		element	= $(element);
		var url = element.href;
		var rel = element.getAttribute('rel');

		// AJAX Powaaah!
		this.ajax = new Ajax(url,
		{
			method: 'get',
			update: $(rel),
			onRequest: function() { }.bind(this),
			onComplete: function()
			{
				this.onCompleteFx($(rel));
				this.Linkinizer();
				rcp_Modal.Linkinizer();
			}.bind(this),
			onFailure: function() { }.bind(this)
		});
		this.ajax.request();
	}
});
