boxr = function(delay) {
	this.jquery = false;
	this.ready = false;
	this.opened = false;
	this.content = '';
	this.delay = typeof(delay)=='number'&&delay ? delay : 2000;
	this.queue = [];
	this.fading = false;
	this.timer = null;
	this.type = 0;
	this.types = ['Alert','Error','Success','Sending request','Confirm','Show'];
	_this = this;

	if (typeof(jQuery)=='undefined'||jQuery==null) {
		var jquery = document.createElement('script');
		jquery.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
		jquery.setAttribute('type','text/javascript');
		document.body.appendChild(jquery);
		if (navigator.userAgent.indexOf('MSIE')==-1) jquery.onload = function() { _this.jquery = true;};
		else {
			jquery_loaded = setInterval( function() {
				if (typeof(jQuery)!='undefined'&&jQuery!=null) { _this.jquery = true; clearInterval(jquery_loaded);};
			}, 10);
		};
	}
	else this.jquery = true;

	this.construct = setInterval( function() {
		if (_this.jquery) {
			jQuery(document).ready( function($) {
				$.ajax({
					url : '/js/boxr.lib',
					dataType : 'html',
					success : function(data) { _this.content = data;},
					complete : function() { 
						var intl = setInterval( function() {
							if ($('#boxr').length) {
								_this.ready = true;
								clearInterval(intl);
								$('#boxr').click( function(event) { if (_this.type!=3&&!_this.fading&&!$(event.target).is($('#boxr-inner *'))) { clearTimeout(_this.timer); if (_this.type!=4) _this.hide(); else _this.reset();};});
								$(document).keydown( function(event) { if (event.which==27&&_this.type!=3&&!_this.fading) { clearTimeout(_this.timer); if (_this.type!=4) _this.hide(); else _this.reset();};});
							};
						}, 10);
					}
				});
				$('body').append('<div id="boxr"><div id="boxr-bg"></div>' + ($.browser.msie&&$.browser.version<8?'<table id="boxr-content"><tr><td id="boxr-inner"><div></div></td></tr></table>':'<div id="boxr-content"><div id="boxr-inner"' + ($.browser.msie?'':' style="opacity: 0;"') + '><div></div></div></div>') + '</div>');
			});
			clearInterval(_this.construct);
		};
	}, 10);
	
	this.alert   = function(content, immediate, confirm) { var options = {type:0, content:content, confirm: confirm}; if (!immediate||_this.type==4) this.process(options); else this.showmsg(options);};
	this.error   = function(content, immediate, confirm) { var options = {type:1, content:content, confirm: confirm}; if (!immediate||_this.type==4) this.process(options); else this.showmsg(options);};
	this.success = function(content, immediate, confirm) { var options = {type:2, content:content, confirm: confirm}; if (!immediate||_this.type==4) this.process(options); else this.showmsg(options);};
	this.request = function(content, immediate) { var options = {type:3, content:content}; if (!immediate||_this.type==4) this.process(options); else this.showmsg(options)};
	this.confirm = function(content, immediate, yesfunc, nofunc) { var options = {type:4, content:content, confirm:true, yesfunc:yesfunc, nofunc:nofunc}; if (!immediate||_this.type==4) this.process(options); else this.showmsg(options);};
	this.show    = function(id, immediate, func) { var options = {type:5, id:'#'+id, func:func}; if (!immediate||_this.type==4) this.process(options); else this.showmsg(options);};
	
	this.process = function(item) {
		this.queue.push(item);
		var opened = setInterval( function() {
			if (!_this.opened) {
				if (_this.queue.length) { _this.showmsg(_this.queue.shift());};
				clearInterval(opened);
			};
		}, 10);
	};
	
	this.set = function (content) { $('#boxr-inner > div').html(content);};
	
	this.get = function(selector) { return $('#boxr-inner > div').find(selector);};
	
	this.showmsg = function (options) {
		this.opened = true;
		var ready = setInterval( function() {
			if (_this.ready) {
				if (options.type==5&&_this.content&&options.id&&$(options.id, _this.content).length) {
					_this.set($(options.id, _this.content).html());
					_this.type = options.type;
					$('#boxr').show();
					if (!$.browser.msie) { $('#boxr-inner').animate({'opacity':'1'}, 250);};
					$('#boxr input:visible:eq(0)').focus();
					if (typeof(options.func)=='function') _this.get('form').submit( function() { options.func(); return false;});
					_this.get('input:reset').click( function() { _this.hide();});
				} else {
					if (options.type==5) options = {type:1, content:'Unable to allocate data.<br />Please, refresh the page and try again...', confirm:true};
					_this.type = options.type;
					_this.set('<h6>' + _this.types[options.type] + '</h6><div style="text-align: center;">' + (options.type==4 ? '<form action="" method="">' : '') + options.content + (options.confirm ? (options.type==4 ? '<br /><input class="boxr-continue" type="submit" value="Continue" /><input class="boxr-cancel" type="reset" value="Cancel" /></form>' : '<input class="boxr-ok" type="submit" value="Ok" />') : '') + '</div>');
					$('#boxr').show();
					if (!$.browser.msie) { $('#boxr-inner').animate({'opacity':'1'}, 250);};
					if (!options.confirm) { if (_this.type!=3) _this.fadeout();}
					else {
						$('#boxr input:submit').focus();
						$('#boxr input:submit').click( function() { if (options.type==4&&typeof(options.yesfunc)=='function') options.yesfunc(); _this.hide(); return false;});
						$('#boxr input:reset').click( function() { if (typeof(options.nofunc)=='function') options.nofunc(); _this.hide(); return false;});
					};
				};
				clearInterval(ready);
			};
		}, 10);
	};
	
	this.reset = function() { $('#boxr input:reset').click();};
	
	this.fadeout = function() {
		_this.timer = setTimeout( function() {
			if (!$.browser.msie) $('#boxr-inner').animate({'opacity':'0'}, 250, function() { _this.__hide(); _this.fading = false;});
			else { _this.__hide(); _this.fading = false;};
		}, _this.delay);
	};
	
	this.hide = function() {
		if (!$.browser.msie) $('#boxr-inner').animate({'opacity':'0'}, 250, function() { _this.__hide(); _this.fading = false;});
		else { _this.__hide(); _this.fading = false;};
	};
	
	this.__hide = function() {
		this.opened = false;
		if (!this.queue.length) {
			$('#boxr').hide();
			this.set('');
		};
	};
};
