Element.implement({

  isHidden: function(){
    var w = this.offsetWidth, h = this.offsetHeight,
    force = (this.tagName === 'TR');
    return (w===0 && h===0 && !force) ? true : (w!==0 && h!==0 && !force) ? false : this.getStyle('display') === 'none';
  },

  isVisible: function(){
    return !this.isHidden();
  }

});

siteCache = new Class({
	siteList : null,
	siteIndex : null,
	siteHistory : null,
	start : function(){
		
		this.siteList=new Array();
		this.siteIndex = new Array();
		this.siteHistory = new Array();

	},
	addSite : function(url,site){
		this.siteList.push(site);
		this.siteIndex.push(url);
	},
	getSite : function(url){
		var index = this.siteIndex.indexOf(url);
		
		if (index != -1)
		{
			return this.siteList[index];
		}else{
			return false;
		}
		
	},
	setHistory : function(url){
		if (window.location.href!=url)
		{
			this.siteHistory.push(url);

		  //document.open("text/html","replace"); // "replace" the current document in the history
		  //document.write(site);
		  //document.close();
		  
				//window.location.replace(url);
		}
		
		
	}
});

siteHeader = new Class({
	hash : null,
	handlers : null,
	start : function(){
		
		this.hash = window.location.hash;
		this.siteIndex = new Array();
		
	},
	addSite : function(url,site){
		this.siteList.push(site);
		this.siteIndex.push(url);
	},
	getSite : function(url){
		var index = this.siteIndex.indexOf(url);
		
		if (index != -1)
		{
			return this.siteList[index];
		}else{
			return false;
		}
		
	}
});

collerclass = new Class ({
	coller : null,
	showme : false,
	morph : false,
	defaultPosition : -40,
	defaultHeight : '30px',
	
	start : function(){
		this.coller=$('menu-coller');
		var myTransition = new Fx.Transition(Fx.Transitions.Circ, 3);
  	  	this.morph = new Fx.Morph(this.coller,{ link: 'cancel', 'duration':'600', transition: myTransition.easeOut });
		},
	show : function(){
		this.coller.setStyles({
			display:'block'
			});
		this.showme = true ;
		},
	move : function(y){
		if (!this.showme){this.show();};

        this.morph.start({'margin-top': y+this.getOffset(),});
		
		//alert('First Value : ' + y );
		},
	moveOut : function(y){
		if (!this.showme){this.show();};
	       this.morph.start({'margin-top': this.defaultPosition, 'height': this.defaultHeight});
			},
	moveOver : function(y){
		if (!this.showme){this.show();};
	       this.morph.start({'margin-top': 600 , 'background-color': y , 'height': '1px'});
			},
	setColor : function(y){
		if (!this.showme){this.show();};
	       this.morph.start({'background-color': y});
			},
	setDefaultPosition : function(y){
		this.defaultPosition=y+this.getOffset();
			},
	setDefaultHeight : function(y){
		this.defaultHeight=y;
			},
	getOffset : function(y){
		if (this.defaultHeight=='40px')
		{
			return (112);
		}else{
			return (118);
		}
	}
	
});

function registerLinks(menucoller){
	
	// check ob unterseite aufgerufen wurde (menu ausgekjlappt)
	var checkDiv = $$('#main-menu div.menu-ul-div');
	checkDiv.each(function(element) {
		//alert(element);
		if (element.offsetHeight>10){
			oldSubDiv=element;
			showUlMenu==true
			//alert(element.id);
		}
	});
	
	// register Links
	var list = $$('#main-menu a');
	var startcontent = 1;

    list.each(function(element) {
		//alert(element);
		element.set('morph', {link : 'cancel'});
		oldcolor=element.getStyle('color');
		defaultBackground=0;
		element.addEvent('mouseenter', function(mouse){
		//alert(element.offsetTop);
			menucoller.move(element.offsetTop);
			lastMenuPosition=element.offsetTop;
			element.morph({
			'color': '#fff'
			});
		});
		element.addEvent('mouseleave', function(){
			element.morph({
				'color': oldcolor,
			});
		});
		element.addEvent('click', function(el){
			el = new Event(el).stop();
			

			
			var alink = element.getProperty('href'); 
			var back = $('body');
			var myTransition = new Fx.Transition(Fx.Transitions.Pow, 3);
			var morphout = new Fx.Morph($('content'),{ 'duration':'100', link: 'chain', transition: myTransition.easeOut }); 
        	var morphin = new Fx.Morph($('content'),{ 'duration':'200', link: 'chain', transition: myTransition.easeOut }); 
        	var morphmenu = new Fx.Morph($('main-menu'),{ 'duration':'400', link: 'chain', transition: myTransition.easeOut }); 
			var morphbackground = new Fx.Morph(back,{ 'duration':'2000', link: 'cancel', transition: myTransition.easeOut }); 

			morphout.addEvents({
		    	'complete': function() {
			
					
					// Hier den ajax Aufruf
					
					//$('content').load(element.href + '&request=content');


					
					
					/*
					startcontent++; 
					if (startcontent > 3){startcontent=1;}
		        	var content=$('content'+startcontent).get('html');
					$('content').set('html',content);
					*/
					// altes untermenü ausblenden
					if (oldSubDiv){
						oldSubDiv.style.display='none';
                       showUlMenu=false;

					}
					// neues untermenü einblenden
					if ($(element.getParent().id + '-ul')!=null){
						var subDiv = $(element.getParent().id + '-ul');
						subDiv.style.display='block';
                        showUlMenu=true;
						
						oldSubDiv=subDiv;
						};
					// cklick ins untermenü
					if ($(element.getParent()).id==oldSubDiv.id)
					{
						oldSubDiv.style.display='block';
                        showUlMenu=true;
						menucoller.setDefaultHeight('20px');
					}else{
						menucoller.setDefaultHeight('30px');
						
					}
					var newUrl=element.href;
					if (MySiteCache.getSite(newUrl))
					{
						$('content').set('html',MySiteCache.getSite(newUrl));
						MySiteCache.setHistory(newUrl);
						// animations back
						menucoller.setDefaultPosition(element.offsetTop);
						morphin.start({'margin-top' : '0','opacity' : '1'});
						morphmenu.start({'margin-left' : '0'});
						menucoller.moveOut();
					}else{
						var myHTMLRequest = new Request.HTML({ onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
							//alert(responseHTML);
							MySiteCache.addSite(element.href, responseHTML);
							MySiteCache.setHistory(element.href);
							$('content').set('html',responseHTML);
							// neustart der Linkregistrierung nach dem der neue content da ist
							//registerLinks(menucoller);
							
							// animations back
							menucoller.setDefaultPosition(element.offsetTop);
							morphin.start({'margin-top' : '0','opacity' : '1'});
							morphmenu.start({'margin-left' : '0'});
							menucoller.moveOut();

						} }).get(element.href + '&request=content');
					}
					
					
					// setzt Block auf den letzten Link

					

		    		}
                    
			});
			
			
			var newposition= defaultBackground + 1;
			if (newposition>3){newposition=1;};
			defaultBackground=newposition;
			var backgroundcolor= ['#049'].getRandom();
            
            // Ausnahmen beim Herausfahren des Menüs 1. wenn wir im Untermen sind, 2. wenn wir auf den gleichen Link drücken
            if (($(element.getParent()).id!=oldSubDiv.id) & (element != oldDiv))
            {
                // wenn der Link ein Untermenue hat oder ein anderes Untermenue ausgefahren ist
                if (($(element.getParent().id + '-ul')!=null) || (showUlMenu==true))
                {
                    morphmenu.start({'margin-left' : '-300'});
                }
            }
            
			morphout.start({'margin-top' : '0', 'opacity' : '0'});
			
			menucoller.moveOver(backgroundcolor);
			document.body.background = 'hintergrund_' + newposition + '.jpg';
		
            oldDiv=element;

			
      		});
	});
}

window.addEvent('domready',function() { 
	oldSubDiv=false;
    oldDiv=false;
    showUlMenu=false;
	/*
	siteList=new Array();
	siteIndex = new Array();
	siteList.push('Seite1');
	var index=siteList.indexOf('Seite1');
	alert(siteList[0] + index);
	*/
	window.onhashchange = function() {
		alert("Really Simple History, your days are numbered!");
	}
	
	

	MySiteCache = new siteCache();
	MySiteCache.start();
	

	var menucoller = new collerclass();
	menucoller.start();
		
   $each($(document.body).getElements('div.menu-div'), function(el) { 
      var original = el.getStyle('color'); 
  	  var myTransition = new Fx.Transition(Fx.Transitions.Quart, 3);
      var morph = new Fx.Morph(el,{ 'duration':'400', link: 'cancel', transition: myTransition.easeOut }); 
      el.addEvents({ 
         'mouseenter' : function() { 

		morph.start({'color': '#c03' }); }, 
         'mouseleave' : function() { morph.start({'color': original  }) }
      }); 
   });
	
	$('main-menu').addEvent('mouseleave',function(){
		menucoller.moveOut();
	})
	
	registerLinks(menucoller);

});
