var MBoxJavascriptChecker = Class.create();
Object.extend(Object.extend(MBoxJavascriptChecker.prototype, new MBox.AjaxRequest()),
{
	initialize: function()
	{
		this.sendRequest
		(
			'ajaxrequest.m-box',
			{ name: 'action', value: 'checkjavascript' }
		);
	}
}
);


var MBoxAjaxChecker = Class.create();
Object.extend(Object.extend(MBoxAjaxChecker.prototype, new MBox.AjaxRequest()),
{
	initialize: function(autologin)
	{
		this.logindialog = $('logindialog');
		this.autologin = autologin;

		var viewport = MBoxUtil.viewport();
		if (this.logindialog)
		{
			this.logindialog.setStyle
			({ 
				left: parseInt(viewport.width/4)+'px', 
				top: parseInt(viewport.height/4)+'px', 
				width: parseInt(viewport.width/2)+'px', 
				height: parseInt(viewport.height/3)+'px' 
			});
		}

		this.sendRequest
		(
			'ajaxrequest.m-box',
			{ name: 'action', value: 'ajaxcheck' }, 
			{ name: 'autologin', value: (this.autologin ? 'true' : 'false') }
		);
	},

	ajaxUpdate: function(xmldoc)
	{
		if (xmldoc)
		{
			var login_elements = document.getElementsByClassName('login_element', 'body');
			// update login elements with the GET param 'have_ajax'
			$A(login_elements).each(
				function(e)
				{
					if (e.tagName == 'A')
						e.setAttribute('href', e.getAttribute('href') + '&have_ajax=true');
					else if (e.tagName == 'FORM')
						e.setAttribute('action', e.getAttribute('action') + '&have_ajax=true');
				}
			);

			// test <m-box.ajaxresponse><autologin>{true|false}</autologin></m-box.ajaxresponse>
			if (MBoxUtil.getContentAsString(xmldoc.documentElement.firstChild) == 'true')
			{
				// automatically login guest
				var login_url = '';
				if (login_elements[0].tagName == 'A')
				{
					login_url = login_elements[0].getAttribute('href');
				}
				else if (login_elements[0].tagName == 'FORM')
				{
					login_elements[0].getAttribute('action');
				}

				if (this.logindialog)
				{
					this.logindialog.setOpacity(0.99);
					this.logindialog.show();
				}
				window.location.replace(login_url);
			}
		}
	}
}
);

mbox.BrowserCapabilityHelper = Class.create();
Object.extend(mbox.BrowserCapabilityHelper.prototype,
{
	initialize: function()
	{
		var autologinanchor = $('m-box_autologin-anchor');
		
		if (autologinanchor)
		{
			var logindialog = $('m-box_autologin-dialog');
			var viewport = MBoxUtil.viewport();
			if (logindialog)
			{
				logindialog.setStyle
				({ 
					left: parseInt(viewport.width/3)+'px', 
					top: parseInt(viewport.height/2.6)+'px', 
					width: parseInt(viewport.width/3)+'px', 
					height: parseInt(viewport.height/4)+'px' 
				});
				logindialog.setOpacity(0.99);
				logindialog.show();
			}

			window.location.replace(autologinanchor.getAttribute('href') + '&have_js=true&have_ajax=' + (this.have_ajax ? 'true' : 'false'));
		}
		else
		{
			this.have_ajax = Ajax.getTransport() != false;

			// TODO: prototype 1.6 offers a context parameter for each()
			var a = $$('a.m-box_loginelement');
			for (var i = 0; i < a.length; ++i)
				Element.observe(a[i], 'click', this.onlogon_byanchor.bindAsEventListener(this));
			var f = $$('form.m-box_loginelement');
			for (var i = 0; i < f.length; ++i)
				Element.observe(f[i], 'submit', this.onlogon_byform.bindAsEventListener(this));
		}
	}, 
	
	onlogon_byanchor: function(event)
	{
		window.location.href = Event.element(event).getAttribute('href') + '&have_js=true&have_ajax=' + (this.have_ajax ? 'true' : 'false');
		Event.stop(event);
	}, 

	onlogon_byform: function(event)
	{
		var form = Event.element(event);
		// append browser capabilities to action url and submit the form
		form.setAttribute('action', form.getAttribute('action') + '&have_js=true&have_ajax=' + (this.have_ajax ? 'true' : 'false'));
		return true;
	}
});

var MBoxSessionRefresher = Class.create();
Object.extend(Object.extend(MBoxSessionRefresher.prototype, MBox.AjaxRequest.prototype),
{
	initialize: function(sessiontimeout, clienttimeout)
	{
		this.sessiontimeout = sessiontimeout;
		this.clienttimeout = clienttimeout;
		this.keepalivecounter = 1;
		this.inProgress = false;
		this.started = (new Date()).getTime()/1000;
		// trigger it every minute
		new PeriodicalExecuter(this.onrefreshMBoxSession.bind(this), 60);
	},

	onrefreshMBoxSession: function()
	{
		if (!this.inProgress)
		{
			var idletime = (new Date()).getTime()/1000 - this.started;
			// check whether client is still allowed to be active
			if ((this.clienttimeout == 0		)	||
				(idletime < this.clienttimeout	)	)
			{
				// check whether idletime is 1 min (plus a buffer) below sessiontimeout
				// and send a signal to keep the sesson alive
				if ((this.sessiontimeout > 0										)	&&
					(((idletime/this.keepalivecounter)+60+10) >= this.sessiontimeout	)	)
				{
					++this.keepalivecounter;
					this.sendRequest(
						'ajaxrequest.m-box',
						{ name: 'action', value: 'keepsessionalive' }
					);
				}
			}
			else
			{
				this.inProgress = true;
				window.location.href = 'sessiontimeout.m-box?m-box_phpsessid=480d0d84e2b3b175c5a382d663d6dbaa';
			}
		}
	}
});


var MBoxErrorDetails = Class.create();
Object.extend(MBoxErrorDetails.prototype,
{
	initialize: function()
	{
		this.infodivelement = $('error_detail');
		this.mboxerrorelement = $('mbox_error');
		detailelement = $('show_error_detail');

		if (!this.infodivelement || !this.mboxerrorelement || !detailelement)
			return;

		Element.show(detailelement);
		// show dialog
		Event.observe(detailelement, 'click', this.onshow.bindAsEventListener(this));
		// hide dialog
		Event.observe(this.infodivelement, 'mouseout', this.onblur.bindAsEventListener(this));
	},

	onshow: function(event)
	{
		Position.clone(this.mboxerrorelement, this.infodivelement, { setHeight: false, setTop: false });
		new Effect.Appear(this.infodivelement, { duration: 0.15, to: 0.98 });
		Event.stop(event);
	},

	onblur: function(event)
	{
		if (MBoxUtil.checkmouseleave(this.infodivelement, event))
			new Effect.Fade(this.infodivelement, { duration: 0.15 });
	}
}
);

