
$(window).addEvent('domready',function() {
	if ($$('ul.main-menu li')) {
		$$('ul.main-menu li').each(function(el) {
			
			var children = el.getChildren();
			var first = children[0];
			var next = children[1]?children[1]:'';
			
			if ($(next)) {
				var parent = $(next).getParent();
				if (parent && parent.getTag()=='li') {
					var pSize = parent.getSize();
					next.setStyle('margin-top',-(pSize.y)+'px');
				}
			}
			
			var firstFx = new Fx.Styles(first,{
					duration: 150,
					wait: false
				});
				
			if (next) {
				next.setStyles({
					'display': 'none',
					'opacity': 0
				});
				var nextFx = new Fx.Styles(next,{
						duration: 200,
						wait: false,
						transition: Fx.Transitions.Quint.easeOut
					});
			}
				
			el.addEvents({
				'mouseenter': function() {
					firstFx.start({
						'color': '#67723a',
						'text-decoration': 'underline'
					});
					
					if (next) {
						
						var parent = $(next).getParent();
						if (parent && parent.getTag()=='li') {
							var pSize = parent.getSize();
							next.setStyle('margin-top',-(pSize.y)+'px');
						}
						
						next.setStyle('display','block');
						nextFx.start({
							'opacity': 1
						});
					}
				},
				'mouseleave': function() {
					firstFx.start({
						'color': '#4c5621',
						'text-decoration': 'none'
					});
					
					if (next) {
						nextFx.start({
							'opacity': 0
						}).chain(function() {
							next.setStyle('display','block');
						})
					}
				}
			});
		});
	}
	
	
	
	if ($$('div.menu ul li')) {
		$$('div.menu ul li').each(function(el) {
			
			var children = el.getChildren();
			var first = children[0];
			var next = children[1]?children[1]:'';
			
			var elFx = new Fx.Styles(first,{
					duration: 150,
					wait: false
				});
				
				el.addEvents({
					'mouseenter': function() {
						elFx.start({
							'color': '#67723a',
							'text-decoration': 'underline'
						});
						
						if (next) {
							next.setStyle('display','block');
						}
					},
					'mouseleave': function() {
						elFx.start({
							'color': '#4c5621',
							'text-decoration': 'none'
						});
						
						if (next) {
							next.setStyle('display','none');
						}
					},
					'click': function() {
						goHref(this.getProperty('rel'));
					}
				});
			});
	}
	
	if ($$('ul.site-menu')) {
		
				
		/**
		 * Rozsuwane menu dla konferencji
		 */
		var yearsAccord = new Accordion('div.accord-title','div.accord-element',{
			opacity: false,
			show: 1
		},$('accord-container'));
		(function() {
			yearsAccord.display(0);
		}).delay(1000);
		
		$$('div.accord-element').each(function(el) {
			var children = el.getChildren();
			var container = el.getFirst();
			
			if (!container) return;
			
			var togglers = container.getElementsByTagName('IMG');
			var elements = container.getChildren().filterByTag('div').filterByClass('accord-year-element');
			//alert(togglers.length+'/'+elements.length);
			
			new Accordion(togglers,elements,{
				opacity: false,
				onActive: function(toggler) {
					toggler.src=imagesSitePath+'accord-out.jpg';
				},
				onBackground: function(toggler) {
					toggler.src=imagesSitePath+'accord-in.jpg';
				}
			},container);
			
		});
		$$('div.accord-title').each(function(el) {
			
			var elFx = new Fx.Styles(el,{
				duration: 150,
				wait: false
			});
			
			el.addEvents({
				'mouseenter': function() {
					elFx.start({
						'background-color': '#fff',
						'color': '#b4cadf'
					});
				},
				'mouseleave': function() {
					elFx.start({
						'background-color': '#b4cadf',
						'color': '#666'
					});
				}
			});
		});
		
		
		
		
		
		/**
		 * Standardowe menu rozwijane
		 */
		$$('ul.site-menu li').each(function(li) {
			
			var liFx = new Fx.Styles(li,{
				duration: 150,
				wait: false
			});
			var aFx = new Fx.Styles(li.getFirst(),{
				duration: 150,
				wait: false
			});
			
			if (li.getParent().getProperty('class')!='site-menu') {
				li.addEvents({
					'mouseenter': function() {
						liFx.start({
							'background-color': '#cccccc'
						});
						aFx.start({
							'color': '#fff'
						});
					},
					'mouseleave': function() {
						liFx.start({
							'background-color': '#fff'
						});
						aFx.start({
							'color': '#666'
						});
					}
				});
			} else {
				li.addEvents({
					'mouseenter': function() {
						liFx.start({
							'background-color': '#fff'
						});
						aFx.start({
							'color': '#b4cadf'
						});
					},
					'mouseleave': function() {
						liFx.start({
							'background-color': '#b4cadf'
						});
						aFx.start({
							'color': '#666'
						});
					}
				});
			}
			
			if (li.getChildren().filterByTag('ul').length<=0) return;
			
			var child = li.getFirst().getNext();
			
			var shadow = new Element('div').inject(child,'before').setClass('menu-shadow');
			
			var chFx = new Fx.Styles(child,{
				duration: 250,
				wait: false
			});
			var shFx = new Fx.Styles(shadow,{
				duration: 250,
				wait: false
			});
			
			if (Browser.Engine.trident) {
				child.setStyle('margin',0);
			}
			
			li.addEvents({
				'mouseenter': function() {
					child.setStyles({
						'display': 'block',
						'opacity': 0,
						'margin-top': -2
					});
					shadow.setStyles({
						'display': 'block',
						'height': child.getSize().y+11
					});
					// Jesli nie IE
					if (!Browser.Engine.trident) {
						child.setStyles({
							'margin-top': -(li.getSize().y-2)
						});
						shadow.setStyles({
							'margin-top': -(li.getSize().y+4)
						});
					} else { // Jesli IE niestety
						var left = li.getParent().getProperty('class')=='site-menu'?li.getPosition().x:0;
						child.setStyles({
							'left': left+li.getSize().x+5
						});
						shadow.setStyles({
							'margin-top': -8,
							'left': left+li.getSize().x-200
						});
					}
					chFx.start({
						'opacity': 1
					});
					shFx.start({
						'opacity': .2
					});
				},
				'mouseleave': function() {
					shFx.start({
						'opacity': 0
					});
					chFx.start({
						'opacity': 0
					}).chain(function() {
						child.setStyles({
							'display': 'none'
						});
						shadow.setStyles({
							'display': 'none'
						});
					});
				}
			});
		});
	}
});
