try {
	
	if (MooTools) {
		var event_tt;
		 
		// extend string class
		if (!String.remove && String.implement) String.implement('remove', function(needle) {
			var str = '';
			
			if (this && needle) {
				var lenNeedle = needle.length;
				var indexLast = 0;
				var index;
		
				while((index = this.indexOf(needle, indexLast)) != -1) {
					str += this.substring(indexLast, index);
					indexLast = index + lenNeedle;
				}
				
				str += this.substr(indexLast);
			}
			
			return str;
		});
		
		if (!String.between && String.implement) String.implement('between', function(tokenStart, tokenEnd) {
			var indexStart = this.indexOf(tokenStart);
			var indexEnd = this.lastIndexOf(tokenEnd);
			
			if (indexStart == -1) indexStart = 0; else indexStart += tokenStart.length;
			if (indexEnd == -1) indexEnd = this.length;
			if (indexEnd < indexStart) {
				var t = indexStart;
				indexStart = indexEnd;
				indexEnd = t;
			}
			
			return this.substring(indexStart, indexEnd);
		});
		
		// change content
		window.changeContent_event = function(config) {
			$try(function() {
				if (config.element) {
					config.element.empty();
					if (!config.empty) config.element.set('html', config.t_content);
					
					var innerHTMLStr = config.element.get('html') + config.html;
					if (String.between && innerHTMLStr.toLowerCase().contains('<body>')) innerHTMLStr = innerHTMLStr.between('<body>', '</body>');
					config.element.set('html', innerHTMLStr);
					config.element.scrollTo(0, 0);
					
					if (config.ready && typeof(config.ready) == 'function') config.ready.call(null, config.readyParams ? config.readyParams : null);
				}
			});
		}
		
		// load content
		window.loadContent_event = function(config) {
			$try(function() {
				if (config.url) {
					config.t_content = config.element.get('html');
					
					var req = new Request({
						url: config.url,
						method: 'get',
						data: (config.data ? config.data : null),
						onRequest: function() {
							if (!config.avoidLoader) {
								config.element.empty();
								config.element.addClass('t_loaderModal');
							}
							else if (config.matteLoader) {
								var t_modalMatteLoader = new Element('div', {
									id: 't_modalMatteLoader'
								});
								
								var winScrollSize = window.getScrollSize();
								
								t_modalMatteLoader.set('opacity', .5);
								t_modalMatteLoader.setStyle('width', winScrollSize.x + 'px');
								t_modalMatteLoader.setStyle('height', winScrollSize.y + 'px');
								$$('body').adopt(t_modalMatteLoader);
							}
						},
						onSuccess: function(responseText) {
							if (config.matteLoader) {
								var t_modalMatteLoader = $('t_modalMatteLoader');
								if (t_modalMatteLoader) t_modalMatteLoader.destroy();
							}
							
							config.element.removeClass('t_loaderModal');
							config.html = responseText;
							window.changeContent_event(config);
						},
						onFailure: function(xhr) {
							alert(xhr);
							if (xhr.readyState == 4) this.fireEvent("success", xhr.responseText).callChain();
						}
					}).send();
				}
			});
		}
		
		// submit a form
		window.formSubmit_events = function(config) {
			if (config) {
				if (config.form) {
					if (config.data) {
						$each(config.data, function(item, index) {
							this.form.adopt(new Element('input', {
								name: index,
								value: item,
								type: 'hidden'
							}));
						}, config);
					}
					
					if (config.exclude) {
						config.exclude.each(function(item, index) {
							if (config.form.elements[item].destroy) config.form.elements[item].destroy(); else config.form.elements[item].value = '';
						});
					}
					
					config.t_content = config.element.get('html');
									
					var req = new Request({
						url: config.form.get('action'),
						method: 'get',
						data: config.form,
						onRequest: function() {
							if (!config.avoidLoader) {
								config.element.empty();
								config.element.addClass('t_loaderModal');
							}
						},
						onSuccess: function(responseText) {
							config.element.removeClass('t_loaderModal');
							config.html = responseText;
							window.changeContent_event(config);
						},
						onFailure: function(xhr) {
							if (xhr.readyState == 4) this.fireEvent("success", xhr.responseText).callChain();
						}
					}).send();
				}
			}
		}
		
		// set tooltips
		window.setTooltips_event = function(config) {
			if (config.elementStr) {
				var tipElements = $$(config.elementStr);
				tipElements.store('tip:text', tipElements.title);
				
				if (!config.singleLine) {
					tipElements.each(function(item, index){
						var delimeter = '|';
						var glue = '<br /><br />';
						var title = item.get('title').split(delimeter).join(glue);
						item.set('title', title.substr(0, title.length - glue.length));
						item.store('tip:text', '');
					});
				}
				
				event_tt = new Tips(config.elementStr, {
					className: config.className ? config.className : '',
					fixed: config.mouseFollow ? false : true,
					hideDelay: 1,
					showDelay: 1,
					offsets: { x: config.offsetX ? config.offsetX : 28, y: config.offsetY ? config.offsetY : 22 }
				});
			}
		}
		
		// open layer
		window.openLayer_event = function(config) {
			$try(function() {
				var modalMatte = new Element('div', {
					id: 'eventModalMatte'
				});
				var modalWin = new Element('div', {
					id: 'eventModalWin'
				});
				
				var winScrollSize = window.getScrollSize();
				
				modalMatte.set('opacity', .5);
				modalMatte.setStyle('width', winScrollSize.x + 'px');
				modalMatte.setStyle('height', winScrollSize.y + 'px');
				modalMatte.setStyle('background-image', 'url(' + app_path + 'ext/images/cal_loader1.gif)');
				modalMatte.setStyle('background-repeat', 'no-repeat');
				modalMatte.setStyle('background-position', 'center');
				
				modalWin.set('opacity', 0);
				modalWin.setStyle('left', '-1600px');
				modalWin.setStyle('top', '-1600px');
				
				$$('body').adopt(modalMatte, modalWin);
				window.loadContent_event({
					url: config.url,
					data: config.data ? config.data : null,
					avoidLoader: true,
					empty: true,
					element: modalWin,
					ready:function(config){
						var winScrollSize = window.getScrollSize();
						var windowScroll = window.getScroll();
						config.element.setStyle('left', (((window.getWidth() - config.element.getWidth()) / 2) + windowScroll.x) + 'px');
						config.element.setStyle('top', (((window.getHeight() - config.element.getHeight()) / 2) + windowScroll.y) + 'px');
						
						var maxDragX = winScrollSize.x - config.element.getWidth();
						var maxDragY = winScrollSize.y - config.element.getHeight();
						modalWin.dragger = new Drag(modalWin, {
							snap: 12,
							limit: { 
								x: [0, maxDragX], 
								y: [0, maxDragY] 
							},
							onSnap: function(el){
								el.set('opacity', '.5');
							},
							onComplete: function(el){
								el.set('opacity', '1');
							}
						});	
						modalWin.dragger.detach();
						
						var modalWinFX = new Fx.Morph(config.element, { duration: 100 } );
						modalWinFX.start({
							opacity: [0,1]
						});
						modalWinFX.addEvent('complete', function() {
							var modalMatte = $('eventModalMatte');
							if (modalMatte) modalMatte.setStyle('background-image', 'none');
						});
					},
					readyParams: { element: modalWin }
				});
			});
		}
		
		// toggle dragging
		window.toggleDragging_event = function(drag) {
			var modalWin = $('eventModalWin');
			
			if (modalWin) {
				if (modalWin.dragger) modalWin.dragger[drag ? 'attach' : 'detach']();
			}
		}
		
		// close modal layer
		window.closeLayer_event = function() {
			var config = arguments.length > 0 ? arguments[0] : new Object();
			var modalMatte = $('eventModalMatte');
			var modalWin = $('eventModalWin');
			
			if (modalMatte && modalWin) {
				var modalWinFX = new Fx.Morph(modalWin, { duration: 50 });
				modalWinFX.addEvent('complete', function() {
					modalMatte.destroy();
					modalWin.destroy();
					
					if (!config.avoidReload) {
						setTimeout('document.location.reload();', 500);
					}
				});
				modalWinFX.start({
					opacity: [1,0]
				});
			}
			else {
				if (modalMatte) modalMatte.destroy();
				if (modalWin) modalWin.destroy();
			}
		}
		
		// toggle detail view
		window.toggleDetail_event = function(elementName) {
			var element = $(elementName);
			
			if(element) {
				if(!element.slider) {
					element.setStyle('display', 'block');
					element.slider = new Fx.Slide(element, {
						mode: 'vertical'
					}).hide();
				}
				element.slider.toggle();
			}
		}
		
		// for the board get images to be added
		window.boardAddImages = function() {
			var boardIFrame = $('boardIFRAME');
			
			$try(function() {
				var boardForm = $('ud_add_edit');
				var boardIFrameForm = boardIFrame.contentWindow.$('images_upload_form');
				var i = 0;
				var curImageInfo = null;
				
				while ((curImageInfo = boardIFrameForm.elements['image_' + String(i)]) != null) {
					boardForm.adopt(new Element('input', {
						type: 'hidden',
						name: curImageInfo.name,
						value: curImageInfo.value
					}));
					i ++;
				}
			});
		}
		
		// dom ready
		window.addEvent('domready', function() {
			// init
			$try(function() {
				window.loadContent_event({
					url: app_path + 'ext/php/lib/calendar.php',
					data: { lang: app_lang },
					avoidLoader: true,
					empty: true,
					element: $('evntCalWrapper'),
					ready: setTooltips_event,
					readyParams: { elementStr: '.evntCalDateActive', className: 'eventCalToolTip' }
				});
			});
			
			window.setTooltips_event({
				elementStr: '.eventUpcomingBook',
				className: 'eventCalToolTip',
				mouseFollow: true,
				singleLine: true,
				offsetX: 8,
				offsetY: 18
			});
			
			window.setTooltips_event({
				elementStr: '.boardTeaser',
				className: 'eventCalToolTip',
				mouseFollow: true,
				singleLine: true,
				offsetX: 8,
				offsetY: 18
			});
		});
	}
}
catch(err) {
}