window.addEvent('domready', function(){
	var tx_xcibweekmenu_current_init = function(){
		var handlers = $$('div.tx_xcibweekmenu_current');
		if(handlers){
			handlers.each(function(h, idx){
				// Init scrolling
				var height = 0;
				var menus = h.getElements('.menu');
				var upScroller = h.getElements('.scroller-up');
				var downScroller = h.getElements('.scroller-down');
				var contentScroll = h.getElement('.content');
				var maxheight = parseInt(contentScroll.getStyle('height'));
				var fxScroll = new Fx.Scroll(contentScroll);
				if(menus && upScroller && downScroller){
					menus.each(function(menu, midx){
						height += menu.getCoordinates().height;
					});
					if(height > maxheight){
						var scrollY = 0;
						upScroller.setStyle('display', '');
						downScroller.setStyle('display', '');
						upScroller.removeEvents('click').addEvent('click', function(){
							fxScroll.set(0, scrollY-50);
							scrollY = scrollY-50;
						})
						downScroller.removeEvents('click').addEvent('click', function(){
							fxScroll.set(0, scrollY+50);
							scrollY = scrollY+50;
						})
					}
				}
				// Closing weekmenu
				var titles = h.getElements('.menu-title');
				var contents = h.getElements('.menu-content');
				if(titles && contents){
					titles.each(function(t, tidx){
						t.removeEvents('click').addEvent('click', function(){
							if(contents[tidx]){
								contents[tidx].setStyle('display', (contents[tidx].getStyle('display') == 'none' ? '' : 'none'));
							}
						})
					});
				}
				// Init ajax form
				var forms = h.getElements('form');
				var ajax = null;
				var p = h.getParent();
				forms.each(function(form, idxform){
					form.removeEvents('submit').addEvent('submit', function(e){
						(new Event(e)).stop();
						new Request({
							url: form.getProperty('action'),
							onRequest: function(){
								ajax = new Element('div', {
									'class': 'tx_xcibweekmenu_current_ajaxloader',
									styles:{
										'width': h.getCoordinates().width,
										'height': h.getCoordinates().height
									}
								});
								p.adopt(ajax);
							},
							onSuccess: function(responseText, responseXML){
								p.set('html', responseText);
								if(ajax) ajax.dispose();
								tx_xcibweekmenu_current_init();
							},
							onComplete: function(){
								if(ajax) ajax.dispose();
							}
						}).send($(form).toQueryString());
					});
				});
			});
		}
	}
	tx_xcibweekmenu_current_init();
});
