window.addEvent('domready', function() {
	
	if (undefined===window.menu_to_open) menu_to_open=-1;
	
	var ap = $$('.active_page')[0];
	
	if(null!= ap) {
		ap.single = true;
		ap.addEvent('mouseleave', function(e){
			if(!ap.single) ap.setStyle('color','#999');
		});
		ap.addEvent('mouseover', function(e){
			if(!ap.single) ap.setStyle('color','#FFF');
		});
	}
	
	var els = $$('div.menu_element');
	els.each( function (el) {
		el.open = false;
		
		el.trans = new Fx.Morph(el,{
			duration: 300, transition: Fx.Transitions.Sine.easeInOut
		});
			
		el.trans.set({
			'opacity': 0,
			'height': 0
		});
		
	});
	
	var tc = 0;
	var togglers = $$('.menu_toggler');
	togglers.each(function(tog) {
		tog._id = tc;
		tog.el = els[tog._id];
		
		var _childs = tog.el.getElements('a.menu_button');
		tog._h = _childs.length*21;
		
		tog.active = false;
		tog._width = tog.getStyle('width');
		tog._width = parseInt(tog._width.substring(0,tog._width.indexOf('px')))+2;
		
		tog.trans = new Fx.Morph (tog, {
			duration: 300, transition: Fx.Transitions.Sine.easeInOut
		});
		
		tog.trans.set({
				'width':tog._width,
				'color':'#999',
				'padding-left':'12px',
				'background-color':'#333'
		});
		
		tog.slideOut = function () {
			var test = false;
			
			tog.el.trans.start({
				'opacity': 0,
				'height' : 0
			});
			
			tog.trans.start({
				'width':tog._width,
				'color':'#999',
				'padding-left':'12px',
				'background-color':'#333'
			});
			
			tog.active = false;
			
			togglers.each (function(tt) {
				test = tt.active;
			});
			if(null!= ap && !test){
				ap.single = true;
				ap.setStyle('background-color','#FF018D');
				ap.setStyle('color','#FFF');
			}
		}
		
		tog.slideIn = function() {
			tog.active = true;
			
			// slide all other out:
			togglers.each( function(tt) { if(tt._id!=tog._id) { if (tt.active) tt.slideOut(); } });
			
			tog.el.trans.start({
				'opacity': 1,
				'height': tog._h
			});
			
			var _W =  (Browser.Engine.trident) ? '109px':'111px';
			
			tog.trans.start({
				'width':_W,
				'padding-left':'6px',
				'color':'#FFF',
				'background-color':'#FF018D'
			});
			if(null!= ap){
				ap.single = false;
				ap.setStyle('background-color','#333');
				ap.setStyle('color','#999');
			}

		}
		
		if (tog._id==menu_to_open) {
			tog.active = true;
			
			// slide all other out:
			togglers.each( function(tt) {
				if(tt._id!=tog._id) { if (tt.active) tt.slideOut(); }
			});
			
			tog.el.trans.set({
				'opacity': 1,
				'height': tog._h
			});
			var _W = '111px';
			if (Browser.Engine.trident) {
				_W = '109px';
			}
			
			tog.trans.set({
				// make the active category wider and adjust the padding
				'width':_W,
				'padding-left':'6px',
				// set the cat of the active post also in color | not if you do not like
				'color':'#FFF',
				'background-color':'#FF018D'
			});
		
		}
		
		tog.addEvent('mouseleave', function(e) {
			if (this.active) { this.setStyle('color','#FFF'); this.setStyle('background-color','#FF018D'); }
			else { this.setStyle('color','#999'); }
		});
		
		tog.addEvent('mouseover', function(e) {
			if (this.active) { this.setStyle('color','#FFF'); this.setStyle('background-color','#BB005A'); }
			else { this.setStyle('color','#FFF'); }
		});
		
		tog.addEvent('click', function () {
			if (!tog.active) {
				tog.slideIn();
			} else {
				tog.slideOut();
			}
		});
		
		tc+=1;
	});

});