window.addEvents(
{
	'domready' : function()
	{
		var Wowenkho = 
		{

			
			'Menu' : new Class(
			{
				Implements : Options, 
				
				options : 
				{
					'id' : 'nav',
					'content' : 'content',
					'loadingCover' : 'loadingCover',
					'listFX' : {
						'duration' : 1200, 
						'transition' : 'expo:in:out',
						'link' : 'chain'
					},
					'subLinksFX' : {
						'duration' : 300, 
						'transition' : 'expo:out',
						'link' : 'chain'
					},
					'containerFX' : {
						'duration' : 400,
						'transition' : 'expo:out',
						'link' : 'chain'
					},
					'loadingFX' : {
						'duration' : 300,
						'transition' : 'expo:out',
						'link' : 'cancel'
					},
					'hideBarFX' : {
						'duration' : 100,
						'transition' : 'expo:out',
						'link' : 'chain'
					},
					'moveBarFX' : {
						'duration' : 500,
						'transition' : 'expo:out',
						'link' : 'chain'
					},
					'linkFX' : {
						'duration' : 300,
						'transition' : 'expo:out',
						'link' : 'cancel'
					},
					'linkColor' : $$('#nav a')[0].getStyle('color'),
					'activeColor' : '#333'
				},
				
				initialize : function(options)
				{
					this.setOptions(options);
					
					if($(this.options.id) && $(this.options.content))
					{
						this.initElements();
						this.collectData();
						this.initAnimations();
						this.initAjax();
						this.initEvents();
						this.initURL();
						this.initCheckHash.delay(2400,this);
					}
				},
				
				initElements : function()
				{
					this.element = $(this.options.id);
					this.container = $(this.options.content);
					this.cover = this.container.clone()
					.empty()
					.set(
					{
						'id' : 'cover',
						'tween' : 
						{
							'duration' : 100,
							'transitions' : 'expo:out',
							'link' : 'chain'
						},
						
						'styles' : 
						{
							'background-color' : '#fff',
							'opacity' : 0
						}
					}).inject(this.container,'after');
					
					this.currentLinks = {};
					this.currentNames = {};
					
					this.list = [];
					this.listEl = this.element.getElement('ul');
					
					this.listEl.set('id','listlinks');
					
					this.subLinks = new Element('ul',
					{
						'id' : 'sublinks',
						'styles' : 
						{
							'margin-top' : 40,
							'clear' : 'both'
						}
					}).inject(this.listEl,'after');
					
					this.endLinks = new Element('ul',
					{
						'id' : 'endlinks',
						'styles' : 
						{
							'margin-top' : 40,
							'clear' : 'both'
						}
					}).inject(this.subLinks,'after');
					
					this.listBar = new Element('div',
					{
						'styles' :
						{
							'width' : 40,
							'height' : 1,
							'background-color' : '#666',
							'position' : 'absolute',
							'left' : -50
						}
					}).inject($(document.body));
					
					this.subBar = this.listBar.clone().inject($(document.body));
					this.endBar = this.listBar.clone().inject($(document.body));
					
					this.listBar.setStyles(
					{
						'top' : this.listEl.getCoordinates().top
					});
					
					this.subBar.setStyles(
					{
						'top' : this.subLinks.getCoordinates().top
					});
					
					this.endBar.setStyles(
					{
						'top' : this.endLinks.getCoordinates().top
					});
					
					this.loadingCover = $(this.options.loadingCover);
					
					this.loadingCover.setStyles(
					{
						'opacity' : 1
					});

				},
				
				collectData : function()
				{
					this.listEl.getChildren('li').each(function(el,i)
					{
						this.list[i] = el;
						var ul = el.getElement('ul');
						
						if(ul)
						{
							this.list[i].links = [];
								
							ul.getChildren('li').each(function(se,ii)
							{
								this.list[i].links[ii] = se;
								var sul = se.getElement('ul');
								
								if(sul)
								{
									this.list[i].links[ii].links = [];
									
									sul.getChildren('li').each(function(ee,iii)
									{
										this.list[i].links[ii].links[iii] = ee;
										ee.dispose();
										
									},this);
									
									sul.destroy();
								}
								
								se.dispose();
							},this);
							
							ul.destroy();
						}
						
					},this);
				},
				
				initAnimations : function()
				{
					this.listSlide = new Fx.Tween(this.listEl,this.options.listFX);
					this.listSlide.start('margin-left',[-(this.listEl.getSize().x),0]);
					
					this.subLinksSlide = new Fx.Tween(this.subLinks,this.options.subLinksFX);
					this.endLinksSlide = new Fx.Tween(this.endLinks,this.options.subLinksFX);
					
					this.containerFX = new Fx.Tween(this.container,this.options.containerFX);
					this.containerFX.set('opacity',0);
					
					this.loadingFX = new Fx.Tween(this.loadingCover,this.options.loadingFX);
				},
				
				hideBar : function(which)
				{
					which.set({'morph' : this.options.hideBarFX}).morph(
					{
						'left' : -50
					});
				},
				
				moveBar : function(which,to)
				{
					var c = which.position(
					{
						'relativeTo' : to,
						'position' : 'bottomLeft',
						'offset' : {'x' : -10, 'y' : -5},
						'edge' : 'bottomRight',
						'returnPos' : true
					});
					
					if(which.getStyle('left').toInt() < 10)
					{
						which.setStyles(
						{
							'top' : c.top
						})
						.set({'morph' : this.options.moveBarFX})
						.morph(
						{
							'left' : 10
						});
					}
					else
					{
						which.set({'morph' : this.options.moveBarFX}).morph(
						{
							'top' : c.top
						});
					}
				},
				
				getCurrent : function(ul)
				{
					return this.currentLinks[ul.get('id')];
				},
				
				setCurrent : function(item)
				{
					var that = this, hashText = [], urlText = [],
						listName = $(item.getParent('ul')).get('id');
					
					item.blur();
					
					this.setHover(item);
					
					this.currentLinks[listName] = item;
					this.currentNames[listName] = item.get('html');
					
					switch(listName)
					{
						case 'listlinks' :
						
							delete this.currentLinks['sublinks'];
							delete this.currentNames['sublinks'];
							delete this.currentLinks['endlinks'];
							delete this.currentNames['endlinks'];
						
						break;
						
						case 'sublinks' :
						
							delete this.currentLinks['endlinks'];
							delete this.currentNames['endlinks'];
							
						break;
					}
					
					$each(this.currentLinks,function(it,id)
					{
						var i = $(id).getElements('a').indexOf(it);
						hashText[hashText.length] = i;
						//urlText[urlText.length] = it.get('html').replace(/[^a-z0-9]/ig,'-');
					});
					
					//this.setHash(hashText.join(':') + '/' + item.get('title').replace(/\s/ig,'-'));
					
					if(this.setHashDelay)
					{
						$clear(this.setHashDelay);
					}
					
					var x = hashText.join(':') + '/' + item.get('title').replace(/\s/ig,'-')
					this.setHashDelay = this.setHash.delay(600,this,x);
					document.title = 'MEYER DUDESEK ARCHITEKTEN : ' + item.get('title');
				},
				
				setHash : function(h)
				{
					this.currentHash = h;
					location.hash = h;
				},
				
				clearCheckHash : function()
				{
					this.hashPeriodical = $clear(this.hashPeriodical);
				},
				
				initCheckHash : function()
				{
					var that = this,
						c = function()
						{
							var l = location.hash.substr(1);
							
							if(l != '' && that.currentHash && l != that.currentHash)
							{
								
							
								that.fetchHash();
								that.clearCheckHash();
								that.initCheckHash.delay(2000,that);
								//window.location.reload(true);		
							}
						};
						
					this.hashPeriodical = c.periodical(500);
				},
				
				setHover : function(item)
				{
					var all = $$(item.getParent('ul').getElements('a')).erase(item), that = this;
					
					//all.set('tween',this.options.linkFX).tween('color',this.options.linkColor);
					all.setStyle('color',this.options.linkColor);
					item.setStyle('color',this.options.activeColor);
					
					all.addEvents(
					{
						'mouseenter' : function()
						{
							this.setStyle('color',that.options.activeColor);
						},
						'mouseleave' : function()
						{
							this.setStyle('color',that.options.linkColor);
						}
					});
					
					item.removeEvents('mouseleave');
				},
				
				showContainer : function()
				{
					var curSize = this.container.getSize();
					this.cover.fade('out');
					this.containerFX.set('opacity',1);
				},
				
				hideContainer : function()
				{
					var wsize = window.getSize();
					
					this.cover.setStyles(
					{
						'opacity' : 0, 
						'width' : (wsize.x - this.container.getPosition().x), 
						'height' : (wsize.y - this.container.getPosition().y),
						'z-index' : 2000
					})
					.position(
					{
						'relativeTo' : this.container,
						'position' : 'topLeft'
					})
					.fade('in');
					
					this.containerFX.set.delay(400,this.containerFX,['opacity',0]);
				},
				
				initAjax : function()
				{
					var that = this;
					this.ajax = new Request.HTML(
					{
						'evalScripts' : true,
						'onRequest' : function()
						{
							if(that.loadingDelay)
							{
								$clear(that.loadingDelay);
							}
							that.loadingDelay = that.loadingFX.start.delay(0,that.loadingFX,['opacity',0]);
							that.hideContainer();
						},
						'onSuccess' : function(t,e,h,j)
						{
							var temp = new Element('div');
							
							temp.adopt(t);
							
							temp = temp.getLast('div#content').getChildren();
							
							//console.log(temp);
							
							that.container.empty().adopt(temp).addEvent('domready',function()
							{
								if(this.getElements('img').length)
								{
									
									var myImages = new Asset.images(this.getElements('img').get('src'),
									{
										'onComplete' : function()
										{
											that.showContainer();
											
											if(that.loadingDelay)
											{
												$clear(that.loadingDelay);
											}
											that.loadingDelay = that.loadingFX.start('opacity',1);
										}
									});
									
								}
								else
								{
									that.showContainer();
									
									if(that.loadingDelay)
									{
										$clear(that.loadingDelay);
									}
									that.loadingDelay = that.loadingFX.start('opacity',1);
								}
								
								
							});
							
							if(j)
							{
								eval(j);
							}
							
						}
					});
				},
				
				callContent : function(url)
				{
					this.ajax.setOptions(
					{
						'url' : url
					}).cancel().send();
				},
				
				
				initEvents : function()
				{
					var that = this,
						setEndLinkEvents = function(endx)
						{
							endx.each(function(ex,iii)
							{
								ex.getElement('a').addEvents(
								{
									'click' : function(e)
									{
										if(e) e.stop();
										
										if(that.getCurrent(this.getParent('ul')) != this)
										{
											that.setCurrent(this);
											
											that.callContent(this.get('href'));
											that.moveBar(that.endBar,ex.getElement('a'));
											
										}
										else
										{
											this.blur();
										}
									}
								});
							});
						},
						setSubLinkEvents = function(subl,l)
						{
							subl.each(function(s,ii)
							{
								s.getElement('a').addEvents(
								{
									'click' : function(e)
									{
										if(e) e.stop();
										
										if(that.getCurrent(this.getParent('ul')) != this)
										{
		
											that.setCurrent(this);
											
											that.moveBar(that.subBar,this);
											that.hideBar(that.endBar);
										
											var ech = new Chain();
											
											ech.chain(
												function()
												{
													that.endLinksSlide.setOptions(
													{
														'onComplete' : function()
														{
															ech.callChain();
														
														}
													});
													
													that.endLinksSlide.start('margin-left', -(that.endLinks.getSize().x + that.element.getStyle('left').toInt()));
												},
												function()
												{
													that.endLinks.empty();
													this.callChain();
												},
												function()
												{
													var endx = $$(l.links[ii].links).clone(),
														ae = endx[0];
													
													that.endLinks.adopt(endx);
													
													setEndLinkEvents(endx);
													
													if(ae)
													{
														var ael = ae.getElement('a');
														
														if(ael)
														{
															that.setCurrent(ael);
															that.callContent(ael.get('href'));
															that.moveBar(that.endBar,ael);
															
														}
													}
													else
													{
														that.callContent(s.getElement('a').get('href'));
													}
													
													this.callChain();
												},
												function()
												{
													that.endLinksSlide.start('margin-left', 0);
												}
											).callChain();
										}
										else
										{
											this.blur();
										}
									}
									
								});
							});
						}
					
					this.list.each(function(l,i)
					{
						var x = l.getElement('a');
						
						if(x)
						{
							x.addEvents(
							{
								'click' : function(e)
								{
									if(e) e.stop();
									
									if(that.getCurrent(this.getParent('ul')) != this)
									{
		
										that.setCurrent(this);
										
										that.moveBar(that.listBar,this);
										that.hideBar(that.subBar);
										that.hideBar(that.endBar);
										
										/*
										var url = ((l.links) ? $$(l.links).getElement('a').get('href') : this.get('href'));
										console.log(url);
										*/
										
										var sc = new Chain();
										
										sc.chain(
											function()
											{
												var ch = this;
												that.subLinksSlide.setOptions(
												{
													'onComplete' : function()
													{
														ch.callChain();
													
													}
												});
												
												if(that.subLinks.get('html') != '')
												{
													that.subLinksSlide.start('margin-left', -(that.subLinks.getSize().x + that.element.getStyle('left').toInt()));
													that.endLinksSlide.start.delay(50,that.endLinksSlide,['margin-left', -(that.endLinks.getSize().x + that.element.getStyle('left').toInt())]);
												}
												else
												{
													that.subLinksSlide.set('margin-left', -(that.subLinks.getSize().x + that.element.getStyle('left').toInt()));
													that.endLinksSlide.set('margin-left', -(that.endLinks.getSize().x + that.element.getStyle('left').toInt()));
													this.callChain();
												}
											},
											function()
											{
												that.subLinks.empty();
												that.endLinks.empty();
												this.callChain();
											},
											function()
											{
												if(l.links)
												{
													var subl = $$(l.links).clone(),
														endl = $$(l.links[0].links).clone(),
														ae = endl[0];
													
													that.subLinks.adopt(subl);
													that.endLinks.adopt(endl);
													
													setSubLinkEvents(subl,l);	
													setEndLinkEvents(endl);
													
													if(ae)
													{
														var ael = ae.getElement('a');
														
														if(ael)
														{
															that.setCurrent(ael);
															that.setCurrent(subl[0].getElement('a'));
															that.callContent(ael.get('href'));
															that.moveBar(that.subBar,subl[0].getElement('a'));
															that.moveBar(that.endBar,ael);
															
														}
													}
													else
													{
														that.setCurrent(subl[0].getElement('a'));
														that.callContent(subl[0].getElement('a').get('href'));
														that.moveBar(that.subBar,subl[0].getElement('a'));
														
													}
												}
												else
												{
													that.callContent(x.get('href'));
													
												}
												this.callChain();
											},
											function()
											{
												that.subLinksSlide.start('margin-left', 0);
												that.endLinksSlide.start.delay(50,that.endLinksSlide,['margin-left', 0]);
											}
										).callChain();
									}
									else
									{
										this.blur();
									}
								}
							});
						}
						
						
					});
					
				},
				
				initURL : function()
				{
					
					if(location.hash.substr(1) == '')
					{
						var proceed = true,
						that = this;
						href = location.href.substr(location.href.lastIndexOf('/')+1);
						
						if(proceed)
						{
							that.list.each(function(el,i)
							{
								if(proceed)
								{
									var x = $(el.getElement('a'));
									if(href == x.get('href'))
									{
										var hash = i + '/' + x.get('title').replace(/\s/ig,'-');
										that.setHash(hash);
										proceed = false;
										that.fetchHash.delay(1000,that);
										document.title = 'MEYER DUDESEK ARCHITEKTEN : ' + x.get('title');
									}
								}
							});
						}
						
						if(proceed)
						{
							that.list.each(function(el,i)
							{
								var x = $(el.getElement('a'));
								
								if(that.list[i].links)
								{
									that.list[i].links.each(function(l,j)
									{
										if(proceed)
										{
											var y = $(l.getElement('a'));
											
											if(href == y.get('href'))
											{
												var hash = i + ':' + j + '/' + y.get('title').replace(/\s/ig,'-');
												that.setHash(hash);
												proceed = false;
												
												that.fetchHash.delay(500,that);
												document.title = 'MEYER DUDESEK ARCHITEKTEN : ' + y.get('title');
											}
										}
									});
								}
							
							});
							
						}
						
						if(proceed)
						{
							that.list.each(function(el,i)
							{
								var x = $(el.getElement('a'));
								
								if(that.list[i].links)
								{
										
									that.list[i].links.each(function(l,j)
									{
										var y = $(el.getElement('a'));
										
										if(that.list[i].links[j].links)
										{
									
											that.list[i].links[j].links.each(function(en,k)
											{
												if(proceed)
												{
													var z = $(en.getElement('a'));
													
													if(href == z.get('href'))
													{
														var hash = i + ':' + j + ':' + k + '/' + z.get('title').replace(/\s/ig,'-');
														that.setHash(hash);
														proceed = false;
														
														that.fetchHash.delay(500,that);
														document.title = 'MEYER DUDESEK ARCHITEKTEN : ' + z.get('title');
													}
												}
											});
										}
									});
								}
							
							});
							
						}
						
						if(proceed)
						{
							this.list[0].getElement('a').fireEvent.delay(600,this.list[0].getElement('a'),'click');
							proceed = false;
						}
					}
					else
					{
						//this.fetchHash();
						var h = location.hash.substring(1,location.hash.indexOf('/')).split(':'),
							url = '';
							
						switch(h.length)
						{
							case 1:
								url = $(this.list[h[0]]).getElement('a').get('href');
							break;
							
							case 2:
								url = $(this.list[h[0]].links[h[1]]).getElement('a').get('href');
							break;
							
							case 3:
								url = $(this.list[h[0]].links[h[1]].links[h[2]]).getElement('a').get('href');
							break;
						}
						
						window.location = url;
					}
					
				},
				
				fetchHash : function()
				{
				
					var h = location.hash.substring(1,location.hash.indexOf('/')).split(':'),
						hChain = new Chain(), that = this; 
					
					h = h.map(function(i)
					{
						return i.toInt();		
					});
					
					//console.log(h);
					
					
					if($chk(h[0]))
					{
						hChain.chain(function()
						{
							if(that.getCurrent($('listlinks')) != $$('#listlinks a')[h[0]])
							{
								$$('#listlinks a')[h[0]].fireEvent.delay(700,$$('#listlinks a')[h[0]],'click');
								this.callChain.delay(1200,this);
							}
							else
							{
								this.callChain();
							}
						});
					}
					
					
					if($chk(h[1]))
					{
						hChain.chain(function()
						{
							if(that.getCurrent($('sublinks')) != $$('#sublinks a')[h[1]])
							{	
								$$('#sublinks a')[h[1]].fireEvent('click');
								this.callChain.delay(1200,this);
							}
							else
							{
								this.callChain();
							}
						});
					}
					
					if($chk(h[2]))
					{
						hChain.chain(function()
						{
							$$('#endlinks a')[h[2]].fireEvent('click');
						});
					}
					
					hChain.callChain();
					
					
				}
			})
		};
	
	
		
		
		$each(Wowenkho, function(c,k,o)
		{
			o['wowenkho_' + k.toLowerCase()] = new c();
		});
	},
	'beforeunload' : function()
	{
		$(document.body).destroy();
	}
});
